Problem p03899 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <queue>

using namespace std;

typedef long long ll;

int n, m, k;
ll a[111111];

ll dp[333][33];

ll dp2[2][333];

ll dp3[111111][333];
priority_queue<ll> q[333], dq[333];

ll dp4[333][111111];
ll deq[333][111111];
ll s[333], t[333];

struct S {};

int main(void) {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
  }
  if (n == m && false) {
    ll res = 0;
    memset(dp2, -1, sizeof(dp2));
    dp2[1][0] = 0;
    for (int i = 0; i < n; i++) {
      dp2[i % 2][0] = 0;
      for (int j = 1; j <= k; j++) {
        if (dp2[i % 2][j - 1] >= 0) {
          dp2[(i + 1) % 2][j] =
              max({dp2[(i + 1) % 2][j], dp2[i % 2][j - 1] + a[i] * j,
                   dp2[i % 2][j]});
        }
      }
      for (int j = 0; j <= k; j++) {
        res = max(res, dp2[(i + 1) % 2][j]);
        if (dp2[i % 2][j] >= 0) {
          dp2[i % 2][j] = 0;
        }
        // printf("%5lld", dp2[(i+1)%2][j]);
      }
      // puts("");
    }
    printf("%lld\n", res);
  } else if (n <= 300 && k <= 30 && false) {
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i <= n; i++) {
      dp[i][0] = 0;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < k; j++) {
        for (int l = 1; l <= m && i - l >= 0; l++) {
          if (dp[i - l][j] >= 0) {
            dp[i][j + 1] = max(dp[i][j + 1], dp[i - l][j] + a[i - 1] * (j + 1));
          }
        }
      }
    }
    ll res = 0;
    for (int i = 0; i <= n + 1; i++) {
      for (int j = 0; j <= k; j++) {
        // printf("%5lld", dp[i][j]);
        res = max(res, dp[i][j]);
      }
      // puts("");
    }
    printf("%lld\n", res);
  } else if (k <= 30 && false) {
    q[0].push(0);
    memset(dp3, -1, sizeof(dp3));
    for (int i = 1; i <= n; i++) {
      for (int j = k - 1; j >= 0; j--) {
        if (q[j].size() == 0)
          continue;
        while (!dq[j].empty() && q[j].top() == dq[j].top()) {
          q[j].pop();
          dq[j].pop();
        }
        dp3[i][j + 1] = q[j].top() + a[i - 1] * (j + 1);
        q[j + 1].push(q[j].top() + a[i - 1] * (j + 1));
      }
      if (i - m >= 0) {
        for (int j = 0; j < k; j++) {
          if (dp3[i - m][j] >= 0) {
            dq[j].push(dp3[i - m][j]);
          }
        }
      }
    }
    printf("%lld\n", q[k].top());
  } else {
    memset(dp4, -1, sizeof(dp4));
    for (int i = 0; i <= n; i++) {
      dp4[0][i] = 0;
      deq[0][t[0]++] = 0;
    }
    ll res = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = k - 1; j >= 0; j--) {
        if (s[j] < t[j]) {
          dp4[j + 1][i] = dp4[j][deq[j][s[j]]] + a[i - 1] * (j + 1);
          res = max(res, dp4[j + 1][i]);
          // printf("%d %d max: %lld\n", i, j, dp4[j][deq[j][s[j]]]);
          while (s[j + 1] < t[j + 1] &&
                 dp4[j + 1][deq[j + 1][t[j + 1] - 1]] <= dp4[j + 1][i])
            --t[j + 1];
          deq[j + 1][t[j + 1]++] = i;
          if (i - m >= 0) {
            if (j > 0 && deq[j][s[j]] == i - m) {
              ++s[j];
            }
          }
        }
      }
    }
    /*
    for (int ii = 0; ii <= k; ii++) {
      for (int jj = 0; jj <= n; jj++) {
        printf("%5lld", dp4[ii][jj]);
      }
      puts("");
    }
    // */
    printf("%lld\n", res);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <deque>
#include <queue>

using namespace std;

typedef long long ll;

int n, m, k;
ll a[111111];

ll dp[333][33];

ll dp2[2][333];

ll dp3[111111][333];
priority_queue<ll> q[333], dq[333];

ll dp4[333][111111];
int deq[333][111111];
int s[333], t[333];

struct S {};

int main(void) {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = 0; i < n; i++) {
    scanf("%lld", a + i);
  }
  if (n == m && false) {
    ll res = 0;
    memset(dp2, -1, sizeof(dp2));
    dp2[1][0] = 0;
    for (int i = 0; i < n; i++) {
      dp2[i % 2][0] = 0;
      for (int j = 1; j <= k; j++) {
        if (dp2[i % 2][j - 1] >= 0) {
          dp2[(i + 1) % 2][j] =
              max({dp2[(i + 1) % 2][j], dp2[i % 2][j - 1] + a[i] * j,
                   dp2[i % 2][j]});
        }
      }
      for (int j = 0; j <= k; j++) {
        res = max(res, dp2[(i + 1) % 2][j]);
        if (dp2[i % 2][j] >= 0) {
          dp2[i % 2][j] = 0;
        }
        // printf("%5lld", dp2[(i+1)%2][j]);
      }
      // puts("");
    }
    printf("%lld\n", res);
  } else if (n <= 300 && k <= 30 && false) {
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i <= n; i++) {
      dp[i][0] = 0;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < k; j++) {
        for (int l = 1; l <= m && i - l >= 0; l++) {
          if (dp[i - l][j] >= 0) {
            dp[i][j + 1] = max(dp[i][j + 1], dp[i - l][j] + a[i - 1] * (j + 1));
          }
        }
      }
    }
    ll res = 0;
    for (int i = 0; i <= n + 1; i++) {
      for (int j = 0; j <= k; j++) {
        // printf("%5lld", dp[i][j]);
        res = max(res, dp[i][j]);
      }
      // puts("");
    }
    printf("%lld\n", res);
  } else if (k <= 30 && false) {
    q[0].push(0);
    memset(dp3, -1, sizeof(dp3));
    for (int i = 1; i <= n; i++) {
      for (int j = k - 1; j >= 0; j--) {
        if (q[j].size() == 0)
          continue;
        while (!dq[j].empty() && q[j].top() == dq[j].top()) {
          q[j].pop();
          dq[j].pop();
        }
        dp3[i][j + 1] = q[j].top() + a[i - 1] * (j + 1);
        q[j + 1].push(q[j].top() + a[i - 1] * (j + 1));
      }
      if (i - m >= 0) {
        for (int j = 0; j < k; j++) {
          if (dp3[i - m][j] >= 0) {
            dq[j].push(dp3[i - m][j]);
          }
        }
      }
    }
    printf("%lld\n", q[k].top());
  } else {
    memset(dp4, -1, sizeof(dp4));
    for (int i = 0; i <= n; i++) {
      dp4[0][i] = 0;
      deq[0][t[0]++] = 0;
    }
    ll res = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = k - 1; j >= 0; j--) {
        if (s[j] < t[j]) {
          dp4[j + 1][i] = dp4[j][deq[j][s[j]]] + a[i - 1] * (j + 1);
          res = max(res, dp4[j + 1][i]);
          // printf("%d %d max: %lld\n", i, j, dp4[j][deq[j][s[j]]]);
          while (s[j + 1] < t[j + 1] &&
                 dp4[j + 1][deq[j + 1][t[j + 1] - 1]] <= dp4[j + 1][i])
            --t[j + 1];
          deq[j + 1][t[j + 1]++] = i;
          if (i - m >= 0) {
            if (j > 0 && deq[j][s[j]] == i - m) {
              ++s[j];
            }
          }
        }
      }
    }
    /*
    for (int ii = 0; ii <= k; ii++) {
      for (int jj = 0; jj <= n; jj++) {
        printf("%5lld", dp4[ii][jj]);
      }
      puts("");
    }
    // */
    printf("%lld\n", res);
  }
  return 0;
}