81eb41d4ba 2012-02-18 kinaba: #include <iostream> 81eb41d4ba 2012-02-18 kinaba: #include <sstream> 81eb41d4ba 2012-02-18 kinaba: #include <iomanip> 81eb41d4ba 2012-02-18 kinaba: #include <vector> 81eb41d4ba 2012-02-18 kinaba: #include <string> 81eb41d4ba 2012-02-18 kinaba: #include <map> 81eb41d4ba 2012-02-18 kinaba: #include <set> 81eb41d4ba 2012-02-18 kinaba: #include <algorithm> 81eb41d4ba 2012-02-18 kinaba: #include <numeric> 81eb41d4ba 2012-02-18 kinaba: #include <iterator> 81eb41d4ba 2012-02-18 kinaba: #include <functional> 81eb41d4ba 2012-02-18 kinaba: #include <complex> 81eb41d4ba 2012-02-18 kinaba: #include <queue> 81eb41d4ba 2012-02-18 kinaba: #include <stack> 81eb41d4ba 2012-02-18 kinaba: #include <cmath> 81eb41d4ba 2012-02-18 kinaba: #include <cassert> 81eb41d4ba 2012-02-18 kinaba: #include <cstring> 81eb41d4ba 2012-02-18 kinaba: #ifdef __GNUC__ 81eb41d4ba 2012-02-18 kinaba: #include <ext/hash_map> 81eb41d4ba 2012-02-18 kinaba: #define unordered_map __gnu_cxx::hash_map 81eb41d4ba 2012-02-18 kinaba: #else 81eb41d4ba 2012-02-18 kinaba: #include <unordered_map> 81eb41d4ba 2012-02-18 kinaba: #endif 81eb41d4ba 2012-02-18 kinaba: using namespace std; 81eb41d4ba 2012-02-18 kinaba: typedef long long LL; 81eb41d4ba 2012-02-18 kinaba: typedef complex<double> CMP; 81eb41d4ba 2012-02-18 kinaba: 81eb41d4ba 2012-02-18 kinaba: class CasketOfStar { public: 81eb41d4ba 2012-02-18 kinaba: int maxEnergy(vector <int> weight) 81eb41d4ba 2012-02-18 kinaba: { 81eb41d4ba 2012-02-18 kinaba: return rec(weight, 0, weight.size()-1); 81eb41d4ba 2012-02-18 kinaba: } 81eb41d4ba 2012-02-18 kinaba: 81eb41d4ba 2012-02-18 kinaba: map<pair<int,int>, int> memo; 81eb41d4ba 2012-02-18 kinaba: int rec(const vector<int>& w, int L, int R) 81eb41d4ba 2012-02-18 kinaba: { 81eb41d4ba 2012-02-18 kinaba: if( L+1 == R ) 81eb41d4ba 2012-02-18 kinaba: return 0; 81eb41d4ba 2012-02-18 kinaba: pair<int,int> key(L,R); 81eb41d4ba 2012-02-18 kinaba: if( memo.count(key) ) 81eb41d4ba 2012-02-18 kinaba: return memo[key]; 81eb41d4ba 2012-02-18 kinaba: int best = 0; 81eb41d4ba 2012-02-18 kinaba: for(int C=L+1; C<R; ++C) 81eb41d4ba 2012-02-18 kinaba: best = max(best, rec(w, L, C) + rec(w, C, R)); 81eb41d4ba 2012-02-18 kinaba: return memo[key] = best + w[L]*w[R]; 81eb41d4ba 2012-02-18 kinaba: } 81eb41d4ba 2012-02-18 kinaba: }; 81eb41d4ba 2012-02-18 kinaba: 81eb41d4ba 2012-02-18 kinaba: // BEGIN CUT HERE 81eb41d4ba 2012-02-18 kinaba: #include <ctime> 81eb41d4ba 2012-02-18 kinaba: double start_time; string timer() 81eb41d4ba 2012-02-18 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 81eb41d4ba 2012-02-18 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 81eb41d4ba 2012-02-18 kinaba: { os << "{ "; 81eb41d4ba 2012-02-18 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 81eb41d4ba 2012-02-18 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 81eb41d4ba 2012-02-18 kinaba: void verify_case(const int& Expected, const int& Received) { 81eb41d4ba 2012-02-18 kinaba: bool ok = (Expected == Received); 81eb41d4ba 2012-02-18 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 81eb41d4ba 2012-02-18 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 81eb41d4ba 2012-02-18 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 81eb41d4ba 2012-02-18 kinaba: #define END verify_case(_, CasketOfStar().maxEnergy(weight));} 81eb41d4ba 2012-02-18 kinaba: int main(){ 81eb41d4ba 2012-02-18 kinaba: 81eb41d4ba 2012-02-18 kinaba: CASE(0) 81eb41d4ba 2012-02-18 kinaba: int weight_[] = {1,2,3,4}; 81eb41d4ba 2012-02-18 kinaba: vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); 81eb41d4ba 2012-02-18 kinaba: int _ = 12; 81eb41d4ba 2012-02-18 kinaba: END 81eb41d4ba 2012-02-18 kinaba: CASE(1) 81eb41d4ba 2012-02-18 kinaba: int weight_[] = {100,2,1,3,100}; 81eb41d4ba 2012-02-18 kinaba: vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); 81eb41d4ba 2012-02-18 kinaba: int _ = 10400; 81eb41d4ba 2012-02-18 kinaba: END 81eb41d4ba 2012-02-18 kinaba: CASE(2) 81eb41d4ba 2012-02-18 kinaba: int weight_[] = {2,2,7,6,90,5,9}; 81eb41d4ba 2012-02-18 kinaba: vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); 81eb41d4ba 2012-02-18 kinaba: int _ = 1818; 81eb41d4ba 2012-02-18 kinaba: END 81eb41d4ba 2012-02-18 kinaba: CASE(3) 81eb41d4ba 2012-02-18 kinaba: int weight_[] = {477,744,474,777,447,747,777,474}; 81eb41d4ba 2012-02-18 kinaba: vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); 81eb41d4ba 2012-02-18 kinaba: int _ = 2937051; 81eb41d4ba 2012-02-18 kinaba: END 81eb41d4ba 2012-02-18 kinaba: CASE(4) 81eb41d4ba 2012-02-18 kinaba: int weight_[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}; 81eb41d4ba 2012-02-18 kinaba: vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); 81eb41d4ba 2012-02-18 kinaba: int _ = 13; 81eb41d4ba 2012-02-18 kinaba: END 81eb41d4ba 2012-02-18 kinaba: /* 81eb41d4ba 2012-02-18 kinaba: CASE(5) 81eb41d4ba 2012-02-18 kinaba: int weight_[] = ; 81eb41d4ba 2012-02-18 kinaba: vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); 81eb41d4ba 2012-02-18 kinaba: int _ = ; 81eb41d4ba 2012-02-18 kinaba: END 81eb41d4ba 2012-02-18 kinaba: CASE(6) 81eb41d4ba 2012-02-18 kinaba: int weight_[] = ; 81eb41d4ba 2012-02-18 kinaba: vector <int> weight(weight_, weight_+sizeof(weight_)/sizeof(*weight_)); 81eb41d4ba 2012-02-18 kinaba: int _ = ; 81eb41d4ba 2012-02-18 kinaba: END 81eb41d4ba 2012-02-18 kinaba: */ 81eb41d4ba 2012-02-18 kinaba: } 81eb41d4ba 2012-02-18 kinaba: // END CUT HERE