题目
题解
由于题目明确可以找零,说明无论如何,只要我们现有总金额大于商品价格,我们就可以购买这个商品,因此题目可以简述为,给定总金额和若干商品的价格,问最多可以买多少件商品,因此我们只需要尽可能买便宜的商品从而最大化购买的商品数量。
#include <bits/stdc++.h>
using i128 = __int128;
using ll = long long;
using ull = unsigned long long;
using de = double;
using ld = long double;
using namespace std;
void solve()
{
int n, m; cin >> n >> m;
vector<int> p(n); for (int i = 0; i < n; ++i) cin >> p[i];
sort(p.begin(), p.end());
ll money = 0;
for (int i = 0; i < m; ++i)
{
int x, c; cin >> x >> c;
money += (1LL << x) * c; // 1LL << x 相当于 2^x
}
int ans = 0;
for (int i = 0; i < n; ++i)
{
if (money < p[i]) break;
money -= p[i];
ans++;
}
cout << ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
// cin >> t;
while (t--)
{
solve();
}
}