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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>

using namespace std;
typedef long long ll;

ll t[1000];
int main() {
  int N;
  while (cin >> N && N) {
    ll A, B;
    scanf("%lld %lld", &A, &B);
    ll C;
    scanf("%lld", &C);
    ll cost = A, cal = C;
    for (int i = 0; i < N; i++) {
      scanf("%lld", t + i);
    }
    sort(t, t + N);
    reverse(t, t + N);
    for (int i = 0; i < N; i++) {
      if (1. * (cal + t[i]) / (cost + B) >= 1. * cal / cost) {
        cal += t[i];
        cost += B;
      } else
        break;
    }
    cout << cal / cost << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;

typedef int INT;
#define int long long

int a[100000];
INT main() {
  int n;
  while (scanf("%lld", &n), n) {
    for (int i = 0; i < n; i++)
      scanf("%lld", a + i);
    sort(a, a + n);
    int ans = 0;
    for (int i = 0; i < n; i++) {
      ans += a[i] * (n - i - 1);
      // printf("%d ",ans);
    }
    printf("%lld\\n", ans);
  }
}
Problem p00490 (C++) — buggy submission, verdict: Time Limit Exceeded

// AOJ 0567: Best Pizza
#include <algorithm>
#include <iostream>
using namespace std;
const int MAX_N = 100;
int n, a, b, c, ans, cal = 0, cost = 0, tcal[MAX_N];
int main() {
  cin >> n >> a >> b >> c;
  for (int i = 0; i < n; i++) {
    cin >> tcal[i];
  }
  ans = c / a;
  cost = a;
  cal = c;
  sort(tcal, tcal + n);
  for (int i = n - 1; i >= 0; i--) {
    if (ans < tcal[i] / b) {
      cal += tcal[i];
      cost += b;
      ans = cal / cost;
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

// AOJ 0567: Best Pizza
#include <algorithm>
#include <iostream>
using namespace std;
const int MAX_N = 100;
int n, a, b, c, ans, cal = 0, cost = 0, roop2 = 0, tcal[MAX_N + 1],
                     save[MAX_N + 1], save2[MAX_N + 1][MAX_N + 1];
void solve(int roop) {
  if (roop == 0)
    return;
  for (int i = 1; i <= n; i++) {
    if (save[i] == 1)
      continue;
    save[i] = 1;
    save2[i][roop] = 1;
    roop--;
    cost += b;
    cal += tcal[i];
    ans = max(ans, cal / cost);
    solve(roop);
    for (int j = 1; j <= n; j++) {
      if (save2[j][roop] == 1) {
        save[j] = 0;
        save2[j][roop] = 0;
      }
    }
    cost -= b;
    cal -= tcal[i];
    roop++;
    if (roop == n)
      save[i] = 1;
  }
}
int main() {
  cin >> n >> a >> b >> c;
  for (int i = 1; i <= n; i++) {
    cin >> tcal[i];
  }
  ans = c / a;
  cost = a;
  cal = c;
  solve(n);
  cout << ans << endl;
}
Problem p00490 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int rec(int, int, int);

using namespace std;

int n;
int k_n;
int k_prise, t_prise;
vector<int> topi;
vector<vector<int>> dp;

int main() {
  cin >> n;
  cin >> k_prise >> t_prise;
  cin >> k_n;

  dp.resize(n + 1);
  int p = k_prise + t_prise * n;
  topi.resize(n + 1);

  for (int i = 1; i < n + 1; i++) {
    int tmp;
    dp[i - 1].resize(p + 1);
    cin >> tmp;
    topi[i] = tmp;
    ;
  }
  dp[n].resize(p + 1);
  for (int i = k_prise; i <= p; i++) {
    dp[0][i] = k_n;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = k_prise; j <= p; j++) {
      dp[i][j] = dp[i - 1][j];
      if (k_prise + t_prise <= j)
        dp[i][j] = max(dp[i][j], dp[i - 1][j - t_prise] + topi[i]);
    }
  }

  // method
  // int ans = rec(0,k_n,k_prise);
  int ans = 0;
  for (int i = k_prise; i <= p; i++)
    ans = max(ans, dp[n][i] / i);

  cout << ans << endl;

  return 0;
}

int rec(int i, int cal, int prise) {
  if (dp[i][prise] > -1) {
    return dp[i][prise];
  }
  int res;
  if (i == n) {
    res = cal / prise;
    dp[i][prise] = res;
  } else {
    int n1 = rec(i + 1, cal, prise);
    int n2 = rec(i + 1, cal + topi[i], prise + t_prise);
    res = max(n1, n2);
    if (res == n2)
      dp[i][prise + t_prise] = res;
    else
      dp[i][prise] = res;
  }
  return res;
}

Error: TLE

Fixed submission:

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

int rec(int, int, int);

using namespace std;

int n;
int k_n;
int k_prise, t_prise;
vector<int> topi;

int main() {
  cin >> n;
  cin >> k_prise >> t_prise;
  cin >> k_n;

  for (int i = 0; i < n; i++) {
    int tmp;
    cin >> tmp;
    topi.push_back(tmp);
  }

  // method
  int ans = rec(0, k_n, k_prise);

  cout << ans << endl;

  return 0;
}

int rec(int i, int cal, int prise) {
  if (i == n) {
    return cal / prise;
  } else {
    return max(rec(i + 1, cal, prise),
               rec(i + 1, cal + topi[i], prise + t_prise));
  }
}
Problem p00490 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;
double max_calory = 0;
int N, A, B, C;
vector<int> topping;

void solve() {
  cin >> N;
  cin >> A >> B;
  cin >> C;
  for (int i = 0; i < N; ++i) {
    int price;
    cin >> price;
    topping.push_back(price);
  }
  sort(topping.begin(), topping.end(), greater<int>());
  int calory = C;
  int cost = A;
  max_calory = (double)C / A;
  int item = 0;
  while (true) {
    calory += topping[item];
    cost += B;
    if ((double)calory / cost > max_calory) {
      max_calory = (double)calory / cost;
    } else {
      break;
    }
    ++item;
  }
  cout << (int)max_calory << endl;
}

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

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;
int max_calory = 0;
int N, A, B, C;
vector<int> topping;

void DFS(int item, int calory, int cost) {
  if (item == N) {
    if (max_calory < calory / cost) {
      max_calory = calory / cost;
    }
    return;
  }
  DFS(item + 1, calory, cost);
  DFS(item + 1, calory + topping[item], cost + B);
}

void solve() {
  cin >> N;
  cin >> A >> B;
  cin >> C;
  for (int i = 0; i < N; ++i) {
    int price;
    cin >> price;
    topping.push_back(price);
  }
  DFS(0, C, A);
  cout << max_calory << endl;
}

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <vector>

using namespace std;

int n, a, b, c, p;

int main() {
  scanf("%d", &n);
  scanf("%d %d", &a, &b);
  scanf("%d", &c);
  std::vector<int> cal(n);
  for (int j = 0; j < n; j++) {
    scanf("%d", &cal[j]);
  }

  sort(cal.begin(), cal.end(), greater<int>());

  float max = c / a; // 値段当たりのカロリー
  for (int k = 0; k < n; k++) {
    int t = 0;
    for (int l = 0; l < k; l++)
      t = t + cal[l];
    p = (c + t) / (a + k * b);
    if (p > max)
      max = p;
  }
  int bp = int(max);
  printf("%d\\n", bp);
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <vector>

using namespace std;

int n, a, b, c, p;
vector<int> cal;

int main() {
  scanf("%d", &n);
  scanf("%d %d", &a, &b);
  scanf("%d", &c);
  for (int j = 0; j < n; j++) {
    scanf("%d", &cal[j]);
  }

  sort(cal.begin(), cal.end());

  int max = c / a; // 値段当たりのカロリー
  for (int k = 0; k < n; k++) {
    int t = 0;
    for (int l = 0; l < k; l++)
      t = +cal[l];
    p = (c + t) / (a + k * b);
    if (p > max)
      max = p;
  }
  int bp = int(max);
  printf("%d", bp);
  return 0;
}
Problem p00490 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int N;
int A, B, C, D[100] = {};
int main() {
  cin >> N >> A >> B >> C;
  for (int i = 0; i < N; i++) {
    cin >> D[i];
  }

  sort(D, D + N);
  reverse(D, D + N);

  int ans = 0;
  for (int i = 0; i < N; i++) {
    int tmp = 0;
    for (int j = 0; j < i; j++) {
      tmp += D[j];
    }

    tmp = (tmp + C) / (A + B * i);
    if (ans < tmp)
      ans = tmp;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int N;
int A, B, C, D[100] = {};

int solve(int t, int cal, int total) {
  if (t == N) {
    // cout<<(C + cal) /(A+B*total)<< endl;
    return (C + cal) / (A + B * total);
  }
  return max(solve(t + 1, cal, total), solve(t + 1, cal + D[t], total + 1));
}

int main() {
  cin >> N >> A >> B >> C;
  for (int i = 0; i < N; i++) {
    cin >> D[i];
  }
  cout << solve(0, 0, 0) << endl;

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


#include <iostream>
using namespace std;

void getToppings(int number);
int NumOfToppings;
int priceOfCrust = 1;
int priceOfTopiings;
int caloryCrust;
int caloryToppings[100];
double bestCalory;
int bestToppings = 0;
int n;
double bestCombo;
double tempCalory;
double tempBest;

int pop_max() {

  for (int i = 0; i < n - 1; i++) {
    if (caloryToppings[i] > caloryToppings[i + 1])
      swap(caloryToppings[i], caloryToppings[i + 1]);
  }

  n--;
  return caloryToppings[n];
}

int main() {
  // get info from a user.
  cin >> NumOfToppings;
  cin >> priceOfCrust;
  cin >> priceOfTopiings;
  cin >> caloryCrust;

  bestCalory = caloryCrust;

  for (int i = 0; i < NumOfToppings; i++)
    cin >> caloryToppings[i];

  n = NumOfToppings;

  int popedMax;
  bestCombo = bestCalory / priceOfCrust;

  for (int i = 0; i < NumOfToppings; i++) {

    tempCalory = bestCalory + pop_max();
    popedMax = NumOfToppings - n;
    tempBest = tempCalory / (priceOfCrust + popedMax * priceOfTopiings);

    if (bestCombo < tempBest) {
      bestCalory = tempCalory;
      bestCombo = tempBest;
    } else
      break;
  }

  cout << (int)bestCombo << endl;
}

Error: 37

Fixed submission:



#include <iostream>
using namespace std;

void getToppings(int number);
int NumOfToppings;
int priceOfCrust = 1;
int priceOfTopiings;
int caloryCrust;
int caloryToppings[100];
double bestCalory;
int bestToppings = 0;
int n;
double bestCombo;
double tempCalory;
double tempBest;

int pop_max() {

  for (int i = 0; i < n - 1; i++) {
    if (caloryToppings[i] > caloryToppings[i + 1])
      swap(caloryToppings[i], caloryToppings[i + 1]);
  }

  n--;
  return caloryToppings[n];
}

int main() {
  // get info from a user.
  cin >> NumOfToppings;
  cin >> priceOfCrust;
  cin >> priceOfTopiings;
  cin >> caloryCrust;

  bestCalory = caloryCrust;

  for (int i = 0; i < NumOfToppings; i++)
    cin >> caloryToppings[i];

  n = NumOfToppings;

  int popedMax;
  bestCombo = bestCalory / priceOfCrust;

  for (int i = 0; i < NumOfToppings; i++) {

    tempCalory = bestCalory + pop_max();
    popedMax = NumOfToppings - n;
    tempBest = tempCalory / (priceOfCrust + popedMax * priceOfTopiings);

    if (bestCombo < tempBest) {
      bestCalory = tempCalory;
      bestCombo = tempBest;
    } else
      break;
  }

  return (int)bestCombo;
}
Problem p00490 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int n;
  int a, b;
  int c;
  int s[101];
  int x, y;

  cin >> n;

  cin >> a >> b;
  cin >> c;

  for (int i = 1; i < n + 1; i++) {
    cin >> s[i];
  }
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < n + 1; j++) {
      if (s[i] > s[j]) {
        x = s[i];
        s[i] = s[j];
        s[j] = x;
      }
    }
  }

  x = c;
  y = a;

  for (int i = 1; i < n + 1; i++) {
    if ((x / y) > ((x + s[i]) / (y + b)))
      break;

    else {
      x += s[i];
      y += b;
    }
  }
  cout << x / y << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n;
  int a, b;
  int c;
  int s[100];
  int x, y;

  cin >> n;

  cin >> a >> b;
  cin >> c;

  for (int i = 1; i < n + 1; i++) {
    cin >> s[i];
  }
  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < n + 1; j++) {
      if (s[i] > s[j]) {
        x = s[i];
        s[i] = s[j];
        s[j] = x;
      }
    }
  }

  x = c;
  y = a;

  for (int i = 1; i < n + 1; i++) {
    if ((x / y) > ((x + s[i]) / (y + b)))
      break;

    else {
      x += s[i];
      y += b;
    }
  }
  cout << x / y << endl;

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

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

int ans, sum, n, a, b, c, pp;
vector<int> piza;

int main() {

  cin >> n >> a >> b >> c;

  for (int i = 0; i < n; i++) {
    int d;
    cin >> d;
    piza.push_back(d);
  }

  sort(piza.begin(), piza.end());

  sum = piza[n - 1] + c;
  ans = sum / (a + b);
  pp = 2;

  for (int i = n - 2; i >= 0; i--) {
    sum += piza[i];
    if (ans <= sum / (a + b * (pp)))
      ans = sum / (a + b * (pp++));
    else
      break;
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int ans, n, a, b, c, piza[105];

int po(int x, int sum, int k) {

  if (x == n)
    return (c + sum) / (a + (b * k));
  else {
    ans = max(ans, po(x + 1, sum + piza[x], k + 1));
    ans = max(ans, po(x + 1, sum, k));
  }
  return ans;
}

int main() {

  cin >> n >> a >> b >> c;
  for (int i = 0; i < n; i++)
    cin >> piza[i];
  cout << po(0, 0, 0) << endl;

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#define int long long
using namespace std;
int N, A, B, C, D[101], ans, cal;
signed main() {
  cin >> N >> A >> B >> C;
  for (int i = 0; i < N; i++) {
    cin >> D[i];
  }
  sort(D, D + N);
  reverse(D, D + N);
  ans = C;
  for (int i = 0; i < N; i++) {
    ans += D[i];
    cal = max(cal, ans / (A + B * (i + 1)));
  }
  cout << cal << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define int long long
using namespace std;
int N, A, B, C, D[101], dp[101], ans;
void dfs(int num, int dol, int cal) {
  if (num == N + 1)
    return;
  ans = max(ans, cal / dol);
  // cout<<cal<<" "<<dol<<" "<<cal/dol<<endl;
  dfs(num + 1, dol + B, cal + D[num]);
  dfs(num + 1, dol, cal);
}
signed main() {
  cin >> N >> A >> B >> C;
  for (int i = 0; i < N; i++) {
    cin >> D[i];
  }
  dfs(0, A, C);
  cout << ans << endl;
  return 0;
}
Problem p00490 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

const int MAX_B = 101;

typedef struct {
  int price;
  int cal;
} Top;

int numtop, price;
Top base;
int item[MAX_B];

int solve() {
  Top now = base;
  int i = 0;
  sort(item, item + MAX_B);
  reverse(item, item + MAX_B);

  while (1) {
    if (now.cal / now.price <= (now.cal + item[i]) / (now.price + price)) {
      now.cal += item[i];
      now.price += price;
    }
    i++;
    if (i == numtop)
      break;
  }
  return now.cal / now.price;
}

int main() {
  cin >> numtop;
  cin >> base.price >> price;
  cin >> base.cal;
  for (int i = 0; i < numtop; i++) {
    cin >> item[i];
  }

  cout << solve() << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

const int MAX_B = 1001;

typedef struct {
  int price;
  int cal;
} Top;

typedef struct {
  int max;
  int sumc;
  int sump;
} Status;

int numtop, price;
int maxave = 0;
Top base;
int item[MAX_B];

int solve() {
  maxave = base.cal / base.price;
  queue<Status> Q;
  Status f;
  f.max = 0;
  f.sumc = base.cal;
  f.sump = base.price;
  Q.push(f);
  while (!Q.empty()) {
    Status now = Q.front();
    Q.pop();
    if (maxave < now.sumc / now.sump)
      maxave = now.sumc / now.sump;
    if (now.sumc / now.sump < base.cal / base.price)
      continue;

    for (int i = now.max + 1; i <= numtop; i++) {
      Status tmp = now;
      tmp.max = i;
      tmp.sumc += item[i];
      tmp.sump += price;
      if (tmp.sumc / tmp.sump > now.sumc / now.sump)
        Q.push(tmp);
    }
  }
  return maxave;
}

int main() {
  cin >> numtop;
  cin >> base.price >> price;
  cin >> base.cal;
  for (int i = 1; i <= numtop; i++) {
    cin >> item[i];
  }
  cout << solve() << endl;
  return 0;
}
Problem p00490 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

int main() {
  int n, a, b, base, top[101], cal[101];
  std::cin >> n >> a >> b >> base;
  for (int i = 0; i < n; i++)
    std::cin >> top[i];
  std::sort(top + 0, top + n);
  std::reverse(top + 0, top + n);
  for (int i = 0; i <= n; i++) {
    int all_cal = base;
    for (int j = 0; j < i; j++)
      all_cal += top[j];
    cal[i] = all_cal / (a + i * b);
  }
  std::sort(cal + 0, cal + n + 1);
  std::reverse(cal + 0, cal + n + 1);
  std::cout << cal[0] << std::endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

int main() {
  int n, a, b, base, top[100], cal[100];
  std::cin >> n >> a >> b >> base;
  for (int i = 0; i < n; i++)
    std::cin >> top[i];
  std::sort(top + 0, top + n);
  std::reverse(top + 0, top + n);
  for (int i = 0; i <= n; i++) {
    int all_cal = base;
    for (int j = 0; j < i; j++)
      all_cal += top[j];
    cal[i] = all_cal / (a + i * b);
  }
  std::sort(cal + 0, cal + n + 1);
  std::reverse(cal + 0, cal + n + 1);
  std::cout << cal[0] << std::endl;
  return 0;
}
Problem p00490 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define INF 1 << 64
#define rep(a, b) for (int a = 0; a < b; a++)
#define int long long int

using namespace std;

int n, kcost, cost, kiji, ans;
int piza[1003];
///?????????????????§????????????
int much_pizza(int m, int kal, int num) {
  if (num > n)
    return kal / (m * cost + kcost);
  else {
    return max(much_pizza(m + 1, kal + piza[num], num + 1),
               much_pizza(m, kal, num + 1));
  }
}

signed main() {
  cin >> n >> kcost >> cost >> kiji;
  rep(i, n) { cin >> piza[i]; }
  ans = kiji;
  int now = 0, before = 0;
  sort(piza, piza + n);
  for (int i = n - 1, j = 1; i > 0, j <= n; i--, j++) {
    ans += piza[i];
    now = ans / (j * cost + kcost);
    if (now < before) {
      break;
    }
    before = now;
  }
  ///?????????sort(piza.begin(),piza.end(),greater<int>());
  cout << before << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1 << 64
#define rep(a, b) for (int a = 0; a < b; a++)
#define int long long int

using namespace std;
///?????????TLE????????????
int n, kcost, cost, kiji, ans;
int memo[1003][10004];
int piza[1003];

int much_pizza(int m, int kal, int num) {
  if (num > n)
    return kal / (m * cost + kcost);
  else {
    return max(much_pizza(m + 1, kal + piza[num], num + 1),
               much_pizza(m, kal, num + 1));
  }
}
signed main() {
  cin >> n >> kcost >> cost >> kiji;
  rep(i, n) { cin >> piza[i]; }
  ///?????????sort(piza.begin(),piza.end(),greater<int>());

  int ans2 = much_pizza(0, kiji, 0);
  cout << ans2 << endl;
  return 0;
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define N 101
using namespace std;
int dp[N][4][4], n, k, d[N];

int solve(int x, int d1, int d2) {
  if (x == 0)
    return 1;
  if (dp[x][d1][d2] > 0)
    return dp[x][d1][d2];
  if (d[x] > 0) {
    if (d1 != d[x] || d2 != d[x])
      dp[x][d1][d2] = (dp[x][d1][d2] + solve(x - 1, d[x], d1)) % 10000;
  } else {
    for (int i = 1; i < 4; i++) {
      if (d1 == i && d2 == i)
        continue;
      else
        dp[x][d1][d2] = (dp[x][d1][d2] + solve(x - 1, i, d1)) % 10000;
    }
  }
  return dp[x][d1][d2];
}

int main() {
  cin >> n >> k;
  int a, b;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 4; j++)
      for (int k = 0; k < 4; k++)
        dp[i][j][k] = 0;

  for (int i = 0; i < k; i++)
    cin >> a >> b, d[a] = b;
  cout << solve(n, 0, 0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define N 101
using namespace std;
int n, k, a, b;
int dp[N][4][4], d[N];

int dfs(int x, int d1, int d2) {
  if (x == n + 1)
    return 1;
  if (d[x] > 0) {
    if (d1 == d[x] && d2 == d[x])
      return 0;
    dp[x][d1][d2] += dfs(x + 1, d[x], d1);
  } else {
    for (int i = 1; i <= 3; i++) {
      if (d1 == i && d2 == i)
        continue;
      dp[x][d1][d2] += dfs(x + 1, i, d1);
    }
  }
}

int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++)
    cin >> a >> b, d[a] = b;

  dfs(1, 0, 0);
  int ans = 0;
  for (int i = 1; i < 4; i++)
    for (int j = 1; j < 4; j++)
      ans += dp[n][i][j];
  cout << ans % 10000 << endl;
  return 0;
}
Problem p00491 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int N, K, num;
int day[101];
int a[101][4][3];

long long int dp(int d, int k, int n) {
  if (n == 3)
    return 0;

  if (a[d][k][n] >= 0)
    return a[d][k][n];

  if (d == N)
    return a[d][k][n] = 1;

  long long int b = 0;
  if (day[d + 1] != -1) {
    if (day[d + 1] == k) {
      b += dp(d + 1, day[d + 1], n + 1) % 10000;
    } else {
      b += dp(d + 1, day[d + 1], 1) % 10000;
    }
  } else {
    for (int i = 1; i <= 3; i++) {
      if (i == k) {
        b += dp(d + 1, i, n + 1) % 10000;
      } else {
        b += dp(d + 1, i, 1) % 10000;
      }
    }
  }
  return a[d][k][n] = b % 10000;
}

int main() {
  fill_n(**a, 101 * 4 * 3, -1);
  fill_n(day, 101, -1);
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> num;
    cin >> day[num];
  }
  cout << dp(0, 0, 0) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int N, K, num;
int day[101];
int a[101][4][3];

long long int dp(int d, int k, int n) {
  if (n == 3)
    return 0;

  if (d == N)
    return a[d][k][n] = 1;

  long long int b = 0;
  if (day[d + 1] != -1) {
    if (day[d + 1] == k) {
      b += dp(d + 1, day[d + 1], n + 1) % 10000;
    } else {
      b += dp(d + 1, day[d + 1], 1) % 10000;
    }
  } else {
    for (int i = 1; i <= 3; i++) {
      if (i == k) {
        b += dp(d + 1, i, n + 1) % 10000;
      } else {
        b += dp(d + 1, i, 1) % 10000;
      }
    }
  }
  return a[d][k][n] = b % 10000;
}

int main() {
  fill_n(**a, 101 * 4 * 3, -1);
  fill_n(day, 101, -1);
  cin >> N >> K;
  for (int i = 0; i < K; i++) {
    cin >> num;
    cin >> day[num];
  }
  cout << dp(0, 0, 0) << endl;
}
Problem p00491 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int MAX_N = 110;
int n, k;
int plan[MAX_N];
int memo[4][4][MAX_N];
int dfs(int a, int b, int depth) {
  if (memo[a][b][depth])
    return memo[a][b][depth];
  if (depth > n)
    return 1;
  int res = 0;
  if (plan[depth]) {
    if (not(a == b && b == plan[depth]))
      res += dfs(b, plan[depth], depth + 1);
  } else {
    for (int c = 1; c <= 3; c++) {
      if (a == b && b == c)
        continue;
      res += dfs(b, c, depth + 1);
    }
  }
  return memo[a][b][depth] = res % 10000;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> k;
  memset(memo, 0, sizeof(memo));
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    plan[a] = b;
  }
  cout << dfs(0, 0, 1) % 10000 << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int MAX_N = 110;
int n, k;
int plan[MAX_N];
int memo[3][3][MAX_N];
int dfs(int a, int b, int depth) {
  if (memo[a][b][depth])
    return memo[a][b][depth];
  if (depth > n)
    return 1;
  int res = 0;
  if (plan[depth]) {
    if (not(a == b && b == plan[depth]))
      res += dfs(b, plan[depth], depth + 1);
  } else {
    for (int c = 1; c <= 3; c++) {
      if (a == b && b == c)
        continue;
      res += dfs(b, c, depth + 1);
    }
  }
  return memo[a][b][depth] = res % 10000;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> k;
  memset(memo, 0, sizeof(memo));
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    plan[a] = b;
  }
  cout << dfs(0, 0, 1) % 10000 << endl;
  return 0;
}