alexjercan/bugnet huggingface dataset:
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef tuple<ll, ll, ll> ti;

#define REP(a, b, c) for (ll a = b; a < (c); a++)
#define PER(a, b, c) for (ll a = b; a >= (c); a--)

inline ll ii() {
  ll x;
  cin >> x;
  return x;
}
inline string is() {
  string x;
  cin >> x;
  return x;
}
inline ld id() {
  ld x;
  cin >> x;
  return x;
}
inline void oi(ll x) { cout << x; }
inline void od(ld x) { cout << fixed << setprecision(10) << x; }
inline void os(string x) { cout << x; }
inline void oe() { cout << endl; }
inline void oie(ll x) {
  oi(x);
  oe();
}
inline void ode(ld x) {
  od(x);
  oe();
}
inline void ose(string x) {
  os(x);
  oe();
}

inline void maxin(ll &a, ll b) { a = max(a, b); }

ll m[3123][3123][5];
ll item[3123][3123];

int main() {
  ll R = ii(), C = ii(), K = ii();
  REP(i, 0, K) {
    ll r = ii();
    ll c = ii();
    ll v = ii();
    item[c - 1][r - 1] = v;
  }
  REP(y, 0, R + 1) {
    REP(x, 0, C + 1) {
      REP(k, 0, 4) {
        maxin(m[x + 1][y][k], m[x][y][k]);
        if (k < 3)
          maxin(m[x + 1][y][k + 1], m[x][y][k] + item[x][y]);

        maxin(m[x][y + 1][0], m[x][y][k]);
        if (k < 3)
          maxin(m[x][y + 1][0], m[x][y][k] + item[x][y]);
      }
    }
  }
  ll ret = 0;
  REP(k, 0, 4) { maxin(ret, m[C][R][k]); }
  oie(ret);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef tuple<ll, ll, ll> ti;

#define REP(a, b, c) for (ll a = b; a < (c); a++)
#define PER(a, b, c) for (ll a = b; a >= (c); a--)

inline ll ii() {
  ll x;
  cin >> x;
  return x;
}
inline string is() {
  string x;
  cin >> x;
  return x;
}
inline ld id() {
  ld x;
  cin >> x;
  return x;
}
inline void oi(ll x) { cout << x; }
inline void od(ld x) { cout << fixed << setprecision(10) << x; }
inline void os(string x) { cout << x; }
inline void oe() { cout << endl; }
inline void oie(ll x) {
  oi(x);
  oe();
}
inline void ode(ld x) {
  od(x);
  oe();
}
inline void ose(string x) {
  os(x);
  oe();
}

std::map<ti, ll> m;
std::map<pii, ll> item;
ll R, C, K;
ll ret = 0;

void f(ll r, ll c, ll k, ll p) {
  ret = max(ret, p);
  if (r > R || c > C)
    return;
  if (k >= 3)
    return;

  ti t = make_tuple(r, c, k);
  auto mm = m.find(t);
  if (mm != m.end() && mm->second > p)
    return;
  m[t] = p;

  f(r + 1, c, 0, p);
  f(r, c + 1, k, p);
  auto ss = item.find(make_pair(r, c));
  if (ss != item.end()) {
    f(r + 1, c, 0, p + ss->second);
    f(r, c + 1, k + 1, p + ss->second);
  }
  return;
}

int main() {
  R = ii();
  C = ii();
  K = ii();
  REP(i, 0, K) {
    ll r = ii();
    ll c = ii();
    ll v = ii();
    item[make_pair(r, c)] = v;
  }
  f(1, 1, 0, 0);
  oie(ret);
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

// E.

#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <vector>

using namespace std;

typedef long long LL;
typedef pair<int, int> II;

int main(int argc, char *argv[]) {
  cout.precision(20);
#ifdef _MSC_VER
  while (true)
#endif
  {
    int r = -1, c, k;
    cin >> r >> c >> k;
    if (r < 0)
      return 0;
    map<II, LL> m;
    for (LL i = 0; i < k; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      m[II(a, b)] = c;
    }
    vector<vector<vector<LL>>> dp(2, vector<vector<LL>>(4, vector<LL>(c + 1)));
    for (int i = 1; i <= r; ++i) {
      int curr = i & 1, prev = curr ^ 1;
      for (int j = 1; j <= c; ++j) {
        for (int k = 0; k <= 3; ++k) {
          dp[curr][k][j] = dp[curr][k][j - 1];
        }
        LL &zero = dp[curr][0][j];
        for (int k = 0; k <= 3; ++k) {
          zero = max(zero, dp[prev][k][j]);
        }
        II key(i, j);
        if (m.find(key) != m.end()) {
          for (int k = 3; k > 0; --k) {
            dp[curr][k][j] =
                max(dp[curr][k][j], dp[curr][k - 1][j] + m[II(i, j)]);
          }
        }
      }
    }
    LL ans = 0;
    for (int k = 0; k <= 3; ++k) {
      ans = max(ans, dp[r & 1][k][c]);
    }
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

// E.

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <sstream>
#include <vector>

using namespace std;

typedef long long LL;
typedef pair<int, int> II;
typedef long double LD;

bool possible(int n, int k, LD x, const vector<LD> &a) {
  int cnt = 0;
  for (LD d : a) {
    cnt += (int)ceil(d / x) - 1;
  }
  return cnt <= k;
}

int main(int argc, char *argv[]) {
  cout.precision(20);
#ifdef _MSC_VER
  while (true)
#endif
  {
    int r = -1, c, k;
    cin >> r >> c >> k;
    if (r < 0)
      return 0;
    map<II, LL> m;
    for (LL i = 0; i < k; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      m[II(a, b)] = c;
    }
    vector<vector<LL>> dp(4, vector<LL>(c + 1));
    for (int i = 1; i <= r; ++i) {
      vector<vector<LL>> next(4, vector<LL>(c + 1));
      for (int j = 1; j <= c; ++j) {
        for (int k = 0; k <= 3; ++k) {
          next[0][j] = max(next[0][j], dp[k][j]);
          next[k][j] = max(next[k][j], next[k][j - 1]);
        }
        for (int k = 3; k > 0; --k) {
          next[k][j] = max(next[k][j], next[k - 1][j] + m[II(i, j)]);
        }
      }
      dp = next;
    }
    LL ans = 0;
    for (int k = 0; k <= 3; ++k) {
      ans = max(ans, dp[k][c]);
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

#define pb push_back
#define mp make_pair
#define all(n) (n).begin(), (n).end()
#define fi first
#define se second

typedef pair<int, int> PII;
typedef long long ll;

const int MOD = 1e9 + 7;
ll gr[3001][3001];
ll dp[3001][3001][4];
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

  int r, c, k;
  cin >> r >> c >> k;

  memset(gr, 0, sizeof(gr));
  for (int i = 0; i < k; i++) {
    int a, b, v;
    cin >> a >> b >> v;
    gr[a][b] = v;
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      dp[i][j][0] = max(dp[i][j - 1][0], dp[i - 1][j][0]);
      for (int k = 1; k < 4; k++) {
        dp[i][j][k] = max(dp[i][j - 1][k], dp[i][j - 1][k - 1] + gr[i][j]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
      }
      dp[i][j][1] = max(dp[i][j][1], dp[i][j][0] + gr[i][j]);
      // cout << dp[i][j][0] << " " << dp[i][j][1] << " " << dp[i][j][2] << " "
      // << dp[i][j][3] << "\\n";
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++)
    ans = max(ans, dp[r][c][i]);
  cout << ans << "\\n";
}

Error: -11

Fixed submission:

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

#define pb push_back
#define mp make_pair
#define all(n) (n).begin(), (n).end()
#define fi first
#define se second

typedef pair<int, int> PII;
typedef long long ll;

const int MOD = 1e9 + 7;
#define MAXN 3009
int main() {

  ios::sync_with_stdio(0);
  cin.tie(0);

  int r, c, k;
  cin >> r >> c >> k;
  ll gr[MAXN][MAXN];
  memset(gr, 0, sizeof(gr));
  for (int i = 0; i < k; i++) {
    int a, b, v;
    cin >> a >> b >> v;
    gr[a][b] = v;
  }
  ll dp[MAXN][MAXN][4];
  memset(dp, 0, sizeof(dp));
  for (int i = 1; i <= r; i++) {
    for (int j = 1; j <= c; j++) {
      dp[i][j][0] = max(dp[i][j - 1][0], dp[i - 1][j][0]);
      for (int k = 1; k < 4; k++) {
        dp[i][j][k] = max(dp[i][j - 1][k], dp[i][j - 1][k - 1] + gr[i][j]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][k]);
      }
      dp[i][j][1] = max(dp[i][j][1], dp[i][j][0] + gr[i][j]);
      // cout << dp[i][j][0] << " " << dp[i][j][1] << " " << dp[i][j][2] << " "
      // << dp[i][j][3] << "\\n";
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++)
    ans = max(ans, dp[r][c][i]);
  cout << ans << "\\n";
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep3(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define chmax(x, y) x = max(x, y)

int a[3005][3005];
long long dp[3005][3005][5];

int main() {
  int R, C, n;
  cin >> R >> C >> n;

  rep(i, n) {
    int r, c, v;
    cin >> r >> c >> v;
    a[r - 1][c - 1] = v;
  }

  rep(i, R) {
    rep(j, C) {
      for (int k = 2; k >= 0; k--) {
        chmax(dp[i][j][k + 1], dp[i][j][k] + a[i][j]);
      }
      rep(k, 4) {
        chmax(dp[i + 1][j][0], dp[i][j][k]);
        chmax(dp[i][j + 1][k], dp[i][j][k]);
      }
    }
  }
  long long ans = 0;
  rep(k, 4) chmax(ans, dp[R - 1][C - 1][k]);

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep3(i, s, n) for (int i = (s); i <= (int)(n); i++)

int main() {
  int R;
  int C;
  int K;
  int x;
  int y;
  long long z;
  long long ans;

  cin >> R >> C >> K;

  int rcv[R + 5][C + 5];
  long long p[R + 5][C + 5][5];

  rep(i, R + 2) {
    rep(j, C + 2) {
      rcv[i][j] = 0;
      rep(kk, 4) p[i][j][kk] = 0;
    }
  }

  rep3(i, 1, K) {
    cin >> x >> y >> z;
    rcv[x][y] = z;
  }

  rep3(i, 1, R) {
    rep3(j, 1, C) {
      z = rcv[i][j];
      p[i][j][3] = max(p[i][j][3], p[i][j][2] + z);
      p[i][j][2] = max(p[i][j][2], p[i][j][1] + z);
      p[i][j][1] = max(p[i][j][1], p[i][j][0] + z);
      p[i + 1][j][0] = max(p[i + 1][j][0], p[i][j][0]);
      p[i + 1][j][0] = max(p[i + 1][j][0], p[i][j][1]);
      p[i + 1][j][0] = max(p[i + 1][j][0], p[i][j][2]);
      p[i + 1][j][0] = max(p[i + 1][j][0], p[i][j][3]);
      p[i][j + 1][0] = max(p[i][j + 1][0], p[i][j][0]);
      p[i][j + 1][1] = max(p[i][j + 1][1], p[i][j][1]);
      p[i][j + 1][2] = max(p[i][j + 1][2], p[i][j][2]);
      p[i][j + 1][3] = max(p[i][j + 1][3], p[i][j][3]);
    }
  }
  ans = max(p[R][C][0], p[R][C][1]);
  ans = max(ans, p[R][C][2]);
  ans = max(ans, p[R][C][3]);

  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()

typedef long long lint;

using namespace std;
lint dp[5000][5000][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  map<pair<int, int>, lint> m;
  REP(i, K) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    m[make_pair(r, c)] = v;
  }
  REP(r, R) {
    REP(c, C) {
      REP(i, 4) dp[r][c][i] = 0;
      if (r > 0) {
        REP(i, 4) dp[r][c][0] = max(dp[r][c][0], dp[r - 1][c][i]);
      }
      if (c > 0) {
        REP(i, 4) dp[r][c][i] = max(dp[r][c][i], dp[r][c - 1][i]);
      }
      if (m.find(make_pair(r, c)) != m.end()) {
        for (int i = 2; i >= 0; i--)
          dp[r][c][i + 1] =
              max(dp[r][c][i + 1], dp[r][c][i] + m[make_pair(r, c)]);
      }
    }
  }
  lint ans = 0;
  REP(i, 4) ans = max(ans, dp[R - 1][C - 1][i]);
  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()

typedef long long lint;

using namespace std;

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  map<pair<int, int>, lint> m;
  REP(i, K) {
    int r, l, v;
    cin >> r >> l >> v;
    r--;
    l--;
    m[make_pair(r, l)] = v;
  }
  lint dp[R][C][4];
  REP(r, R) {
    REP(c, C) {
      REP(i, 4) dp[r][c][i] = 0;
      if (r > 0) {
        REP(i, 4) dp[r][c][0] = max(dp[r][c][0], dp[r - 1][c][i]);
      }
      if (c > 0) {
        REP(i, 4) dp[r][c][i] = max(dp[r][c][i], dp[r][c - 1][i]);
      }
      if (m.find(make_pair(r, c)) != m.end()) {
        for (int i = 2; i >= 0; i--)
          dp[r][c][i + 1] =
              max(dp[r][c][i + 1], dp[r][c][i] + m[make_pair(r, c)]);
      }
    }
  }
  lint ans = 0;
  REP(i, 4) ans = max(ans, dp[R - 1][C - 1][i]);
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

ll dp[3001][3001][3], a[3001][3001];
int vis[3001][3001][3];

ll func(int r, int c, int cnt) {
  if (r < 0 || c < 0 || cnt < 0)
    return 0;
  if (vis[r][c][cnt])
    return dp[r][c][cnt];
  vis[r][c][cnt] = 1;
  return dp[r][c][cnt] = max({func(r - 1, c, 2) + a[r][c], func(r, c - 1, cnt),
                              func(r, c - 1, cnt - 1) + a[r][c]});
}

int main() {
  ll r, c, items, x, y, v, maxi = 0;
  cin >> r >> c >> items;
  while (items--) {
    cin >> x >> y >> v;
    a[x - 1][y - 1] = v;
  }
  maxi = func(r - 1, c - 1, 2);
  cout << maxi;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

ll dp[3001][3001][3], a[3001][3001];

ll func(int r, int c, int cnt) {
  if (r < 0 || c < 0 || cnt < 0)
    return 0;
  if (dp[r][c][cnt])
    return dp[r][c][cnt];
  return dp[r][c][cnt] = max({func(r - 1, c, 2) + a[r][c], func(r, c - 1, cnt),
                              func(r, c - 1, cnt - 1) + a[r][c]});
}

int main() {
  ll r, c, items, x, y, v, maxi = 0;
  cin >> r >> c >> items;
  while (items--) {
    cin >> x >> y >> v;
    a[x - 1][y - 1] = v;
  }
  maxi = func(r - 1, c - 1, 2);
  cout << maxi;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

ll dp[3001][3001][4];

int main() {
  int r, c, items, x, y, v;
  cin >> r >> c >> items;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < 4; k++)
        dp[i][j][k] = 0;
    }
  }
  ll a[r + 1][c + 1];
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      a[i][j] = 0;
  while (items--) {
    cin >> x >> y >> v;
    a[x - 1][y - 1] = v;
  }
  if (r == 1 && c == 1) {
    cout << a[r - 1][c - 1];
    return 0;
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 2; k >= 0; k--) {
        dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + a[i][j]);
      }
      for (int k = 0; k < 4; k++) {
        if (j + 1 != c)
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (i + 1 != r)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }
  ll maxi = 0;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < 4; k++) {
        maxi = max(maxi, dp[i][j][k]);
      }
    }
  }
  cout << maxi;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main() {
  int r, c, items, x, y, v;
  cin >> r >> c >> items;
  ll dp[3001][3001][4];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < 4; k++)
        dp[i][j][k] = 0;
    }
  }
  ll a[r + 1][c + 1];
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      a[i][j] = 0;
  while (items--) {
    cin >> x >> y >> v;
    a[x - 1][y - 1] = v;
  }
  if (r == 1 && c == 1) {
    cout << a[r - 1][c - 1];
    return 0;
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 2; k >= 0; k--) {
        dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + a[i][j]);
      }
      for (int k = 0; k < 4; k++) {
        if (j + 1 != c)
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (i + 1 != r)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }
  ll maxi = 0;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < 4; k++) {
        maxi = max(maxi, dp[i][j][k]);
      }
    }
  }
  cout << maxi;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

ll dp[3001][3001][4], a[3001][3001];

int main() {
  ll r, c, items, x, y, v, maxi = 0;
  cin >> r >> c >> items;
  while (items--) {
    cin >> x >> y >> v;
    a[x - 1][y - 1] = v;
  }
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++) {
      for (int k = 2; k >= 0; k--) {
        dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + a[i][j]);
      }
      for (int k = 0; k < 4; k++) {
        if (j + 1 != c)
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (i + 1 != r)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      for (int k = 0; k < 4; k++)
        maxi = max(maxi, dp[i][j][k]);
  cout << maxi;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define mp make_pair
// GCD inbuilt func: __gcd(a,b)
// LCM formula: (a*b)/__gcd(a,b)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  // CODE goes here
  int r, c, items;
  cin >> r >> c >> items;
  ll dp[r][c][4];
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < 4; k++)
        dp[i][j][k] = 0;
    }
  }
  ll a[r][c];
  for (int i = 0; i < r; i++)
    for (int j = 0; j < c; j++)
      a[i][j] = 0;
  while (items--) {
    ll i, j, v;
    cin >> i >> j >> v;
    a[i - 1][j - 1] = v;
  }
  if (r == 1 && c == 1) {
    cout << a[r - 1][c - 1];
    return 0;
  }
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 2; k >= 0; k--) {
        dp[i][j][k + 1] = max(dp[i][j][k + 1], dp[i][j][k] + a[i][j]);
      }
      for (int k = 0; k < 4; k++) {
        if (j + 1 != c)
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (i + 1 != r)
          dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
      }
    }
  }
  ll maxi = 0;
  for (int i = 0; i < r; i++) {
    for (int j = 0; j < c; j++) {
      for (int k = 0; k < 4; k++) {
        maxi = max(maxi, dp[i][j][k]);
      }
    }
  }
  cout << maxi;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
long long D[3010][3010];
long long dp[3010][3010][4];
int main() {
  long long R, C, K;
  cin >> R >> C >> K;
  long long r, c, v;
  for (int i = 0; i < K; i++) {
    cin >> r >> c >> v;
    r--;
    c--;
    D[r][c] = v;
  }
  dp[0][0][1] = D[0][0];
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      if (j + 1 < C) {
        dp[i][j + 1][3] =
            max({dp[i][j + 1][3], dp[i][j][3], dp[i][j][2] + D[i][j + 1]});
        dp[i][j + 1][2] =
            max({dp[i][j + 1][2], dp[i][j][2], dp[i][j][1] + D[i][j + 1]});
        // cout<<i<<" "<<j<<"#"<<dp[i][j+1][2]<<endl;
        dp[i][j + 1][1] =
            max({dp[i][j + 1][1], dp[i][j][1], dp[i][j][0] + D[i][j + 1]});
        dp[i][j + 1][0] = max(dp[i][j + 1][0], dp[i][j][0]);
      }
      if (i + 1 < R) {
        for (int k = 0; k < 4; k++) {
          dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0]);
          dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + D[i + 1][j]);
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[R - 1][C - 1][i]);
  }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

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

int main() {
  long long R, C, K;
  cin >> R >> C >> K;
  long long D[3000][3000];
  long long dp[3000][3000][4];
  long long r, c, v;
  for (int i = 0; i < K; i++) {
    cin >> r >> c >> v;
    r--;
    c--;
    D[r][c] = v;
  }
  dp[0][0][1] = D[0][0];
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      if (j + 1 < C) {
        dp[i][j + 1][3] =
            max({dp[i][j + 1][3], dp[i][j][3], dp[i][j][2] + D[i][j + 1]});
        dp[i][j + 1][2] =
            max({dp[i][j + 1][2], dp[i][j][2], dp[i][j][1] + D[i][j + 1]});
        // cout<<i<<" "<<j<<"#"<<dp[i][j+1][2]<<endl;
        dp[i][j + 1][1] =
            max({dp[i][j + 1][1], dp[i][j][1], dp[i][j][0] + D[i][j + 1]});
        dp[i][j + 1][0] = max(dp[i][j + 1][0], dp[i][j][0]);
      }
      if (i + 1 < R) {
        for (int k = 0; k < 4; k++) {
          dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0]);
          dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + D[i + 1][j]);
        }
      }
    }
  }
  long long ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[R - 1][C - 1][i]);
  }
  cout << ans << endl;
  /*for(int i=0;i<R;i++){
    for(int j=0;j<C;j++){
     cout<<dp[i][j][0]<<endl;
     cout<<dp[i][j][1]<<endl;
     cout<<dp[i][j][2]<<endl;
      //cout<<"#"<<endl;
     //cout<<dp[i][j][3]<<endl;
    }
  }
  */
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  vector<vector<int>> m(R + 1, vector<int>(C + 1));
  int r, c, v;
  for (int i = 0; i != K; ++i) {
    cin >> r >> c >> v;
    m[r][c] = v;
  }
  ll dp[2][3001][4];
  fill((ll *)dp, (ll *)dp + 2 * 3001 * 4, 0ll);

  for (int i = 1; i <= R; ++i) {
    for (int j = 1; j <= C; ++j) {
      auto umax =
          max({dp[i % 2][j - 1][0], dp[1 - i % 2][j][0], dp[1 - i % 2][j][1],
               dp[1 - i % 2][j][2], dp[1 - i % 2][j][3]});
      dp[i % 2][j][0] = umax;
      if (int V = m[i][j]; V) {
        dp[i % 2][j][1] = max(dp[i % 2][j - 1][1], V + umax);
        dp[i % 2][j][2] = max(dp[i % 2][j - 1][2], dp[i % 2][j - 1][1] + V);
        dp[i % 2][j][3] = max(dp[i % 2][j - 1][3], dp[i % 2][j - 1][2] + V);
      } else {
        dp[i % 2][j][1] = dp[i % 2][j - 1][1];
        dp[i % 2][j][2] = dp[i % 2][j - 1][2];
        dp[i % 2][j][3] = dp[i % 2][j - 1][3];
      }
    }
  }
  cout << *max_element(dp[R % 2][C], dp[R % 2][C] + 4) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  map<pair<int, int>, int> m;
  int r, c, v;
  for (int i = 0; i != K; ++i) {
    cin >> r >> c >> v;
    m[{r, c}] = v;
  }
  ll dp[2][3001][4];
  for (int i = 0; i != 2; ++i) {
    for (int j = 0; j != 3000; ++j) {
      for (int k = 0; k != 3; ++k) {
        dp[i][j][k] = 0;
      }
    }
  }

  for (int i = 1; i <= R; ++i) {
    for (int j = 1; j <= C; ++j) {
      auto umax =
          max({dp[i % 2][j - 1][0], dp[1 - i % 2][j][0], dp[1 - i % 2][j][1],
               dp[1 - i % 2][j][2], dp[1 - i % 2][j][3]});
      dp[i % 2][j][0] = umax;
      if (int V = m[{i, j}]; V) {
        dp[i % 2][j][1] = max(dp[i % 2][j - 1][1], V + umax);
        dp[i % 2][j][2] = max(dp[i % 2][j - 1][2], dp[i % 2][j - 1][1] + V);
        dp[i % 2][j][3] = max(dp[i % 2][j - 1][3], dp[i % 2][j - 1][2] + V);
      } else {
        dp[i % 2][j][1] = dp[i % 2][j - 1][1];
        dp[i % 2][j][2] = dp[i % 2][j - 1][2];
        dp[i % 2][j][3] = dp[i % 2][j - 1][3];
      }
    }
  }
  cout << *max_element(dp[R % 2][C], dp[R % 2][C] + 4) << endl;
}