Artifact Content
Not logged in

Artifact 40a888ac9f7816df4d14e6b87a749e609308a2df


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;

class CreatePairs {
public:
	int maximalSum(vector <int> data) 
	{
		sort(data.begin(), data.end());
		vector<int>::iterator neg = upper_bound(data.begin(), data.end(), 0);

		LL sum = 0;
		for(vector<int>::iterator it=data.begin(); it!=neg; ++it)
			if( it+1 == neg )
				sum += *it;
			else
				sum += *it * *(it+1), ++it;

		vector<int>::iterator pos = lower_bound(data.begin(), data.end(), 2);
		for(vector<int>::iterator it=data.end()-1; it>=pos; --it)
			if( it == pos )
				sum += *it;
			else
				sum += *it * *(it-1), --it;
		for(vector<int>::iterator it=neg; it!=pos; ++it)
			sum += *it;
		return sum;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int data_[] = {0, 1, 2, 4, 3, 5};
	  vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 
	int RetVal = 27; 
	return verify_case(RetVal, CreatePairs().maximalSum(data)); }
int Test_(Case_<1>) {
	int data_[] = {-1, 1, 2, 3};
	  vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 
	int RetVal = 6; 
	return verify_case(RetVal, CreatePairs().maximalSum(data)); }
int Test_(Case_<2>) {
	int data_[] = {-1};
	  vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 
	int RetVal = -1; 
	return verify_case(RetVal, CreatePairs().maximalSum(data)); }
int Test_(Case_<3>) {
	int data_[] = {-1, 0, 1};
	  vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 
	int RetVal = 1; 
	return verify_case(RetVal, CreatePairs().maximalSum(data)); }
int Test_(Case_<4>) {
	int data_[] = {1, 1};
	  vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 
	int RetVal = 2; 
	return verify_case(RetVal, CreatePairs().maximalSum(data)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE