alexjercan/bugnet huggingface dataset:
Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int D;
int c[26];
int s[365][26];
int selected[365];
int scores[26];

int calculateScore(int t) {
  int retVal = 0;
  int last = -1;
  for (int i = 0; i < D; i++) {
    if (selected[i] == t) {
      last = i;
      retVal += s[i][t];
    } else {
      retVal -= c[t] * (i - last);
    }
  }
  return retVal;
}

int main() {
  clock_t start = clock();
  cin >> D;
  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];
    }

  for (int i = 0; i < D; i++) {
    int maxNum = -(1e7);
    int maxArg = 0;
    for (int j = 0; j < 26; j++) {
      if (s[i][j] - c[j] * (D / 26) > maxNum) {
        maxNum = s[i][j] - c[j] * (D / 26);
        maxArg = j;
      }
    }
    selected[i] = maxArg;
  }
  for (int i = 0; i < 26; i++) {
    scores[i] = calculateScore(i);
  }
  int itr = 1;
  while (clock() - start < (CLOCKS_PER_SEC * 2 - 100000)) {
    // cerr <<"itr =" << itr <<"\\n";
    itr++;
    int q = rand() % 26;
    int t = rand() % D;
    int oldq = selected[t];
    int oldscore = scores[oldq] + scores[q];
    selected[t] = q;
    int newScore = calculateScore(oldq) + calculateScore(q);
    // cerr <<"oldScore=" << oldscore <<"newScore=" << newScore << "\\n";
    if (newScore < oldscore) {
      selected[t] = oldq;
    }
  }
  for (int i = 0; i < D; i++) {
    cout << selected[i] + 1 << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int D;
int c[26];
int s[365][26];
int selected[365];
int scores[26];

int calculateScore(int t) {
  int retVal = 0;
  int last = -1;
  for (int i = 0; i < D; i++) {
    if (selected[i] == t) {
      last = i;
      retVal += s[i][t];
    } else {
      retVal -= c[t] * (i - last);
    }
  }
  return retVal;
}

int main() {
  clock_t start = clock();
  cin >> D;
  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];
    }

  for (int i = 0; i < D; i++) {
    selected[i] = rand() % 26;
  }
  for (int i = 0; i < 26; i++) {
    scores[i] = calculateScore(i);
  }
  int itr = 1;
  while (clock() - start < (CLOCKS_PER_SEC * 2 - 1000)) {
    // cerr <<"itr =" << itr <<"\\n";
    itr++;
    int q = rand() % 26;
    int t = rand() % D;
    int oldq = selected[t];
    int oldscore = scores[oldq] + scores[q];
    selected[t] = q;
    int newScore = calculateScore(oldq) + calculateScore(q);
    // cerr <<"oldScore=" << oldscore <<"newScore=" << newScore << "\\n";
    if (newScore < oldscore) {
      selected[t] = oldq;
    }
  }
  for (int i = 0; i < D; i++) {
    cout << selected[i] + 1 << "\\n";
  }
  return 0;
}
Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)

int cd = 20000;

int d, c[26], s[366][26];
int t[366];
ll r;

void init() {
  cin >> d;
  REP(i, 26) cin >> c[i];
  REP(i, d) REP(j, 26) cin >> s[i][j];
}

void greedy() { REP(i, d) REP(j, 26) t[i] = s[i][j] > s[i][t[i]] ? j : t[i]; }

ll score(int *t) {
  ll ans = 0;
  vector<int> cnt(26, 0);
  REP(i, d) {
    cnt[t[i]] = -1;
    ans += s[i][t[i]];
    REP(j, 26) cnt[j]++, ans -= cnt[j] * c[j];
  }
  return ans;
}

void crt_new() {
  int idx = rand() % d;
  int q = rand() % 26;
  while (q == t[idx])
    q = rand() % 26;
  int tmp = t[idx];
  t[idx] = q;
  ll res = score(t);
  if (res > r)
    r = res;
  else
    t[idx] = tmp;
}

void solve() {

  init();
  // ll begin_t=clock();
  greedy();
  r = score(t);
  while (cd--)
    crt_new();
  // REP(i,d)cout<<t[i]<<" ";cout<<endl;
  REP(i, d) cout << t[i] + 1 << endl;
  // cout<<r<<endl;

  // ll end_t=clock();cout<<end_t-begin_t<<"ms"<<endl;
}

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)

int cd = 350000;

int d, c[26], s[366][26];
int t[366];
ll r;

void init() {
  cin >> d;
  REP(i, 26) cin >> c[i];
  REP(i, d) REP(j, 26) cin >> s[i][j];
}

void greedy() { REP(i, d) REP(j, 26) t[i] = s[i][j] > s[i][t[i]] ? j : t[i]; }

ll score(int *t) {
  ll ans = 0;
  vector<int> cnt(26, 0);
  REP(i, d) {
    cnt[t[i]] = -1;
    ans += s[i][t[i]];
    REP(j, 26) cnt[j]++, ans -= cnt[j] * c[j];
  }
  return ans;
}

void crt_new() {
  int idx = rand() % d;
  int q = rand() % 26;
  while (q == t[idx])
    q = rand() % 26;
  int tmp = t[idx];
  t[idx] = q;
  ll res = score(t);
  if (res > r)
    r = res;
  else
    t[idx] = tmp;
}

void solve() {

  init();
  // ll begin_t=clock();
  greedy();
  r = score(t);
  while (cd--)
    crt_new();
  // REP(i,d)cout<<t[i]<<" ";cout<<endl;
  REP(i, d) cout << t[i] + 1 << endl;
  // cout<<r<<endl;

  // ll end_t=clock();cout<<end_t-begin_t<<"ms"<<endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)

int cd = 100000;

int d, c[26], s[366][26];
int t[366];
ll r;

void init() {
  cin >> d;
  REP(i, 26) cin >> c[i];
  REP(i, d) REP(j, 26) cin >> s[i][j];
}

void greedy() { REP(i, d) REP(j, 26) t[i] = s[i][j] > s[i][t[i]] ? j : t[i]; }

ll score(int *t) {
  ll ans = 0;
  vector<int> cnt(26, 0);
  REP(i, d) {
    cnt[t[i]] = -1;
    ans += s[i][t[i]];
    REP(j, 26) cnt[j]++, ans -= cnt[j] * c[j];
  }
  return ans;
}

void crt_new() {
  int idx = rand() % d;
  int q = rand() % 26;
  while (q == t[idx])
    q = rand() % 26;
  int tmp = t[idx];
  t[idx] = q;
  ll res = score(t);
  if (res > r)
    r = res;
  else
    t[idx] = tmp;
}

void solve() {

  init();
  // ll begin_t=clock();
  greedy();
  r = score(t);
  while (cd--)
    crt_new();
  // REP(i,d)cout<<t[i]<<" ";cout<<endl;
  REP(i, d) cout << t[i] + 1 << endl;
  // cout<<r<<endl;

  // ll end_t=clock();cout<<end_t-begin_t<<"ms"<<endl;
}

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

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)

int cd = 1000000;

int d, c[26], s[366][26];
int t[366];
ll r;

void init() {
  cin >> d;
  REP(i, 26) cin >> c[i];
  REP(i, d) REP(j, 26) cin >> s[i][j];
}

void greedy() { REP(i, d) REP(j, 26) t[i] = s[i][j] > s[i][t[i]] ? j : t[i]; }

ll score(int *t) {
  ll ans = 0;
  vector<int> cnt(26, 0);
  REP(i, d) {
    cnt[t[i]] = -1;
    ans += s[i][t[i]];
    REP(j, 26) cnt[j]++, ans -= cnt[j] * c[j];
  }
  return ans;
}

void crt_new() {
  int idx = rand() % d;
  int q = rand() % 26;
  while (q == t[idx])
    q = rand() % 26;
  int tmp = t[idx];
  t[idx] = q;
  ll res = score(t);
  if (res > r)
    r = res;
  else
    t[idx] = tmp;
}

void solve() {
  init();
  greedy();
  r = score(t);
  while (cd--)
    crt_new();
  // REP(i,d)cout<<t[i]<<" ";cout<<endl;
  REP(i, d) cout << t[i] << endl;
  // cout<<r<<endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  solve();
  return 0;
}
Problem p02618 (C++) — buggy submission, verdict: Runtime Error

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

vector<int> l(26, 0);
int last(int d, int i) { return l.at(i); }

int score(int d, int t, vector<int> &c, vector<vector<int>> &s) {
  int res = s.at(d).at(t);
  // l.at(t) = d;
  for (int i = 0; i < 26; i++) {
    if (i != t) {
      res -= c.at(i) * (d - last(d, i));
    }
  }
  return res;
}

int main() {
  int D;
  cin >> D;
  vector<int> c(26);
  vector<vector<int>> s(D + 1, vector<int>(26));

  for (int i = 0; i < 26; i++) {
    cin >> c.at(i);
  }
  for (int d = 1; d <= D; d++) {
    for (int i = 0; i < 26; i++) {
      cin >> s.at(d).at(i);
    }
  }

  int v = 0;
  for (int d = 1; d <= D; d++) {
    int max_score = -9999999;
    int max_t = -1;
    for (int i = 0; i < 26; i++) {
      int is = score(d, i, c, s);
      if (max_score < is) {
        max_score = is;
        max_t = i;
      }
    }
    v += max_score;
    l.at(max_t) = d;
    cout << (max_t + 1) << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

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

vector<int> l(26, 0);
int last(int d, int i) { return l.at(i); }

int score(int d, int t, vector<int> &c, vector<vector<int>> &s) {
  int res = s.at(d).at(t);
  // l.at(t) = d;
  for (int i = 0; i < 26; i++) {
    if (i != t) {
      res -= c.at(i) * (d - last(d, i));
    }
  }
  return res;
}

int main() {
  int D;
  cin >> D;
  vector<int> c(26);
  vector<vector<int>> s(D + 1, vector<int>(26));

  for (int i = 0; i < 26; i++) {
    cin >> c.at(i);
  }
  for (int d = 1; d <= D; d++) {
    for (int i = 0; i < 26; i++) {
      cin >> s.at(d).at(i);
    }
  }

  int v = 0;
  for (int d = 1; d <= D; d++) {
    int max_score = 0;
    int max_t = -1;
    for (int i = 0; i < 26; i++) {
      int is = score(d, i, c, s);
      // cout << "d:" << d << " t:" << i << " is:" << is << endl;
      if (max_score < is) {
        max_score = is;
        max_t = i;
      }
    }
    v += max_score;
    l.at(max_t) = d;
    cout << (max_t + 1) << endl;
  }

  return 0;
}
Problem p02618 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using ll = long long;
using P = pair<ll, ll>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll d;
  cin >> d;
  vector<ll> contest_variety(26, 0);
  vector<ll> c(26, 0);
  rep(i, 26) { cin >> c[i]; }
  vector<vector<ll>> calender(d, vector<ll>(26, 0));
  rep(i, d) {
    rep(j, 26) { cin >> calender[i][j]; }
  }

  vector<ll> choose(d);

  ll ans = 0;
  vector<ll> tmp_minus(26, 0);
  rep(i, d) {
    ll tmp = ans;
    ll minus_sum = 0;
    rep(j, 26) {
      tmp_minus[j] = c[j] * (i + 1 - contest_variety[j]);
      minus_sum += tmp_minus[j];
    }
    ll ind = 0;
    ll tmp_max = ans;
    rep(j, 26) {
      if (tmp_max < tmp - minus_sum + tmp_minus[j] + calender[i][j]) {
        ind = j;
        tmp_max = tmp - minus_sum + tmp_minus[j] + calender[i][j];
      }
    }
    cout << ind + 1 << endl;
    ans = tmp_max;
    contest_variety[ind] = i + 1;
  }
}

Error: WA

Fixed submission:

#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
using ll = long long;
using P = pair<ll, ll>;

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  ll d;
  cin >> d;
  vector<ll> contest_variety(26, 0);
  vector<ll> c(26, 0);
  rep(i, 26) { cin >> c[i]; }
  vector<vector<ll>> calender(d, vector<ll>(26, 0));
  rep(i, d) {
    rep(j, 26) { cin >> calender[i][j]; }
  }
  vector<P> choose(d, {0, 0});
  rep(i, d) {
    rep(j, 26) {
      if (choose[i].first < calender[i][j]) {
        choose[i] = {calender[i][j], i};
      }
    }
  }
  ll ans = 0;
  rep(i, d) {
    ans += choose[i].first;
    contest_variety[choose[i].second] = i + 1;
    rep(j, 26) { ans -= (j + 1) * (i + 1 - contest_variety[j]); }
    cout << ans << endl;
  }
}
Problem p02618 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

ll d, c[33], s[444][33], last[33] = {};

vector<ll> score(vector<ll> t) {
  ll last[33] = {}, i, j, sc = 0;
  vector<ll> v;
  for (i = 1; i <= d; i++) {
    sc += s[i - 1][t[i - 1]];
    last[t[i - 1]] = i;
    for (j = 0; j < 26; j++)
      sc -= c[j] * (i - last[j]);
    v.pb(sc);
  }
  return v;
}

int main() {
  ll i, j;
  cin >> d;
  for (i = 0; i < 26; i++)
    cin >> c[i];
  for (i = 0; i < d; i++)
    for (j = 0; j < 26; j++)
      cin >> s[i][j];
  for (i = 1; i <= d; i++) {
    ll ma = -1e9, p = -1;
    for (j = 0; j < 26; j++) {
      if (c[j] * (i - last[j]) + s[i - 1][j] > ma) {
        ma = c[j] * (i - last[j]) + s[i - 1][j];
        p = j;
      }
    }
    cout << p + 1 << endl;
    last[p] = i;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb(x) push_back(x)
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

ll d, c[33], s[444][33];

vector<ll> score(vector<ll> t) {
  ll last[33] = {}, i, j, sc = 0;
  vector<ll> v;
  for (i = 0; i < d; i++) {
    sc += s[i][t[i]];
    last[t[i]] = i;
    for (j = 0; j < 26; j++)
      sc -= c[j] * (i - last[j]);
    v.pb(sc);
  }
  return v;
}

int main() {
  ll i, j;
  cin >> d;
  for (i = 0; i < 26; i++)
    cin >> c[i];
  for (i = 0; i < d; i++)
    for (j = 0; j < 26; j++)
      cin >> s[i][j];
  vector<ll> v;
  ll t;
  for (i = 0; i < d; i++) {
    cin >> t;
    v.pb(t--);
  }
  auto ans = score(v);
  for (i = 0; i < d; i++)
    cout << ans[i] << endl;
}
Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll d, c[26], s[365][26], t[365], ans = 0, m, x, y, v;
  set<ll> se[26];
  srand(time(NULL));
  cin >> d;
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
    se[i].insert(0);
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 0; i < d; i++) {
    t[i] = i % 26;
    se[t[i]].insert(i + 1);
    ans += s[i][t[i]];
    for (int j = 0; j < 26; j++) {
      ans -= (i + 1 - *se[j].rbegin()) * c[j];
    }
  }
  for (int i = 0; i < 26; i++) {
    se[i].insert(d + 1);
  }
  for (int i = 0; i < 20000000; i++) {
    x = i % d + 1;
    y = i % 26;
    v = 0LL;
    ans += s[x - 1][y] - s[x - 1][t[x - 1]];
    set<ll>::iterator it = se[t[x - 1]].find(x);
    it--;
    ll z = *se[t[x - 1]].upper_bound(x);
    v -= c[t[x - 1]] * (z - x) * (x - *it);
    it = se[y].lower_bound(x);
    it--;
    v += c[y] * (*se[y].lower_bound(x) - x) * (x - *it);
    if (v > 0LL) {
      ans += v;
      se[t[x - 1]].erase(x);
      t[x - 1] = y;
      se[y].insert(x);
    }
  }
  for (int i = 0; i < d; i++) {
    cout << t[i] + 1 << endl;
  }
}

Error: TLE

Fixed submission:

#include <cstdlib>
#include <ctime>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

int main() {
  ll d, c[26], s[365][26], t[365], ans = 0, m, x, y, v;
  set<ll> se[26];
  srand(time(NULL));
  cin >> d;
  for (int i = 0; i < 26; i++) {
    cin >> c[i];
    se[i].insert(0);
  }
  for (int i = 0; i < d; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> s[i][j];
    }
  }
  for (int i = 0; i < d; i++) {
    t[i] = i % 26;
    se[t[i]].insert(i + 1);
    ans += s[i][t[i]];
    for (int j = 0; j < 26; j++) {
      ans -= (i + 1 - *se[j].rbegin()) * c[j];
    }
  }
  for (int i = 0; i < 26; i++) {
    se[i].insert(d + 1);
  }
  for (int i = 0; i < 20000000; i++) {
    x = rand() % d + 1;
    y = rand() % 26;
    v = 0LL;
    ans += s[x - 1][y] - s[x - 1][t[x - 1]];
    set<ll>::iterator it = se[t[x - 1]].find(x);
    it--;
    ll z = *se[t[x - 1]].upper_bound(x);
    v -= c[t[x - 1]] * (z - x) * (x - *it);
    it = se[y].lower_bound(x);
    it--;
    v += c[y] * (*se[y].lower_bound(x) - x) * (x - *it);
    if (v > 0LL) {
      ans += v;
      se[t[x - 1]].erase(x);
      t[x - 1] = y;
      se[y].insert(x);
    }
  }
  for (int i = 0; i < d; i++) {
    cout << t[i] + 1 << endl;
  }
}
Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll mod = 1e9 + 7;

int main() {
  ll d;
  cin >> d;
  vector<ll> c(26);
  rep(i, 26) { cin >> c[i]; }
  vector<vector<pll>> s(d);
  rep(i, d) {
    rep(j, 26) {
      ll v;
      cin >> v;
      s[i].push_back(make_pair(v, j + 1));
    }
    sort(s[i].rbegin(), s[i].rend());
  }
  vector<ll> ans(d), used(26);
  ll tot = d;
  ll cnt = 0;
  while (tot) {
    rep(i, 26) {
      if (used[s[tot - 1][i].se - 1] == 0) {
        used[s[tot - 1][i].se - 1] = 1;
        ans[tot - 1] = s[tot - 1][i].se;
        tot--;
        cnt++;
        break;
      }
    }
    if (cnt == 26) {
      for (ll i = d - 27; i >= 0; i--) {
        ans[i] = s[i][0].se;
        tot--;
      }
      break;
    }
  }
  rep(i, d) { cout << ans[i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pll pair<ll, ll>
#define pq priority_queue
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define lb(c, x) distance(c.begin(), lower_bound(all(c), x))
#define ub(c, x) distance(c.begin(), upper_bound(all(c), x))

using namespace std;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ll mod = 1e9 + 7;

int main() {
  ll d;
  cin >> d;
  vector<ll> c(26);
  rep(i, 26) { cin >> c[i]; }
  vector<vector<pll>> s(d);
  rep(i, d) {
    rep(j, 26) {
      ll v;
      cin >> v;
      s[i].push_back(make_pair(v, j + 1));
    }
    sort(s[i].rbegin(), s[i].rend());
  }
  vector<ll> ans(d), used(26);
  ll tot = d;
  ll cnt = 0;
  while (tot) {
    rep(i, 26) {
      if (used[s[tot - 1][i].se - 1] == 0) {
        used[s[tot - 1][i].se - 1] = 1;
        ans[tot - 1] = s[tot - 1][i].se;
        tot--;
        cnt++;
        break;
      }
    }
    if (cnt == 26) {
      used = vector<ll>(26, 0LL);
    }
  }
  rep(i, d) { cout << ans[i] << endl; }
  return 0;
}
Problem p02618 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  int d;
  cin >> d;
  vector<int> c(26);
  for (int i = 0; i < 26; i++)
    cin >> c.at(i);

  vector<vector<int>> s(d, vector<int>(26));
  for (int j = 0; j < d; j++)
    for (int i = 0; i < 26; i++)
      cin >> s.at(j).at(i);

  vector<int> score(d, 0), contest(d, -1);
  vector<vector<int>> last(d, vector<int>(26, -1));

  for (int i = 0; i < d; i++) {
    int mx;
    for (int j = 0; j < 26; j++) {
      int temp = 0;
      for (int k = 0; k < 26; k++) {
        if (k == j)
          temp += s.at(i).at(k);
        else
          temp -= c.at(k) * (i - last.at(i).at(k));
      }
      if (j == 0) {
        mx = temp;
        contest.at(i) = j;
      }
      if (temp > mx) {
        mx = temp;
        contest.at(i) = j;
      }
    }
    if (i == 0)
      score.at(0) = mx;
    else
      score.at(i) = score.at(i - 1) + mx;
    if (i == d - 1)
      break;
    last.at(i + 1) = last.at(i);
    last.at(i + 1).at(contest.at(i)) = i;
  }
  for (int i = 0; i < d; i++)
    cout << contest.at(i) + 1 << endl;
}


Error: WA

Fixed submission:

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

int main(void) {
  int d;
  cin >> d;
  vector<int> c(26);
  for (int i = 0; i < 26; i++)
    cin >> c.at(i);

  vector<vector<int>> s(d, vector<int>(26));
  for (int j = 0; j < d; j++)
    for (int i = 0; i < 26; i++)
      cin >> s.at(j).at(i);

  vector<int> score(d, 0), contest(d, -1);
  vector<vector<int>> last(d, vector<int>(26, -1));

  for (int i = 0; i < d; i++) {
    int mx = 0;
    for (int j = 0; j < 26; j++) {
      int temp = 0;
      ;
      for (int k = 0; k < 26; k++) {
        if (k == j)
          temp += s.at(i).at(k);
        else
          temp -= c.at(k) * (i - last.at(i).at(k));
      }
      if (temp > mx) {
        mx = temp;
        contest.at(i) = j;
      }
    }
    if (i == 0)
      score.at(0) = mx;
    else
      score.at(i) = score.at(i - 1) + mx;
    if (i == d - 1)
      break;
    last.at(i + 1) = last.at(i);
    last.at(i + 1).at(contest.at(i)) = i;
  }

  for (int i = 0; i < d; i++)
    cout << contest.at(i) + 1 << endl;
}
Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <random>
#include <stdio.h>

int last(int d, int i, int *t) {
  int l = 0;
  for (int j = 1; j <= d; j++) {
    if (t[j] == i) {
      l = j;
    }
  }
  return l;
}

int rand_int(int max) {
  std::random_device rd;
  std::mt19937 gen(rd());
  std::uniform_int_distribution<> dis(1, max);

  return dis(gen);
}

int main(void) {
  int D;
  int c[27];

  scanf("%d", &D);
  for (int i = 1; i <= 26; i++) {
    scanf("%d", &c[i]);
  }

  int **s;
  s = new int *[D + 1];
  for (int i = 1; i <= D; i++) {
    s[i] = new int[27];
  }

  for (int d = 1; d <= D; d++) {
    for (int i = 1; i <= 26; i++) {
      scanf("%d", &s[d][i]);
    }
  }

  int *T = new int[D + 1];

  for (int d = 1; d <= D; d++) {
    T[d] = rand_int(26);
  }

  int score = 0;
  for (int d = 1; d <= D; d++) {
    score += s[d][T[d]];
    for (int i = 1; i <= 26; i++) {
      score -= c[i] * (d - last(d, i, T));
    }
  }

  int d_new, t_new, t_old, score_old, res;

  for (int sample = 0; sample < 1000; sample++) {
    score_old = score;
    score = 0;

    d_new = rand_int(D);
    t_new = rand_int(26);

    t_old = T[d_new];
    T[d_new] = t_new;

    for (int d = 1; d <= D; d++) {
      score += s[d][T[d]];
      for (int i = 1; i <= 26; i++) {
        score -= c[i] * (d - last(d, i, T));
      }
    }

    res = score - score_old;

    if (res < 0) {
      T[d_new] = t_old;
      score = score_old;
    }

    // printf("step = %d, score = %d\\n", sample, score);
  }

  // printf("%d\\n", score);

  for (int d = 1; d <= D; d++) {
    printf("%d\\n", T[d]);
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <random>
#include <stdio.h>

int last(int d, int i, int *t) {
  int l = 0;
  for (int j = 1; j <= d; j++) {
    if (t[j] == i) {
      l = j;
    }
  }
  return l;
}

int rand_int(int max) {
  std::random_device rd;
  std::mt19937 gen(rd());
  std::uniform_int_distribution<> dis(1, max);

  return dis(gen);
}

int main(void) {
  int D;
  int c[27];

  scanf("%d", &D);
  for (int i = 1; i <= 26; i++) {
    scanf("%d", &c[i]);
  }

  int **s;
  s = new int *[D + 1];
  for (int i = 1; i <= D; i++) {
    s[i] = new int[27];
  }

  for (int d = 1; d <= D; d++) {
    for (int i = 1; i <= 26; i++) {
      scanf("%d", &s[d][i]);
    }
  }

  int *T = new int[D + 1];

  for (int d = 1; d <= D; d++) {
    T[d] = rand_int(26);
  }

  int score = 0;
  for (int d = 1; d <= D; d++) {
    score += s[d][T[d]];
    for (int i = 1; i <= 26; i++) {
      score -= c[i] * (d - last(d, i, T));
    }
  }

  int d_new, t_new, t_old, score_old, res;

  for (int sample = 0; sample < 10000; sample++) {
    score_old = score;
    score = 0;

    d_new = rand_int(D);
    t_new = rand_int(26);

    t_old = T[d_new];
    T[d_new] = t_new;

    for (int d = 1; d <= D; d++) {
      score += s[d][T[d]];
      for (int i = 1; i <= 26; i++) {
        score -= c[i] * (d - last(d, i, T));
      }
    }

    res = score - score_old;

    if (res < 0) {
      T[d_new] = t_old;
      score = score_old;
    }
  }

  for (int d = 1; d <= D; d++) {
    printf("%d\\n", T[d]);
  }

  return 0;
}