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

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
#define P pair<int, int>
const int MOD = 1e9 + 7;
const ll INF = 1e16;

int main() {
  ios_base::sync_with_stdio(false);

  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n + 1) cin >> a[i];
  vector<ll> up(n + 1);
  bool flag = 1;
  up[0] = 1 - a[0];
  vector<ll> down(n + 1);
  if (n > 0) {
    rep(i, n) {
      if (up[i] < INF)
        up[i + 1] = up[i] * 2 - a[i + 1];
      else
        up[i + 1] = up[i];
      if ((up[i + 1] <= 0 && i < n - 1) || up[i + 1] < 0)
        flag = 0;
    }
    down[n - 1] = a[n];
    if (a[n] == 0)
      flag = 0;
    invrep(i, n - 1) {
      down[i] = down[i + 1] + a[i + 1];
      if (down[i] <= 0)
        flag = 0;
    }
  } else {
    if (a[0] != 1)
      flag = 0;
  }
  ll ans = 0;
  rep(i, n) {
    ll mi = (up[i] < down[i]) ? up[i] : down[i];
    ans += mi + a[i];
  }
  if (flag)
    cout << ans + a[n] << endl;
  else
    cout << -1 << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (ll i = 0; i < n; i++)
#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)
#define invrep(i, n) invrepr(i, 0, n)
#define repitr(itr, a) for (auto itr = a.begin(); itr != a.end(); ++itr)
#define P pair<int, int>
const int MOD = 1e9 + 7;
const int INF = 2e9;
;

int main() {
  ios_base::sync_with_stdio(false);

  ll n;
  cin >> n;
  vector<ll> a(n + 1);
  rep(i, n + 1) cin >> a[i];
  vector<ll> up(n + 1);
  bool flag = 1;
  up[0] = 1 - a[0];
  rep(i, n) {
    up[i + 1] = up[i] * 2 - a[i + 1];
    if (up[i + 1] < 0)
      flag = 0;
  }
  vector<ll> down(n + 1);
  down[n - 1] = a[n];
  invrep(i, n - 1) {
    down[i] = down[i + 1] + a[i + 1];
    if (down[i] < 0)
      flag = 0;
  }
  ll ans = 0;
  rep(i, n) {
    ll mi = (up[i] < down[i]) ? up[i] : down[i];
    ans += mi + a[i];
  }
  if (flag)
    cout << ans + a[n] << endl;
  else
    cout << -1 << endl;

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

#include <iomanip>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int64_t> A(N + 1);
  int64_t ASUM = 0;
  rep(i, N + 1) {
    cin >> A[i];
    ASUM += A[i];
  }
  if (N == 0) {
    if (A[0] == 1) {
      cout << 1 << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }
  if (A[0] != 0) {
    cout << -1 << endl;
    return 0;
  }

  int64_t answer = 1;
  int64_t prev = 1;
  for (int i = 1; i <= N; ++i) {
    int64_t current = min(prev * 2, ASUM);
    if (current < A[i]) {
      cout << -1 << endl;
      return 0;
    }
    prev = current - A[i];
    ASUM -= A[i];
    answer += current;
  }
  cout << answer << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <iomanip>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int64_t> A(N);
  int64_t ASUM = 0;
  rep(i, N + 1) {
    cin >> A[i];
    ASUM += A[i];
  }
  if (A[0] != 0) {
    cout << -1 << endl;
    return 0;
  }

  int64_t answer = 1;
  int64_t prev = 1;
  for (int i = 1; i <= N; ++i) {
    int64_t current = min(prev * 2, ASUM);
    if (current < A[i]) {
      cout << -1 << endl;
      return 0;
    }
    prev = current - A[i];
    ASUM -= A[i];
    answer += current;
  }
  cout << answer << endl;
  return 0;
}
Problem p02666 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <vector>

typedef long long LL;
const int Mod = 1000000007;
const int MN = 5005;

inline int qPow(int b, int e) {
  int a = 1;
  for (; e; e >>= 1, b = (LL)b * b % Mod)
    if (e & 1)
      a = (LL)a * b % Mod;
  return a;
}

int N, Num, A[MN], Fac[MN];
std::vector<int> G[MN];
int M, B[MN];

int vis[MN];
void DFS(int u) {
  ++B[M], vis[u] = 1;
  for (int v : G[u])
    DFS(v);
}
void DFS2(int u, int o) {
  vis[u] = 1;
  for (int v : G[u])
    if (!vis[v])
      DFS2(v, o);
    else if (v == o)
      ++Num;
}

int dp[MN][MN];

int main() {
  scanf("%d", &N), Fac[0] = 1;
  for (int i = 1; i <= N; ++i)
    scanf("%d", &A[i]), Fac[i] = (LL)Fac[i - 1] * i % Mod;
  for (int i = 1; i <= N; ++i)
    if (~A[i])
      G[A[i]].push_back(i);
  for (int i = 1; i <= N; ++i)
    if (!~A[i])
      ++M, DFS(i);
  for (int i = 1; i <= N; ++i)
    if (!vis[i])
      DFS2(i, i);
  //	printf("Num = %d\\n", Num);
  //	for (int i = 1; i <= M; ++i) printf("%d, ", B[i]); puts("");
  dp[0][0] = 1;
  for (int i = 1; i <= M; ++i) {
    dp[i][0] = 1;
    for (int j = 1; j <= i; ++j)
      dp[i][j] = (dp[i - 1][j] + (LL)dp[i - 1][j - 1] * B[i]) % Mod;
  }
  //	for (int j = 1; j <= M; ++j) printf("%d, ", dp[M][j]); puts("");
  dp[M][1] -= M;
  int Ans = (LL)Num * qPow(N - 1, M) % Mod;
  for (int j = 1; j <= M; ++j)
    Ans = (Ans + (LL)dp[M][j] * Fac[j - 1] % Mod * qPow(N - 1, M - j)) % Mod;
  Ans = ((LL)N * qPow(N - 1, M) % Mod - Ans + Mod) % Mod;
  printf("%d\\n", Ans);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <vector>

typedef long long LL;
const int Mod = 1000000007;
const int MN = 2005;

inline int qPow(int b, int e) {
  int a = 1;
  for (; e; e >>= 1, b = (LL)b * b % Mod)
    if (e & 1)
      a = (LL)a * b % Mod;
  return a;
}

int N, A[MN], Fac[MN];
std::vector<int> G[MN];
int M, B[MN];

int vis[MN];
void DFS(int u) {
  ++B[M], vis[u] = 1;
  for (int v : G[u])
    DFS(v);
}
void DFS2(int u) {
  vis[u] = 1;
  for (int v : G[u])
    if (!vis[v])
      DFS2(v);
}

int dp[MN][MN];

int main() {
  scanf("%d", &N), Fac[0] = 1;
  for (int i = 1; i <= N; ++i)
    scanf("%d", &A[i]), Fac[i] = (LL)Fac[i - 1] * i % Mod;
  for (int i = 1; i <= N; ++i)
    if (~A[i])
      G[A[i]].push_back(i);
  for (int i = 1; i <= N; ++i)
    if (!~A[i])
      ++M, DFS(i);
  int Num = 0;
  for (int i = 1; i <= N; ++i)
    if (!vis[i])
      ++Num, DFS2(i);
  //	printf("Num = %d\\n", Num);
  //	for (int i = 1; i <= M; ++i) printf("%d, ", B[i]); puts("");
  dp[0][0] = 1;
  for (int i = 1; i <= M; ++i) {
    dp[i][0] = 1;
    for (int j = 1; j <= M; ++j)
      dp[i][j] = (dp[i - 1][j] + (LL)dp[i - 1][j - 1] * B[i]) % Mod;
  }
  //	for (int j = 1; j <= M; ++j) printf("%d, ", dp[M][j]); puts("");
  dp[M][1] -= M;
  int Ans = Num * qPow(N - 1, M) % Mod;
  for (int j = 1; j <= M; ++j)
    Ans = (Ans + (LL)dp[M][j] * Fac[j - 1] % Mod * qPow(N - 1, M - j)) % Mod;
  Ans = ((LL)N * qPow(N - 1, M) % Mod - Ans + Mod) % Mod;
  printf("%d\\n", Ans);
  return 0;
}
Problem p02667 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>

typedef long long LL;
const int MN = 200005;

int N, C0, C1, K;
char T[MN];
int stk[MN], cnt[MN], tp;
LL Ans;

int main() {
  scanf("%s", T + 1), N = strlen(T + 1);
  for (int i = 1; i <= N; ++i)
    ++(T[i] & 1 ? C1 : C0);
  Ans = (LL)(C1 + 1) * (C1 + 1) / 4;
  T[0] = T[N + 1] = '0';
  for (int i = 0; i <= N + 1; ++i) {
    int x = T[i] - '0';
    if (!x) {
      if (tp && !stk[tp])
        ++cnt[tp];
      else
        stk[++tp] = 0, cnt[tp] = 1;
    } else {
      if (tp && stk[tp])
        --tp, Ans += C0;
      else
        stk[++tp] = 1, cnt[tp] = 1;
    }
  }
  --cnt[1], --cnt[tp];
  for (int i = 1, s = 0; 2 * i <= tp; ++i)
    s += cnt[2 * i - 1], Ans += (s + i + 1) / 2 - (i + 1) / 2 + (C0 - s);
  printf("%lld\\n", Ans);
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>

typedef long long LL;
const int MN = 100005;

int N, C0, C1, K;
char T[MN];
int stk[MN], cnt[MN], tp;
LL Ans;

int main() {
  scanf("%s", T + 1), N = strlen(T + 1);
  for (int i = 1; i <= N; ++i)
    ++(T[i] & 1 ? C1 : C0);
  Ans = (LL)(C1 + 1) * (C1 + 1) / 4;
  T[0] = T[N + 1] = '0';
  for (int i = 0; i <= N + 1; ++i) {
    int x = T[i] - '0';
    if (!x) {
      if (tp && !stk[tp])
        ++cnt[tp];
      else
        stk[++tp] = 0, cnt[tp] = 1;
    } else {
      if (tp && stk[tp])
        --tp, Ans += C0;
      else
        stk[++tp] = 1, cnt[tp] = 1;
    }
  }
  --cnt[1], --cnt[tp];
  for (int i = 1, s = 0; 2 * i <= tp; ++i)
    s += cnt[2 * i - 1], Ans += (s + i + 1) / 2 - (i + 1) / 2 + (C0 - s);
  printf("%lld\\n", Ans);
  return 0;
}
Problem p02667 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = (int)2e5 + 10;
char inp[N];

int main() {
  fastIO;
  string t;
  cin >> t;
  int n = t.size();
  int zer = 0, one = 0;
  for (int i = 0; i < n; i++) {
    inp[i] = t[i];
    if (t[i] == '0')
      zer++;
    else
      one++;
  }
  vector<int> rem;
  int pf = 0;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (t[i] == '0') {
      rem.push_back(0);
    } else {
      if (!rem.empty() && rem.back() == 1) {
        rem.pop_back();
        ans += zer;
        continue;
      }
      rem.push_back(1);
    }
  }
  pf = 0;
  int xr = 1;
  for (auto x : rem) {
    if (x == 0)
      pf++;
    else {
      ans += (pf + xr) / 2;
      ans += zer - pf;
    }
    xr ^= 1;
  }
  for (int i = 1; i <= one; i++) {
    ans += (i + 1) / 2;
  }
  cout << ans << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO                                                                 \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int N = (int)1e5 + 10;
char inp[N];

int main() {
  fastIO;
  string t;
  cin >> t;
  int n = t.size();
  int zer = 0, one = 0;
  for (int i = 0; i < n; i++) {
    inp[i] = t[i];
    if (t[i] == '0')
      zer++;
    else
      one++;
  }
  vector<int> rem;
  int pf = 0;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    if (t[i] == '0') {
      rem.push_back(0);
    } else {
      if (!rem.empty() && rem.back() == 1) {
        rem.pop_back();
        ans += zer;
        continue;
      }
      rem.push_back(1);
    }
  }
  pf = 0;
  for (auto x : rem) {
    if (x == 0)
      pf++;
    else {
      ans += pf / 2;
      ans += zer - pf;
    }
  }
  for (int i = 1; i <= one; i++) {
    ans += (i + 1) / 2;
  }
  cout << ans << "\\n";
  return 0;
}
Problem p02667 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int, int>
#define pll pair<ll, ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define CLR(a, v) memset(a, v, sizeof(a));
#define CPY(a, b) memcpy(a, b, sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
char s[200005];
int n, a[2];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int sum = 0, sum2 = 0;
  ll res = 0;
  For(i, 1, n) if (s[i] == '1')++ sum;
  For(i, 1, sum) res += (i + 1) / 2;
  sum = 0;
  sum2 = 0;
  For(i, 1, n) {
    if (s[i] == '1') {
      res += (sum2 + sum % 2) / 2;
      ++a[sum % 2];
      ++sum;
    } else {
      ++sum2;
      res += max(a[0], a[1]);
      a[0] = a[1] = max(a[0], a[1]);
    }
  }
  cout << res << endl;
}
/*
+1:1
+0:11
+1:12
+0:122
+1:223
*/

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int, int>
#define pll pair<ll, ll>
#define IT iterator
#define PB push_back
#define fi first
#define se second
#define For(i, j, k) for (int i = (int)(j); i <= (int)(k); i++)
#define Rep(i, j, k) for (int i = (int)(j); i >= (int)(k); i--)
#define CLR(a, v) memset(a, v, sizeof(a));
#define CPY(a, b) memcpy(a, b, sizeof(a));
#define debug puts("wzpakking")
#define y1 ysghysgsygsh
using namespace std;
char s[100005];
int n, a[2];
int main() {
  scanf("%s", s + 1);
  n = strlen(s + 1);
  int sum = 0, sum2 = 0;
  ll res = 0;
  For(i, 1, n) if (s[i] == '1')++ sum;
  For(i, 1, sum) res += (i + 1) / 2;
  sum = 0;
  sum2 = 0;
  For(i, 1, n) {
    if (s[i] == '1') {
      res += (sum2 + sum % 2) / 2;
      ++a[sum % 2];
      ++sum;
    } else {
      ++sum2;
      res += max(a[0], a[1]);
      if (a[0] > a[1])
        a[0] = a[1];
    }
    // cout<<a[0]<<' '<<a[1]<<' '<<endl;
  }
  cout << res << endl;
}
/*
1:1
10:11
101:112
1011:1122
*/
Problem p02668 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int md = 1000000007;
int n, m, i, j;
long long f[5005][5005], s[5005][5005], pw2[5005], o[5005], k;
long long pw(long long a, int b) {
  if (b == 0)
    return 1LL;
  if (b & 1)
    return (pw(a, b - 1) * a) % md;
  long long x = pw(a, b / 2);
  return (x * x) % md;
}
int main() {
  scanf("%d%d", &m, &n);
  for (pw2[0] = o[0] = i = 1; i <= n; i++) {
    f[0][i] = 1;
    pw2[i] = (pw2[i - 1] * 2) % md;
    o[i] = pw(pw2[i], md - 2);
  }
  for (i = 1; i <= m; i++) {
    for (k = 1; k <= n; k++)
      s[i - 1][k] =
          (s[i - 1][k - 1] + ((f[i - 1][k] * k) % md) * o[k + 1]) % md;
    for (j = 1; j <= n; j++) {
      f[i][j] = (f[i - 1][j] * (j + 1LL)) % md;
      f[i][j] = (f[i][j] + s[i - 1][j - 1] * pw2[j]) % md;
      // f[i][j]=(f[i][j]+s[i-1][j-1]*pw2[j])%md;
      // s[i][j]=(s[i][j-1]+((f[i][j]*j)%md)*o[j+1])%md;
      // s[i][j]=(((f[i][j]*j)%md)*o[j+1])%md;
      // printf("%d %d = %d\\n",i,j,f[i][j]);
    }
  }
  printf("%d\\n", f[m][n]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int md = 1000000007;
int n, m, i, j, s[5005][5005];
long long f[5005][5005], pw2[5005], o[5005], cur, k;
long long pw(long long a, int b) {
  if (b == 0)
    return 1LL;
  if (b & 1)
    return (pw(a, b - 1) * a) % md;
  long long x = pw(a, b / 2);
  return (x * x) % md;
}
int main() {
  scanf("%d%d", &m, &n);
  for (pw2[0] = o[0] = i = 1; i <= n; i++) {
    f[0][i] = 1;
    pw2[i] = (pw2[i - 1] * 2) % md;
    o[i] = pw(pw2[i], md - 2);
  }
  for (i = 1; i <= m; i++) {
    for (j = 1; j <= n; j++) {
      f[i][j] = (f[i - 1][j] * (j + 1LL)) % md;
      cur = 0;
      for (k = 1; k < j; k++) {
        s[i - 1][k] = (((f[i - 1][k] * k) % md) * o[k + 1]) % md;
        cur = (cur + s[i - 1][k]) % md;
      }
      f[i][j] = (f[i][j] + cur * pw2[j]) % md;
      // f[i][j]=(f[i][j]+s[i-1][j-1]*pw2[j])%md;
      // s[i][j]=(s[i][j-1]+((f[i][j]*j)%md)*o[j+1])%md;
      s[i][j] = (((f[i][j] * j) % md) * o[j + 1]) % md;
      // printf("%d %d = %d\\n",i,j,f[i][j]);
    }
  }
  printf("%d\\n", f[m][n]);
  return 0;
}
Problem p02668 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
const int P = 1e9 + 7;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
void update(int &x, int y) {
  x += y;
  if (x >= P)
    x -= P;
}
int power(int x, int y) {
  if (y == 0)
    return 1;
  int tmp = power(x, y / 2);
  if (y % 2 == 0)
    return 1ll * tmp * tmp % P;
  else
    return 1ll * tmp * tmp % P * x % P;
}
int dp[MAXN][MAXN];
int main() {
  int n, m;
  read(n), read(m);
  for (int i = 1; i <= m; i++)
    dp[0][i] = 1;
  for (int i = 1; i <= n; i++) {
    int sum = 0;
    for (int j = 1; j <= m; j++) {
      update(dp[i][j], 1ll * dp[i - 1][j] * (j + 1) % P);
      update(dp[i][j], sum);
      sum = (2ll * sum + 1ll * dp[i - 1][j] * j) % P;
    }
  }
  cout << dp[n][m] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5005;
const int P = 1e9 + 7;
typedef long long ll;
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
template <typename T> void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar())
    if (c == '-')
      f = -f;
  for (; isdigit(c); c = getchar())
    x = x * 10 + c - '0';
  x *= f;
}
void update(int &x, int y) {
  x += y;
  if (x >= P)
    x -= P;
}
int power(int x, int y) {
  if (y == 0)
    return 1;
  int tmp = power(x, y / 2);
  if (y % 2 == 0)
    return 1ll * tmp * tmp % P;
  else
    return 1ll * tmp * tmp % P * x % P;
}
int dp[MAXN][MAXN];
int main() {
  int n, m;
  read(n), read(m);
  for (int i = 1; i <= m; i++)
    dp[0][i] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      update(dp[i][j], 1ll * dp[i - 1][j] * (j + 1) % P);
      for (int k = 2; k <= j; k++)
        update(dp[i][j], 1ll * dp[i - 1][j - k + 1] * (j - k + 1) % P *
                             power(2, k - 2) % P);
    }
  cout << dp[n][m] << endl;
  return 0;
}
Problem p02668 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
// #define int ll

const int MOD = 1e9 + 7;
inline int sum(int a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
  return a;
}

inline void add(int &a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

inline int mult(int a, int b) { return (a * 1ll * b) % MOD; }

const int N = 5010;
int dp[N][N], pw[N], kek[N];

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  pw[0] = 1;
  for (int i = 1; i <= m; i++) {
    pw[i] = sum(pw[i - 1], pw[i - 1]);
  }
  for (int i = 0; i <= m; i++) {
    dp[i][0] = 1;
    for (int j = 1; j <= n; j++) {
      dp[i][j] = mult(dp[i][j - 1], i + 1);
      if (i >= 2) {
        kek[j] = sum(kek[j], kek[j]);
        add(kek[j], mult(dp[i - 1][j - 1], i - 1));
        add(dp[i][j], kek[j]);
      }
    }
  }
  cout << dp[m][n] << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define SZ(x) (int)(x).size()
// #define int ll

const int MOD = 1e9 + 7;
inline int sum(int a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
  return a;
}

inline void add(int &a, int b) {
  a += b;
  if (a >= MOD) {
    a -= MOD;
  }
}

inline int mult(int a, int b) { return (a * 1ll * b) % MOD; }

const int N = 5010;
int dp[N][N], pw[N];

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  pw[0] = 1;
  for (int i = 1; i <= m; i++) {
    pw[i] = sum(pw[i - 1], pw[i - 1]);
  }
  for (int i = 0; i <= m; i++) {
    dp[i][0] = 1;
    for (int j = 1; j <= n; j++) {
      dp[i][j] = mult(dp[i][j - 1], i + 1);
      for (int len = 2; len <= i; len++) {
        add(dp[i][j],
            mult(pw[len - 2], mult(dp[i - len + 1][j - 1], i - len + 1)));
      }
    }
  }
  cout << dp[m][n] << '\\n';
}
Problem p02669 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

const long long INF = 1000000000000000000;
long long n;
int a, b, c, d;
map<long long, long long> memo;

long long rec(long long x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return memo[1];
  if (memo.count(x))
    return memo[x];
  long long res = INF;
  if (n <= (INF / d))
    res = x * d;
  if (x % 5 == 0) {
    res = min(res, rec(x / 5) + c);
  } else if (x % 5 == 1) {
    res = min(res, rec(x / 5) + c + d);
  } else if (x % 5 == 2) {
    res = min(res, rec(x / 5) + c + d + d);
  } else if (x % 5 == 3) {
    res = min(res, rec((x / 5) + 1) + c + d + d);
  } else if (x % 5 == 4) {
    res = min(res, rec((x / 5) + 1) + c + d);
  }
  if (x % 3 == 0) {
    res = min(res, rec(x / 3) + b);
  } else if (x % 3 == 1) {
    res = min(res, rec(x / 3) + b + d);
  } else if (x % 3 == 2) {
    res = min(res, rec((x / 3) + 1) + b + d);
  }
  if (x % 2 == 0) {
    res = min(res, rec(x / 2) + a);
  } else if (x % 2 == 1) {
    res = min(res, rec(x / 2) + a + d);
    res = min(res, rec((x / 2) + 1) + a + d);
  }
  memo[x] = res;
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n;
    cin >> a >> b >> c >> d;
    memo.clear();
    memo[0] = 0;
    memo[1] = d;
    cout << rec(n) << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const long long INF = 10000000000000000;
long long n;
int a, b, c, d;
map<long long, long long> memo;

long long rec(long long n) {
  if (n == 0)
    return 0;
  if (memo.count(n))
    return memo[n];
  long long res = n * d;
  if (n % 5 == 0) {
    res = min(res, rec(n / 5) + c);
  } else if (n % 5 == 1) {
    res = min(res, rec(n / 5) + c + d);
  } else if (n % 5 == 2) {
    res = min(res, rec(n / 5) + c + d + d);
  } else if (n % 5 == 3) {
    res = min(res, rec((n / 5) + 1) + c + d + d);
  } else if (n % 5 == 4) {
    res = min(res, rec((n / 5) + 1) + c + d);
  }
  if (n % 3 == 0) {
    res = min(res, rec(n / 3) + b);
  } else if (n % 3 == 1) {
    res = min(res, rec(n / 3) + b + d);
  } else if (n % 3 == 2) {
    res = min(res, rec((n / 3) + 1) + b + d);
  }
  if (n % 2 == 0) {
    res = min(res, rec(n / 2) + a);
  } else if (n % 2 == 1) {
    res = min(res, rec(n / 2) + a + d);
    res = min(res, rec((n / 2) + 1) + a + d);
  }
  return res;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n;
    cin >> a >> b >> c >> d;
    memo.clear();
    memo[0] = 0;
    memo[1] = d;
    cout << rec(n) << '\\n';
  }
  return 0;
}