Artifact Content
Not logged in

Artifact e86c37863d2b6878935ab6a74ff49643d5f9f7fc


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class P8XGraphBuilder { public:
	int solve(vector <int> scores)
	{
		scores.insert(scores.begin(), 9999999);
		const int N = scores.size();

		int best = 0;
		for(int leaves=1; leaves<N; ++leaves)
			best = max(best, scores[1]*leaves + calc(leaves, N-leaves, scores));
		return best;
	}

	map<pair<int,int>,int> memo;
	int calc(int leaves, int nodes, const vector<int>& scores)
	{
		if( nodes == 1 )
			return scores[leaves];

		pair<int,int> key(leaves, nodes);
		if( memo.count(key) )
			return memo[key];

		int best = 0;
		for(int k=1; k<=leaves; ++k)
			best = max(best, scores[k+1] + calc(leaves-k+1, nodes-1, scores));
		return memo[key] = best;
	}
};

// 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> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, P8XGraphBuilder().solve(scores));}
int main(){

CASE(0)
	int scores_[] = {1, 3, 0};
	  vector <int> scores(scores_, scores_+sizeof(scores_)/sizeof(*scores_)); 
	int _ = 8; 
END
CASE(1)
	int scores_[] = {0, 0, 0, 10};
	  vector <int> scores(scores_, scores_+sizeof(scores_)/sizeof(*scores_)); 
	int _ = 10; 
END
CASE(2)
	int scores_[] = {1, 2, 3, 4, 5, 6};
	  vector <int> scores(scores_, scores_+sizeof(scores_)/sizeof(*scores_)); 
	int _ = 12; 
END
CASE(3)
	int scores_[] = {5, 0, 0};
	  vector <int> scores(scores_, scores_+sizeof(scores_)/sizeof(*scores_)); 
	int _ = 15; 
END
CASE(4)
	int scores_[] = {1, 3, 2, 5, 3, 7, 5};
	  vector <int> scores(scores_, scores_+sizeof(scores_)/sizeof(*scores_)); 
	int _ = 20; 
END
CASE(5)
int scores_[] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10};
	  vector <int> scores(scores_, scores_+sizeof(scores_)/sizeof(*scores_)); 
	int _ = -1; 
END
CASE(6)
	int scores_[] = {2};
	  vector <int> scores(scores_, scores_+sizeof(scores_)/sizeof(*scores_)); 
	int _ = 4; 
END

}
// END CUT HERE