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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<V>;
using VVV = vector<VV>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
template <class T> using VE = vector<T>;
template <class T> using P = pair<T, T>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define REP(i, k, n) for (int i = (k); i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define output(x, y) cout << fixed << setprecision(y) << x << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
const int D = 365;
V p(D);
V l(D, 0);
VV up(D, V(26));
void calc(ll &now, int x,
          int d) { // 今のスコア、どれを選んだか、何日目か(0-indexed)
  l[x] = d + 1;
  now += up[d][x];
  rep(j, 26) { now -= p[j] * ((d + 1) - l[j]); }
}
ll test(ll now, int x, int d) { // その選択をしてみる
  ll tmp = now;
  tmp += up[d][x];
  rep(j, 26) { tmp -= p[j] * ((d + 1) - l[j]); }
  return tmp - now;
}
int main() { // とりあえずgreedyにやる
  int d;
  cin >> d;
  ll now = 0;
  queue<int> que;
  rep(i, 26) cin >> p[i];
  rep(i, d) rep(j, 26) cin >> up[i][j];
  rep(i, d) {
    ll diff = UNDER;
    int select = -1;
    rep(j, 26) {
      if (chmax(diff, test(now, j, i)))
        select = j;
    }
    que.push(select + 1);
    calc(now, select, i);
  }
  while (!que.empty()) {
    cout << que.front() << endl;
    que.pop();
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using V = vector<int>;
using VV = vector<V>;
using VVV = vector<VV>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VVVL = vector<VVL>;
template <class T> using VE = vector<T>;
template <class T> using P = pair<T, T>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define REP(i, k, n) for (int i = (k); i < (n); i++)
#define all(a) (a).begin(), (a).end()
#define output(x, y) cout << fixed << setprecision(y) << x << endl;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
ll upper = MOD + MOD;
ll under = -upper;
ll UPPER = MOD * MOD;
ll UNDER = -UPPER;
const long double pi = 3.141592653589793;
const int D = 26;
V p(D);
V l(D, 0);
VV up(D, V(26));
void calc(ll &now, int x,
          int d) { // 今のスコア、どれを選んだか、何日目か(0-indexed)
  l[x] = d + 1;
  now += up[d][x];
  rep(j, 26) { now -= p[j] * ((d + 1) - l[j]); }
}
ll test(ll now, int x, int d) { // その選択をしてみる
  ll tmp = now;
  tmp += up[d][x];
  rep(j, 26) { tmp -= p[j] * ((d + 1) - l[j]); }
  return tmp - now;
}
int main() { // とりあえずgreedyにやる
  int d;
  cin >> d;
  ll now = 0;
  queue<int> que;
  rep(i, 26) cin >> p[i];
  rep(i, d) rep(j, 26) cin >> up[i][j];
  rep(i, d) {
    ll diff = UNDER;
    int select = -1;
    rep(j, 26) {
      if (chmax(diff, test(now, j, i)))
        select = j;
    }
    que.push(select + 1);
    calc(now, select, i);
  }
  while (!que.empty()) {
    cout << que.front() << endl;
    que.pop();
  }
  return 0;
}