fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef long long ll;
  6.  
  7. constexpr int N = 310;
  8. constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
  9.  
  10. int n, h[N], c[N], cnt[N << 1], mnc[N << 1];
  11. ll K, f[N << 1][N][N];
  12.  
  13. vector<int> hs;
  14.  
  15. inline ll F(int n, int m, int k) {
  16. n = min(n, k / m);
  17. return n * (n - 1) / 2 * m + n * (k - n * m);
  18. }
  19.  
  20. inline void chkmin(int &lhs, int rhs) {lhs = min(lhs, rhs);}
  21. inline void chkmin(ll &lhs, ll rhs) {lhs = min(lhs, rhs);}
  22.  
  23. int main() {
  24. ios_base::sync_with_stdio(0); cin.tie(nullptr), cout.tie(nullptr);
  25. cin >> n >> K;
  26. for (int i = 1; i <= n; i++) cin >> h[i] >> c[i], hs.emplace_back(h[i]), hs.emplace_back(h[i] + 1);
  27. sort(hs.begin(), hs.end()); hs.erase(unique(hs.begin(), hs.end()), hs.end());
  28. memset(mnc, 0x3f, sizeof(mnc));
  29. for (int i = 1; i <= n; i++) {
  30. h[i] = lower_bound(hs.begin(), hs.end(), h[i]) - hs.begin();
  31. cnt[h[i]]++, chkmin(mnc[h[i]], c[i]);
  32. }
  33. memset(f, 0x3f, sizeof(f)), f[0][1][0] = 0;
  34. int m = hs.size(), mn = 2e9;
  35. for (int i = 0; i < m; i++) {
  36. int wid = i + 1 < m ? min(n, hs[i + 1] - hs[i]) : n;
  37. for (int j = 1; j <= n; j++) {
  38. for (int k = 0; k <= n; k++) if (f[i][j][k] < INF) {
  39. chkmin(f[i][j + 1][k], f[i][j][k] + mn);
  40. cout << i << ' ' << j << ' ' << k << ' ' << f[i][j][k] << '\n';
  41. chkmin(f[i + 1][j][max(k + cnt[i] - wid * j, 0)], f[i][j][k] + F(wid, j, k + cnt[i]) * K);
  42. }
  43. }
  44. mn = min(mn, mnc[i]);
  45. }
  46. ll ans = INF;
  47. for (int j = 1; j <= n; j++) ans = min(ans, f[m][j][0]);
  48. cout << ans;
  49. return 0;
  50. }
Success #stdin #stdout 0.07s 469112KB
stdin
8 8
0 36
1 47
2 95
0 59
1 54
0 95
1 87
2 92
stdout
0 1 0 0
0 2 0 2000000000
0 3 0 4000000000
0 4 0 6000000000
0 5 0 8000000000
0 6 0 10000000000
0 7 0 12000000000
0 8 0 14000000000
1 1 2 16
1 2 1 2000000008
1 2 2 52
1 3 0 4000000000
1 3 1 2000000044
1 3 2 88
1 4 0 4000000036
1 4 1 2000000080
1 4 2 124
1 5 0 4000000072
1 5 1 2000000116
1 5 2 160
1 6 0 4000000108
1 6 1 2000000152
1 6 2 196
1 7 0 4000000144
1 7 1 2000000188
1 7 2 232
1 8 0 4000000180
1 8 1 2000000224
1 8 2 268
2 1 4 48
2 2 2 2000000024
2 2 3 76
2 2 4 84
2 3 0 4000000000
2 3 1 2000000052
2 3 2 104
2 3 3 112
2 3 4 120
2 4 0 2000000080
2 4 1 132
2 4 2 140
2 4 3 148
2 4 4 156
2 5 0 160
2 5 1 168
2 5 2 176
2 5 3 184
2 5 4 192
2 6 0 196
2 6 1 204
2 6 2 212
2 6 3 220
2 6 4 228
2 7 0 232
2 7 1 240
2 7 2 248
2 7 3 256
2 7 4 264
2 8 0 268
2 8 1 276
2 8 2 284
2 8 3 292
2 8 4 300
3 1 5 88
3 2 2 2000000040
3 2 3 100
3 2 4 116
3 2 5 124
3 3 0 2000000052
3 3 1 112
3 3 2 128
3 3 3 136
3 3 4 152
3 3 5 160
3 4 0 132
3 4 1 148
3 4 2 164
3 4 3 172
3 4 4 188
3 4 5 196
3 5 0 160
3 5 1 184
3 5 2 200
3 5 3 208
3 5 4 224
3 5 5 232
3 6 0 196
3 6 1 220
3 6 2 236
3 6 3 244
3 6 4 260
3 6 5 268
3 7 0 232
3 7 1 256
3 7 2 272
3 7 3 280
3 7 4 296
3 7 5 304
3 8 0 268
3 8 1 292
3 8 2 308
3 8 3 316
3 8 4 332
3 8 5 340
108