Problem p03899 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <utility>
// #include <memory>
// #include <functional>
// #include <deque>
// #include <cctype>
// #include <ctime>
// #include <numeric>
// #include <list>
// #include <iomanip>

// #if __cplusplus >= 201103L
// #include <array>
// #include <tuple>
// #include <initializer_list>
// #include <forward_list>
//
// #define cauto const auto&
// #else

// #endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)

template <class T> class RMQ {
public:
  vector<T> data;
  vector<vector<int>> block;
  vector<int> sblock;
  vector<int> lookup;
  int N;
  int lg;
  int argMin(int a, int b) { return data[b] < data[a] ? b : a; }
  // x の下位 y bit を 0 にする
  int maskBits(int x, int y) { return (x >> y) << y; }
  RMQ(vector<T> &v) {
    data = v;
    N = data.size();

    lg = 32 - __builtin_clz(N);

    block.assign((N + lg - 1) / lg, vector<int>(lg, 0));

    for (int i = 0; i < N; i++) {
      if (i % lg == 0)
        block[i / lg][0] = i;
      block[i / lg][0] = argMin(block[i / lg][0], i);
    }

    for (int j = 1; j < lg; j++) {
      for (int i = 0; i < block.size(); i++) {
        block[i][j] = argMin(
            block[i][j - 1],
            block[i + (1 << (j - 1)) < block.size() ? i + (1 << (j - 1))
                                                    : block.size() - 1][j - 1]);
      }
    }

    sblock.assign(N, 0);
    stack<int> st;
    for (int i = 0; i < N; i++) {
      if (i % lg == 0)
        while (st.size())
          st.pop();
      while (st.size() && i == argMin(i, st.top()))
        st.pop();
      if (st.size()) {
        int g = st.top();
        sblock[i] = sblock[g] | (1 << (g % lg));
      }
      st.push(i);
    }
  }
  // return = min{data[i] : i in [l .. r]}
  T query(int l, int r) {
    if (l == r)
      return data[l];
    int l1 = l / lg + 1;
    int r1 = r / lg - 1;
    int ret = l;
    if (l1 <= r1) {
      int d = r1 - l1 + 1;
      if (d == 1) {
        ret = argMin(ret, block[l1][0]);
      } else if (d == 2) {
        ret = argMin(ret, argMin(block[l1][0], block[r1][0]));
      } else {
        int lg2 = 32 - __builtin_clz(d + 1);
        // int lg2 = (31 ^ __builtin_clz(d + 2)) - 1;
        ret = argMin(ret,
                     argMin(block[l1][lg2], block[r1 - (1 << lg2) + 1][lg2]));
      }
    }
    if (l1 - r1 == 2) { // same block
      int t = maskBits(sblock[r], l % lg);
      if (t == 0) {
        ret = argMin(ret, r);
      } else {
        ret = argMin(ret, __builtin_ctz(t) + (l1 - 1) * lg);
      }
    } else { // other block
      int t1 = maskBits(sblock[l1 * lg - 1], l % lg);
      if (t1 == 0) {
        ret = argMin(ret, l1 * lg - 1);
      } else {
        ret = argMin(ret, __builtin_ctz(t1) + (l1 - 1) * lg);
      }
      int t2 = sblock[r];
      if (t2 == 0) {
        ret = argMin(ret, r);
      } else {
        ret = argMin(ret, __builtin_ctz(t2) + (r1 + 1) * lg);
      }
    }
    return data[ret];
  }
};

void mainmain() {
  int n, m, K;
  cin >> n >> m >> K;

  // assert(n <= 300 && K <= 30);

  vll v(n);
  vll dp(n);
  rep(i, n) {
    cin >> v[i];
    dp[i] = -v[i];
  }
  rep(i, K - 1) {
    RMQ<ll> rmq(dp);
    dp = vll(n);
    reep(j, i + 1, n) {
      ll t = 0;
      int a = max(j - m, 0);
      int b = j - 1;
      t = rmq.query(a, b);
      if (t)
        dp[j] = t - v[j] * (i + 2);
    }
    // 3 7 2 6 9 4 8 5 1 1000000000
    // if(i==0){
    // 	cout << -rmq.query(1,2) << endl;
    // }
    // rep(i,n){
    // 	rep(j,i+1){
    // 		cout<<j<<" "<<i<<" "<<-rmq.query(j,i)<<endl;
    // 	}
    // }
    // break;
    // rep(j,n){
    // 	cout << -dp[j] << " ";
    // }
    // cout<<endl;
  }
  ll ans = 0;
  rep(i, n) { maxs(ans, -dp[i]); }
  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  mainmain();
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
// #include <utility>
// #include <memory>
// #include <functional>
// #include <deque>
// #include <cctype>
// #include <ctime>
// #include <numeric>
// #include <list>
// #include <iomanip>

// #if __cplusplus >= 201103L
// #include <array>
// #include <tuple>
// #include <initializer_list>
// #include <forward_list>
//
// #define cauto const auto&
// #else

// #endif

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

typedef vector<int> vint;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll, vLL;
typedef vector<vector<long long>> vvll, vvLL;

#define VV(T) vector<vector<T>>

template <class T>
void initvv(vector<vector<T>> &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}

template <class F, class T> void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) reep((i), 0, (n))
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(), (v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x) cout << #x << " = " << (x) << endl;
#define debug(x)                                                               \
  cout << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \
       << " " << __FILE__ << endl;
#else
#define dump(x)
#define debug(x)
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)

template <class T> class RMQ {
public:
  vector<T> data;
  vector<vector<int>> block;
  vector<int> sblock;
  vector<int> lookup;
  int N;
  int lg;
  int argMin(int a, int b) { return data[b] < data[a] ? b : a; }
  // x の下位 y bit を 0 にする
  int maskBits(int x, int y) { return (x >> y) << y; }
  RMQ(vector<T> &v) {
    data = v;
    N = data.size();

    lg = 32 - __builtin_clz(N);

    block.assign((N + lg - 1) / lg, vector<int>(lg, 0));

    for (int i = 0; i < N; i++) {
      if (i % lg == 0)
        block[i / lg][0] = i;
      block[i / lg][0] = argMin(block[i / lg][0], i);
    }

    for (int j = 1; j < lg; j++) {
      for (int i = 0; i < block.size(); i++) {
        block[i][j] = argMin(
            block[i][j - 1],
            block[i + (1 << (j - 1)) < block.size() ? i + (1 << (j - 1))
                                                    : block.size() - 1][j - 1]);
      }
    }

    sblock.assign(N, 0);
    stack<int> st;
    for (int i = 0; i < N; i++) {
      if (i % lg == 0)
        while (st.size())
          st.pop();
      while (st.size() && i == argMin(i, st.top()))
        st.pop();
      if (st.size()) {
        int g = st.top();
        sblock[i] = sblock[g] | (1 << (g % lg));
      }
      st.push(i);
    }
  }
  // return = min{data[i] : i in [l .. r]}
  T query(int l, int r) {
    if (l == r)
      return data[l];
    int l1 = l / lg + 1;
    int r1 = r / lg - 1;
    int ret = l;
    if (l1 <= r1) {
      int d = r1 - l1 + 1;
      if (d == 1) {
        ret = argMin(ret, block[l1][0]);
      } else if (d == 2) {
        ret = argMin(ret, argMin(block[l1][0], block[r1][0]));
      } else {
        int lg2 = 32 - __builtin_clz(d) - 1;
        // int lg2 = (31 ^ __builtin_clz(d + 2)) - 1;
        ret = argMin(ret,
                     argMin(block[l1][lg2], block[r1 - (1 << lg2) + 1][lg2]));
      }
    }
    if (l1 - r1 == 2) { // same block
      int t = maskBits(sblock[r], l % lg);
      if (t == 0) {
        ret = argMin(ret, r);
      } else {
        ret = argMin(ret, __builtin_ctz(t) + (l1 - 1) * lg);
      }
    } else { // other block
      int t1 = maskBits(sblock[l1 * lg - 1], l % lg);
      if (t1 == 0) {
        ret = argMin(ret, l1 * lg - 1);
      } else {
        ret = argMin(ret, __builtin_ctz(t1) + (l1 - 1) * lg);
      }
      int t2 = sblock[r];
      if (t2 == 0) {
        ret = argMin(ret, r);
      } else {
        ret = argMin(ret, __builtin_ctz(t2) + (r1 + 1) * lg);
      }
    }
    return data[ret];
  }
};

void mainmain() {
  int n, m, K;
  cin >> n >> m >> K;

  // assert(n <= 300 && K <= 30);

  vll v(n);
  vll dp(n);
  rep(i, n) {
    cin >> v[i];
    dp[i] = -v[i];
  }
  rep(i, K - 1) {
    RMQ<ll> rmq(dp);
    dp = vll(n);
    reep(j, i + 1, n) {
      ll t = 0;
      int a = max(j - m, 0);
      int b = j - 1;
      t = rmq.query(a, b);
      if (t)
        dp[j] = t - v[j] * (i + 2);
    }
    // 3 7 2 6 9 4 8 5 1 1000000000
    // if(i==0){
    // 	cout << -rmq.query(1,2) << endl;
    // }
    // rep(i,n){
    // 	rep(j,i+1){
    // 		cout<<j<<" "<<i<<" "<<-rmq.query(j,i)<<endl;
    // 	}
    // }
    // break;
    // rep(j,n){
    // 	cout << -dp[j] << " ";
    // }
    // cout<<endl;
  }
  ll ans = 0;
  rep(i, n) { maxs(ans, -dp[i]); }
  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);
  mainmain();
}