Problem p02618 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define M 998244353
#define INF 1e16
typedef long long ll;
typedef pair<ll, ll> P;

ll A(ll x) {
  if (x >= 0)
    return x;
  else
    return -x;
}

ll gcd(ll a, ll b) {
  if (b > a) {
    ll tmp = b;
    b = a;
    a = tmp;
  }
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

ll c[27];
ll s[400][27];
ll last[400][27];
ll score[27];

int main(void) {
  int D;
  cin >> D;
  for (int i = 1; i <= 26; i++)
    cin >> c[i];
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }
  vector<ll> mx(D + 1, 0);
  for (int d = 1; d <= D; d++) {
    ll S = 0;
    for (int i = 1; i <= 26; i++) {
      if (S < s[d][i]) {
        mx[d] = i;
        S = s[d][i];
      }
    }
  }
  ll ans = 0;
  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<> rand26(1, 26), randday(1, D);
  for (ll d = 1; d <= D; d++) {
    for (int i = 1; i <= 26; i++) {
      last[d][i] = last[d - 1][i];
      if (i == mx[d])
        last[d][mx[d]] = d;
    }
    ans += s[d][mx[d]];
    for (ll j = 1; j <= 26; j++) {
      ans -= c[j] * (d - last[d][j]);
    }
  }
  for (ll qu = 0; qu < 5000000; qu++) {
    ll d = randday(mt);
    ll q = rand26(mt);
    // cout<<d<<" "<<q<<endl;
    ll tmp = ans;
    tmp += s[d][q] - s[d][mx[d]];
    ll old1 = last[d][mx[d]];
    ll new1 = last[d - 1][mx[d]];
    ll old2 = last[d][q];
    ll new2 = d;
    for (ll i = d; i <= D; i++) {
      tmp += c[mx[d]] * (i - last[i][mx[d]]);
      if (last[i][mx[d]] == old1)
        last[i][mx[d]] = new1;
      tmp -= c[mx[d]] * (i - last[i][mx[d]]);
    }
    for (ll i = d; i <= D; i++) {
      tmp += c[q] * (i - last[i][q]);
      if (last[i][q] == old2)
        last[i][q] = new2;
      tmp -= c[q] * (i - last[i][q]);
    }
    if (ans < tmp) {
      ans = tmp;
      mx[d] = q;
    } else {
      for (ll i = d; i <= D; i++) {
        if (last[i][mx[d]] == new1)
          last[i][mx[d]] = old1;
      }
      for (ll i = d; i <= D; i++) {
        if (last[i][q] == new2)
          last[i][q] = old2;
      }
    }
  }
  for (int i = 1; i <= D; i++)
    cout << mx[i] << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define N (1000000000 + 7)
#define M 998244353
#define INF 1e16
typedef long long ll;
typedef pair<ll, ll> P;

ll A(ll x) {
  if (x >= 0)
    return x;
  else
    return -x;
}

ll gcd(ll a, ll b) {
  if (b > a) {
    ll tmp = b;
    b = a;
    a = tmp;
  }
  if (a % b == 0)
    return b;
  else
    return gcd(b, a % b);
}

ll c[27];
ll s[400][27];
ll last[400][27];
ll score[27];

int main(void) {
  int D;
  cin >> D;
  for (int i = 1; i <= 26; i++)
    cin >> c[i];
  for (int i = 1; i <= D; i++) {
    for (int j = 1; j <= 26; j++) {
      cin >> s[i][j];
    }
  }
  vector<ll> mx(D + 1, 0);
  for (int d = 1; d <= D; d++) {
    ll S = 0;
    for (int i = 1; i <= 26; i++) {
      if (S < s[d][i]) {
        mx[d] = i;
        S = s[d][i];
      }
    }
  }
  ll ans = 0;
  random_device rnd;
  mt19937 mt(rnd());
  uniform_int_distribution<> rand26(1, 26), randday(1, D);
  for (ll d = 1; d <= D; d++) {
    for (int i = 1; i <= 26; i++) {
      last[d][i] = last[d - 1][i];
      if (i == mx[d])
        last[d][mx[d]] = d;
    }
    ans += s[d][mx[d]];
    for (ll j = 1; j <= 26; j++) {
      ans -= c[j] * (d - last[d][j]);
    }
  }
  for (ll qu = 0; qu < 2000000; qu++) {
    ll d = randday(mt);
    ll q = rand26(mt);
    // cout<<d<<" "<<q<<endl;
    ll tmp = ans;
    tmp += s[d][q] - s[d][mx[d]];
    ll old1 = last[d][mx[d]];
    ll new1 = last[d - 1][mx[d]];
    ll old2 = last[d][q];
    ll new2 = d;
    for (ll i = d; i <= D; i++) {
      tmp += c[mx[d]] * (i - last[i][mx[d]]);
      if (last[i][mx[d]] == old1)
        last[i][mx[d]] = new1;
      tmp -= c[mx[d]] * (i - last[i][mx[d]]);
    }
    for (ll i = d; i <= D; i++) {
      tmp += c[q] * (i - last[i][q]);
      if (last[i][q] == old2)
        last[i][q] = new2;
      tmp -= c[q] * (i - last[i][q]);
    }
    if (ans < tmp) {
      ans = tmp;
      mx[d] = q;
    } else {
      for (ll i = d; i <= D; i++) {
        if (last[i][mx[d]] == new1)
          last[i][mx[d]] = old1;
      }
      for (ll i = d; i <= D; i++) {
        if (last[i][q] == new2)
          last[i][q] = old2;
      }
    }
  }
  for (int i = 1; i <= D; i++)
    cout << mx[i] << endl;
  return 0;
}