f7e7bde312 2012-09-01 kinaba: #include <iostream> f7e7bde312 2012-09-01 kinaba: #include <sstream> f7e7bde312 2012-09-01 kinaba: #include <iomanip> f7e7bde312 2012-09-01 kinaba: #include <vector> f7e7bde312 2012-09-01 kinaba: #include <string> f7e7bde312 2012-09-01 kinaba: #include <map> f7e7bde312 2012-09-01 kinaba: #include <set> f7e7bde312 2012-09-01 kinaba: #include <algorithm> f7e7bde312 2012-09-01 kinaba: #include <numeric> f7e7bde312 2012-09-01 kinaba: #include <iterator> f7e7bde312 2012-09-01 kinaba: #include <functional> f7e7bde312 2012-09-01 kinaba: #include <complex> f7e7bde312 2012-09-01 kinaba: #include <queue> f7e7bde312 2012-09-01 kinaba: #include <stack> f7e7bde312 2012-09-01 kinaba: #include <cmath> f7e7bde312 2012-09-01 kinaba: #include <cassert> f7e7bde312 2012-09-01 kinaba: using namespace std; f7e7bde312 2012-09-01 kinaba: typedef long long LL; f7e7bde312 2012-09-01 kinaba: typedef long double LD; f7e7bde312 2012-09-01 kinaba: typedef complex<LD> CMP; f7e7bde312 2012-09-01 kinaba: f7e7bde312 2012-09-01 kinaba: class SafeRemoval { public: f7e7bde312 2012-09-01 kinaba: int removeThem(vector <int> seq, int K) f7e7bde312 2012-09-01 kinaba: { f7e7bde312 2012-09-01 kinaba: sort(seq.begin(), seq.end()); f7e7bde312 2012-09-01 kinaba: const int sum = accumulate(seq.begin(), seq.end(), 0); f7e7bde312 2012-09-01 kinaba: f7e7bde312 2012-09-01 kinaba: vector<int> cls[4]; f7e7bde312 2012-09-01 kinaba: for(int i=0; i<seq.size(); ++i) f7e7bde312 2012-09-01 kinaba: cls[seq[i]%4].push_back(seq[i]); f7e7bde312 2012-09-01 kinaba: f7e7bde312 2012-09-01 kinaba: int best = -1; f7e7bde312 2012-09-01 kinaba: for(int r=0; r<4; ++r) { f7e7bde312 2012-09-01 kinaba: int b = rec(cls, 0, 0, 0, 0, K, sum%4); f7e7bde312 2012-09-01 kinaba: if(b != -1) f7e7bde312 2012-09-01 kinaba: best = max(best, sum-b); f7e7bde312 2012-09-01 kinaba: } f7e7bde312 2012-09-01 kinaba: return best; f7e7bde312 2012-09-01 kinaba: } f7e7bde312 2012-09-01 kinaba: f7e7bde312 2012-09-01 kinaba: map<int, int> memo; f7e7bde312 2012-09-01 kinaba: int rec(const vector<int> cls[4], int a, int b, int c, int d, int K, int R) f7e7bde312 2012-09-01 kinaba: { f7e7bde312 2012-09-01 kinaba: if(K==0) f7e7bde312 2012-09-01 kinaba: return (R==0 ? -1 : 0); f7e7bde312 2012-09-01 kinaba: const int key = ((((K*51+a)*51+b)*51+c)*51+d)*4+R; f7e7bde312 2012-09-01 kinaba: if(memo.count(key)) f7e7bde312 2012-09-01 kinaba: return memo[key]; f7e7bde312 2012-09-01 kinaba: f7e7bde312 2012-09-01 kinaba: int i[4] = {a,b,c,d}; f7e7bde312 2012-09-01 kinaba: int best = -1; f7e7bde312 2012-09-01 kinaba: for(int k=0; k<4; ++k) if(R!=k && i[k]<cls[k].size()) { f7e7bde312 2012-09-01 kinaba: i[k]++; f7e7bde312 2012-09-01 kinaba: int b = rec(cls, i[0], i[1], i[2], i[3], K-1, (R-k+4)%4); f7e7bde312 2012-09-01 kinaba: i[k]--; f7e7bde312 2012-09-01 kinaba: if(b != -1) { f7e7bde312 2012-09-01 kinaba: b += cls[k][i[k]]; f7e7bde312 2012-09-01 kinaba: best = (best==-1 ? b : min(best, b)); f7e7bde312 2012-09-01 kinaba: } f7e7bde312 2012-09-01 kinaba: } f7e7bde312 2012-09-01 kinaba: return memo[key] = best; f7e7bde312 2012-09-01 kinaba: } f7e7bde312 2012-09-01 kinaba: }; f7e7bde312 2012-09-01 kinaba: f7e7bde312 2012-09-01 kinaba: // BEGIN CUT HERE f7e7bde312 2012-09-01 kinaba: #include <ctime> f7e7bde312 2012-09-01 kinaba: double start_time; string timer() f7e7bde312 2012-09-01 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } f7e7bde312 2012-09-01 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) f7e7bde312 2012-09-01 kinaba: { os << "{ "; f7e7bde312 2012-09-01 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) f7e7bde312 2012-09-01 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } f7e7bde312 2012-09-01 kinaba: void verify_case(const int& Expected, const int& Received) { f7e7bde312 2012-09-01 kinaba: bool ok = (Expected == Received); f7e7bde312 2012-09-01 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; f7e7bde312 2012-09-01 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } f7e7bde312 2012-09-01 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); f7e7bde312 2012-09-01 kinaba: #define END verify_case(_, SafeRemoval().removeThem(seq, k));} f7e7bde312 2012-09-01 kinaba: int main(){ f7e7bde312 2012-09-01 kinaba: f7e7bde312 2012-09-01 kinaba: CASE(0) f7e7bde312 2012-09-01 kinaba: int seq_[] = {3, 8, 4}; f7e7bde312 2012-09-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f7e7bde312 2012-09-01 kinaba: int k = 1; f7e7bde312 2012-09-01 kinaba: int _ = 11; f7e7bde312 2012-09-01 kinaba: END f7e7bde312 2012-09-01 kinaba: CASE(1) f7e7bde312 2012-09-01 kinaba: int seq_[] = {1,1,1,1,1,1}; f7e7bde312 2012-09-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f7e7bde312 2012-09-01 kinaba: int k = 3; f7e7bde312 2012-09-01 kinaba: int _ = -1; f7e7bde312 2012-09-01 kinaba: END f7e7bde312 2012-09-01 kinaba: CASE(2) f7e7bde312 2012-09-01 kinaba: int seq_[] = {1,6,1,10,1,2,7,11}; f7e7bde312 2012-09-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f7e7bde312 2012-09-01 kinaba: int k = 6; f7e7bde312 2012-09-01 kinaba: int _ = 21; f7e7bde312 2012-09-01 kinaba: END f7e7bde312 2012-09-01 kinaba: CASE(3) f7e7bde312 2012-09-01 kinaba: int seq_[] = {1,1,1,1,1,1,1,1,7}; f7e7bde312 2012-09-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f7e7bde312 2012-09-01 kinaba: int k = 3; f7e7bde312 2012-09-01 kinaba: int _ = 6; f7e7bde312 2012-09-01 kinaba: END f7e7bde312 2012-09-01 kinaba: /* f7e7bde312 2012-09-01 kinaba: CASE(4) f7e7bde312 2012-09-01 kinaba: int seq_[] = ; f7e7bde312 2012-09-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f7e7bde312 2012-09-01 kinaba: int k = ; f7e7bde312 2012-09-01 kinaba: int _ = ; f7e7bde312 2012-09-01 kinaba: END f7e7bde312 2012-09-01 kinaba: CASE(5) f7e7bde312 2012-09-01 kinaba: int seq_[] = ; f7e7bde312 2012-09-01 kinaba: vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); f7e7bde312 2012-09-01 kinaba: int k = ; f7e7bde312 2012-09-01 kinaba: int _ = ; f7e7bde312 2012-09-01 kinaba: END f7e7bde312 2012-09-01 kinaba: */ f7e7bde312 2012-09-01 kinaba: } f7e7bde312 2012-09-01 kinaba: // END CUT HERE