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

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

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string> vst;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define all(ct) ct.begin(), ct.end()
#define endl "\\n"
#define fr(i, a) for (auto i : a)
#define f(i, a, b) for (int i = a; i < b; ++i)
#define fd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define in(d, v) d.find(v) != d.end()
#define mp make_pair
#define size(a) int(a.size())

const ll mod = 1e9 + 7;
const ll inf = ll(1e18);
const double PI = acos(-1);

ll modexp(ll x, ll n, int md) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return modexp((x * x) % md, n / 2, md);
  return (x * modexp((x * x) % md, n / 2, md)) % md;
}

int main() {
  fastio;
  // clock_t tm = clock();
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i <= n; ++i) {
    ans = ans + (i * (i + 1)) / 2;
  }
  if (n > 1) {
    vll e(n - 1);
    f(i, 0, n - 1) { cin >> e[i].first >> e[i].second; }
    f(i, 0, n - 1) {
      ans = ans - min(e[i].first, e[i].second) *
                      (n + 1 - max(e[i].first, e[i].second));
    }
  }
  cout << ans << endl;
  // cout<<((clock()-tm)*1.0)/CLOCKS_PER_SEC<<endl;
}


Error: 0

Fixed submission:

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

typedef long long int ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef vector<string> vst;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define all(ct) ct.begin(), ct.end()
#define endl "\\n"
#define fr(i, a) for (auto i : a)
#define f(i, a, b) for (int i = a; i < b; ++i)
#define fd(i, a, b) for (int i = a; i >= b; --i)
#define pb push_back
#define in(d, v) d.find(v) != d.end()
#define mp make_pair
#define size(a) int(a.size())

const ll mod = 1e9 + 7;
const ll inf = ll(1e18);
const double PI = acos(-1);

ll modexp(ll x, ll n, int md) {
  if (n == 0)
    return 1;
  if (n % 2 == 0)
    return modexp((x * x) % md, n / 2, md);
  return (x * modexp((x * x) % md, n / 2, md)) % md;
}

int main() {
  fastio;
  // clock_t tm = clock();
  ll n;
  cin >> n;
  vll e(n - 1);
  f(i, 0, n - 1) { cin >> e[i].first >> e[i].second; }
  ll ans = 0;
  for (ll i = 1; i <= n; ++i) {
    ans = ans + (i * (i + 1)) / 2;
  }
  f(i, 0, n) {
    ans = ans -
          min(e[i].first, e[i].second) * (n + 1 - max(e[i].first, e[i].second));
  }
  cout << ans << endl;
  // cout<<((clock()-tm)*1.0)/CLOCKS_PER_SEC<<endl;
}
Problem p02617 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n;
  cin >> n;
  ll ans = 0;
  rep(i, n) ans += ll(i + 1) * ll(n - i);
  rep(i, n - 1) {
    ll s, t;
    cin >> s >> t;
    s--;
    t--;
    if (s > t)
      swap(s, t);
    ans -= ll(s + 1) * ll(n - t);
  }
  cout << ans << endl;

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> G(n, vector<int>(0));
  rep(i, n) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    G[s].emplace_back(t);
    G[t].emplace_back(s);
  }
  ll ans = 0;
  rep(i, n) {
    // ll cnt = (i+1)*(n-i);
    int left = i;
    for (auto x : G[i])
      left = min(left, x);
    ll cnt = (i - left + 1) * (n - i);
    ans += cnt;
  }
  cout << ans << endl;

  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: 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, 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;
}

Error: TLE

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;
  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;
}
Problem p02617 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;
#define MAX 200005
#define MOD 1000000007
#define EPS 0.00000000001
#define INF 998244353
int main() {
  int n, i, u, v;
  scanf("%d", &n);
  long long temp, ans = n + 2;
  ans = ans * (n + 1);
  ans = ans * n / 6;
  for (i = 1; i < n; i++) {
    scanf("%d %d", &u, &v);
    if (u > v)
      swap(u, v);
    temp = n + 1 - v;
    temp = temp * u;
    ans = ans - temp;
  }
  printf("%lld", ans);
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <math.h>
#include <string.h>
#include <vector>
using namespace std;
#define MAX 200005
#define MOD 1000000007
#define EPS 0.00000000001
#define INF 998244353
vector<int> Graph[MAX];
vector<pair<int, int>> E;
int P[MAX];
pair<int, int> M[MAX];
int Find(int x) {
  if (P[x] == x)
    return x;
  else {
    P[x] = Find(P[x]);
    return P[x];
  }
}
bool Join(int x, int y) {
  int PoX = Find(x), PoY = Find(y);
  if (PoX == PoY)
    return false;
  else if (PoX < PoY) {
    M[PoX].second = M[PoY].second;
    P[PoY] = PoX;
  } else {
    swap(PoX, PoY);
    M[PoX].second = M[PoY].second;
    P[PoY] = PoX;
  }
}
int main() {
  int n, i, u, v;
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    P[i] = M[i].first = M[i].second = i;
  }
  long long temp, ans = n;
  ans = ans * (ans + 1) / 2;
  pair<int, int> p;
  for (i = 1; i < n; i++) {
    scanf("%d %d", &u, &v);
    if (u > v)
      swap(u, v);
    if (v - u == 1)
      Join(u, v);
    else {
      p.first = u;
      p.second = v;
      E.push_back(p);
    }
  }
  for (i = 0; i < E.size(); i++) {
    u = Find(E[i].first);
    v = Find(E[i].second);
    temp = (u - E[i].first + 1);
    temp = temp * (v - E[i].second);
    swap(u, v);
    ans += temp;
    swap(E[i].first, E[i].second);
    temp = (u - E[i].first + 1);
    temp = temp * (v - E[i].second);
    ans += temp;
  }
  printf("%lld", ans);
  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: Runtime Error

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

int n;
long long ans;

int main() {
  int u, v;
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &u, &v);
    if (u > v)
      swap(u, v);
    ans -= (long long)u * (n - v + 1);
  }
  for (int i = 1; i <= n; i++)
    ans += (long long)i * (n - i + 1);
  printf("%lld\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define mn 100005
#define inf 4444444444444444444
#define mod (ll)1000000007
#define base 31
#define fi first
#define se second
#define pii pair<int, int>
#define pb push_back
#define mp make_pair

// int t, ntest;
int n, ans;
pii edge[mn];

void input() {
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    if (x > y)
      swap(x, y);
    edge[i] = mp(x, y);
  }
}

void solve() {
  for (int i = 1; i <= n; i++)
    ans += (i + 1) * i / 2;
  for (int i = 1; i < n; i++) {
    ans -= edge[i].fi * (n - edge[i].se + 1);
  }
  cout << ans << endl;
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  //	freopen("test.inp", "r", stdin);
  //	freopen("test.out", "w", stdout);
  //	cin>>t;
  //	for (ntest=1; ntest<=t; ntest++){
  input();
  solve();
  //	}
}

// By: 646487264
Problem p02617 (C++) — buggy submission, verdict: Time Limit Exceeded

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

long aps(long a, long d, long n) { return n * (2 * a + (n - 1) * d) / 2; }

int main() {
  int N;
  cin >> N;
  long ans = 0;
  for (int i = 1; i <= N; i++) {
    ans += aps(1, 1, i);
  }
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    if (u > v)
      swap(u, v);
    ans -= (long)u * (N - v + 1);
  }
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

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

class UnionFind {
private:
  int sz;
  vector<int> par, size_;

public:
  UnionFind() {}
  UnionFind(int n) : sz(n), par(sz), size_(sz, 1) {
    iota(par.begin(), par.end(), 0);
  }
  int root(int x) {
    if (par.at(x) == x)
      return x;
    else
      return par.at(x) = root(par.at(x));
  }
  void unit(int x, int y) {
    x = root(x), y = root(y);
    if (x == y)
      return;
    if (size_.at(x) < size_.at(y))
      swap(x, y);
    par.at(y) = x;
    size_.at(x) += size_.at(y);
  }
  int size(int x) {
    x = root(x);
    return size_.at(x);
  }
  bool same(int x, int y) { return root(x) == root(y); }
};

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<pair<int, int>> V(N - 1);
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    cin >> u >> v;
    V.at(i) = {--u, --v};
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    UnionFind UF(N);
    for (int j = i; j < N; j++) {
      if (i == j) {
        ans++;
        continue;
      }
      for (auto v : V) {
        auto [a, b] = v;
        if (i <= a && a <= j && i <= b && b <= j) {
          if (!UF.same(a, b))
            UF.unit(a, b);
        }
      }
      set<int> SE;
      for (int k = i; k <= j; k++) {
        SE.insert(UF.root(k));
      }
      ans += SE.size();
    }
  }
  cout << ans << "\\n";
}
Problem p02617 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define endl '\\n'
const int N = 2e5 + 5;
vector<ll> node[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  if (fopen("GUARDS.INP", "r")) {
    freopen("GUARDS.INP", "r", stdin);
    freopen("GUARDS.OUT", "w", stdout);
  }
  ll n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    node[u].push_back(v);
    node[v].push_back(u);
  }
  ll ccp = 0, ans = 0;
  for (int i = 1; i <= n; i++) {
    ccp = ccp + i;
    for (auto j : node[i]) {
      if (j < i)
        ccp -= j;
    }
    ans += ccp;
  }
  cout << ans;
}
/*
 */


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define endl '\\n'
const int N = 2e5 + 5;
vector<ll> node[N];
/*
ll par[N], sz[N];
ll find_set(ll u){
        if (par[u] == u) return u;
        return par[u] = find_set(par[u]);
}
bool dsu(ll u, ll v){
        u = find_set(u);
        v = find_set(v);
        if (u == v) return false;
        if (sz[u] > sz[v])
        par[v] = u;
}*/
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  if (fopen("GUARDS.INP", "r")) {
    freopen("GUARDS.INP", "r", stdin);
    freopen("GUARDS.OUT", "w", stdout);
  }
  ll n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    ll u, v;
    cin >> u >> v;
    node[u].push_back(v);
    node[v].push_back(u);
  }
  ll ccp, ans = 0;
  for (int i = 1; i <= n; i++) {
    ccp = 1;
    ans++;
    /*
    for (int i = 1; i <= n; i++){
            par[i] = i;
            sz[i] = 1;
    }*/
    for (int j = i - 1; j >= 1; j--) {
      ccp = ccp + 1;
      for (auto t : node[j]) {
        if (t >= j && t <= i) {
          ccp--;
        }
      }
      ans += ccp;
    }
  }
  cout << ans;
}
/*
 */
Problem p02617 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
int main() {
  int N;
  std::cin >> N;
  long long answer = 0;
  int u, v;
  for (int i = 0; i < N - 1; i++) {
    answer += ((long long)(i + 1)) * (N - i);
    std::cin >> u >> v;
    if (u > v) {
      answer -= ((long long)v) * (N - u + 1);
    } else {
      answer -= ((long long)u) * (N - v + 1);
    }
  }
  answer += N;
  std::cout << answer;
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
int main() {
  int N;
  std::cin >> N;
  std::vector<std::vector<bool>> M(N, std::vector<bool>(N, 0));
  long answer = 0;
  for (int i = 0; i < N; i++) {
    answer += (i + 1) * (N - i);
  }
  for (int i = 0; i < N - 1; i++) {
    int u, v;
    std::cin >> u >> v;
    if (u > v) {
      answer -= v * (N - u + 1);
    } else {
      answer -= u * (N - v + 1);
    }
  }
  std::cout << answer;
  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;
using ll = long long;

#define Rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i, s, n) for (int i = s; i < (int)(n); ++i)

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

  ll answer = 0;
  For(i, 1, N + 1) { answer += ll(i) * (N - i + 1); }

  Rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    if (u > v)
      swap(u, v);
    answer -= ll(u) * (N - v + 1);
  }

  cout << answer << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define Rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define For(i, s, n) for (int i = s; i < (int)(n); ++i)

// Based on https://qiita.com/ofutonfuton/items/c17dfd33fc542c222396
class UnionFind {
  vector<int> par;

public:
  int union_count;

  UnionFind(int size) : par(size) {
    union_count = 0;
    for (int i = 0; i < size; i++)
      par[i] = i;
  }

  int root(int x) {
    if (par[x] == x)
      return x;
    return par[x] = root(par[x]);
  }

  bool unite(int x, int y) {
    int rx = root(x);
    int ry = root(y);
    if (rx == ry)
      return false;

    par[rx] = ry;
    union_count++;
    return true;
  }
};

void unite(UnionFind &uf, map<int, vector<int>> &connections, int l, int r,
           int x, int y) {
  if (!(l <= x && x <= r) || !(l <= y && y <= r))
    return;
  if (uf.unite(x, y)) {
    for (int a : connections[x]) {
      unite(uf, connections, l, r, a, x);
    }
    for (int a : connections[y]) {
      unite(uf, connections, l, r, a, x);
    }
  }
}

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

  map<int, vector<int>> connections;
  Rep(i, N - 1) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    connections[u].push_back(v);
    connections[v].push_back(u);
  }

  unsigned int answer = 0;
  Rep(l, N) {
    auto uf = UnionFind(N);
    For(r, l, N) {
      for (int u : connections[r]) {
        unite(uf, connections, l, r, u, r);
      }
      int count = (r - l) - uf.union_count + 1;
      answer += count;
    }
  }
  cout << answer << endl;
  return 0;
}
Problem p02617 (C++) — buggy submission, verdict: 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 - 1) {
    ll a = min(u[i], v[i]);
    ll b = max(u[i], v[i]);
    ans -= a * (N - b + 1);
  }
  cout << ans << endl;
}


Error: 0

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) {
    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, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

int main() {
  int d, i, j, k;
  int c[26];
  int s[365][26];
  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];

  int arr[365] = {};

  int t1, t2, s1, s2, diff, count;
  count = 0;
  while (true) {
  label1:
    for (i = 0; i < 365; ++i) {
      for (j = 0; j < 26; ++j) {
        if (arr[i] == j)
          continue;
        if (count > 10000000)
          goto label2;
        ++count;
        for (s1 = i + 1; s1 < 365; ++s1) {
          if (arr[s1] == arr[i])
            break;
        }
        for (t1 = i - 1; t1 >= 0; --t1) {
          if (arr[t1] == arr[i])
            break;
        }
        for (s2 = i + 1; s2 < 365; ++s2) {
          if (arr[s2] == j)
            break;
        }
        for (t2 = i - 1; t2 >= 0; --t2) {
          if (arr[t2] == j)
            break;
        }
        diff = s[i][j] - s[i][arr[i]] +
               (((s1 - i) * (s1 - i - 1)) / 2 + ((i - t1) * (i - t1 - 1)) / 2 -
                ((s1 - t1) * (s1 - t1 - 1)) / 2) *
                   c[arr[i]] +
               (-((s2 - i) * (s2 - i - 1)) / 2 - ((i - t2) * (i - t2 - 1)) / 2 +
                ((s2 - t2) * (s2 - t2 - 1)) / 2) *
                   c[j];
        if (diff > 0) {
          arr[i] = j;
          goto label1;
        }
      }
    }
  }
label2:
  for (i = 0; i < 365; ++i) {
    cout << (arr[i] + 1) << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <math.h>
#include <string>

using namespace std;

int main() {
  int d, i, j, k;
  int c[26];
  int s[365][26];
  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];

  int arr[365] = {};

  int t1, t2, s1, s2, diff;

  while (true) {
  label1:
    for (i = 0; i < 365; ++i) {
      for (j = 0; j < 26; ++j) {
        if (arr[i] == j)
          continue;
        for (s1 = i + 1; s1 < 365; ++s1) {
          if (arr[s1] == arr[i])
            break;
        }
        for (t1 = i - 1; t1 >= 0; --t1) {
          if (arr[t1] == arr[i])
            break;
        }
        for (s2 = i + 1; s2 < 365; ++s2) {
          if (arr[s2] == j)
            break;
        }
        for (t2 = i - 1; t2 >= 0; --t2) {
          if (arr[t2] == j)
            break;
        }
        diff = s[i][j] - s[i][arr[i]] +
               (((s1 - i) * (s1 - i - 1)) / 2 + ((i - t1) * (i - t1 - 1)) / 2 -
                ((s1 - t1) * (s1 - t1 - 1)) / 2) *
                   c[arr[i]] +
               (-((s2 - i) * (s2 - i - 1)) / 2 - ((i - t2) * (i - t2 - 1)) / 2 +
                ((s2 - t2) * (s2 - t2 - 1)) / 2) *
                   c[j];
        if (diff > 0) {
          arr[i] = j;
          goto label1;
        }
      }
    }
  }
  for (i = 0; i < 365; ++i) {
    cout << (arr[i] + 1) << "\\n";
  }

  return 0;
}
Problem p02618 (C++) — buggy submission, verdict: 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 = 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;
}

Error: TLE

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 = 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;
}