#include #include #include #include using namespace std; void insertElt(vector& longestEmpty, int elt, int maxTeams) { if (longestEmpty.empty() || elt <= longestEmpty.back()) longestEmpty.emplace_back(elt); else { for (size_t i = 0; i < longestEmpty.size(); ++i) { if (elt > longestEmpty[i]) { longestEmpty.emplace(longestEmpty.begin() + i, elt); break; } } } if ((int)longestEmpty.size() > maxTeams) longestEmpty.pop_back(); } vector input(vector>& teams) { int H, T, l; cin >> H >> T; vector longestEmpty; vector vec(T, 0); for (int i = 0; i < H; ++i) { cin >> l; l = min(l, T); for (int j = 0; j < l; ++j) { vec[j] ++; } for (int j = l; j < T; ++j) { if (vec[j] > 0) { insertElt(longestEmpty, vec[j], T); vec[j] = 0; } } } for (int j = 0; j < T; ++j) { if (vec[j] > 0) insertElt(longestEmpty, vec[j], T); } for (int i = 0; i < T; ++i) { pair t; cin >> t.second >> t.first; teams.push_back(t); } return longestEmpty; } struct GroupTeamInfo { GroupTeamInfo(int sumCoins, int firstEltLen, int teamId) { SumCoins = sumCoins; lengths.emplace_back(firstEltLen); teamIds.emplace(teamId); totalLen = firstEltLen; } GroupTeamInfo() {} GroupTeamInfo AddElt(int coins, int len, int teamId) { GroupTeamInfo info; info.SumCoins = SumCoins + coins; info.lengths = lengths; info.lengths.push_back(len); info.teamIds = teamIds; info.teamIds.emplace(teamId); info.totalLen = totalLen + len; sort(info.lengths.begin(), info.lengths.end()); reverse(info.lengths.begin(), info.lengths.end()); return info; } bool operator<(const GroupTeamInfo& second) { const GroupTeamInfo& first = *this; if (first.SumCoins != second.SumCoins) return first.SumCoins > second.SumCoins; if (first.totalLen != second.totalLen) return first.totalLen > second.totalLen; size_t idx = 0; while (true) { if (idx >= first.lengths.size()) return false; if (idx >= second.lengths.size()) return true; if (first.lengths[idx] != second.lengths[idx]) return first.lengths[idx] > second.lengths[idx]; idx++; } } bool hasId(const std::unordered_set& ids) { for (const int& id : ids) { if (teamIds.count(id) > 0) return true; } return false; } bool taken = false; int SumCoins = 0; int totalLen = 0; std::vector lengths; std::unordered_set teamIds; }; vector getAllSums(vector> teams) { vector sums; for (size_t i = 0; i < teams.size(); ++i) { size_t sz = sums.size(); for (size_t j = 0; j < sz; ++j) { sums.push_back(sums[j].AddElt(teams[i].first, teams[i].second, i)); } sums.emplace_back(teams[i].first, teams[i].second, i); } sort(sums.begin(), sums.end()); return sums; } int main() { vector < pair> teams; auto termins = input(teams); auto sums = getAllSums(teams); long long result = 0; std::reverse(termins.begin(), termins.end()); for (size_t j = 0; j < termins.size(); ++j) { for (size_t i = 0; i < sums.size(); ++i) { if (sums[i].taken == false && sums[i].totalLen <= termins[j]) { result += sums[i].SumCoins; sums[i].taken = true; for (size_t k = 0; k < sums.size(); ++k) { if (sums[k].hasId(sums[i].teamIds)) sums[k].taken = true; } break; } } } cout << result; return 0; }