Problem p03899 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-6;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int64 mod = 1e9 + 7;

/*
int main(void){
        int32 N;
        cin >> N;
        vector<int64> v(N);
        REP(i, N) cin >> v[i];

        int64 maxi = 0;
        int64 res = 0;
        REP(i, N){
                int64 cnt = (v[i]-1)/(maxi+1);
                if(cnt){ res += cnt; chmax(maxi, 1); }
                else maxi = v[i];
        }
        cout << res << endl;
}
*/

int main(void) {
  int32 M, N, K;
  scanf("%d %d %d", &N, &M, &K);
  int64 A[112345];
  deque<PLL> deq[301];
  REP(i, N) scanf("%lld", A[i]);

  REP(i, N) {
    for (int32 j = K - 1; j >= 1; j--) {
      while (deq[j - 1].size() && i - deq[j - 1].front().sc > M)
        deq[j - 1].pop_front();
      if (deq[j - 1].empty())
        continue;
      int64 val = deq[j - 1].front().fs + A[i] * (j + 1);
      while (deq[j].size() && val > deq[j].back().fs)
        deq[j].pop_back();

      deq[j].push_back(PLL(val, i));
    }
    while (deq[0].size() && A[i] > deq[0].back().fs)
      deq[0].pop_back();
    deq[0].push_back(PLL(A[i], i));
  }

  cout << deq[K - 1].front().fs << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-6;

template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

const int64 mod = 1e9 + 7;

/*
int main(void){
        int32 N;
        cin >> N;
        vector<int64> v(N);
        REP(i, N) cin >> v[i];

        int64 maxi = 0;
        int64 res = 0;
        REP(i, N){
                int64 cnt = (v[i]-1)/(maxi+1);
                if(cnt){ res += cnt; chmax(maxi, 1); }
                else maxi = v[i];
        }
        cout << res << endl;
}
*/

int main(void) {
  int32 M, N, K;
  scanf("%d %d %d", &N, &M, &K);
  int64 A[112345];
  deque<PLL> deq[301];
  REP(i, N) scanf("%lld", &A[i]);

  REP(i, N) {
    for (int32 j = K - 1; j >= 1; j--) {
      while (deq[j - 1].size() && i - deq[j - 1].front().sc > M)
        deq[j - 1].pop_front();
      if (deq[j - 1].empty())
        continue;
      int64 val = deq[j - 1].front().fs + A[i] * (j + 1);
      while (deq[j].size() && val > deq[j].back().fs)
        deq[j].pop_back();

      deq[j].push_back(PLL(val, i));
    }
    while (deq[0].size() && A[i] > deq[0].back().fs)
      deq[0].pop_back();
    deq[0].push_back(PLL(A[i], i));
  }

  cout << deq[K - 1].front().fs << endl;
}