fork download
  1. #include<iostream>
  2. #include<queue>
  3. #include<algorithm>
  4. using namespace std;
  5. const int SIZE = 1 << 17;
  6. int a[SIZE], b[SIZE], c[SIZE];
  7. long long f(int subscript, long long x) {
  8. return a[subscript] * x * x + b[subscript] * x + c[subscript];
  9. }
  10. int v[SIZE];
  11. void solve() {
  12. int n, K;
  13. cin >> n >> K;
  14. long long an = 0;
  15. priority_queue<pair<long long, int>> pq;
  16. for(int i = 0; i < n; i++) {
  17. cin >> a[i] >> b[i] >> c[i];
  18. an += f(i, 0);
  19. v[i] = 0;
  20. pq.push({f(i, 0) - f(i, 1), i});
  21. }
  22. while(K--) {
  23. long long inc = pq.top().first;
  24. int id = pq.top().second;
  25. pq.pop();
  26. an -= inc;
  27. v[id]++;
  28. pq.push({f(id, v[id]) - f(id, v[id] + 1), id});
  29. }
  30. cout << an << '\n';
  31. }
  32. int main() {
  33. cin.tie(0);
  34. ios_base::sync_with_stdio(false);
  35. solve();
  36. return 0;
  37. }
Success #stdin #stdout 0.01s 5280KB
stdin
2 3
1 1 1
2 2 2
stdout
-2 0
-4 1
-4 0
13