Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using ll = long long;

void solve(long long R, long long C, long long K, std::vector<long long> r,
           std::vector<long long> c, std::vector<long long> v) {
  vector<vector<vector<ll>>> dp(R, vector<vector<ll>>(C, vector<ll>(4)));
  vector<vector<ll>> V(R, vector<ll>(C));
  for (int i = 0; i < K; i++) {
    V[r[i] - 1][c[i] - 1] = v[i];
  }
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        ll v = V[i][j];
        if (i + 1 < R)
          dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0]);
        if (j + 1 < C)
          dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k]);
        if (v != 0 && k != 3) {
          if (i + 1 < R)
            dp[i + 1][j][0] = max(dp[i][j][k] + v, dp[i + 1][j][0]);
          if (j + 1 < C)
            dp[i][j + 1][k + 1] = max(dp[i][j][k] + v, dp[i][j + 1][k + 1]);
        }
      }
    }
  }
  for (int i = 2; i >= 0; i--) {
    ll v = V[R - 1][C - 1];
    dp[R - 1][C - 1][i + 1] =
        max(dp[R - 1][C - 1][i + 1], dp[R - 1][C - 1][i] + v);
  }
  cout << *max_element(dp[R - 1][C - 1].begin(), dp[R - 1][C - 1].end())
       << endl;
}

int main() {
  long long R;
  scanf("%lld", &R);
  long long C;
  scanf("%lld", &C);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> r(K);
  std::vector<long long> c(K);
  std::vector<long long> v(K);
  for (int i = 0; i < K; i++) {
    scanf("%lld", &r[i]);
    scanf("%lld", &c[i]);
    scanf("%lld", &v[i]);
  }
  solve(R, C, K, std::move(r), std::move(c), std::move(v));
  return 0;
}


Error: TLE

Fixed submission:

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

using ll = long long;

void solve(long long R, long long C, long long K, std::vector<long long> r,
           std::vector<long long> c, std::vector<long long> v) {
  vector<vector<vector<ll>>> dp(R, vector<vector<ll>>(C, vector<ll>(4)));
  map<pair<int, int>, ll> mp;
  for (int i = 0; i < K; i++) {
    mp[{r[i] - 1, c[i] - 1}] = v[i];
  }
  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      for (int k = 0; k < 4; k++) {
        ll v = mp[{i, j}];
        if (i + 1 < R)
          dp[i + 1][j][0] = max(dp[i][j][k], dp[i + 1][j][0]);
        if (j + 1 < C)
          dp[i][j + 1][k] = max(dp[i][j][k], dp[i][j + 1][k]);
        if (v != 0 && k != 3) {
          if (i + 1 < R)
            dp[i + 1][j][0] = max(dp[i][j][k] + v, dp[i + 1][j][0]);
          if (j + 1 < C)
            dp[i][j + 1][k + 1] = max(dp[i][j][k] + v, dp[i][j + 1][k + 1]);
        }
      }
    }
  }
  for (int i = 2; i >= 0; i--) {
    ll v = mp[{R - 1, C - 1}];
    dp[R - 1][C - 1][i + 1] =
        max(dp[R - 1][C - 1][i + 1], dp[R - 1][C - 1][i] + v);
  }
  cout << *max_element(dp[R - 1][C - 1].begin(), dp[R - 1][C - 1].end())
       << endl;
}

int main() {
  long long R;
  scanf("%lld", &R);
  long long C;
  scanf("%lld", &C);
  long long K;
  scanf("%lld", &K);
  std::vector<long long> r(K);
  std::vector<long long> c(K);
  std::vector<long long> v(K);
  for (int i = 0; i < K; i++) {
    scanf("%lld", &r[i]);
    scanf("%lld", &c[i]);
    scanf("%lld", &v[i]);
  }
  solve(R, C, K, std::move(r), std::move(c), std::move(v));
  return 0;
}