alexjercan/bugnet huggingface dataset:
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

ll dp[3005][3005][4];
int A[3005][3005];

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

  rep(i, K) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    A[r][c] = v;
  }

  rep(i, R) rep(j, C) {
    if (i > 0) {
      rep(k, 4) { chmax(dp[i][j][0], dp[i - 1][j][k]); }
    }
    if (j > 0) {
      rep(k, 4) { chmax(dp[i][j][k], dp[i][j - 1][k]); }
    }
    int v = A[i][j];
    if (v > 0) {
      for (int k = 3; k > 0; k--) {
        chmax(dp[i][j][k], dp[i][j][k - 1] + v);
      }
    }
  }

  ll ans = 0;
  rep(k, 4) { chmax(ans, dp[R - 1][C - 1][k]); }
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

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

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

  ll dp[3005][3005][4];
  int A[3005][3005];
  rep(i, K) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    A[r][c] = v;
  }

  rep(i, R) rep(j, C) {
    if (i > 0) {
      rep(k, 4) { chmax(dp[i][j][0], dp[i - 1][j][k]); }
    }
    if (j > 0) {
      rep(k, 4) { chmax(dp[i][j][k], dp[i][j - 1][k]); }
    }
    int v = A[i][j];
    if (v > 0) {
      for (int k = 3; k > 0; k--) {
        chmax(dp[i][j][k], dp[i][j][k - 1] + v);
      }
    }
  }

  ll ans = 0;
  rep(k, 4) { chmax(ans, dp[R - 1][C - 1][k]); }
  cout << ans << endl;
  return 0;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

ll grid[3010][3010] = {};
ll dp[3010][3010][4] = {};

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

  for (int i = 0; i < k; i++) {
    int r, c;
    ll v;
    cin >> r >> c >> v;
    r--;
    c--;
    grid[r][c] = v;
  }

  dp[0][0][1] = grid[0][0];

  for (int r = 0; r < R; r++) {
    for (int c = 0; c < C; c++) {
      for (int i = 0; i < 4; i++) {
        if (r + 1 < R) {
          dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][i]);
          dp[r + 1][c][1] = max(dp[r + 1][c][1], dp[r][c][i] + grid[r + 1][c]);
        }
        if (c + 1 < C) {
          dp[r][c + 1][i] = max(dp[r][c + 1][i], dp[r][c][i]);
          if (i + 1 <= 3) {
            dp[r][c + 1][i + 1] =
                max(dp[r][c][i] + grid[r][c + 1], dp[r][c + 1][i + 1]);
          }
        }
      }
    }
  }
  cout << *max_element(dp[R - 1][C - 1], dp[R - 1][C - 1] + 4) << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int R, C, k;
  cin >> R >> C >> k;
  int grid[3010][3010];
  int r, c, v;
  rep(i, k) {
    cin >> r >> c >> v;
    r--;
    c--;
    grid[r][c] = v;
  }
  long long dp[3010][3010][4];
  dp[0][0][1] = grid[0][0];

  for (int r = 0; r < R; r++) {
    for (int c = 0; c < C; c++) {
      for (int i = 0; i < 4; i++) {
        if (r + 1 < R) {
          dp[r + 1][c][0] = max(dp[r + 1][c][0], dp[r][c][i]);
          dp[r + 1][c][1] = max(dp[r + 1][c][1], dp[r][c][i] + grid[r + 1][c]);
        }
        if (c + 1 < C) {
          dp[r][c + 1][i] = max(dp[r][c + 1][i], dp[r][c][i]);
          if (i + 1 <= 3) {
            dp[r][c + 1][i + 1] =
                max(dp[r][c][i] + grid[r][c + 1], dp[r][c + 1][i + 1]);
          }
        }
      }
    }
  }
  cout << *max_element(dp[R - 1][C - 1], dp[R - 1][C - 1] + 4) << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep3(i, s, n) for (int i = (s); i <= (int)(n); i++)
int rcv[3005][3005];
long long p[3005][3005][4];

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

  cin >> R >> C >> K;

  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) {
      zz = rcv[i][j];
      p[i][j][3] = max(p[i][j][3], p[i][j][2] + zz);
      p[i][j][2] = max(p[i][j][2], p[i][j][1] + zz);
      p[i][j][1] = max(p[i][j][1], p[i][j][0] + zz);
      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;
}


Error: WA

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];

  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 <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll v[3100][3100], dp[3100][3100][4];
int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  for (int i = 0; i < 3100; i++) {
    for (int j = 0; j < 3100; j++) {
      v[i][j] = 0;
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b, c;
    cin >> a >> b;
    a--;
    b--;
    cin >> c;
    v[a][b] = c;
  }
  if (v[0][0] > 0)
    dp[0][0][1] = v[0][0];
  for (int i = 0; i < 3100; i++) {
    for (int j = 0; j < 3100; j++) {
      for (int k = 0; k < 4; k++) {
        if (i - 1 >= 0) {
          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 - 1][j][k] + v[i][j]);
        }
        if (j - 1 >= 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k >= 1) {
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + v[i][j]);
          }
        }
      }
    }
  }
  ll ans = 0;
  for (int k = 0; k < 4; k++) {
    ans = max(ans, dp[r - 1][c - 1][k]);
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

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

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  ll v[3100][3100], dp[3100][3100][4];
  for (int i = 0; i < 3100; i++) {
    for (int j = 0; j < 3100; j++) {
      v[i][j] = 0;
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b, c;
    cin >> a >> b;
    a--;
    b--;
    cin >> c;
    v[a][b] = c;
  }
  if (v[0][0] > 0)
    dp[0][0][1] = v[0][0];
  for (int i = 0; i < 3100; i++) {
    for (int j = 0; j < 3100; j++) {
      for (int k = 0; k < 4; k++) {
        if (i - 1 >= 0) {
          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 - 1][j][k] + v[i][j]);
        }
        if (j - 1 >= 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k >= 1) {
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + v[i][j]);
          }
        }
      }
    }
  }
  ll ans = 0;
  for (int k = 0; k < 4; k++) {
    ans = max(ans, dp[r - 1][c - 1][k]);
  }
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

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

ll v[3100][3100], dp[3100][3100][4];
int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  for (int i = 0; i < 3100; i++) {
    for (int j = 0; j < 3100; j++) {
      v[i][j] = 0;
      for (int k = 0; k < 4; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b, c;
    cin >> a >> b;
    a--;
    b--;
    cin >> c;
    v[a][b] = c;
  }
  if (v[0][0] > 0)
    dp[0][0][1] = v[0][0];
  for (int i = 0; i < 3100; i++) {
    for (int j = 0; j < 3100; j++) {
      for (int k = 0; k < 4; k++) {
        if (i - 1 >= 0) {
          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 - 1][j][k] + v[i][j]);
        }
        if (j - 1 >= 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k >= 1) {
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + v[i][j]);
          }
        }
      }
    }
  }
  ll ans = 0;
  for (int k = 0; k < 4; k++) {
    ans = max(ans, dp[r - 1][c - 1][k]);
  }
  cout << ans << endl;
}

Error: -11

Fixed submission:

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

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  ll v[3100][3100], dp[3100][3100][5];
  for (int i = 0; i < 3100; i++) {
    for (int j = 0; j < 3100; j++) {
      v[i][j] = 0;
      for (int k = 0; k < 5; k++) {
        dp[i][j][k] = 0;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b, c;
    cin >> a >> b;
    a--;
    b--;
    cin >> c;
    v[a][b] = c;
  }
  if (v[0][0] > 0)
    dp[0][0][1] = v[0][0];
  for (int i = 0; i < 3100; i++) {
    for (int j = 0; j < 3100; j++) {
      for (int k = 0; k < 4; k++) {
        if (i - 1 >= 0) {
          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 - 1][j][k] + v[i][j]);
        }
        if (j - 1 >= 0) {
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
          if (k >= 1) {
            dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k - 1] + v[i][j]);
          }
        }
      }
    }
  }
  ll ans = 0;
  for (int k = 0; k <= 3; k++) {
    ans = max(ans, dp[r - 1][c - 1][k]);
    // cout << dp[1][1][i] << endl;
  }
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
ll gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }             // 最小公倍数
using Graph = vector<vector<int>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll o[r][c];
  rep(i, r) rep(j, c) o[i][j] = 0;
  rep(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    o[a][b] = c;
  }
  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4)));
  rep(i, r) rep(j, c) rep(l, 4) dp[i][j][l] = 0;
  dp[0][0][1] = o[0][0];
  rep(i, r) {
    rep(j, c) {
      if (i + j == 0)
        continue;
      if (o[i][j] == 0) {
        rep(l, 4) {
          if (j != 0) {
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
          }
          if (i != 0) {
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
          }
        }
      } else {
        if (j != 0) {
          rep(l, 4) {
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
            if (l != 3)
              dp[i][j][l + 1] = max(dp[i][j][l + 1], o[i][j] + dp[i][j - 1][l]);
          }
        }
        if (i != 0) {
          rep(l, 4) {
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
            dp[i][j][1] = max(dp[i][j][1], o[i][j] + dp[i - 1][j][l]);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
ll gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }             // 最小公倍数
using Graph = vector<vector<int>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll o[r][c];
  rep(i, r) rep(j, c) o[i][j] = 0;
  rep(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    o[a][b] = c;
  }
  ll dp[r][c][4];
  rep(i, r) rep(j, c) rep(l, 4) dp[i][j][l] = 0;
  dp[0][0][1] = o[0][0];
  rep(i, r) {
    rep(j, c) {
      if (i + j == 0)
        continue;
      if (o[i][j] == 0) {
        rep(l, 4) {
          if (j != 0) {
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
          }
          if (i != 0) {
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
          }
        }
      } else {
        if (j != 0) {
          rep(l, 4) {
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
            if (l != 3)
              dp[i][j][l + 1] = max(dp[i][j][l + 1], o[i][j] + dp[i][j - 1][l]);
          }
        }
        if (i != 0) {
          rep(l, 4) {
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
            dp[i][j][1] = max(dp[i][j][1], o[i][j] + dp[i - 1][j][l]);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
ll gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }             // 最小公倍数
using Graph = vector<vector<int>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll o[r][c];
  rep(i, r) rep(j, c) o[i][j] = 0;
  rep(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    o[a][b] = c;
  }
  vector<vector<vector<ll>>> dp(r, vector<vector<ll>>(c, vector<ll>(4, 0)));
  rep(i, r) rep(j, c) rep(l, 4) dp[i][j][l] = 0;
  if (o[0][0] > 0) {
    dp[0][0][1] = o[0][0];
  }
  rep(i, r) {
    rep(j, c) {
      if (i + j == 0)
        continue;
      if (o[i][j] == 0) {
        rep(l, 4) {
          if (j != 0) {
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
          }
          if (i != 0) {
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
          }
        }
      } else {
        if (j != 0) {
          rep(l, 4) {
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
            if (l != 3)
              dp[i][j][l + 1] = max(dp[i][j][l + 1], o[i][j] + dp[i][j - 1][l]);
          }
        }
        if (i != 0) {
          rep(l, 4) {
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
            dp[i][j][1] = max(dp[i][j][1], o[i][j] + dp[i - 1][j][l]);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(i, 4) ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ll long long
ll gcd(int x, int y) { return (x % y) ? gcd(y, x % y) : y; } // 最大公約数
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }             // 最小公倍数
using Graph = vector<vector<int>>;
ll inf = 300000000000000000;
const double PI = 3.14159265358979323846;
int main() {
  int r, c, k;
  cin >> r >> c >> k;
  ll o[r][c];
  rep(i, r) rep(j, c) o[i][j] = 0;
  rep(i, k) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    o[a][b] = c;
  }
  ll dp[r][c][4];
  rep(i, r) rep(j, c) rep(l, 4) dp[i][j][l] = 0;
  if (o[0][0] > 0) {
    dp[0][0][1] = o[0][0];
  }
  cout << 1 << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

// E - Picking Goods
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define chmax(x, y) x = max(x, (y))

int V[3000 + 1][3000 + 1];
ll dp[3000 + 1][3000 + 1][3 + 1];

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

  rep(i, K) {
    int r, c, v;
    cin >> r >> c >> v;
    V[r][c] = v;
  }

  rep(i, R + 1) rep(j, C + 1) rep(k, 3 + 1) {
    if (i - 1 >= 0) {
      chmax(dp[i][j][0], dp[i - 1][j][k]);
      chmax(dp[i][j][1], dp[i - 1][j][k] + V[i][j]);
    }
    if (j - 1 >= 0) {
      chmax(dp[i][j][k], dp[i][j - 1][k]);
      if (k - 1 >= 0)
        chmax(dp[i][j][k], dp[i][j - 1][k - 1] + V[i][j]);
    }
  }

  ll ans = 0;
  rep(k, 3 + 1) chmax(ans, dp[R][C][k]);
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
#define all(x) (x).begin(), (x).end()
#define rp(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define irp(i, s, e) for (int i = (int)(s); i > (int)(e); --i)

int R, C, K;
vvi mesh;

ll dfs(int i, int j, vvi rows, vvi maxs) {
  ll v = 0;
  if (i == R - 1 && j == C - 1) {
    maxs[i][3] = mesh[i][j];
    sort(maxs[i].rbegin(), maxs[i].rend());
    for (auto &x : maxs) {
      v += x[0] + x[1] + x[2];
    }
    return v;
  }
  if (i == R || j == C)
    return v;
  rows[i].push_back(mesh[i][j]);
  maxs[i][3] = mesh[i][j];
  sort(maxs[i].rbegin(), maxs[i].rend());
  if (i + 1 < R)
    v = max(v, dfs(i + 1, j, rows, maxs));
  if (j + 1 < C)
    v = max(v, dfs(i, j + 1, rows, maxs));
  rows[i].pop_back();
  return v;
}

int main() {
  cin >> R >> C >> K;
  mesh.resize(R, vi(C, 0));
  rp(i, 0, K) {
    int r, c, v;
    cin >> r >> c >> v;
    mesh[r - 1][c - 1] = v;
  }
  vvi maxs(R, vi(4, 0));
  cout << dfs(0, 0, vvi(R, vi()), maxs) << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int H, W, K;
int M[3000][3000];
long dp[3000][3000][4];

int main() {
  cin >> H >> W >> K;
  int r, c, v;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      M[i][j] = 0;
  for (int i = 0; i < K; i++) {
    cin >> r >> c >> v;
    r--;
    c--;
    M[r][c] = v;
  }
  dp[0][0][0] = 0;
  dp[0][0][1] = dp[0][0][2] = dp[0][0][3] = M[0][0];
  for (int i = 1; i < H; i++) {
    for (int k = 0; k <= 3; k++) {
      dp[i][0][k] = dp[i - 1][0][3];
      if (k > 0)
        dp[i][0][k] = max(dp[i][0][k], dp[i - 1][0][3] + M[i][0]);
    }
  }
  for (int j = 1; j < W; j++) {
    for (int k = 0; k <= 3; k++) {
      dp[0][j][k] = dp[0][j - 1][k];
      if (k > 0)
        dp[0][j][k] = max(dp[0][j][k], dp[0][j - 1][k - 1] + M[0][j]);
    }
  }
  for (int i = 1; i < H; i++) {
    for (int j = 1; j < W; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = max(dp[i - 1][j][3], dp[i][j - 1][k]);
        if (k > 0)
          dp[i][j][k] = max(
              dp[i][j][k], max(dp[i - 1][j][3], dp[i][j - 1][k - 1]) + M[i][j]);
      }
    }
  }
  cout << dp[H - 1][W - 1][3] << endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int M[H][W], r, c, v;
  for (int i = 0; i < H; i++)
    for (int j = 0; j < W; j++)
      M[i][j] = 0;
  for (int i = 0; i < K; i++) {
    cin >> r >> c >> v;
    r--;
    c--;
    M[r][c] = v;
  }
  long dp[H][W][4];
  dp[0][0][0] = 0;
  dp[0][0][1] = dp[0][0][2] = dp[0][0][3] = M[0][0];
  for (int i = 1; i < H; i++) {
    for (int k = 0; k <= 3; k++) {
      dp[i][0][k] = dp[i - 1][0][3];
      if (k > 0)
        dp[i][0][k] = max(dp[i][0][k], dp[i - 1][0][3] + M[i][0]);
    }
  }
  for (int j = 1; j < W; j++) {
    for (int k = 0; k <= 3; k++) {
      dp[0][j][k] = dp[0][j - 1][k];
      if (k > 0)
        dp[0][j][k] = max(dp[0][j][k], dp[0][j - 1][k - 1] + M[0][j]);
    }
  }
  for (int i = 1; i < H; i++) {
    for (int j = 1; j < W; j++) {
      for (int k = 0; k <= 3; k++) {
        dp[i][j][k] = max(dp[i - 1][j][3], dp[i][j - 1][k]);
        if (k > 0)
          dp[i][j][k] = max(
              dp[i][j][k], max(dp[i - 1][j][3], dp[i][j - 1][k - 1]) + M[i][j]);
      }
    }
  }
  cout << dp[H - 1][W - 1][3] << endl;
}
Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
long long R, C, K;
vector<vector<long long>> a;
long long dfs(int s, int t, priority_queue<long long> q) {
  long long res = 0, res1 = 0;
  ;
  priority_queue<long long> p, q1;
  if (a[s][t] != 0)
    q.push(a[s][t]);
  q1 = q;
  int flag = 0;
  if (t + 1 < C) {
    res1 = dfs(s, t + 1, q1);
    flag = 1;
  }
  if (s + 1 < R) {
    long long r = 0;
    int cnt = 0;
    while (!q.empty() && cnt != 3) {
      r += q.top();
      q.pop();
      cnt++;
    }
    res = dfs(s + 1, t, p) + r;
    flag = 1;
  }
  if (flag == 0) {
    int cnt = 0;
    while (!q.empty() && cnt != 3) {
      cnt++;
      res += q.top();
      q.pop();
    }
  }
  return max(res, res1);
}
int main() {
  cin >> R >> C >> K;
  vector<long long> r(K), c(K), v(K);
  a.resize(R, vector<long long>(C, 0));
  for (int i = 0; i < K; i++) {
    cin >> r[i] >> c[i] >> v[i];
    r[i]--;
    c[i]--;
    a[r[i]][c[i]] = v[i];
  }
  priority_queue<long long> q;
  long long ans = dfs(0, 0, q);
  cout << ans << endl;
}