Artifact Content
Not logged in

Artifact 410a4764b987c8ec9f934af1aa38279f5c69ad66


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

class ClassicProblem { public:
	long long maximalValue(vector <int> cnt, vector <int> w, vector <int> v, int limit)
	{
		vector<int> idx;
		for(int i=0; i<cnt.size(); ++i)
			idx.push_back(i);
		sort(idx.begin(), idx.end(), [&](int a, int b) {
			// sort by increasing order of v[a]/w[a]
			if(LL(v[a])*w[b] == LL(v[b])*w[a])
				return w[a]>w[b];
			return LL(v[a])*w[b] < LL(v[b])*w[a];
		});

		LL gain = 0;
		while(limit > 6480) {
			if(idx.empty())
				break;
			int i = idx.back(), ww=w[i], vv=v[i];
			int n = min(cnt[i], (limit-6400)/ww);
			limit -= LL(ww)*n;
			gain += LL(vv)*n;
			cnt[i] -= n;
			idx.pop_back();
		}

		vector<LL> w2v = rec(0, cnt, w, v, limit);
		return gain + *max_element(w2v.begin(), w2v.end());
	}

	vector<LL> rec(int i, const vector <int>& C, const vector<int>& W, const vector<int>& V, int limit)
	{
		if(i == C.size())
			return vector<LL>(1, 0LL);

		vector<LL> w2v = rec(i+1, C, W, V, limit);
		if(C[i] == 0)
			return w2v;

		vector<LL> w2v_neo(limit+1, 0LL);
		for(int w=0; w<w2v.size(); ++w) {
			for(int k=0; k<=C[i]; ++k) { // this type of typical DP can be faster
				int ww = w + LL(W[i])*k;
				if(ww > limit)
					break;
				w2v_neo[ww] = max(w2v_neo[ww], w2v[w] + LL(V[i])*k);
			}
		}
		return w2v_neo;
	}
};

// 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 long long& Expected, const long long& 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(_, ClassicProblem().maximalValue(cnt, w, v, limit));}
int main(){

CASE(0)
	int cnt_[] = {100,100};
	  vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 
	int w_[] = {2,3};
	  vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 
	int v_[] = {3,5};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int limit = 6; 
	long long _ = 10LL; 
END
CASE(1)
	int cnt_[] = {100,100};
	  vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 
	int w_[] = {2,3};
	  vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 
	int v_[] = {3,5};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int limit = 5; 
	long long _ = 8LL; 
END
CASE(2)
	int cnt_[] = {100,102};
	  vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 
	int w_[] = {2,3};
	  vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 
	int v_[] = {3,5};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int limit = 1000000000; 
	long long _ = 810LL; 
END
CASE(3)
	int cnt_[] = {100,100};
	  vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 
	int w_[] = {2,3};
	  vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 
	int v_[] = {3,5};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int limit = 1; 
	long long _ = 0LL; 
END
CASE(4)
	int cnt_[] = {1,2,3,4,5,6,7,8};
	  vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 
	int w_[] = {4,2,6,7,5,8,3,1};
	  vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 
	int v_[] = {3,6,4,1,2,8,5,7};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int limit = 15; 
	long long _ = 73LL; 
END
CASE(5)
	int cnt_[] = {1000000000};
	  vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 
	int w_[] = {1};
	  vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 
	int v_[] = {1000000000};
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int limit = 1000000000; 
	long long _ = 1000000000000000000LL; 
END
/*
CASE(6)
	int cnt_[] = ;
	  vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 
	int w_[] = ;
	  vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 
	int v_[] = ;
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int limit = ; 
	long long _ = LL; 
END
CASE(7)
	int cnt_[] = ;
	  vector <int> cnt(cnt_, cnt_+sizeof(cnt_)/sizeof(*cnt_)); 
	int w_[] = ;
	  vector <int> w(w_, w_+sizeof(w_)/sizeof(*w_)); 
	int v_[] = ;
	  vector <int> v(v_, v_+sizeof(v_)/sizeof(*v_)); 
	int limit = ; 
	long long _ = LL; 
END
*/
}
// END CUT HERE