Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, j, n) for (ll i = (ll)(j); i < (ll)(n); i++)
#define REP(i, j, n) for (ll i = (ll)(j); i <= (ll)(n); i++)
#define per(i, j, n) for (ll i = (ll)(j); (ll)(n) <= i; i--)
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (ll)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (ll)(key)))
#define pb emplace_back
#define mp std::make_pair
//
#define endl "\n"
// using std::endl;
using std::cin;
using std::cout;
using std::lower_bound;
using std::string;
using std::upper_bound;
using std::vector;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll, ll>;
//
constexpr ll MOD = 1e9 + 7;
// constexpr ll MOD=998244353;
// constexpr ll MOD=10000000;
// constexpr ll MOD=1e5;
constexpr ll MAX = 1e7;
constexpr ll INF = (1ll << 62);
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
const ll N = 26;
ll solve(ll D, vii &A, vi &C, vi &S) {
  vi last(N, -1);
  ll a = 0;
  rep(i, 0, D) {
    ll memo;
    memo = S[i];
    last[memo] = i;
    a += A[i][memo];
    rep(j, 0, N) a -= (i - last[j]) * C[j];
  }
  return a;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  srand((unsigned)time(NULL));
  ll D;
  cin >> D;
  vi C(N);
  rep(i, 0, N) cin >> C[i];
  vii A(D, vi(N));
  rep(i, 0, D) { rep(j, 0, N) cin >> A[i][j]; }
  vi last(N, -1);
  vi ans(D);
  rep(i, 0, D) {
    ll max = 0, memo = 0;
    rep(j, 0, N) {
      if (max < A[i][j] + (i - last[j]) * C[j]) {
        max = A[i][j] + (i - last[j]) * C[j];
        memo = j;
      }
    }
    last[memo] = i;
    ans[D] = memo;
  }
  ll max = solve(D, A, C, ans);
  rep(i, 0, D) {
    rep(j, 0, N) {
      vi S = ans;
      S[i] = j;
      ll X = solve(D, A, C, S);
      if (max < X) {
        max = X;
        ans = S;
      }
    }
  }
  rep(i, 0, D) cout << ans[i] + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
// using namespace std;
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define rep(i, j, n) for (ll i = (ll)(j); i < (ll)(n); i++)
#define REP(i, j, n) for (ll i = (ll)(j); i <= (ll)(n); i++)
#define per(i, j, n) for (ll i = (ll)(j); (ll)(n) <= i; i--)
#define ll long long
#define ALL(a) (a).begin(), (a).end()
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (ll)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (ll)(key)))
#define pb emplace_back
#define mp std::make_pair
//
#define endl "\n"
// using std::endl;
using std::cin;
using std::cout;
using std::lower_bound;
using std::string;
using std::upper_bound;
using std::vector;
using vi = vector<ll>;
using vii = vector<vi>;
using pii = std::pair<ll, ll>;
//
constexpr ll MOD = 1e9 + 7;
// constexpr ll MOD=998244353;
// constexpr ll MOD=10000000;
// constexpr ll MOD=1e5;
constexpr ll MAX = 1e7;
constexpr ll INF = (1ll << 62);
template <class T>
class prique : public std::priority_queue<T, std::vector<T>, std::greater<T>> {
};
const ll N = 26;
ll solve(ll D, vii &A, vi &C, vi &S) {
  vi last(N, -1);
  ll a = 0;
  rep(i, 0, D) {
    ll memo;
    memo = S[i];
    last[memo] = i;
    a += A[i][memo];
    rep(j, 0, N) a -= (i - last[j]) * C[j];
  }
  return a;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  srand((unsigned)time(NULL));
  ll D;
  cin >> D;
  vi C(N);
  rep(i, 0, N) cin >> C[i];
  vii A(D, vi(N));
  rep(i, 0, D) { rep(j, 0, N) cin >> A[i][j]; }
  vi last(N, -1);
  vi ans(D);
  rep(i, 0, D) {
    ll max = 0, memo = 0;
    rep(j, 0, N) {
      if (max < A[i][j] + (i - last[j]) * C[j]) {
        max = A[i][j] + (i - last[j]) * C[j];
        memo = j;
      }
    }
    last[memo] = i;
    ans[i] = memo;
  }
  ll max = solve(D, A, C, ans);
  rep(i, 0, D) {
    rep(j, 0, N) {
      vi S = ans;
      S[i] = j;
      ll X = solve(D, A, C, S);
      if (max < X) {
        max = X;
        ans = S;
      }
    }
  }
  rep(i, 0, D) cout << ans[i] + 1 << endl;
}