Advertisement
Guest User

Juanzhang

a guest
Jun 5th, 2019
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef double db;
  5. const int maxn = 2010;
  6. int n, m, W, AP[maxn], BP[maxn], AS[maxn], BS[maxn], Q[maxn], f[maxn][maxn];
  7.  
  8. inline void chkmax(int &x, int y) {
  9.   if (x < y) x = y;
  10. }
  11.  
  12. int main() {
  13.   scanf("%d %d %d", &n, &m, &W);
  14.   for (int i = 1; i <= n; i++) {
  15.     scanf("%d %d %d %d", AP + i, BP + i, AS + i, BS + i);
  16.   }
  17.   int ans = 0;
  18.   memset(f, -0x3f, sizeof f);
  19.   f[0][0] = 0;
  20.   for (int i = 1; i <= n; i++) {
  21.     memset(Q, 0, sizeof Q);
  22.     int l = 1, r = 1;
  23.     int pos = max(0, i - W - 1);
  24.     for (int j = 0; j <= m; j++) {
  25.       if (Q[l] < j - AS[i]) l++;
  26.       chkmax(f[i][j], f[pos][Q[l]] + (Q[l] - j) * AP[i]);
  27.       while (l <= r && f[pos][Q[r]] + Q[r] * AP[i] <= f[pos][j] + j * AP[i]) r--;
  28.       Q[++r] = j;
  29.     }
  30.     l = 1, r = 1, Q[1] = m;
  31.     for (int j = m; ~j; j--) {
  32.       if (Q[l] > j + BS[i]) l++;
  33.       chkmax(f[i][j], f[pos][Q[l]] + (Q[l] - j) * BP[i]);
  34.       while (l <= r && f[pos][Q[r]] + Q[r] * BP[i] <= f[pos][j] + j * BP[i]) r--;
  35.       Q[++r] = j;
  36.     }
  37.     for (int j = 0; j <= m; j++) {
  38.       chkmax(f[i][j], f[i - 1][j]), chkmax(ans, f[i][j]);
  39.     }
  40.   }
  41.   printf("%d", ans);
  42.   return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement