↓あとで通したやつ
vector <int> BB; VI w; map<PII, PII> memo; PII f(int bi, int wi) { PII key = MP(bi, wi); if(memo.count(key)) return memo[key]; if(wi==w.size()) return MP(0, 0); if(bi==BB.size()) return MP(-100000000, -100000000); PII rest = f(bi+1, wi+1); if(w[wi]==0) rest.first-=BB[bi]; else rest.first+=BB[bi]; rest.second+=BB[bi]; PII restB = f(bi+1, wi); //cout<<bi<<" "<<wi<<" "<<rest<<" "<<restB<<endl; return memo[key] = max(rest, restB); } class HeavyBooks { public: vector <int> findWeight(vector <int> B, vector <int> M) { memo = map<PII, PII>(); sort(ALL(B)); w = VI(M[0], 1); int to=0; REP(i, M.size()-1) { int mo = M[i+1]; for(int j=M[0]-1;j>=0;j--) { if(w[j]==1-to) { w[j]=to; mo--; if(mo==0) break; } } to=1-to; } cout<<w<<endl; BB=B; PII r = f(0, 0); cout<<r<<endl; vector<int> ans(2); ans[0]=-(r.first-r.second)/2; ans[1]=(r.first+r.second)/2; return ans; } };