Artifact Content
Not logged in

Artifact 1cbd37b2af1920c67781c6d82a7efc70fa7d4ca5


#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>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

class SafeRemoval { public:
	int removeThem(vector <int> seq, int K)
	{
		sort(seq.begin(), seq.end());
		const int sum = accumulate(seq.begin(), seq.end(), 0);

		vector<int> cls[4];
		for(int i=0; i<seq.size(); ++i)
			cls[seq[i]%4].push_back(seq[i]);

		int best = -1;
		for(int r=0; r<4; ++r) {
			int b = rec(cls, 0, 0, 0, 0, K, sum%4);
			if(b != -1)
				best = max(best, sum-b);
		}
		return best;
	}

	map<int, int> memo;
	int rec(const vector<int> cls[4], int a, int b, int c, int d, int K, int R)
	{
		if(K==0)
			return (R==0 ? -1 : 0);
		const int key = ((((K*51+a)*51+b)*51+c)*51+d)*4+R;
		if(memo.count(key))
			return memo[key];

		int i[4] = {a,b,c,d};
		int best = -1;
		for(int k=0; k<4; ++k) if(R!=k && i[k]<cls[k].size()) {
			i[k]++;
			int b = rec(cls, i[0], i[1], i[2], i[3], K-1, (R-k+4)%4);
			i[k]--;
			if(b != -1) {
				b += cls[k][i[k]];
				best = (best==-1 ? b : min(best, b));
			}
		}
		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(_, SafeRemoval().removeThem(seq, k));}
int main(){

CASE(0)
	int seq_[] = {3, 8, 4};
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int k = 1; 
	int _ = 11; 
END
CASE(1)
	int seq_[] = {1,1,1,1,1,1};
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int k = 3; 
	int _ = -1; 
END
CASE(2)
	int seq_[] = {1,6,1,10,1,2,7,11};
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int k = 6; 
	int _ = 21; 
END
CASE(3)
	int seq_[] = {1,1,1,1,1,1,1,1,7};
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int k = 3; 
	int _ = 6; 
END
/*
CASE(4)
	int seq_[] = ;
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int k = ; 
	int _ = ; 
END
CASE(5)
	int seq_[] = ;
	  vector <int> seq(seq_, seq_+sizeof(seq_)/sizeof(*seq_)); 
	int k = ; 
	int _ = ; 
END
*/
}
// END CUT HERE