alexjercan/bugnet huggingface dataset:
Problem p02618 (C++) — buggy submission, verdict: Runtime Error

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

int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  int C[26];
  int S[26][365];
  int T[365];
  int res_do[26];

  int min_num = 100;
  int min_c = 0;
  for (int i = 0; i < 26; i++) {
    cin >> C[i];
    if (min_num > C[i]) {
      min_c = i + 1;
      min_num = C[i + 1];
    }
  }

  int max_num;
  int max_s;
  int check = 26;

  for (int i = 0; i < 365; i++) {
    if (check == 26) {
      for (int j = 0; j < 26; j++) {
        res_do[j] = 0;
      }
      check = 0;
    }

    max_num = 0;
    max_s = 26;
    for (int j = 0; j < 26; j++) {
      cin >> S[j][i];
      if (max_num < S[j][i] && res_do[j] == 0) {
        max_num = S[j][i];
        max_s = j;
      }
    }
    // cout << max_s <<":"<<max_num<<endl;
    T[i] = max_s;
    res_do[max_s] = 1;
    check++;
  }

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

  return 0;
}

int Exp(long long int exp_n, long long int exp_e, long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}


Error: -11

Fixed submission:

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

int Exp(long long int exp_n, long long int exp_e, long long exp_mod);

int main() {
  int C[26];
  int S[26][365];
  int T[365];
  int res_do[26];

  int min_num = 100;
  int min_c = 0;
  for (int i = 0; i < 26; i++) {
    cin >> C[i];
    if (min_num > C[i]) {
      min_c = i + 1;
      min_num = C[i + 1];
    }
  }

  int max_num;
  int max_s = 27;
  for (int i = 0; i < 365; i++) {
    if (max_s == 27) {
      for (int j = 0; j < 26; j++) {
        res_do[j] = 0;
      }
    }
    max_num = 0;
    for (int j = 0; j < 26; j++) {
      cin >> S[i][j];
      if (max_num < S[i][j]) {
        max_num = S[i][j];
        max_s = j;
      }
    }
    T[i] = max_s;
  }

  for (int i = 0; i < 365; i++) {
    cout << T[i] << endl;
  }

  return 0;
}

int Exp(long long int exp_n, long long int exp_e, long long int exp_mod) {
  long long int exp_o = 1;
  long long int exp_x = exp_n;
  long long int exp_tmp;

  while (0 < exp_e) {
    exp_tmp = exp_e / 2;
    if (exp_e - exp_tmp * 2 == 1) {
      exp_o = exp_o * exp_x;
      if (exp_o > exp_mod) {
        exp_o = exp_o % exp_mod;
      }
    }

    exp_x = exp_x * exp_x;
    if (exp_x > exp_mod) {
      exp_x = exp_x % exp_mod;
    }
    exp_e = exp_tmp;
  }
  return exp_o;
}
Problem p02618 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
static const double PI = acos(-1);

// Inputdata
static const int D = 365;
vector<int> c(D);
int s[D][26];
ll ans[D];
int last[26]; // コンテストが最後に行われた日
vector<int> t(D);

int checkScore(int d, int t) {
  int score = 0;
  score += s[d][t];
  last[t] = d;
  REP(i, 26) { score -= c[i] * (d - last[i]); }
  return score;
}

void input() {
  // input data
  int _;
  cin >> _;

  REP(i, 26) cin >> c[i];
  REP(d, D) {
    REP(i, 26) {
      int x;
      cin >> x;
      s[d][i] = x;
    }
  }
}

void initial() {
  REP(i, 26) last[i] = -1;
  REP(d, D) ans[d] = 0;
  // ans = 0;
}

int main() {
  input();

  initial();

  // cout << endl << "==ans==" << endl;

  // REP(d,D){
  //   int t;
  //   int score;
  //   cin >> t;
  //   --t;
  //   score = checkScore(d,t);
  //   cout << score << endl;
  // }

  int MX_score = 0;
  int MX_i = 0;

  // maxscore
  REP(d, D) {
    MX_score = 0;
    REP(i, 26) {
      int score = 0;
      ans[d] = 0;
      score = checkScore(d, i);
      if (score > MX_score) {
        MX_score = max(MX_score, score);
        ans[d] = score;
        t[d] = i + 1;
        MX_i = i;
        last[i] = 0;
      }
    }
    last[MX_i] = d;
    // cout << d   << " "<< MX_score << endl;
  }

  REP(d, D) cout << t[d] << endl;

  return 0;
}


Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) v.begin(), v.end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
static const double PI = acos(-1);

// Inputdata
static const int D = 5;
vector<int> c(D);
int s[D][26];
ll ans;
int last[26]; // コンテストが最後に行われた日
vector<int> t(D);

int checkScore(int d, int t) {
  ans += s[d][t];
  last[t] = d;
  REP(i, 26) { ans -= c[i] * (d - last[i]); }
  return ans;
}

void input() {
  // input data
  int _;
  cin >> _;

  REP(i, 26) cin >> c[i];
  REP(d, D) {
    REP(i, 26) {
      int x;
      cin >> x;
      s[d][i] = x;
    }
  }
}

void initial() {
  REP(i, 26) last[i] = -1;
  ans = 0;
}

int main() {
  input();

  initial();

  // cout << endl << "==ans==" << endl;

  // REP(d,D){
  //   int t;
  //   int score;
  //   cin >> t;
  //   --t;
  //   score = checkScore(d,t);
  //   cout << score << endl;
  // }

  ans = 0;
  int MX_score = 0;

  // maxscore
  REP(d, D) {
    MX_score = 0;
    REP(i, 26) {
      int score;
      ans = 0;
      score = checkScore(d, i);
      if (score > MX_score) {
        MX_score = max(MX_score, score);
        t[d] = i + 1;
      }
    }
    // cout << d   << " "<< MX_score << endl;
  }

  REP(d, D) cout << t[d] + 1 << endl;

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int D;
  cin >> D;

  vector<int> C(26); // 0 to 25
  rep(i, 26) { cin >> C.at(i); }

  vector<vector<int>> S(D, vector<int>(26)); // 0 to 364, 0 to 25
  rep(i, D) {
    rep(j, 26) { cin >> S.at(i).at(j); }
  }

  // int ans = 0;
  vector<int> T(D);     // 0 to 364
  vector<int> L(26, 0); // 0 to 25
  rep(i, D) {
    // int diff = 0;
    // rep(j, 26) {
    //   diff -= C.at(j) * (i + 1 - L.at(j));
    // }
    int maxplus = -1;
    int t = -1;
    rep(j, 26) {
      int plus = C.at(j) * (i + 1 - L.at(j)) + S.at(i).at(j);
      if (plus > maxplus) {
        t = j;
        maxplus = plus;
      }
    }
    L.at(t) = i + 1;
    T.at(i) = t + 1;
    // todo
  }

  rep(i, D) { cout << T.at(i) << endl; }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  int D;
  cin >> D;

  vector<int> C(26); // 0 to 25
  rep(i, 26) { cin >> C.at(i); }

  vector<vector<int>> S(D, vector<int>(26)); // 0 to 364, 0 to 25
  rep(i, D) {
    rep(j, 26) { cin >> S.at(i).at(j); }
  }

  // int ans = 0;
  vector<int> T(D);     // 0 to 25
  vector<int> L(26, 0); // 0 to 25
  rep(i, D) {
    // int diff = 0;
    // rep(j, 26) {
    //   diff -= C.at(j) * (i + 1 - L.at(j));
    // }
    int maxplus = -1;
    int t = -1;
    rep(j, 26) {
      int plus = C.at(j) * (i + 1 - L.at(j)) + S.at(i).at(j);
      if (plus > maxplus) {
        t = j;
        maxplus = plus;
      }
    }
    L.at(i) = t;
    T.at(i) = t + 1;
    // todo
  }

  rep(i, D) { cout << T.at(i) << endl; }
}
Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")

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

int c[26];
int64_t csum;
int64_t s[366][26];

int64_t eval(const vector<int> &state, int d) {
  int last[26] = {0};
  int64_t decl = -csum;
  int64_t score = 0;

  for (int i = 1; i <= d; ++i) {
    score += s[i][state[i]];
    decl += (i - last[state[i]]) * c[state[i]];
    last[state[i]] = i;
    score += decl;
    decl -= csum;
  }
  return score;
}

vector<int> beam(int D, int w) {
  map<int64_t, vector<int>, greater<int64_t>> mp;
  for (int i = 0; i < 26; ++i) {
    mp[s[1][i] + c[i] - csum] = vector<int>{2, i};
  }
  for (int d = 2; d <= D; ++d) {
    int cnt = 0;
    vector<vector<int>> states;
    for (auto p : mp) {
      states.push_back(p.second);
      ++cnt;
      if (cnt >= w)
        break;
    }
    mp.clear();
    for (auto v : states) {
      for (int i = 0; i < 26; ++i) {
        vector<int> state{v};
        state.emplace_back(i);
        mp[eval(state, d)] = state;
      }
    }
  }
  return mp.begin()->second;
}

int main() {
  int d;
  scanf("%d", &d);
  for (int i = 0; i < 26; ++i)
    scanf("%d", &c[i]);
  for (int i = 0; i < 26; ++i)
    csum += c[i];

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

  auto ans = beam(d, 270);
  for (int i = 1; i <= d; ++i)
    printf("%d\\n", ans[i] + 1);

  return 0;
}

Error: TLE

Fixed submission:

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

int c[26];
int64_t csum;
int64_t s[366][26];

int64_t eval(const vector<int> &state, int d) {
  int last[26] = {0};
  int64_t decl = -csum;
  int64_t score = 0;

  for (int i = 1; i <= d; ++i) {
    score += s[i][state[i]];
    decl += (i - last[state[i]]) * c[state[i]];
    last[state[i]] = i;
    score += decl;
    decl -= csum;
  }
  return score;
}

vector<int> beam(int D, int w) {
  map<int64_t, vector<int>, greater<int64_t>> mp;
  for (int i = 0; i < 26; ++i) {
    mp[s[1][i] + c[i] - csum] = vector<int>{2, i};
  }
  for (int d = 2; d <= D; ++d) {
    int cnt = 0;
    vector<vector<int>> states;
    for (auto p : mp) {
      states.push_back(p.second);
      ++cnt;
      if (cnt >= w)
        break;
    }
    mp.clear();
    for (auto v : states) {
      for (int i = 0; i < 26; ++i) {
        vector<int> state{v};
        state.emplace_back(i);
        mp[eval(state, d)] = state;
      }
    }
  }
  return mp.begin()->second;
}

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

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

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

  auto ans = beam(d, 280);
  for (int i = 1; i <= d; ++i)
    cout << ans[i] + 1 << endl;

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

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef pair<int,int> P;
int const INF = 1001001001;
ll const LINF = 1001001001001001001;
ll const MOD = 1000000007;

ll D;
ll c[26 + 1];
ll s[365 + 1][26 + 1];

ll last[365 + 1];

void input() {
  cin >> D;
  repn(i, 26) cin >> c[i];
  repn(i, D) repn(j, 26) cin >> s[i][j];
}

// 貪欲法2
void solve2() {
  ll v[27][400];
  ll max_score = -LINF;
  ll max_idx = 0;
  rep(k, 27) {
    ll score = 0;
    fill(last, last + 27, 0);
    repn(d, D + k) {
      ll maxi = -LINF;
      ll idx = 0;
      repn(i, 26) {
        ll res = 0;
        if (d <= D)
          res += s[d][i];
        repn(j, 26) {
          if (i == j)
            continue;
          res -= c[j] * (d - last[j]);
        }
        if (maxi < res) {
          maxi = res;
          idx = i;
        }
      }
      last[idx] = d;
      score += maxi;
      v[k][d] = idx;
    }
    if (max_score < score) {
      max_score = score;
      max_idx = k;
    }
  }

  repn(i, D) { cout << v[max_idx][i] << endl; }
}

int main() {
  input();
  solve2();
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repn(i, n) for (int i = 1; i <= n; i++)
#define repr(e, x) for (auto &e : x)
using namespace std;
typedef long long ll;
typedef long double ld;
// typedef pair<int,int> P;
int const INF = 1001001001;
ll const LINF = 1001001001001001001;
ll const MOD = 1000000007;

ll D;
ll c[26 + 1];
ll s[365 + 1][26 + 1];

ll last[365 + 1];

void input() {
  cin >> D;
  repn(i, 26) cin >> c[i];
  repn(i, D) repn(j, 26) cin >> s[i][j];
}

// 貪欲法2
void solve2() {
  ll v[27][366];
  ll max_score = -LINF;
  ll max_idx = 0;
  rep(k, 27) {
    ll score = 0;
    fill(last, last + 27, 0);
    repn(d, D + k) {
      ll maxi = -LINF;
      ll idx = 0;
      repn(i, 26) {
        ll res = 0;
        if (d <= D)
          res += s[d][i];
        repn(j, 26) {
          if (i == j)
            continue;
          res -= c[j] * (d - last[j]);
        }
        if (maxi < res) {
          maxi = res;
          idx = i;
        }
      }
      last[idx] = d;
      score += maxi;
      v[k][d] = idx;
    }
    if (max_score < score) {
      max_score = score;
      max_idx = k;
    }
  }

  repn(i, D) { cout << v[max_idx][i] << endl; }
}

int main() {
  input();
  solve2();
}
Problem p02618 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int De;
  cin >> De;
  vector<vector<int>> vec(De, vector<int>(26));
  vector<int> ve(De);
  for (int X = 0; X < De; X++) {
    cin >> ve.at(X);
  }
  for (int i = 0; i < De; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> vec.at(i).at(j);
    }
  }
  for (int X = 0; X < De; X++) {
    cout << 1 << endl;
  }
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 5) >= this->size() (which is 5)


Fixed submission:

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

int main() {
  int De;
  cin >> De;
  vector<vector<int>> vec(De, vector<int>(26));
  vector<int> ve(De);
  vector<int> v(De);
  for (int X = 0; X < De; X++) {
    cin >> ve.at(X);
    ve.at(X) = v.at(X);
  }
  sort(v.begin(), v.end());
  vector<int> A{v.at(0), v.at(1), v.at(2)};
  vector<int> B{v.at(3), v.at(4), v.at(5), v.at(6), v.at(7), v.at(8)};
  vector<int> C{v.at(9),  v.at(10), v.at(11), v.at(12), v.at(13), v.at(14),
                v.at(15), v.at(16), v.at(17), v.at(18), v.at(19)};
  vector<int> D{v.at(20), v.at(21), v.at(22), v.at(23), v.at(24), v.at(25)};
  for (int i = 0; i < De; i++) {
    for (int j = 0; j < 26; j++) {
      cin >> vec.at(i).at(j);
    }
  }
  for (int X = 1; X <= De; X++) {
    if (X % 74 > 0 && X % 74 < 4) {
      cout << A.at(X % 74 - 1) << endl;
    }
    if (X % 74 > 3 && X % 74 < 10) {
      cout << B.at(X % 74 - 1) << endl;
    }
    if (X % 74 > 9 && X % 74 < 13) {
      cout << A.at(X % 74 - 1) << endl;
    }
    if (X % 74 > 12 && X % 74 < 24) {
      cout << C.at(X % 74 - 1) << endl;
    }
    if (X % 74 > 23 && X % 74 < 30) {
      cout << B.at(X % 74 - 1) << endl;
    }
    if (X % 74 > 29 && X % 74 < 32) {
      cout << A.at(X % 74 - 1) << endl;
    }
    if (X % 74 > 31 && X % 74 < 43) {
      cout << C.at(X % 74 - 1) << endl;
    }
    if (X % 74 > 42 && X % 74 < 49) {
      cout << D.at(X % 74 - 1) << endl;
    }
    if (X % 74 > 48 && X % 74 < 55) {
      cout << B.at(X % 74 - 1) << endl;
    }
    if (X % 74 > 54 && X % 74 < 58) {
      cout << A.at(X % 74 - 1) << endl;
    }
    if (X % 74 > 57 && X % 74 < 69) {
      cout << C.at(X % 74 - 1) << endl;
    }
    if (X % 74 > 68 && X % 74 < 75) {
      cout << B.at(X % 74 - 1) << endl;
    }
  }
}
Problem p02618 (C++) — buggy submission, verdict: Runtime Error

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

ll sum_score(vector<int> &contest_no, vector<vector<int>> &s, vector<int> &c) {
  vector<int> day(26, 0);
  // スコアの合計を求める
  ll score_daysum = 0;
  rep(z, D) {
    score_daysum += s[z][contest_no[z] - 1];
    day[contest_no[z] - 1] = z + 1;
    rep(i, 26) { score_daysum -= c[i] * ((z + 1) - day[i]); }
    // cout << score_daysum << endl;
  }
  return score_daysum;
}

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

  vector<int> day(26, 0);
  vector<int> contest_no;
  rep(z, D) {
    int m_contest = 1;
    ll m_score = -1e9;
    rep(i, 26) {
      if (m_score < s[z][i] + c[i] * ((z + 1) - day[i])) {
        m_score = s[z][i] + c[i] * ((z + 1) - day[i]);
        m_contest = i + 1;
      }
    }
    day[m_contest - 1] = z + 1;

    contest_no.push_back(m_contest);
  }

  while (clock() < 180000) {
    int day = rand() % D;
    int contest = rand() % 26;
    // 変える前のデータ保持
    int old_score = sum_score(contest_no, s, c);
    int old_contest = contest_no[day];

    // //データを入れ替える
    contest_no[day] = contest;

    // // //スコアの合計を改めて計算
    // int new_score = sum_score(contest_no, s ,c);

    // 新旧のスコアを比較
    // if(old_score > new_score){
    contest_no[day] = old_contest;
    //}
  }
  // 解の出力
  for (int i : contest_no) {
    cout << i << endl;
  }
}


Error: WA

Fixed submission:

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

ll sum_score(vector<int> &contest_no, vector<vector<int>> &s, vector<int> &c) {
  vector<int> day(26, 0);
  // スコアの合計を求める
  ll score_daysum = 0;
  rep(z, D) {
    score_daysum += s[z][contest_no[z] - 1];
    day[contest_no[z]] = z + 1;
    rep(i, 26) { score_daysum -= c[i] * ((z + 1) - day[i]); }
    // cout << score_daysum << endl;
  }
  return score_daysum;
}

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

  vector<int> day(26, 0);
  vector<int> contest_no;
  rep(z, D) {
    int m_contest = 1;
    ll m_score = -1e9;
    rep(i, 26) {
      if (m_score < s[z][i] + c[i] * ((z + 1) - day[i])) {
        m_score = s[z][i] + c[i] * ((z + 1) - day[i]);
        m_contest = i + 1;
      }
    }
    day[m_contest - 1] = z + 1;

    contest_no.push_back(m_contest);
  }

  while (clock() < 180000) {
    int day = rand() % D;
    int contest = rand() % 26;
    // 変える前のデータ保持
    int old_score = sum_score(contest_no, s, c);
    int old_contest = contest_no[day];

    // //データを入れ替える
    contest_no[day] = contest;

    // // //スコアの合計を改めて計算
    int new_score = sum_score(contest_no, s, c);

    // 新旧のスコアを比較
    if (old_score > new_score) {
      contest_no[day] = old_contest;
    }
  }

  // 解の出力
  for (int i : contest_no) {
    cout << i << endl;
  }
}
Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int d, ans[365], c[26], s[365][26], last[26], tlast[26];
const int INF = 1e9;

#define REP(i, n) for (int i = 0; i < n; i++)

void FIN(int a) {
  if (a) {
    ofstream fout("output.txt");
    REP(i, d) fout << ans[i] << "\\n";
    fout.close();
    return;
  }
  REP(i, d) cout << ans[i] << "\\n";
  return;
}

int score() {
  int res = 0;
  memset(tlast, -1, sizeof(tlast));
  REP(day, d) {
    res += s[day][ans[day] - 1];
    tlast[ans[day] - 1] = day;
    REP(i, 26) res -= c[i] * (day - tlast[i]);
  }
  return res;
}
void swap() {
  int day = rand() % 362, diff = rand() % 3, nowscore = score();
  swap(ans[day], ans[day + diff]);
  if (score() < nowscore)
    swap(ans[day], ans[day + diff]);
}
void onechange() {
  int day = rand() % 365, nowscore = score();
  REP(i, 26) {
    int tmp = ans[day];
    ans[day] = i + 1;
    if (score() < nowscore)
      ans[day] = tmp;
    else
      nowscore = score();
  }
}

int main() {
  memset(last, -1, sizeof(last));
  cin >> d;
  REP(i, 26) cin >> c[i];
  REP(i, d) REP(j, 26) cin >> s[i][j];
  REP(day, d) {
    int idx, mx = -INF;
    REP(i, 26) {
      int A = c[i] * (day - last[i]) + s[day][i];
      if (mx < A)
        mx = A, idx = i;
    }
    ans[day] = idx + 1, last[idx] = day;
  }
  REP(_, 80) {
    REP(__, 100) swap();
    REP(__, 100) onechange();
  }
  FIN(0);
}


Error: TLE

Fixed submission:

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

int d, ans[365], c[26], s[365][26], last[26], tlast[26];
const int INF = 1e9;

#define REP(i, n) for (int i = 0; i < n; i++)

void FIN(int a) {
  if (a) {
    ofstream fout("output.txt");
    REP(i, d) fout << ans[i] << "\\n";
    fout.close();
    return;
  }
  REP(i, d) cout << ans[i] << "\\n";
  return;
}

int score() {
  int res = 0;
  memset(tlast, -1, sizeof(tlast));
  REP(day, d) {
    res += s[day][ans[day] - 1];
    tlast[ans[day] - 1] = day;
    REP(i, 26) res -= c[i] * (day - tlast[i]);
  }
  return res;
}
void swap() {
  int day = rand() % 362, diff = rand() % 3, nowscore = score();
  swap(ans[day], ans[day + diff]);
  if (score() < nowscore)
    swap(ans[day], ans[day + diff]);
}
void onechange() {
  int day = rand() % 365, nowscore = score();
  REP(i, 26) {
    int tmp = ans[day];
    ans[day] = i + 1;
    if (score() < nowscore)
      ans[day] = tmp;
    else
      nowscore = score();
  }
}

int main() {
  memset(last, -1, sizeof(last));
  cin >> d;
  REP(i, 26) cin >> c[i];
  REP(i, d) REP(j, 26) cin >> s[i][j];
  REP(day, d) {
    int idx, mx = -INF;
    REP(i, 26) {
      int A = c[i] * (day - last[i]) + s[day][i];
      if (mx < A)
        mx = A, idx = i;
    }
    ans[day] = idx + 1, last[idx] = day;
  }
  REP(_, 90) {
    REP(__, 100) swap();
    REP(__, 100) onechange();
  }
  FIN(0);
}