#include using namespace std; struct hash_pair { template size_t operator()(const pair& p) const { auto hash1 = hash{}(p.first); auto hash2 = hash{}(p.second); return hash1 ^ hash2; } }; vector len; int h, n; int input[100001]; void init(){ cin >> h >> n; for(int i=0;i> input[i]; } input[h]=0; for(int i=1;i<=20;i++){ int start=0; for(int j=0;j<=h;j++){ if(input[j] < i){ if(start> teams; int main(){ init(); if(len.size() > n){ cout << 0 << '\n'; return 0; } sort(len.begin(), len.end()); /*for(int i : len){ cout << i << ' '; } cout<< "\n\n";*/ teams.resize(n); for(int i=0;i> teams[i].first >> teams[i].second; } int maxi = 0; for(int i = 0;i< (1<> team1; vector> team2; for(int j=0;j< n;j++){ if(i & (1< perm(team1.size()); for(int i=0;i, int, hash_pair> m; m[{0,0}] = 0; bool oksi = 0; int tcurrInd, tcurrD, ertek; do{ int currInd=0, currD=0, nyer=0; int i=0; for(;i=0;i++){ if(currD == 0){ --currInd; if(currInd >=0) currD = len[currInd]; } if(currInd >=0 && currD-team2[perm[i]].first >= 0){ nyer+=team2[perm[i]].second; currD-=team2[perm[i]].first; if(currInd == tcurrInd && currD == tcurrD){ maxi = max(maxi, nyer+ertek); } } else { break; } } } while(next_permutation(perm.begin(), perm.end())); } } } cout << maxi << '\n'; return 0; }