题目
![](https://i-blog.csdnimg.cn/direct/fa66691511ee427e891b707994f84908.png)
代码
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, int>;
#define x first
#define y second
const int N = 1e5 + 10;
int n, m;
int k[N], b[N], cnt[N];
priority_queue<pll, vector<pll>> pq; // d(k*x*x + b*x)
ll cal(int j, int x)
{return max((1ll * k[j] * x + b[j]) * x, 0ll);
}
int main()
{cin >> n >> m;for (int i = 1; i <= m; i++){cin >> k[i] >> b[i];pq.push({cal(i, 1), i});cnt[i] = 1;}ll ans = 0;for (int i = 1; i <= n; i++){auto u = pq.top();pq.pop();ll d = u.x; int j = u.y;if(d <= 0) break;ans += d;ll nd = cal(j, cnt[j] + 1) - cal(j, cnt[j]);cnt[j]++;pq.push({nd, j});}cout << ans;
}