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

#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;

static const double EPS = 1e-9;
static const int MOD = 256;

int I[257];
int R[257];
int O[257];

int main() {
  int N;

  while (cin >> N && N) {
    for (int i = 1; i <= N; i++) {
      cin >> I[i];
    }

    int ansS, ansA, ansC;
    double ansH = 100000000.0;

    for (int S = 0; S <= 15; S++) {
      for (int A = 0; A <= 15; A++) {
        for (int C = 0; C <= 15; C++) {

          double H = 0;
          R[0] = S;
          memset(O, 0, sizeof(O));

          for (int i = 1; i <= N; i++) {
            R[i] = (A * R[i - 1] + C) % MOD;
            O[(I[i] + R[i]) % MOD]++;
          }

          for (int i = 0; i < 256; i++) {
            if (O[i] != 0)
              H -= (O[i] / (double)(N)*log(O[i] / (double)(N)));
          }

          if (H + EPS < ansH) {
            ansH = H;
            ansS = S;
            ansA = A;
            ansC = C;
          }
        }
      }
    }

    cout << ansS << ' ' << ansA << ' ' << ansC << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define ll long long
#define vvi vector<vector<int>>
#define vi vector<int>
#define All(X) X.begin(), X.end()
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define pi 3.14159265359
#define shosu(X) fixed << setprecision(X)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
  int n;
  while (cin >> n, n) {
    int data[300];
    FOR(i, 1, n) cin >> data[i];
    int R[300];
    int O[300];
    int as, aa, ac;
    double lastmin = 9999999;
    REP(s, 16) {
      REP(a, 16) {
        REP(c, 16) {
          REP(i, 300) O[i] = 0;
          R[0] = s;
          FOR(i, 1, n + 1) {
            R[i] = (a * R[i - 1] + c) % 256;
            O[(data[i] + R[i]) % 256]++;
          }
          long double sum = 0;
          FOR(i, 0, 256) {
            if (O[i] != 0)
              sum -= (double)O[i] / (double)n * log((double)O[i] / (double)n);
          }
          /// cout << sum << endl;
          if (sum + 1e-11 < lastmin) {
            as = s;
            aa = a;
            ac = c;
            lastmin = sum + 1e-11;
          }
        }
      }
    }
    cout << as << " " << aa << " " << ac << endl;
  }
}
Problem p01283 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <cstdio>
#define EPS 1e-9
using namespace std;
int S, A, C, N, M = 256;
int l[257];
int u[257];

int main() {
  int i, s, a, c;
  double b;
  while (1) {
    scanf("%d", &N);
    if (!N) {
      break;
    }
    for (i = 1; i <= N; i++) {
      scanf("%d", &l[i]);
    }
    double r = 1e5;
    for (S = 15; S >= 0; S--) {
      for (A = 15; A >= 0; A--) {
        for (C = 15; C >= 0; C--) {
          int q, m[256] = {0};
          double p = 0.0;
          u[0] = S;
          for (i = 1; i <= N; i++) {
            u[i] = (A * u[i - 1] + C) % M;
            m[(l[i] + u[i]) % M]++;
          }
          for (i = 0; i < 256; i++) {
            if (m[i]) {
              p += ((double)(m[i]) / N * log((double)(m[i]) / N));
            }
          }
          if (r + EPS >= -p) {
            r = -p;
            s = S, a = A, c = C;
          }
        }
      }
    }
    printf("%d %d %d\\n", s, a, c);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
int S, A, C, N, M = 256;
int l[257];

int f(int i) {
  if (!i) {
    return S;
  }
  return ((A * f(i - 1) + C) % M);
}

int main() {
  int i, s, a, c;
  double b;
  while (1) {
    scanf("%d", &N);
    if (!N) {
      break;
    }
    for (i = 1; i <= N; i++) {
      scanf("%d", &l[i]);
    }
    double r = 1e5;
    for (S = 15; S >= 0; S--) {
      for (A = 15; A >= 0; A--) {
        for (C = 15; C >= 0; C--) {
          int q, m[256] = {0};
          double p = 0.0;
          for (i = 1; i <= N; i++) {
            m[(l[i] + f(i)) % M]++;
          }
          for (i = 0; i < 256; i++) {
            if (m[i]) {
              p += ((double)(m[i]) / N * log((double)(m[i]) / N));
            }
          }
          if (r >= -p) {
            r = -p;
            s = S, a = A, c = C;
          }
        }
      }
    }
    printf("%d %d %d\\n", s, a, c);
  }
  return 0;
}
Problem p01283 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

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

typedef long double Double;

int main() {

  typedef pair<int, int> Pii;
  typedef pair<int, Pii> Piii;

  int N;
  int const M = 256;
  while (cin >> N && N) {
    vector<int> I(N);
    rep(i, N) cin >> I[i];
    Double Entropy = 1e60;
    Piii ans;
    rep(S, 16) rep(A, 16) rep(C, 16) {
      int numofx[256] = {};
      int R = S;
      rep(i, N) {
        R = (A * R + C) % M;
        numofx[(I[i] + R) % M]++;
      }
      Double H = 0.;
      rep(i, M) {
        if (numofx[i]) {
          Double x = (Double)numofx[i] / N;
          H -= x * log(x);
        }
      }
      if (H < Entropy - 1e-9) {
        Entropy = H;
        ans = Piii(S, Pii(A, C));
      } else if (H == Entropy) {
        ans = min(ans, Piii(S, Pii(A, C)));
      }
    }
    cout << ans.first << " " << ans.second.first << " " << ans.second.second
         << endl;
  }

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

int N;
int S, A, C;
int const M = 256;
vector<int> I;
int R[M];

int calcR(int i) {
  if (i == 0) {
    return R[0] = S;
  }
  return R[i] = (A * calcR(i - 1) + C) % M;
}

inline int O(int i) { return (I[i - 1] + R[i]) % M; }
int numofx[256];
inline double H() {
  double entropy = 0.;
  rep(i, M) {
    if (numofx[i]) {
      double e = (double)numofx[i] / N;
      entropy -= e * log(e);
    }
  }
  return entropy;
}

int main() {

  typedef pair<int, int> Pii;
  typedef pair<int, Pii> Piii;
  while (cin >> N && N) {
    I.clear();
    I.resize(N);
    rep(i, N) { cin >> I[i]; }

    double entropy = 1e60;
    Piii ans = Piii(1 << 29, Pii(1 << 29, 1 << 29));
    for (S = 0; S <= 15; S++)
      for (A = 0; A <= 15; A++)
        for (C = 0; C <= 15; C++) {
          calcR(N + 10);
          rep(i, 256) numofx[i] = 0;
          rep(i, N) numofx[O(i)]++;
          double r = H();
          if (entropy > r) {
            entropy = r;
            ans = Piii(S, Pii(A, C));
          } else if (entropy == r) {
            Piii a = Piii(S, Pii(A, C));
            ans = min(ans, a);
          }
        }
    cout << ans.first << " " << ans.second.first << " " << ans.second.second
         << endl;
  }

  return 0;
}
Problem p01283 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;
int n, a[256];
int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int mi, mj, mk;
    long double v = 1e+300L;
    for (int i = 0; i < 16; i++) {
      for (int j = 0; j < 16; j++) {
        for (int k = 0; k < 16; k++) {
          int b[257] = {0}, c[257] = {0};
          b[0] = i;
          for (int l = 1; l <= n; l++)
            b[l] = (j * b[l - 1] + k) % 256;
          for (int l = 0; l < n; l++)
            c[(a[l] + b[l + 1]) % 256]++;
          long double res = 0;
          for (int l = 0; l < 256; l++) {
            if (c[l])
              res -= c[l] * log(1.0L * c[l] / n);
          }
          if (v - 1e-13L > res)
            v = res, mi = i, mj = j, mk = k;
        }
      }
    }
    cout << mi << ' ' << mj << ' ' << mk << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;
int n, a[256];
int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int mi, mj, mk;
    long double v = 1e+300L;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        for (int k = 0; k < n; k++) {
          int b[257] = {0}, c[257] = {0};
          b[0] = i;
          for (int l = 1; l <= n; l++)
            b[l] = (j * b[l - 1] + k) % 256;
          for (int l = 0; l < n; l++)
            c[(a[l] + b[l + 1]) % 256]++;
          long double res = 0;
          for (int l = 0; l < 256; l++) {
            if (c[l])
              res -= c[l] * log(1.0L * c[l] / n);
          }
          if (v - 1e-13L > res)
            v = res, mi = i, mj = j, mk = k;
        }
      }
    }
    cout << mi << ' ' << mj << ' ' << mk << endl;
  }
}
Problem p01284 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

const int INF = 1e+8;

int main() {
  int T, N;
  while (cin >> T, T) {
    int t[31], day[101], maxd = 0;
    fill(day, day + 101, INF);

    for (int i = 0; i < T; i++)
      cin >> t[i];
    cin >> N;
    for (int i = 0; i < N; i++) {
      int D, M;
      cin >> D >> M;
      day[D - 1] = min(day[D - 1], M);
      maxd = max(maxd, D);
    }

    // dp[k][t] := k日目で周期がt番目のときの最小コスト
    int dp[101][31];
    for (int i = 0; i < 101; i++)
      fill(dp[i], dp[i] + 31, INF);
    dp[0][0] = 0;

    for (int i = 0; i < maxd; i++) {
      for (int j = 0; j < T; j++) {
        if (t[j] <= day[i]) {
          dp[i + 1][(j + 1) % T] = min(dp[i + 1][(j + 1) % T], dp[i][j]);
        }
        // 周期をリセットする
        dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + 1);
      }
    }
    int ans = INF;
    for (int i = 0; i < T; i++) {
      ans = min(ans, dp[maxd][i]);
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;

int bfs(int k, string s, string g) {
  map<string, int> d;
  d[s] = 0;
  queue<string> q;
  q.push(s);

  int res = 0;
  while (!q.empty()) {
    string now = q.front();
    q.pop();
    int cost = d[now];
    if (now == g)
      return cost;

    int pos = 0, diff;
    for (int i = 0; i < k; i++) {
      if (now[i] != g[i]) {
        pos = i;
        diff = (g[i] - '0') - (now[i] - '0');
        break;
      }
    }
    for (int i = pos; i < k; i++) {
      string next = now;

      for (int j = pos; j <= i; j++) {
        next[j] += diff;
        while ('9' < next[j])
          next[j] -= 10;
        while (next[j] < '0')
          next[j] += 10;
      }
      if (!d.count(next)) {
        d[next] = cost + 1;
        q.push(next);
      }
    }
  }
  return -1;
}

int main() {
  int k;
  while (cin >> k, k) {
    string s, g;
    cin >> s >> g;

    cout << bfs(k, s, g) << endl;
  }
}
Problem p01284 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

using lint = long long;
using ldouble = long double;

const int INF = 1 << 30;

bool solve() {
  int T;
  cin >> T;
  if (T == 0)
    return false;

  vector<int> P(T);
  for (auto &p : P)
    cin >> p;

  int N;
  cin >> N;
  vector<int> M(100, 24);
  for (int i = 0; i < N; ++i) {
    int D;
    cin >> D;
    --D;

    int m;
    cin >> m;
    M[D] = min(M[D], m);
  }

  vector<int> dp(T, INF);
  dp[0] = 0;
  // dp[t] = t番目の周期にいるときのカフェインの最小値

  for (int d = 0; d < 100; ++d) {
    for (int t = 0; t < T; ++t) {
      if (P[t] > M[d])
        dp[t] = INF;
    }

    rotate(dp.begin(), dp.end() - 1, dp.end());
    dp[0] = min(dp[0], *min_element(dp.begin(), dp.end()) + 1);
  }

  cout << *min_element(dp.begin(), dp.end()) << endl;
  return true;
}

int main() {
  while (solve()) {
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

using lint = long long;
using ldouble = long double;

const int INF = 1 << 30;

bool solve() {
  int T;
  cin >> T;
  if (T == 0)
    return false;

  vector<int> P(T);
  for (auto &p : P)
    cin >> p;

  int N;
  cin >> N;
  vector<int> M(100, 24);
  for (int i = 0; i < N; ++i) {
    int D;
    cin >> D;
    cin >> M[--D];
  }

  vector<int> dp(N, INF);
  dp[0] = 0;
  // dp[t] = t番目の周期にいるときのカフェインの最小値

  for (int d = 0; d < 100; ++d) {
    for (int t = 0; t < T; ++t) {
      if (P[t] > M[d])
        dp[t] = INF;
    }

    rotate(dp.begin(), dp.end() - 1, dp.end());
    dp[0] = min(dp[0], *min_element(dp.begin(), dp.end()) + 1);
  }

  cout << *min_element(dp.begin(), dp.end()) << endl;
  return true;
}

int main() {
  while (solve()) {
  }
  return 0;
}
Problem p01284 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

const int My_Inf = 2147483647;
const long long int My_LInf = 9223372036854775807;

int main() {
  while (1) {
    int T;
    cin >> T;
    if (!T)
      break;
    vector<int> sleeps;
    for (int i = 0; i < T; ++i) {
      int t;
      cin >> t;
      sleeps.push_back(t);
    }
    int N;
    cin >> N;
    vector<int> needtimes(100, 25);
    for (int i = 0; i < N; ++i) {
      int d, m;
      cin >> d >> m;
      d--;
      needtimes[d] = min(needtimes[d], m);
    }

    vector<vector<int>> dp(100 + 1, vector<int>(T, 999));
    dp[0][0] = 0;
    for (int i = 0; i < 100; ++i) {
      for (int j = 0; j < T; ++j) {
        if (needtimes[i] >= sleeps[j]) {
          dp[i + 1][(j + 1) % T] = min(dp[i][j], dp[i + 1][(j + 1) % T]);
        }
        dp[i + 1][1 % T] = min(dp[i + 1][1 % T], dp[i][j] + 1);
      }
    }
    int ans = 99999999;
    for (int i = 0; i < T; ++i) {
      ans = min(dp[100][i], ans);
    }
    cout << ans << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#pragma warning(disable : 4996)
using namespace std;

const int My_Inf = 2147483647;
const long long int My_LInf = 9223372036854775807;

int main() {
  while (1) {
    int T;
    cin >> T;
    if (!T)
      break;
    vector<int> sleeps;
    for (int i = 0; i < T; ++i) {
      int t;
      cin >> t;
      sleeps.push_back(t);
    }
    int N;
    cin >> N;
    vector<int> needtimes(100, 25);
    for (int i = 0; i < N; ++i) {
      int d, m;
      cin >> d >> m;
      d--;
      needtimes[d] = min(needtimes[d], m);
    }

    vector<vector<int>> dp(100 + 1, vector<int>(T, 999));
    dp[0][0] = 0;
    for (int i = 0; i < 100; ++i) {
      for (int j = 0; j < T; ++j) {
        if (needtimes[i] >= sleeps[j]) {
          dp[i + 1][(j + 1) % T] = min(dp[i][j], dp[i + 1][(j + 1) % T]);
        }
        dp[i + 1][1 % T] = min(dp[i + 1][1 % T], dp[i][j] + 1);
      }
    }
    int ans = 99999999;
    for (int i = 0; i < T; ++i) {
      ans = min(dp[101][i], ans);
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p01284 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#define INF 100000
using namespace std;
int T, N;
int t[101];
typedef pair<int, int> P;
P p[201];
int dp[201][31];
int d[101];

int main(void) {
  while (1) {
    scanf("%d", &T);
    if (T == 0)
      break;
    for (int i = 0; i < T; i++) {
      scanf("%d", &t[i]);
    }
    for (int i = 0; i <= 100; i++)
      d[i] = 23;
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
      scanf("%d %d", &p[i].first, &p[i].second);
      p[i].first--;
      d[p[i].first] = min(d[p[i].first], p[i].second);
    }
    for (int i = 0; i <= 100; i++) {
      for (int j = 0; j <= T; j++) {
        dp[i][j] = INF;
      }
    }
    dp[0][0] = 0;
    int res = INF;
    for (int i = 0; i < 100; i++) {
      for (int j = 0; j < T; j++) {
        if (dp[i][j] != INF) {
          if (d[i] >= t[j]) {
            dp[i + 1][(j + 1) % T] = min(dp[i + 1][(j + 1) % T], dp[i][j]);
            if (i + 1 == 100)
              res = min(res, dp[i + 1][(j + 1) % T]);
          }
          dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + 1);
          if (i + 1 == 100)
            res = min(res, dp[i + 1][1]);
        }
      }
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
int T, N;
int t[31];
typedef pair<int, int> P;
P p[101];

int main(void) {
  while (1) {
    scanf("%d", &T);
    if (T == 0)
      break;
    for (int i = 0; i < T; i++) {
      scanf("%d", &t[i]);
    }
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
      scanf("%d %d", &p[i].first, &p[i].second);
    }
    sort(p, p + N);
    int cnt = 0, prev = -1, cafe = 0, turn = 0;
    for (int i = 0; i < N; i++) {
      if (i > 0 && p[i].first == p[i - 1].first &&
          p[i].second == p[i - 1].second)
        continue;
      if (i == 0) {
        int k = (p[i].first - 1) * 24 + p[i].second;
        while (k >= 24)
          k -= -24, turn = (turn + 1) % T;
        if (k < t[turn]) {
          cnt++;
          turn = 0;
        }
      } else {
        int k = p[i].second;
        turn = (turn + (p[i].first - p[i - 1].first)) % T;
        // printf("%d %d\\n",k,t[turn]);
        if (k < t[turn]) {
          cnt++;
          turn = 0;
        }
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p01284 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  double w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
  for (;;) {
    int T;
    cin >> T;
    if (T == 0)
      break;
    vector<int> t(T);
    for (int j = 0; j < T; j++)
      cin >> t[j];
    int N;
    cin >> N;
    vector<int> a(100, 24);
    while (N--) {
      int D, M;
      cin >> D >> M;
      a[D - 1] = min(a[D - 1], M);
    }
    vector<int> dp(T, INT_MAX);
    dp[0] = 0;
    for (int d = 0; d < 100; d++) {
      for (int j = 0; j < T; j++)
        if (t[j] > a[d])
          dp[j] = INT_MAX;
      vector<int> _dp(T);
      for (int j = 0; j < T; j++)
        _dp[(j + 1) % T] = dp[j];
      int mini = INT_MAX;
      for (int j = 0; j < T; j++)
        mini = min(mini, dp[j]);
      _dp[0] = min(_dp[0], mini + 1);
      dp = _dp;
    }
    int mini = INT_MAX;
    for (int j = 0; j < T; j++)
      mini = min(mini, dp[j]);
    cout << mini << endl;
  }
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  double w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int main() {
  for (;;) {
    int T;
    cin >> T;
    if (T == 0)
      break;
    vector<int> t(T);
    for (int j = 0; j < T; j++)
      cin >> t[j];
    vector<int> dp(T, INT_MAX);
    dp[0] = 0;
    int preD = 1;
    int N;
    cin >> N;
    while (N--) {
      int D, M;
      cin >> D >> M;
      vector<int> _dp(T, INT_MAX);
      int mini = INT_MAX;
      for (int j = 0; j < T; j++) {
        int _j = (j + D - preD) % T;
        _dp[_j] = dp[j];
        mini = min(mini, dp[j]);
      }
      _dp[0] = min(_dp[0], mini + 1);
      for (int j = 0; j < T; j++)
        if (t[j] > M)
          _dp[j] = INT_MAX;
      dp = _dp;
      preD = D;
    }
    int mini = INT_MAX;
    for (int j = 0; j < T; j++)
      mini = min(mini, dp[j]);
    cout << mini << endl;
  }
}
Problem p01284 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
const int INF = 1000000000;

int main() {
  ios_base::sync_with_stdio(false);
  while (true) {
    int T;
    cin >> T;
    if (T == 0) {
      break;
    }
    vector<int> t(T);
    for (int i = 0; i < T; ++i) {
      cin >> t[i];
    }
    int N;
    cin >> N;
    const int M = 100;
    vector<int> req(M, 24);
    for (int i = 0; i < N; ++i) {
      int d, m;
      cin >> d >> m;
      --d;
      req[d] = min(req[d], m);
    }
    vector<vector<int>> dp(M, vector<int>(T, INF));
    dp[0][0] = 0;
    for (int i = 0; i + 1 < M; ++i) {
      for (int j = 0; j < T; ++j) {
        if (dp[i][j] >= INF) {
          continue;
        }
        dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + 1);
        const int next = (j + 1) % T;
        if (t[next] > req[i + 1]) {
          continue;
        }
        dp[i + 1][next] = min(dp[i + 1][next], dp[i][j]);
      }
    }
    int answer = INF;
    for (int i = 0; i < T; ++i) {
      answer = min(answer, dp[M - 1][i]);
    }
    cout << answer << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
const int INF = 1000000000;

int main() {
  ios_base::sync_with_stdio(false);
  while (true) {
    int T;
    cin >> T;
    if (T == 0) {
      break;
    }
    vector<int> t(T);
    for (int i = 0; i < T; ++i) {
      cin >> t[i];
    }
    int N;
    cin >> N;
    vector<int> req(N, 24);
    for (int i = 0; i < N; ++i) {
      int d, m;
      cin >> d >> m;
      --d;
      req[d] = min(req[d], m);
    }
    vector<vector<int>> dp(N, vector<int>(T, INF));
    dp[0][0] = 0;
    for (int i = 0; i + 1 < N; ++i) {
      for (int j = 0; j < T; ++j) {
        if (dp[i][j] >= INF) {
          continue;
        }
        dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + 1);
        const int next = (j + 1) % T;
        if (t[next] > req[i + 1]) {
          continue;
        }
        dp[i + 1][next] = min(dp[i + 1][next], dp[i][j]);
      }
    }
    int answer = INF;
    for (int i = 0; i < T; ++i) {
      answer = min(answer, dp[N - 1][i]);
    }
    cout << answer << endl;
  }
  return 0;
}