Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bit>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;
using ll = long long;

struct UnionFind {
  vector<int> parent;

  UnionFind(int N) : parent(N) {
    for (int i = 0; i < N; i++) {
      parent[i] = -1;
    }
  }

  int root(int i) {
    if (parent[i] < 0) {
      return i;
    }
    return (parent[i] = root(parent[i]));
  }

  bool unite(int from, int to) {
    int rx = root(from);
    int ry = root(to);
    if (rx != ry) {
      parent[ry] += parent[rx];
      parent[rx] = ry;

      return true;
    } else {
      return false;
    }
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int treeSize(int x) { return -parent[root(x)]; }
};

long long int modpow(long long int base, long long int pow, long long int mod) {
  if (pow == 1) {
    return base;
  } else if (pow == 0) {
    return 1;
  }

  if (pow % 2 == 0) {
    auto temp = modpow(base, pow / 2, mod);
    return (temp * temp) % mod;
  } else {
    return (base * modpow(base, pow - 1, mod)) % mod;
  }
}

long long int moddiv(long long int X, long long int Y, long long int mod) {
  auto fermatDiv = modpow(Y, mod - 2, mod);

  return (X * fermatDiv) % mod;
}

long long modCombination(long long left, long long right, long long int mod) {
  long long answer = 1;
  if (left > right) {
    for (long long i = 0; i < right; i++) {
      answer = (answer * (left - i)) % mod;
      answer = moddiv(answer, (i + 1), mod);
    }
  }
  return answer;
}

bool IsPrime(long long N) {
  if (N == 1) {
    return false;
  }
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      return false;
    }
  }

  return true;
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

vector<long long> enum_divisors(long long N) {
  vector<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);
      // 重複しないならば i の相方である N/i も push
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  // 小さい順に並び替える
  sort(res.begin(), res.end());
  return res;
}

long long gcd(long long a, long long b) {
  if (b > a) {
    long long temp = b;
    b = a;
    a = temp;
  }
  // cout << "a:" << a << "b:" << b << endl;
  long long c = a % b;
  if (c == 0) {
    return b;
  } else {
    return gcd(b, c);
  }
}

ll R = 0, C = 0, K = 0;
struct Data {
public:
  vector<priority_queue<ll, vector<ll>, greater<ll>>> getItems;
  ll totalValue;
  Data() {
    getItems = vector<priority_queue<ll, vector<ll>, greater<ll>>>(R + 1);
    totalValue = 0;
  }
};

int main() {

  cin >> R >> C >> K;

  vector<vector<ll>> field(R, vector<ll>(C, 0));

  for (ll i = 0; i < K; i++) {
    ll r, c, v;
    cin >> r >> c >> v;
    field[r - 1][c - 1] = v;
  }

  vector<vector<vector<ll>>> dp(
      R, vector<vector<ll>>(C, vector<ll>(4, -pow(10, 18))));

  dp[0][0][0] = 0;

  for (ll i = 0; i < R; i++) {
    for (ll j = 0; j < C; j++) {
      for (ll k = 2; k >= 0; k--) {
        if (dp[i][j][k] >= 0 && dp[i][j][k] + field[i][j] > dp[i][j][k + 1]) {
          dp[i][j][k + 1] = dp[i][j][k] + field[i][j];
        }
      }

      for (ll k = 0; k <= 3; k++) {
        if (i + 1 < R) {
          if (dp[i + 1][j][0] < dp[i][j][k]) {
            dp[i + 1][j][0] = dp[i][j][k];
          }
        }

        if (j + 1 < C) {
          if (dp[i][j + 1][k] < dp[i][j][k]) {
            dp[i][j + 1][k] = dp[i][j][k];
          }
        }
      }
    }
  }

  ll max = -pow(10, 18);
  for (ll k = 0; k <= 3; k++) {
    if (dp[R - 1][C - 1][k] > max) {
      max = dp[R - 1][C - 1][k];
    }
  }

  cout << max << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bit>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
const double PI = 3.14159265358979323846;
const long long int MOD = 1000000000 + 7;
using ll = long long;

struct UnionFind {
  vector<int> parent;

  UnionFind(int N) : parent(N) {
    for (int i = 0; i < N; i++) {
      parent[i] = -1;
    }
  }

  int root(int i) {
    if (parent[i] < 0) {
      return i;
    }
    return (parent[i] = root(parent[i]));
  }

  bool unite(int from, int to) {
    int rx = root(from);
    int ry = root(to);
    if (rx != ry) {
      parent[ry] += parent[rx];
      parent[rx] = ry;

      return true;
    } else {
      return false;
    }
  }

  bool same(int x, int y) { return root(x) == root(y); }

  int treeSize(int x) { return -parent[root(x)]; }
};

long long int modpow(long long int base, long long int pow, long long int mod) {
  if (pow == 1) {
    return base;
  } else if (pow == 0) {
    return 1;
  }

  if (pow % 2 == 0) {
    auto temp = modpow(base, pow / 2, mod);
    return (temp * temp) % mod;
  } else {
    return (base * modpow(base, pow - 1, mod)) % mod;
  }
}

long long int moddiv(long long int X, long long int Y, long long int mod) {
  auto fermatDiv = modpow(Y, mod - 2, mod);

  return (X * fermatDiv) % mod;
}

long long modCombination(long long left, long long right, long long int mod) {
  long long answer = 1;
  if (left > right) {
    for (long long i = 0; i < right; i++) {
      answer = (answer * (left - i)) % mod;
      answer = moddiv(answer, (i + 1), mod);
    }
  }
  return answer;
}

bool IsPrime(long long N) {
  if (N == 1) {
    return false;
  }
  for (long long i = 2; i * i <= N; i++) {
    if (N % i == 0) {
      return false;
    }
  }

  return true;
}

vector<pair<long long, long long>> prime_factorize(long long N) {
  vector<pair<long long, long long>> res;
  for (long long a = 2; a * a <= N; ++a) {
    if (N % a != 0)
      continue;
    long long ex = 0; // 指数

    // 割れる限り割り続ける
    while (N % a == 0) {
      ++ex;
      N /= a;
    }

    // その結果を push
    res.push_back({a, ex});
  }

  // 最後に残った数について
  if (N != 1)
    res.push_back({N, 1});
  return res;
}

vector<long long> enum_divisors(long long N) {
  vector<long long> res;
  for (long long i = 1; i * i <= N; ++i) {
    if (N % i == 0) {
      res.push_back(i);
      // 重複しないならば i の相方である N/i も push
      if (N / i != i)
        res.push_back(N / i);
    }
  }
  // 小さい順に並び替える
  sort(res.begin(), res.end());
  return res;
}

long long gcd(long long a, long long b) {
  if (b > a) {
    long long temp = b;
    b = a;
    a = temp;
  }
  // cout << "a:" << a << "b:" << b << endl;
  long long c = a % b;
  if (c == 0) {
    return b;
  } else {
    return gcd(b, c);
  }
}

ll R = 0, C = 0, K = 0;
struct Data {
public:
  vector<priority_queue<ll, vector<ll>, greater<ll>>> getItems;
  ll totalValue;
  Data() {
    getItems = vector<priority_queue<ll, vector<ll>, greater<ll>>>(R + 1);
    totalValue = 0;
  }
};

int main() {

  cin >> R >> C >> K;

  vector<vector<ll>> field(R, vector<ll>(C, 0));

  for (ll i = 0; i < K; i++) {
    ll r, c, v;
    cin >> r >> c >> v;
    field[r - 1][c - 1] = v;
  }

  vector<vector<Data>> datas(R + 1, vector<Data>(C + 1));

  ll pivotR = R - 1, pivotC = C - 1;

  ll great = max(R, C);

  for (ll i = great - 1; i >= 0; i--) {
    for (ll j = R - 1; j >= pivotR; j--) {
      ll currentItem = field[j][pivotC];

      Data tempData1 = datas[j + 1][pivotC], tempData2 = datas[j][pivotC + 1];

      if (tempData1.getItems[j].size() >= 3 &&
          tempData1.getItems[j].top() < currentItem) {
        ll dump = tempData1.getItems[j].top();
        tempData1.getItems[j].pop();
        tempData1.getItems[j].push(currentItem);
        tempData1.totalValue -= dump;
        tempData1.totalValue += currentItem;
      } else {
        tempData1.getItems[j].push(currentItem);
        tempData1.totalValue += currentItem;
      }

      if (tempData2.getItems[j].size() >= 3 &&
          tempData2.getItems[j].top() < currentItem) {
        ll dump = tempData2.getItems[j].top();
        tempData2.getItems[j].pop();
        tempData2.getItems[j].push(currentItem);
        tempData2.totalValue -= dump;
        tempData2.totalValue += currentItem;
      } else {
        tempData2.getItems[j].push(currentItem);
        tempData2.totalValue += currentItem;
      }

      if (tempData1.totalValue > tempData2.totalValue) {
        datas[j][pivotC] = tempData1;
      } else {
        datas[j][pivotC] = tempData2;
      }
    }

    for (ll j = C - 1; j >= pivotC; j--) {
      ll currentItem = field[pivotR][j];

      Data tempData1 = datas[pivotR + 1][j], tempData2 = datas[pivotR][j + 1];

      if (tempData1.getItems[pivotR].size() >= 3 &&
          tempData1.getItems[pivotR].top() < currentItem) {
        ll dump = tempData1.getItems[pivotR].top();
        tempData1.getItems[pivotR].pop();
        tempData1.getItems[pivotR].push(currentItem);
        tempData1.totalValue -= dump;
        tempData1.totalValue += currentItem;
      } else {
        tempData1.getItems[pivotR].push(currentItem);
        tempData1.totalValue += currentItem;
      }

      if (tempData2.getItems[pivotR].size() >= 3 &&
          tempData2.getItems[pivotR].top() < currentItem) {
        ll dump = tempData2.getItems[pivotR].top();
        tempData2.getItems[pivotR].pop();
        tempData2.getItems[pivotR].push(currentItem);
        tempData2.totalValue -= dump;
        tempData2.totalValue += currentItem;
      } else {
        tempData2.getItems[pivotR].push(currentItem);
        tempData2.totalValue += currentItem;
      }

      if (tempData1.totalValue > tempData2.totalValue) {
        datas[pivotR][j] = tempData1;
      } else {
        datas[pivotR][j] = tempData2;
      }
    }

    pivotR--;
    pivotC--;
    if (pivotR < 0) {
      pivotR = 0;
    }
    if (pivotC < 0) {
      pivotC = 0;
    }
  }

  cout << datas[0][0].totalValue << endl;
  return 0;
}