↓修正版(test pass in practice room)
class FoxAndKgram { public: int maxK(vector <int> len) { int N=len.size(); for(int k=50;k>=0;k--) { VI used(N); int ok=1; int L=0; //cout<<k<<endl; REP(i, N) { if(used[i]) continue; if(len[i]==k) {used[i]=1;L++;continue;} REP(j, N) { if(used[j]||i==j) continue; if(len[i]+len[j]+1==k) {used[i]=used[j]=1;L++;break;} } } ok &= L>=k; //cout<<ok<<endl; if(ok) return k; } return 0; } };
↓修正版(test pass in practice room)
vector <int> WC; int SC; int N; map<PII, int> memo; class FoxAndDoraemon { public: // [i0, i1) int f(int i0, int i1) { PII key=MP(i0, i1); if(memo.count(key)) return memo[key]; int rest = i1-i0; if(rest<=0) return memo[key] = 0; if(rest==1) return memo[key] = WC[i0]; if(rest==2) return memo[key] = SC+max(WC[i0], WC[i0+1]); if(rest==3) return memo[key] = SC+max(WC[i0], SC+max/*min*/(WC[i0+1], WC[i0+2])); // >=4 int ans = 1000000; for(int i=1;i<rest;i++) { int c = SC+max(f(i0, i0+i), f(i0+i, i1)); ans = min(ans, c); } return memo[key] = ans; } int minTime(vector <int> _WC, int _SC) { sort(ALLR(_WC)); WC=_WC; SC=_SC; memo.clear(); N=WC.size(); int ans = f(0, N); return ans; } };