prithivMLmods/Coder-Stat huggingface dataset:
Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include<unordered_map>
using namespace std;
#define ll long long
#define dd cout << endl
const long long int inf = 1e18 + 7;
const int mod = 1e9 + 7;

using namespace std;

inline ll int max(ll int a, ll int b) { return a > b ? a : b; }
inline ll int min(ll int a, ll int b) { return a < b ? a : b; }

const int N = 2e5 + 10;

int n, k;
ll a[N];
ll mp[N];

inline bool cmp(ll x, ll y) { return abs(x) < abs(y); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] >= 0)
      mp[1]++;
    else
      mp[2]++;
  }

  int l = 1, r = n;

  ll ans = 1;

  if (mp[1] == 0 && k % 2 == 1) {
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= k; ++i) {
      ans = a[i] * ans % mod;
    }
  } else {
    sort(a + 1, a + 1 + n, greater<int>());
    while (k > 0) {
      if (k % 2 == 1)
        ans = ans * a[l++] % mod;
      else if (k >= 2) {
        if (a[l] * a[l + 1] >= a[r] * a[r - 1]) {
          ans = ans * a[l] % mod * a[l + 1] % mod;
          l += 2;
          k -= 2;
        } else {
          ans = ans * a[r - 1] % mod * a[r] % mod;
          r -= 2;
          k -= 2;
        }
      }
    }
  }

  cout << (ans % mod + mod) % mod << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include<unordered_map>
using namespace std;
#define ll long long
#define dd cout << endl
const long long int inf = 1e18 + 7;
const int mod = 1e9 + 7;

using namespace std;

inline ll int max(ll int a, ll int b) { return a > b ? a : b; }
inline ll int min(ll int a, ll int b) { return a < b ? a : b; }

const int N = 2e5 + 10;

int n, k;
ll a[N];
ll mp[N];

inline bool cmp(ll x, ll y) { return abs(x) < abs(y); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (a[i] >= 0)
      mp[1]++;
    else
      mp[2]++;
  }

  int l = 1, r = n;

  ll ans = 1;

  if (mp[1] == 0 && k % 2 == 1) {
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= k; ++i) {
      ans = a[i] * ans % mod;
    }
  } else {
    sort(a + 1, a + 1 + n, greater<int>());
    while (k > 0) {
      if (k % 2 == 1)
        ans = ans * a[l++] % mod, k--;
      else if (k >= 2) {
        if (a[l] * a[l + 1] >= a[r] * a[r - 1]) {
          ans = ans * a[l] % mod * a[l + 1] % mod;
          l += 2;
          k -= 2;
        } else {
          ans = ans * a[r - 1] % mod * a[r] % mod;
          r -= 2;
          k -= 2;
        }
      }
    }
  }

  cout << (ans % mod + mod) % mod << endl;

  return 0;
}
Problem p02616 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cout << elem;
  }
  cerr << endl;
}

// 解説参考. 解法2.
int main() {
  int n, k;
  cin >> n >> k;

  vector<llong> a, b;
  REP(i, n) {
    llong in;
    cin >> in;
    if (in < 0)
      a.push_back(in);
    else
      b.push_back(in);
  }
  sort(ALL(a));
  sort(ALL(b), greater<llong>());

  if (a.size() == n and k % 2 == 1) {
    llong anss = 1;
    for (int i = a.size() - 1; i >= a.size() - k; --i)
      anss = anss * (MOD + a[i]) % MOD;
    cout << anss << endl;
    return 0;
  }

  llong ans = 1;
  int pa = 0, pb = 0;
  while (k > 0) {
    if (k <= 1) {
      ans = ans * b[pb++] % MOD;
      k--;
    } else {
      if (a.size() - pa >= 2 and
          (b.size() - pb <= 1 or
           (b.size() - pb >= 2 and a[pa] * a[pa + 1] >= b[pb] * b[pb + 1]))) {
        REP(i, 2) ans = ans * (MOD + a[pa++]) % MOD;
        k -= 2;
      } else {
        ans = ans * b[pb++] % MOD;
        k--;
      }
    }
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cerr << ' ';
    cout << elem;
  }
  cerr << endl;
}

// 解説参考. 解法2.
int main() {
  int n, k;
  cin >> n >> k;

  vector<llong> a, b;
  REP(i, n) {
    llong in;
    cin >> in;
    if (in < 0)
      a.push_back(in);
    else
      b.push_back(in);
  }
  sort(ALL(a));
  sort(ALL(b), greater<llong>());

  if (k == n) {
    llong anss = 1;
    REP(i, a.size()) anss = anss * (MOD + a[i]) % MOD;
    REP(i, b.size()) anss = anss * b[i] % MOD;
    cout << anss << endl;
    return 0;
  }

  if (a.size() == n and k % 2 == 1) {
    llong anss = 1;
    for (int i = a.size() - 1; i >= a.size() - k; --i)
      anss = anss * (MOD + a[i]) % MOD;
    cout << anss << endl;
    return 0;
  }

  llong ans = 1;
  int pa = 0, pb = 0;
  while (k > 0) {
    if (k <= 1) {
      ans = ans * b[pb++] % MOD;
      k--;
    } else {
      if (a.size() - pa >= 2 and
          (b.size() - pb <= 1 or
           (b.size() - pb >= 2 and a[pa] * a[pa + 1] >= b[pb] * b[pb + 1]))) {
        REP(i, 2) ans = ans * (MOD + a[pa++]) % MOD;
        k -= 2;
      } else {
        ans = ans * b[pb++] % MOD;
        k--;
      }
    }
  }

  cout << ans << endl;
}
Problem p02617 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
using namespace std;
#define rep_1(i, m, n) for (int i = m; i <= n; i++)
#define mem(st) memset(st, 0, sizeof st)
inline int read() {
  int num = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    num = (num << 3) + (num << 1) + (ch ^ 48);
    ch = getchar();
  }
  return w ? -num : num;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef pair<double, double> pdd;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  //	for(ll i=0; i<=n; i++)
  //		ans+=(n-i)*(i+1);
  for (ll i = 1; i <= n; i++)
    for (int j = i; j <= n; j++)
      ans += j - i + 1;
  for (ll i = 1; i <= n - 1; i++) {
    ll l, r;
    cin >> l >> r;
    if (l > r)
      swap(l, r);
    ans -= l * (n - r + 1);
  }
  cout << ans << endl;
}
signed main() {
  int t = 1;
  while (t--)
    solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
using namespace std;
#define rep_1(i, m, n) for (int i = m; i <= n; i++)
#define mem(st) memset(st, 0, sizeof st)
inline int read() {
  int num = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    num = (num << 3) + (num << 1) + (ch ^ 48);
    ch = getchar();
  }
  return w ? -num : num;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef pair<double, double> pdd;
const int inf = 0x3f3f3f3f;
const int N = 2e5 + 10;
void solve() {
  ll n;
  cin >> n;
  ll ans = 0;
  //	for(ll i=0; i<=n; i++)
  //		ans+=(n-i)*(i+1);
  for (ll i = 1; i <= n; i++) {
    //		for(int j=i;j<=n;j++)
    //			ans+=j-i+1;
    ans += (i + n) * (n - i + 1) / 2 + (n - i + 1) - i * (n - i + 1);
  }
  for (ll i = 1; i <= n - 1; i++) {
    ll l, r;
    cin >> l >> r;
    if (l > r)
      swap(l, r);
    ans -= l * (n - r + 1);
  }
  cout << ans << endl;
}
signed main() {
  int t = 1;
  while (t--)
    solve();
  return 0;
}
Problem p02617 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int M = 1000000007;
signed main() {
  int N;
  cin >> N;
  vector<vector<int>> A(N, vector<int>(0));
  for (int i = 0; i < N - 1; i++) {
    int n, m;
    cin >> n >> m;
    A[n - 1].push_back(m - 1);
    A[m - 1].push_back(n - 1);
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    int k = 0;
    for (int j = i; j < N; j++) {
      k++;
      for (int n : A[j])
        if (i <= n && n < j)
          k--;
      ans += k;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int M = 1000000007;
signed main() {
  int N;
  cin >> N;
  vector<vector<int>> A(N, vector<int>(0));
  for (int i = 0; i < N - 1; i++) {
    int n, m;
    cin >> n >> m;
    A[n - 1].push_back(m - 1);
    A[m - 1].push_back(n - 1);
  }
  int ans = 0, k = 0, l = 0;
  vector<int> B(500);
  for (int j = 0; j < N; j++) {
    k += j + 1;
    for (int n : A[j])
      if (n < j)
        l += n + 1;
    ans += k - l;
  }
  cout << ans << endl;
}
Problem p02617 (C++)

Buggy submission (status: Runtime Error):
// https://atcoder.jp/contests/abc173/tasks/abc173_f

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define DEBUG 1
#if DEBUG
#define _GLIBCXX_DEBUG
#define DUMP(a)                                                                \
  REP(_i, a.size()) cout << a[_i] << (_i + 1 == a.size() ? "\n" : " ")
#define DUMP2D(b)                                                              \
  REP(_j, b.size()) DUMP(b[_j]);                                               \
  cout << endl
#else
#define DUMP(a)
#define DUMP2D(b)
#endif

const int N_MAX = 2 * 1e5;

ll N;
vector<int> u;
vector<int> v;

int main() {
  cin >> N;
  u = vector<int>(N - 1);
  v = vector<int>(N - 1);
  REP(i, N - 1) cin >> u[i] >> v[i];

  ll ans = (N * N * N + N * N * 3 + N * 2) / 6;
  REP(i, N) {
    ll a = min(u[i], v[i]);
    ll b = max(u[i], v[i]);
    ans -= a * (N - b + 1);
  }
  cout << ans << endl;
}


Fixed submission:
// https://atcoder.jp/contests/abc173/tasks/abc173_f

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, b, e) for (int i = (b); i <= (int)(e); i++)
#define DEBUG 1
#if DEBUG
#define _GLIBCXX_DEBUG
#define DUMP(a)                                                                \
  REP(_i, a.size()) cout << a[_i] << (_i + 1 == a.size() ? "\n" : " ")
#define DUMP2D(b)                                                              \
  REP(_j, b.size()) DUMP(b[_j]);                                               \
  cout << endl
#else
#define DUMP(a)
#define DUMP2D(b)
#endif

const int N_MAX = 2 * 1e5;

ll N;
vector<int> u;
vector<int> v;

int main() {
  cin >> N;
  u = vector<int>(N - 1);
  v = vector<int>(N - 1);
  REP(i, N - 1) cin >> u[i] >> v[i];

  ll ans = (N * N * N + N * N * 3 + N * 2) / 6;
  REP(i, N - 1) {
    ll a = min(u[i], v[i]);
    ll b = max(u[i], v[i]);
    ans -= a * (N - b + 1);
  }
  cout << ans << endl;
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> ll LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> ll UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

ll d, c[30] = {}, s[370][30] = {}, last[30] = {}, ans[370] = {};

int main() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) { rep(j, 26) cin >> s[i][j]; }
  rep(i, d) {
    ll satisfaction = -1000000, contest = 0;
    rep(j, 26) {
      if (satisfaction < s[i][j] - c[i] * (i - last[j])) {
        satisfaction = s[i][j] - c[i] * (i - last[j]);
        contest = j;
      }
      ans[i] = contest;
      last[contest] = i;
    }
  }
  rep(i, d) cout << ans[i] + 1 << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef double lldo;
#define mp make_pair
#define pub push_back
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return b;
  } else
    return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
  if (a == 0) {
    return b;
  }
  return a / gcd(a, b) * b;
}
template <class T> ll LBI(vector<T> &ar, T in) {
  return lower_bound(ar.begin(), ar.end(), in) - ar.begin();
}
template <class T> ll UBI(vector<T> &ar, T in) {
  return upper_bound(ar.begin(), ar.end(), in) - ar.begin();
}

ll d, c[30] = {}, s[370][30] = {}, last[30] = {}, ans[370] = {};

int main() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) { rep(j, 26) cin >> s[i][j]; }
  rep(i, d) {
    ll satisfaction = -1000000, contest = 0;
    rep(j, 26) {
      if (satisfaction < s[i][j] - c[j] * (i - last[j])) {
        satisfaction = s[i][j] - c[j] * (i - last[j]);
        contest = j;
      }
      ans[i] = contest;
      last[contest] = i;
    }
  }
  rep(i, d) cout << ans[i] + 1 << endl;
  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 4611686018427387903;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

const int w = 18;
const int k = 350;
const int n = 26;
int d;

VI c(n);
VVI s(365, VI(n));

struct st {
  VI a;
  ll score;
  bool operator<(const st &another) const { return score < another.score; };
};

vector<st> f(st p) {
  VI a = p.a;
  ll score = p.score;
  int now = a.size() + 1;
  VI last(n, 0);
  REP(i, a.size())
  last[a[i]] = i + 1;
  VI down(n);
  ll downsum = 0;
  REP(i, n) {
    down[i] = c[i] * (now - last[i]);
    downsum += down[i];
  }
  a.push_back(-1);
  vector<st> tmp;
  REP(i, n) {
    a[now - 1] = i;
    tmp.push_back({a, score - downsum + down[i] + s[now - 1][i]});
  }
  sort(ALL(tmp));
  vector<st> ret;
  REP(i, w)
  ret.push_back(tmp[n - 1 - i]);
  return ret;
}

int main() {
  cin >> d;
  REP(i, n) cin >> c[i];
  REP(i, d) REP(j, n) cin >> s[i][j];
  priority_queue<st> q;
  VI a(0);
  q.push({a, 0});
  REP(i, d) {
    priority_queue<st> next;
    REP(j, k) {
      if (q.empty())
        break;
      for (auto nx : f(q.top()))
        next.push(nx);
      q.pop();
    }
    q = next;
  }
  REP(i, d)
  cout << q.top().a[i] + 1 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 4611686018427387903;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

const int w = 13;
const int k = 300;
const int n = 26;
int d;

VI c(n);
VVI s(365, VI(n));

struct st {
  VI a;
  ll score;
  bool operator<(const st &another) const { return score < another.score; };
};

vector<st> f(st p) {
  VI a = p.a;
  ll score = p.score;
  int now = a.size() + 1;
  VI last(n, 0);
  REP(i, a.size())
  last[a[i]] = i + 1;
  VI down(n);
  ll downsum = 0;
  REP(i, n) {
    down[i] = c[i] * (now - last[i]);
    downsum += down[i];
  }
  a.push_back(-1);
  vector<st> tmp;
  REP(i, n) {
    a[now - 1] = i;
    tmp.push_back({a, score - downsum + down[i] + s[now - 1][i]});
  }
  sort(ALL(tmp));
  vector<st> ret;
  REP(i, w)
  ret.push_back(tmp[n - 1 - i]);
  return ret;
}

int main() {
  cin >> d;
  REP(i, n) cin >> c[i];
  REP(i, d) REP(j, n) cin >> s[i][j];
  priority_queue<st> q;
  VI a(0);
  q.push({a, 0});
  REP(i, d) {
    priority_queue<st> next;
    REP(j, k) {
      if (q.empty())
        break;
      for (auto nx : f(q.top()))
        next.push(nx);
      q.pop();
    }
    q = next;
  }
  REP(i, d)
  cout << q.top().a[i] + 1 << endl;
  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, -1, sizeof(x))
const int INF = 1e9 + 1;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793;
const int MAX_N = 2e5;

int d, c[365], s[365][26], t[365], last[26], u;

void input() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) {
    rep(j, 26) { cin >> s[i][j]; }
  }
  rep(i, d) {
    int k;
    cin >> k;
    k--;
    t[i] = k;
  }
  fill(last, last + 26, 0);
}

int test(int k, int i) {
  ll res = 0;
  rep(j, 26) {
    if (j == k)
      continue;
    res -= c[j] * last[j];
  }

  return res + s[i][k];
}

void greedy() {
  rep(i, d) {
    rep(i, 26) last[i]++;
    int res = -INF;
    rep(k, 26) {
      if (res < test(k, i)) {
        u = k;
        res = test(k, i);
      }
    }
    last[u] = 0;
    t[i] = u;
  }
}

int p_calc() {
  ll res = 0;
  int last[26];
  fill(last, last + 26, 0);
  rep(i, d) {
    res += s[i][t[i]];
    rep(i, 26) last[i]++;
    last[t[i]] = 0;
    rep(j, 26) { res -= c[j] * last[j]; }
  }

  return res;
}

int main() {
  input();
  greedy();
  ll ans = p_calc();

  rep(i, 1000000) {
    int p = i % 365, q, tmp = t[p];

    q = s[i % 365][i % 26] % 26;

    t[p] = q;
    if (ans > p_calc()) {
      t[p] = tmp;
    }
  }

  rep(i, d) cout << t[i] + 1 << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, -1, sizeof(x))
const int INF = 1e9 + 1;
const ll MOD = 1e9 + 7;
const double PI = 3.141592653589793;
const int MAX_N = 2e5;

int d, c[365], s[365][26], t[365], last[26], u;

void input() {
  cin >> d;
  rep(i, 26) cin >> c[i];
  rep(i, d) {
    rep(j, 26) { cin >> s[i][j]; }
  }
  rep(i, d) {
    int k;
    cin >> k;
    k--;
    t[i] = k;
  }
  fill(last, last + 26, 0);
}

int test(int k, int i) {
  ll res = 0;
  rep(j, 26) {
    if (j == k)
      continue;
    res -= c[j] * last[j];
  }

  return res + s[i][k];
}

void greedy() {
  rep(i, d) {
    rep(i, 26) last[i]++;
    int res = -INF;
    rep(k, 26) {
      if (res < test(k, i)) {
        u = k;
        res = test(k, i);
      }
    }
    last[u] = 0;
    t[i] = u;
  }
}

int p_calc() {
  ll res = 0;
  int last[26];
  fill(last, last + 26, 0);
  rep(i, d) {
    res += s[i][t[i]];
    rep(i, 26) last[i]++;
    last[t[i]] = 0;
    rep(j, 26) { res -= c[j] * last[j]; }
  }

  return res;
}

int main() {
  input();
  greedy();
  ll ans = p_calc();

  rep(i, 10000) {
    int p = i % 365, q, tmp = t[p];

    q = s[i % 365][i % 26] % 26;

    t[p] = q;
    if (ans > p_calc()) {
      t[p] = tmp;
    }
  }

  rep(i, d) cout << t[i] + 1 << endl;
}
Problem p02618 (Python)

Buggy submission (status: Runtime Error):
import numpy as np

D = int(input())
Cs = list(map(int, input().split()))
Ss = []
for d in range(D):
    s = list(map(int, input().split()))
    Ss.append(s)

Ss = np.array(Ss)

solution = np.argmax(Ss, axis=1)

days_till_next = np.ones_like(Ss) * (D - 1) - np.arange(D).reshape((D, 1))


def update_days_till_next(d):
    contest = solution[d]
    for _d in range(d - 1, -1, -1):
        if solution[_d] == contest or _d == 0:
            days_till_next[_d:d, contest] -= days_till_next[d, contest]
            break


for d in range(D - 1, -1, -1):
    update_days_till_next(d)

for _ in range(10):
    n_contests = len(Ss[0])
    days_since_last = np.zeros(n_contests)
    for d, i in enumerate(solution):
        days_since_last += 1
        new_scores = days_till_next[d] * days_since_last[d] * Cs + Ss[d]
        new_i = new_scores.argmax()
        days_since_last[new_i] = 0
        if new_i != i:
            solution[d] = new_i
            update_days_till_next(d)


for s in solution:
    print(s + 1)


Fixed submission:
import numpy as np

D = int(input())
Cs = list(map(int, input().split()))
Ss = []
for d in range(D):
    s = list(map(int, input().split()))
    Ss.append(s)

Ss = np.array(Ss)

solution = np.argmax(Ss, axis=1)
#
# days_till_next = np.ones_like(Ss) * (D - 1) - np.arange(D).reshape((D, 1))
#
#
# def update_days_till_next(d):
#     contest = solution[d]
#     for _d in range(d - 1, -1, -1):
#         if solution[_d] == contest or _d == 0:
#             days_till_next[_d:d, contest] -= days_till_next[d, contest]
#             break
#
#
# for d in range(D - 1, -1, -1):
#     update_days_till_next(d)
#
# for _ in range(10):
#     n_contests = len(Ss[0])
#     days_since_last = np.zeros(n_contests)
#     for d, i in enumerate(solution):
#         days_since_last += 1
#         new_scores = days_till_next[d] * days_since_last[d] * Cs + Ss[d]
#         new_i = new_scores.argmax()
#         days_since_last[new_i] = 0
#         if new_i != i:
#             solution[d] = new_i
#             update_days_till_next(d)
#

for s in solution:
    print(s + 1)
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int D;
vector<int> c(26);
vector<vector<int>> s(365, vector<int>(26));

int cal_score(vector<int> t) {
  vector<int> res(D);
  int satis = 0;
  vector<int> last(26, 0);
  rep(d, D) {
    satis += s[d][t[d] - 1];
    last[t[d] - 1] = d + 1;
    rep(i, 26) { satis -= c[i] * (d - last[i] + 1); }
    res[d] = satis;
  }
  return res[D - 1];
}

int main() {
  // input
  cin >> D;
  rep(i, 26) cin >> c[i];
  rep(i, D) {
    rep(j, 26) { cin >> s[i][j]; }
  }

  // greedy
  vector<int> last(26, 0);
  vector<int> res(D);

  rep(d, D) {
    int max_plus = -10000;
    int use = -1;
    rep(i, 26) {
      int tmp = s[d][i] + c[i] * (d - last[i] + 1);
      if (max_plus < tmp) {
        max_plus = tmp;
        use = i;
      }
    }
    last[use] = d + 1;
    res[d] = use + 1;
  }

  int cur_score = cal_score(res);

  while (clock() / CLOCKS_PER_SEC <= 1.5) {
    int ch_d = rand() % D + 1;
    int ch_c = rand() % 26 + 1;
    int old_c = res[ch_d - 1];
    res[ch_d - 1] = ch_c;
    int new_score = cal_score(res);
    if (cur_score < new_score) {
      cur_score = new_score;
    } else {
      res[ch_d - 1] = old_c;
    }
  }

  rep(i, D) { cout << res[i] << endl; }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int D;
vector<int> c(26);
vector<vector<int>> s(365, vector<int>(26));

int cal_score(vector<int> t) {
  vector<int> res(D);
  int satis = 0;
  vector<int> last(26, 0);
  rep(d, D) {
    satis += s[d][t[d] - 1];
    last[t[d] - 1] = d + 1;
    rep(i, 26) { satis -= c[i] * (d - last[i] + 1); }
    res[d] = satis;
  }
  return res[D - 1];
}

int main() {
  // input
  cin >> D;
  rep(i, 26) cin >> c[i];
  rep(i, D) {
    rep(j, 26) { cin >> s[i][j]; }
  }

  // greedy
  vector<int> last(26, 0);
  vector<int> res(D);

  rep(d, D) {
    int max_plus = -10000;
    int use = -1;
    rep(i, 26) {
      int tmp = s[d][i] + c[i] * (d - last[i] + 1);
      if (max_plus < tmp) {
        max_plus = tmp;
        use = i;
      }
    }
    last[use] = d + 1;
    res[d] = use + 1;
  }

  int cur_score = cal_score(res);

  while ((clock() / (double)CLOCKS_PER_SEC) < 1.5) {
    int ch_d = rand() % D + 1;
    int ch_c = rand() % 26 + 1;
    int old_c = res[ch_d - 1];
    res[ch_d - 1] = ch_c;
    int new_score = cal_score(res);
    if (cur_score < new_score) {
      cur_score = new_score;
    } else {
      res[ch_d - 1] = old_c;
    }
  }

  rep(i, D) { cout << res[i] << endl; }
  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <climits>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

vector<int> cs;
vector<vector<int>> sm;

int total_satisfaction(vector<int> &ts, int d) {
  vector<int> ls;
  for (int i = 0; i < 26; i++)
    ls.push_back(0);
  int s = 0;

  for (int i = 0; i < d; i++) {
    int t = ts[i];
    t--;
    s += sm[i][t];
    ls[t] = i + 1;

    int dis = 0;
    for (int j = 0; j < 26; j++) {
      dis += cs[j] * ((i + 1) - ls[j]);
    }
    s -= dis;
  }

  return s;
}

int main() {
  int d;
  vector<int> ts;

  cin >> d;

  int x;
  for (int i = 0; i < 26; i++) {
    cin >> x;
    cs.push_back(x);
  }
  for (int i = 0; i < d; i++) {
    vector<int> v;
    sm.push_back(v);
    for (int j = 0; j < 26; j++) {
      cin >> x;
      sm[i].push_back(x);
    }
  }

  for (int i = 0; i < d; i++) {
    int mx = INT_MIN;
    int mxt = -1;

    for (int t = 1; t <= d; t++) {
      ts.push_back(t);
      int s = total_satisfaction(ts, i + 1);
      if (s > mx) {
        mx = s;
        mxt = t;
      }
      ts.pop_back();
    }

    ts.push_back(mxt);
    cout << mxt << endl;
  }
}


Fixed submission:
#include <climits>
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;

vector<int> cs;
vector<vector<int>> sm;

int total_satisfaction(vector<int> &ts, int d) {
  vector<int> ls;
  for (int i = 0; i < 26; i++)
    ls.push_back(0);
  int s = 0;

  for (int i = 0; i < d; i++) {
    int t = ts[i];
    t--;
    s += sm[i][t];
    ls[t] = i + 1;

    int dis = 0;
    for (int j = 0; j < 26; j++) {
      dis += cs[j] * ((i + 1) - ls[j]);
    }
    s -= dis;
  }

  return s;
}

int main() {
  int d;
  vector<int> ts;

  cin >> d;

  int x;
  for (int i = 0; i < 26; i++) {
    cin >> x;
    cs.push_back(x);
  }
  for (int i = 0; i < d; i++) {
    vector<int> v;
    sm.push_back(v);
    for (int j = 0; j < 26; j++) {
      cin >> x;
      sm[i].push_back(x);
    }
  }

  for (int i = 0; i < d; i++) {
    int mx = INT_MIN;
    int mxt = -1;

    for (int t = 1; t <= 26; t++) {
      ts.push_back(t);
      int s = total_satisfaction(ts, i + 1);
      if (s > mx) {
        mx = s;
        mxt = t;
      }
      ts.pop_back();
    }

    ts.push_back(mxt);
    cout << mxt << endl;
  }
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define INF 1e9

static const int seed = 6;
static const int D = 365;
static const int ntype = 26;
static int ans[D + 1];
static int max_score = -INF;
static int t = 1e6;
class Contest {
public:
  Contest() { last = 0; }
  int s[D + 1];
  int c;
  int last;
};

static Contest contest[ntype + 1];

void Input() {
  int d;
  cin >> d;
  for (int i = 1; i <= ntype; ++i) {
    int t;
    cin >> t;
    contest[i].c = t;
  }
  for (int day = 1; day <= D; ++day) {
    for (int type = 1; type <= ntype; ++type) {
      int t;
      cin >> t;
      contest[type].s[day] = t;
    }
  }
}

int evaluate() {
  int sum = 0;
  vector<int> l(ntype + 1, 0);
  for (int day = 1; day <= D; ++day) {
    int type = ans[day];
    sum += contest[type].s[day];
    l[type] = day;
    for (int t = 1; t <= ntype; ++t) {
      sum -= contest[t].c * (day - l[t]);
    }
  }
  return sum;
}

void Output() {
  for (int d = 1; d <= D; ++d)
    cout << ans[d] << endl;
}

void Init() {
  for (int d = 1; d <= D; ++d)
    ans[d] = rand() % ntype + 1;
}

void solve(int time) {
  max_score = evaluate();
  for (int i = 0; i < time; ++i) {
    int p = rand() % D + 1;
    int temp = ans[p];
    ans[p] = rand() % ntype + 1;
    int temp_score = evaluate();
    if (max_score < temp_score)
      max_score = temp_score;
    else
      ans[p] = temp;
  }
}

int main() {
  srand(seed);
  Input();
  Init();
  solve(t);
  Output();
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;
#define INF 1e9

static const int seed = 6;
static const int D = 365;
static const int ntype = 26;
static int ans[D + 1];
static int max_score = -INF;
static int t = 1e4;
class Contest {
public:
  Contest() { last = 0; }
  int s[D + 1];
  int c;
  int last;
};

static Contest contest[ntype + 1];

void Input() {
  int d;
  cin >> d;
  for (int i = 1; i <= ntype; ++i) {
    int t;
    cin >> t;
    contest[i].c = t;
  }
  for (int day = 1; day <= D; ++day) {
    for (int type = 1; type <= ntype; ++type) {
      int t;
      cin >> t;
      contest[type].s[day] = t;
    }
  }
}

int evaluate() {
  int sum = 0;
  vector<int> l(ntype + 1, 0);
  for (int day = 1; day <= D; ++day) {
    int type = ans[day];
    sum += contest[type].s[day];
    l[type] = day;
    for (int t = 1; t <= ntype; ++t) {
      sum -= contest[t].c * (day - l[t]);
    }
  }
  return sum;
}

void Output() {
  for (int d = 1; d <= D; ++d)
    cout << ans[d] << endl;
}

void Init() {
  for (int d = 1; d <= D; ++d)
    ans[d] = rand() % ntype + 1;
}

void solve(int time) {
  max_score = evaluate();
  for (int i = 0; i < time; ++i) {
    int p = rand() % D + 1;
    int temp = ans[p];
    ans[p] = rand() % ntype + 1;
    int temp_score = evaluate();
    if (max_score < temp_score)
      max_score = temp_score;
    else
      ans[p] = temp;
  }
}

int main() {
  srand(seed);
  Input();
  Init();
  solve(t);
  Output();
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

int D;
vector<int> c(26);
vector<vector<int>> s;

int scoring(const vector<int> &t) {
  int v = 0;
  vector<int> last(26, -1);
  rep(i, D) {
    v += s[i][t[i]];
    last[t[i]] = i;
    rep(j, 26) { v -= c[j] * (i - last[j]); }
  }
  return v;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  auto start = chrono::system_clock::now();

  cin >> D;
  rep(i, 26) cin >> c[i];
  s.resize(D, vector<int>(26));
  rep(i, D) rep(j, 26) cin >> s[i][j];

  vector<int> t(D);

  rep(i, D) t[i] = max_element(s[i].begin(), s[i].end()) - s[i].begin();

  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<> rand_d(0, D - 1);
  uniform_int_distribution<> rand_t(0, 25);

  int v = scoring(t);
  int x = 0;
  while (true) {
    int to_d = rand_d(mt);
    int to_t = rand_t(mt);
    if (t[to_d] == to_t)
      continue;
    int old = t[to_d];
    t[to_d] = to_t;
    int nv = scoring(t);
    if (nv < v)
      t[to_d] = old;
    if (x % 10000 == 0) {
      auto end = chrono::system_clock::now();
      auto dur = end - start;
      auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
      if (msec >= 1970)
        break;
    }
    x++;
  }
  rep(i, D) cout << t[i] + 1 << '\n';
}


Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
using namespace std;

int D;
vector<int> c(26);
vector<vector<int>> s;

int scoring(const vector<int> &t) {
  int v = 0;
  vector<int> last(26, -1);
  rep(i, D) {
    v += s[i][t[i]];
    last[t[i]] = i;
    rep(j, 26) { v -= c[j] * (i - last[j]); }
  }
  return v;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  auto start = chrono::system_clock::now();

  cin >> D;
  rep(i, 26) cin >> c[i];
  s.resize(D, vector<int>(26));
  rep(i, D) rep(j, 26) cin >> s[i][j];

  vector<int> t(D);

  rep(i, D) t[i] = max_element(s[i].begin(), s[i].end()) - s[i].begin();

  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<> rand_d(0, D - 1);
  uniform_int_distribution<> rand_t(0, 25);

  int v = scoring(t);
  int x = 0;
  while (true) {
    int to_d = rand_d(mt);
    int to_t = rand_t(mt);
    if (t[to_d] == to_t)
      continue;
    int old = t[to_d];
    t[to_d] = to_t;
    int nv = scoring(t);
    if (nv < v)
      t[to_d] = old;
    if (x % 1000 == 0) {
      auto end = chrono::system_clock::now();
      auto dur = end - start;
      auto msec = chrono::duration_cast<chrono::milliseconds>(dur).count();
      if (msec >= 1970)
        break;
    }
    x++;
  }
  rep(i, D) cout << t[i] + 1 << '\n';
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  int d;
  cin >> d;
  const int ContestCount = 26;
  vector<int> c(ContestCount);
  rep(i, ContestCount) cin >> c.at(i);
  vector<vector<int>> s(d, vector<int>(ContestCount));
  rep(i, d) {
    rep(j, ContestCount) { cin >> s.at(i).at(j); }
  }
  vector<int> res(ContestCount);
  rep(i, d) { res.at(i) = i; }
  rep(i, d) { cout << res.at(i) << endl; }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)

using namespace std;

using ll = long long;
using P = pair<int, int>;

int main() {
  int d;
  cin >> d;
  const int ContestCount = 26;
  vector<int> c(ContestCount);
  rep(i, ContestCount) cin >> c.at(i);
  vector<vector<int>> s(d, vector<int>(ContestCount));
  rep(i, d) {
    rep(j, ContestCount) { cin >> s.at(i).at(j); }
  }
  vector<int> res(d);
  rep(i, d) { res.at(i) = 1; }
  rep(i, d) { cout << res.at(i) << endl; }
  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;

int main() {
  auto start = steady_clock::now();

  random_device rnd;

  int D;
  vector<int> cs(26);
  cin >> D;
  for (auto &&c : cs) {
    cin >> c;
  }
  vector<vector<int>> sss(D, vector<int>(26));
  for (auto &&ss : sss) {
    for (auto &&s : ss) {
      cin >> s;
    }
  }

  vector<int> ts(D);
  for (auto &&t : ts) {
    t = rnd() % 26;
  }

  auto calc_score = [&] {
    vector<int> last(26, -1);

    int s = 0;

    for (int d = 0; d < D; d++) {
      s += sss[d][ts[d]];

      last[ts[d]] = d;

      for (int i = 0; i < 26; i++) {
        s -= cs[i] * (d - last[i]);
      }
    }

    // return max(0, 1000000 + s);
    return s;
  };

  auto calc_diff = [&](int d, int q) {
    int p = ts[d];

    ts[d] = q;

    int ret = calc_score();

    ts[d] = p;

    return ret;
  };

  int score = calc_score();

  while (duration_cast<milliseconds>(steady_clock::now() - start).count() <
         1970) {
    for (int d = 0; d < D; d++) {
      for (int q = 0; q < 26; q++) {
        if (ts[d] == q) {
          continue;
        }

        int next_score = calc_diff(d, q);

        if (score < next_score) {
          ts[d] = q;
          score = next_score;
        }
      }
    }
  }

  for (auto &&t : ts) {
    cout << t + 1 << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using namespace chrono;

int main() {
  auto start = steady_clock::now();

  random_device rnd;

  int D;
  vector<int> cs(26);
  cin >> D;
  for (auto &&c : cs) {
    cin >> c;
  }
  vector<vector<int>> sss(D, vector<int>(26));
  for (auto &&ss : sss) {
    for (auto &&s : ss) {
      cin >> s;
    }
  }

  vector<int> ts(D);
  for (auto &&t : ts) {
    t = rnd() % 26;
  }

  auto calc_score = [&] {
    vector<int> last(26, -1);

    int s = 0;

    for (int d = 0; d < D; d++) {
      s += sss[d][ts[d]];

      last[ts[d]] = d;

      for (int i = 0; i < 26; i++) {
        s -= cs[i] * (d - last[i]);
      }
    }

    // return max(0, 1000000 + s);
    return s;
  };

  auto calc_diff = [&](int d, int q) {
    int p = ts[d];

    ts[d] = q;

    int ret = calc_score();

    ts[d] = p;

    return ret;
  };

  int score = calc_score();

  while (duration_cast<milliseconds>(steady_clock::now() - start).count() <
         1950) {
    for (int d = 0; d < D; d++) {
      for (int q = 0; q < 26; q++) {
        if (ts[d] == q) {
          continue;
        }

        int next_score = calc_diff(d, q);

        if (score < next_score) {
          ts[d] = q;
          score = next_score;
        }
      }
    }
  }

  for (auto &&t : ts) {
    cout << t + 1 << endl;
  }

  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

const char nl = '\n';

void solve() {
  int d;
  cin >> d;
  vector<int> c;
  for (int i = 0; i < 26; ++i) {
    int val;
    cin >> val;
    c.push_back(val);
  }
  for (int i = 0; i < d; ++i) {
    map<int, int, greater<int>> m;
    for (int j = 0; j < 26; ++j) {
      int val;
      cin >> val;
      m[val / c[j]] = j + 1;
    }
    cout << m.begin()->second << nl;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;

const char nl = '\n';

void solve() {
  int d;
  cin >> d;
  vector<int> c;
  for (int i = 0; i < 26; ++i) {
    int val;
    cin >> val;
    c.push_back(val);
  }
  for (int i = 0; i < d; ++i) {
    map<int, int, greater<int>> m;
    for (int j = 0; j < 26; ++j) {
      int val;
      cin >> val;
      m[val / max(c[j], 1)] = j + 1;
    }
    cout << m.begin()->second << nl;
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  solve();
  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int d;
vector<int> c;
vector<vector<int>> s;

int satisfaction(const vector<int> &t) {
  vector<int> last(26, -1);
  int ans = 0;
  for (int i = 0; i < (int)t.size(); i++) {
    ans += s[i][t[i]];
    last[t[i]] = i;
    for (int j = 0; j < 26; j++) {
      ans -= c[j] * (i - last[j]);
    }
  }
  return ans;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int myrand(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  srand(time(nullptr));
  cin >> d;
  c.resize(26);
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
  }
  s.assign(d, vector<int>(26));
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }
  vector<int> t;
  for (int i = 0; i < d; i++) {
    int best = i;
    for (int j = 1; j < 26; j++) {
      if (s[i][j] > s[i][best]) {
        best = j;
      }
    }
    t.push_back(best);
  }
  for (int i = 0; i < 50000; i++) {
    int pos = myrand(0, d - 1), x = myrand(0, 25);
    vector<int> tmp = t;
    tmp[pos] = x;
    if (satisfaction(tmp) > satisfaction(t)) {
      swap(t, tmp);
    }
  }
  for (auto &x : t) {
    cout << x + 1 << '\n';
  }
  return 0;
}

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

int d;
vector<int> c;
vector<vector<int>> s;

int satisfaction(const vector<int> &t) {
  vector<int> last(26, -1);
  int ans = 0;
  for (int i = 0; i < (int)t.size(); i++) {
    ans += s[i][t[i]];
    last[t[i]] = i;
    for (int j = 0; j < 26; j++) {
      ans -= c[j] * (i - last[j]);
    }
  }
  return ans;
}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int myrand(int l, int r) { return uniform_int_distribution<int>(l, r)(rng); }

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  srand(time(nullptr));
  cin >> d;
  c.resize(26);
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
  }
  s.assign(d, vector<int>(26));
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }
  vector<int> t;
  for (int i = 0; i < d; i++) {
    int best = 0;
    for (int j = 1; j < 26; j++) {
      if (s[i][j] > s[i][best]) {
        best = j;
      }
    }
    t.push_back(best);
  }
  for (int i = 0; i < 50000; i++) {
    int pos = myrand(0, d - 1), x = myrand(0, 25);
    vector<int> tmp = t;
    tmp[pos] = x;
    if (satisfaction(tmp) > satisfaction(t)) {
      swap(t, tmp);
    }
  }
  for (auto &x : t) {
    cout << x + 1 << '\n';
  }
  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int d;

  cin >> d;

  vector<int> c(26, 0);
  vector<vector<int>> s(d, vector<int>(26, 0));
  vector<int> last(26, 0);

  vector<int> ans(d, 0);

  for (int i = 0; i < 26; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }

  int max;
  int k;
  for (int i = 0; i < d; i++) {
    max = -1;
    k = -1;
    for (int j = 0; j < 26; j++) {
      int p = s[i][j] + c[i] * (i + 1 - last[j]);
      if (max < p) {
        max = p;
        k = j;
      } else if (max == p) {
        if (c[k] < c[j]) {
          max = p;
          k = j;
        }
      }
    }
    ans[i] = k;
    last[k]++;
  }

  for (int i = 0; i < d; i++) {
    cout << ans[i] + 1 << endl;
  }

  return 0;
}


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

int main() {
  int d;

  cin >> d;

  vector<int> c(26, 0);
  vector<vector<int>> s(d, vector<int>(26, 0));
  vector<int> last(26, 0);

  vector<int> ans(d, 0);

  for (int i = 0; i < 26; i++) {
    cin >> c[i];
  }

  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }

  int max;
  int k;
  for (int i = 0; i < d; i++) {
    max = s[i][0] + c[0] * (i + 1 - last[0]);
    k = 0;
    for (int j = 1; j < 26; j++) {
      int p = s[i][j] + c[j] * (i + 1 - last[j]);
      if (max < p) {
        max = p;
        k = j;
      } else if (max == p) {
        if (c[k] < c[j]) {
          max = p;
          k = j;
        }
      }
    }
    ans[i] = k;
    last[k]++;
  }

  for (int i = 0; i < d; i++) {
    cout << ans[i] + 1 << endl;
  }

  return 0;
}
Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int D;
  cin >> D;
  vector<int> c(26);
  for (int &i : c)
    cin >> i;
  vector<vector<int>> s(D, vector<int>(26));
  for (vector<int> &v : s)
    for (int &i : v)
      cin >> i;
  vector<int> t(D);
  vector<int> A(26, 0);
  int mzint = 0;
  for (int i = 0; i < D; i++) {
    int mzi = mzint, mzin = 0, k;
    for (int j = 0; j < 26; j++)
      mzi -= (i + 1 - A[j]) * c[j];
    for (int j = 0; j < 26; j++) {
      int l = mzi + s[i][j] - (i + 1 - A[j]) * c[j];
      if (l > mzin) {
        k = j;
        mzin = l;
      }
    }
    A[k] = i + 1;
    mzint = mzin;
    t[i] = k + 1;
  }
  int M = 100000000, ans = mzint;
  while (M--) {
    int d = rand() % D, q = rand() % 26, ans2 = ans;
    ans += s[d][q] - s[d][t[d] - 1];
    int mae = -1, ato = D;
    for (int i = d - 1; i >= 0; i--) {
      if (t[i] == t[d]) {
        mae = i;
        break;
      }
    }
    for (int i = d + 1; i < D; i++) {
      if (t[i] == t[d]) {
        ato = i;
        break;
      }
    }
    ans -= (ato - d) * (d - mae) * c[t[d] - 1];
    mae = -1, ato = D;
    for (int i = d; i >= 0; i--) {
      if (t[i] == q + 1) {
        mae = i;
        break;
      }
    }
    for (int i = d; i < D; i++) {
      if (t[i] == q + 1) {
        ato = i;
        break;
      }
    }
    ans += (ato - d) * (d - mae) * c[q];
    if (ans2 < ans)
      t[d] = q + 1;
    else
      ans = ans2;
  }
  for (int i : t)
    cout << i << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main() {
  int D;
  cin >> D;
  vector<int> c(26);
  for (int &i : c)
    cin >> i;
  vector<vector<int>> s(D, vector<int>(26));
  for (vector<int> &v : s)
    for (int &i : v)
      cin >> i;
  vector<int> t(D);
  vector<int> A(26, 0);
  int mzint = 0;
  for (int i = 0; i < D; i++) {
    int mzi = mzint, mzin = 0, k;
    for (int j = 0; j < 26; j++)
      mzi -= (i + 1 - A[j]) * c[j];
    for (int j = 0; j < 26; j++) {
      int l = mzi + s[i][j] - (i + 1 - A[j]) * c[j];
      if (l > mzin) {
        k = j;
        mzin = l;
      }
    }
    A[k] = i + 1;
    mzint = mzin;
    t[i] = k + 1;
  }
  int M = 18000000, ans = mzint;
  while (M--) {
    int d = rand() % D, q = rand() % 26, ans2 = ans;
    ans += s[d][q] - s[d][t[d] - 1];
    int mae = -1, ato = D;
    for (int i = d - 1; i >= 0; i--) {
      if (t[i] == t[d]) {
        mae = i;
        break;
      }
    }
    for (int i = d + 1; i < D; i++) {
      if (t[i] == t[d]) {
        ato = i;
        break;
      }
    }
    ans -= (ato - d) * (d - mae) * c[t[d] - 1];
    mae = -1, ato = D;
    for (int i = d; i >= 0; i--) {
      if (t[i] == q + 1) {
        mae = i;
        break;
      }
    }
    for (int i = d; i < D; i++) {
      if (t[i] == q + 1) {
        ato = i;
        break;
      }
    }
    ans += (ato - d) * (d - mae) * c[q];
    if (ans2 < ans)
      t[d] = q + 1;
    else
      ans = ans2;
  }
  for (int i : t)
    cout << i << endl;
}