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

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
#define Y first
#define X second
signed main() {
  int w, h;
  while (cin >> w >> h, w || h) {
    string s[h][w];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> s[i][j];
    vector<P> v[50000];
    int gy, gx, gd = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (s[i][j] == ".")
          continue;
        if (s[i][j] == "S") {
          v[0].push_back(P(i, j));
          continue;
        }
        if (s[i][j] == "G") {
          gy = i;
          gx = j;
          continue;
        }
        int k = stoi(s[i][j]);
        v[k].push_back(P(i, j));
        gd = max(gd, k);
      }
    }
    v[++gd].push_back(P(gy, gx));
    int dp[h][w];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        dp[i][j] = 1LL << 55LL;
    dp[v[0][0].Y][v[0][0].X] = 0;
    for (int i = 0; i < gd; i++) {
      for (int j = 0; j < (int)v[i].size(); j++) {
        for (int k = 0; k < (int)v[i + 1].size(); k++) {
          int cost =
              abs(v[i + 1][k].Y - v[i][j].Y) + abs(v[i + 1][k].X - v[i][j].X);
          dp[v[i + 1][k].Y][v[i + 1][k].X] =
              min(dp[v[i + 1][k].Y][v[i + 1][k].X],
                  dp[v[i][j].Y][v[i][j].X] + cost);
        }
      }
    }
    cout << dp[v[gd][0].Y][v[gd][0].X] << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> P;
#define Y first
#define X second
signed main() {
  int w, h;
  while (cin >> w >> h, w || h) {
    string s[h][w];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> s[i][j];
    vector<P> v[2500];
    int gy, gx, gd = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (s[i][j] == ".")
          continue;
        if (s[i][j] == "S") {
          v[0].push_back(P(i, j));
          continue;
        }
        if (s[i][j] == "G") {
          gy = i;
          gx = i;
          continue;
        }
        int k = stoi(s[i][j]);
        v[k].push_back(P(i, j));
        gd = max(gd, k);
      }
    }
    gd++;
    v[gd].push_back(P(gy, gx));
    int dp[h][w];
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        dp[i][j] = 1LL << 55LL;
    dp[v[0][0].Y][v[0][0].X] = 0;
    for (int i = 0; i < gd; i++) {
      for (int j = 0; j < (int)v[i].size(); j++) {
        for (int k = 0; k < (int)v[i + 1].size(); k++) {
          int cost =
              abs(v[i + 1][k].Y - v[i][j].Y) + abs(v[i + 1][k].X - v[i][j].X);
          dp[v[i + 1][k].Y][v[i + 1][k].X] =
              min(dp[v[i + 1][k].Y][v[i + 1][k].X],
                  dp[v[i][j].Y][v[i][j].X] + cost);
        }
      }
    }
    cout << dp[v[gd][0].Y][v[gd][0].X] << endl;
  }
  return 0;
}
Problem p01401 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
vector<P> house[5000];
int sx, sy, gx, gy, n, w, h;
int memo[103][103];
int dfs(int next, int y, int x);
int manha(P a, P b);
int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    n = 0;
    for (int i = 0; i < 5000; i++) {
      house[i].clear();
    }
    memset(memo, -1, sizeof(memo));

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        string s;
        cin >> s;
        if (s == "S") {
          sx = j;
          sy = i;
        } else if (s == "G") {
          gx = j;
          gy = i;
        } else if (s != ".") {
          int num = atoi(s.data());
          house[num].push_back(P(i, j));
          if (n < num) {
            n = num;
          }
        }
      }
    }
    cout << dfs(1, sy, sx) << endl;
  }
}

int dfs(int next, int y, int x) {
  if (next > n) {
    return manha(P(y, x), P(gy, gx));
  }
  if (memo[y][x] != -1)
    return memo[y][x];

  int minn = (1 << 30);
  for (int i = 0; i < house[next].size(); i++) {
    minn =
        min(minn, dfs(next + 1, house[next][i].first, house[next][i].second) +
                      manha(P(y, x), house[next][i]));
  }
  return memo[y][x] = minn;
}

int manha(P a, P b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
vector<P> house[2502];
int sx, sy, gx, gy, n, w, h;
int memo[100][100];
int dfs(int next, int y, int x);
int manha(P a, P b);
int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    n = 0;
    for (int i = 0; i < 5000; i++) {
      house[i].clear();
    }
    memset(memo, -1, sizeof(memo));

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        string s;
        cin >> s;
        if (s == "S") {
          sx = j;
          sy = i;
        } else if (s == "G") {
          gx = j;
          gy = i;
        } else if (s != ".") {
          int num = atoi(s.data());
          house[num].push_back(P(i, j));
          if (n < num) {
            n = num;
          }
        }
      }
    }
    cout << dfs(1, sy, sx) << endl;
  }
}

int dfs(int next, int y, int x) {
  if (next > n) {
    return manha(P(y, x), P(gy, gx));
  }
  if (memo[y][x] != -1)
    return memo[y][x];

  int minn = (1 << 30);
  for (int i = 0; i < house[next].size(); i++) {
    minn =
        min(minn, dfs(next + 1, house[next][i].first, house[next][i].second) +
                      manha(P(y, x), house[next][i]));
  }
  return memo[y][x] = minn;
}

int manha(P a, P b) {
  return abs(a.first - b.first) + abs(a.second - b.second);
}
Problem p01402 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int x[100], y[100], sx[100], sy[100], n;

int lim, N, M, X;

int dp[2][1001][101];
int main() {
  while (cin >> lim >> N >> M >> X && lim) {
    memset(x, -1, sizeof(x));
    memset(y, -1, sizeof(y));
    memset(sx, -1, sizeof(sx));
    memset(sy, -1, sizeof(sy));
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < 1001; j++)
        for (int k = 0; k < 101; k++)
          dp[i][j][k] = -99999999;
    for (int i = 0; i < N; i++) {
      string t;
      cin >> t;
      cin >> sx[i] >> sy[i];
    }
    for (int i = 0; i < M; i++) {
      string t;
      cin >> t;
      cin >> x[i] >> y[i];
    }
    dp[0][0][0] = 0;

    for (int i = 0; i < M; i++) {
      for (int j = 0; j <= lim; j++) {
        for (int k = 0; k <= M; k++) {
          dp[i & 1 ^ 1][j][k] = dp[i & 1][j][k];
        }
      }
      for (int j = 0; j + x[i] <= lim; j++) {
        for (int k = 0; k < M; k++) {
          dp[i & 1 ^ 1][j + x[i]][k + 1] =
              max(dp[i & 1 ^ 1][j + x[i]][k + 1], dp[i & 1][j][k] + y[i]);
        }
      }
    }
    int ans = 0;
    for (int i = 0; i < N; i++)
      for (int j = i + 1; j < N; j++) {
        int rem = lim - sx[i] - sx[j];
        for (int w = 0; w <= rem; w++) {
          for (int z = X; z <= M; z++) {
            ans = max(ans, sy[i] + sy[j] + dp[M & 1][w][z]);
          }
        }
      }
    for (int i = 0; i < N; i++) {
      int rem = lim - sx[i];
      for (int w = 0; w <= rem; w++) {
        for (int z = X; z <= M; z++) {
          ans = max(ans, sy[i] + dp[M & 1][w][z]);
        }
      }
    }
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

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

int x[1000], y[1000], sx[1000], sy[1000], n;

map<string, int> p;
int num(string s) {
  if (p.count(s))
    return p[s];
  else {
    int t = p.size();
    return p[s] = t;
  }
}

int lim, N, M, X;

int dp[201][1001][3][101];

int dfs(int pos, int r, int a, int b) {
  if (r < 0)
    return -99999999;
  if (pos == n) {
    return (b < X || a < 1 ? -99999999 : 0);
  }
  if (~dp[pos][r][a][b])
    return dp[pos][r][a][b];

  int ans = dfs(pos + 1, r, a, b);
  if (~sx[pos] && a < 2) {
    ans = max(ans, dfs(pos + 1, r - sx[pos], a + 1, b) + sy[pos]);
  }
  if (~x[pos]) {
    ans = max(ans, dfs(pos + 1, r - x[pos], a, b + 1) + y[pos]);
  }
  return dp[pos][r][a][b] = ans;
}
int main() {
  while (cin >> lim >> N >> M >> X && lim) {
    p.clear();
    memset(x, -1, sizeof(x));
    memset(y, -1, sizeof(y));
    memset(sx, -1, sizeof(sx));
    memset(sy, -1, sizeof(sy));
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < N; i++) {
      string t;
      cin >> t;
      cin >> sx[num(t)] >> sy[num(t)];
    }
    for (int i = 0; i < M; i++) {
      string t;
      cin >> t;
      cin >> x[num(t)] >> y[num(t)];
    }
    n = p.size();
    cout << dfs(0, lim, 0, 0) << endl;
  }
}
Problem p01403 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define MAX 1000010
long long euler[MAX];
long long F[MAX];

void phi() {
  for (int i = 0; i < MAX; i++)
    euler[i] = i;
  for (int i = 2; i < MAX; i++) {
    if (euler[i] == i) {
      for (int j = i; j < MAX; j += i)
        euler[j] = euler[j] / i * (i - 1);
    }
  }
}

int main() {
  phi();
  F[0] = F[1] = 2;
  for (int i = 2; i < MAX; i++)
    F[i] = F[i - 1] + euler[i];
  int t, n;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n;
    cout << F[n] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int euclid(int a, int b) {
  int temp;

  if (a < b) {
    temp = a;
    a = b;
    b = temp;
  }
  if (b < 1)
    return -1;

  if (a % b == 0)
    return b;
  return euclid(b, a % b);
}

bool is_prim[10010];
int F[10010] = {};
int main() {

  // 素数表
  is_prim[0] = is_prim[1] = false;
  for (int i = 2; i < 10010; i++) {
    is_prim[i] = true;
  }
  for (int i = 2; i < 10010; i++) {
    if (is_prim[i]) {
      for (int j = 2; i * j < 10010; j++) {
        is_prim[i * j] = false;
      }
    }
  }
  /*
    for(int i = 2; i < 10010; i++){
    if(is_prim[i] == true){
    cout << i << " ";
    }
    }
  */

  // F[] 生成
  F[0] = 0;
  F[1] = 0;
  for (int i = 2; i < 10010; i++) {
    F[i] += F[i - 1];
    if (is_prim[i])
      F[i] += i - 1;
    else {
      for (int j = i - 1; j >= 1; j--) {
        if (euclid(i, j) == 1)
          F[i]++;
      }
    }
  }
  // 出力
  // cout << "F[3] = " << F[3] << endl;

  int n, t;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t;
    cout << F[t] + 2 << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
ll out[1000100] = {0};
ll dp[1000100] = {0};
#define MAX 1000100
int main() {
  out[1] = 2;
  rep(i, MAX) dp[i] = i;
  for (int i = 2; i < MAX; i++) {
    if (dp[i] == i) {
      for (int j = i; j < MAX; j += i)
        dp[j] = dp[j] / i * (i - 1);
    }
  }
  for (int i = 2; i < MAX; i++)
    out[i] = out[i - 1] + dp[i];
  int n;
  cin >> n;
  while (n--) {
    int a;
    cin >> a;
    cout << out[a] << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int out[10000100] = {0};
int dp[10000100] = {0};
#define MAX 10000100
int main() {
  out[1] = 2;
  rep(i, MAX) dp[i] = i;
  for (int i = 2; i < MAX; i++) {
    if (dp[i] == i) {
      for (int j = i; j < MAX; j += i)
        dp[j] = dp[j] / i * (i - 1);
    }
  }
  for (int i = 2; i < MAX; i++)
    out[i] = out[i - 1] + dp[i];
  int n;
  cin >> n;
  while (n--) {
    int a;
    cin >> a;
    cout << out[a] << endl;
  }
}
Problem p01403 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX_N 1000100

using namespace std;

typedef unsigned long long ll;

ll euler_phi(ll n) {
  ll res = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      res = res / i * (i - 1);
      for (; n % i == 0; n /= i)
        ;
    }
  }
  if (n != 1)
    res = res / n * (n - 1);
  return res;
}

ll euler[MAX_N];
ll answer[MAX_N];

void euler_phi2() {
  for (ll i = 0; i < MAX_N; i++)
    euler[i] = i;
  for (ll i = 2; i < MAX_N; i++) {
    if (euler[i] == i) {
      for (ll j = i; j < MAX_N; j += i)
        euler[j] = euler[j] / i * (i - 1);
    }
  }
}

int main() {
  euler_phi2();
  answer[1] = 2;
  REP(i, 2, MAX_N) answer[i] = answer[i - 1] + euler[i];
  int t, n;
  scanf("%d", &t);
  rep(_, t) {
    scanf("%d", &n);
    printf("%lld\\n", answer[n]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define IINF (INT_MAX)
#define MAX_N 1000100

using namespace std;

typedef unsigned long long ll;

ll euler_phi(ll n) {
  ll res = n;
  for (ll i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      res = res / i * (i - 1);
      for (; n % i == 0; n /= i)
        ;
    }
  }
  if (n != 1)
    res = res / n * (n - 1);
  return res;
}

ll euler[MAX_N];
ll answer[MAX_N];

void euler_phi2() {
  for (ll i = 0; i < MAX_N; i++)
    euler[i] = i;
  for (ll i = 2; i < MAX_N; i++) {
    if (euler[i] == i) {
      for (ll j = i; j < MAX_N; j += i)
        euler[j] = euler[j] / i * (i - 1);
    }
  }
}

int main() {
  euler_phi2();
  answer[1] = 2;
  REP(i, 2, MAX_N) answer[i] = answer[i - 1] + euler[i];
  int t, n;
  while (scanf("%d", &t), t)
    rep(_, t) {
      scanf("%d", &n);
      printf("%lld\\n", answer[n]);
    }
  return 0;
}
Problem p01403 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

vector<int> solve(int n) {
  vector<int> a(n);
  for (int i = 2; i < n; i++)
    a[i] = i - 1;
  a[1] = 2;
  for (int i = 2; i * 2 < n; i++) {
    for (int j = i * 2; j < n; j += i)
      a[j] -= a[i];
  }
  return a;
}

int main() {
  vector<int> a = solve(1000001);
  vector<ll> ans(1000001);
  ans[1] = a[1];
  for (int i = 2; i < 1000001; i++)
    ans[i] = ans[i - 1] + a[i];
  int t;
  cin >> t;

  while (t--) {
    int x;
    cin >> x;
    cout << ans[x] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

vector<int> solve(int n) {
  vector<int> a(n);
  for (int i = 2; i < n; i++)
    a[i] = i - 1;
  a[1] = 2;
  for (int i = 2; i * i < n; i++) {
    for (int j = i * i; j < n; j += i)
      a[j] -= a[i];
  }
  return a;
}

int main() {
  vector<int> a = solve(10001);
  vector<int> ans(10001);
  ans[1] = a[1];
  for (int i = 2; i < 10001; i++)
    ans[i] = ans[i - 1] + a[i];
  int t;
  cin >> t;
  while (t--) {
    int x;
    cin >> x;
    cout << ans[x] << endl;
  }
  return 0;
}
Problem p01403 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
long long ans[1100000];
int prime[1100];
int p[1100];
int main() {
  int t;
  scanf("%d", &t);
  p[0] = p[1] = -1;
  for (int i = 2; i < 1100; i++) {
    if (~p[i]) {
      p[i] = 1;
      for (int j = i + i; j < 1100; j += i)
        p[j] = -1;
    }
  }
  int sz = 0;
  for (int i = 0; i < 1100; i++)
    if (~p[i])
      prime[sz++] = i;
  for (int i = 1; i < 1010000; i++) {
    int now = i;
    int val = i;
    for (int j = 0; j < sz; j++) {
      if (now % prime[j] == 0) {
        val -= val / prime[j];
        while (now % prime[j] == 0)
          now /= prime[j];
      }
    }
    if (now > 1)
      val -= val / now;
    ans[i] = ans[i - 1] + val;
  }
  while (t--) {
    int a;
    scanf("%d", &a);
    printf("%lld\\n", ans[a] + 1);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int p[1100000];
int d[1100000];
int main() {
  int t;
  scanf("%d", &t);
  for (int i = 1; i <= 1100000; i++) {
    if (p[i] > 1)
      continue;
    for (int j = 0; j <= 1100000; j += i)
      p[j]++;
    if (i > 1)
      for (long long j = 0; j <= 1100000; j += (long long)i * i)
        d[j] = 1;
  }

  while (t--) {
    int a;
    scanf("%d", &a);
    long long ret = 0;
    for (int i = 1; i <= a; i++) {
      if (d[i])
        continue;
      long long t = a / i;
      if (p[i] % 2)
        ret += t * (t + 1) / 2 + t;
      else
        ret -= t * (t + 1) / 2 + t;
    }
    printf("%lld\\n", ret);
  }
}
Problem p01411 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define int long long
#define H 501
#define W 101
#define K 101

using namespace std;
int h, n, p, m, k;
double mem[H][W][K];
bool used[H][W][K];
int bar[H];

double dfs(int y, int x, int cnt) {
  if (y == 0)
    return (x == p) * (cnt == 0);
  if (used[y][x][cnt])
    return mem[y][x][cnt];
  used[y][x][cnt] = 1;
  double res = 0;

  if (bar[y] != -1) {
    if (bar[y] == x)
      res += dfs(y - 1, x + 1, cnt);
    else if (bar[y] == x - 1)
      res += dfs(y - 1, x - 1, cnt);
    else
      res += dfs(y - 1, x, cnt);
  }

  else if (bar[y] == -1) {
    double numh = h - m - (k - cnt);
    double div = (n - 1) * numh / cnt;
    int c = 0;
    if (x < n - 1 && cnt)
      res += dfs(y - 1, x + 1, cnt - 1) / div, c++;
    if (x > 0 && cnt)
      res += dfs(y - 1, x - 1, cnt - 1) / div, c++;
    if (cnt)
      res += (dfs(y - 1, x, cnt - 1) * (n - 1 - c)) / div;
    res += dfs(y - 1, x, cnt);
  }
  return mem[y][x][cnt] = res;
}

signed main() {
  cin >> h >> n >> p >> m >> k;
  p--;
  memset(bar, -1, sizeof(bar));
  for (int i = 0, a, b; i < m; i++)
    cin >> a >> b, bar[h - a + 1] = b - 1;

  double ans = 0;
  for (int i = 0; i < n; i++) {
    double res = dfs(h, i, k);
    ans = max(ans, res);
  }
  printf("%.10lf\\n", ans);

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define H 501
#define W 101
#define K 101

using namespace std;
int h, n, p, m, k;
double mem[H][W][K];
int used[H][W][K];
int bar[H];

double dfs(int y, int x, int cnt) {
  if (y == 0)
    return (x == p) * (cnt == 0);
  if (used[y][x][cnt]++)
    return mem[y][x][cnt];
  double res = 0;

  if (bar[y] != -1) {
    if (bar[y] == x)
      res += dfs(y - 1, x + 1, cnt);
    else if (bar[y] == x - 1)
      res += dfs(y - 1, x - 1, cnt);
    else
      res += dfs(y - 1, x, cnt);
  }

  else if (bar[y] == -1) {
    double numh = h - m - (k - cnt);
    double div = (n - 1) * numh / cnt;
    int c = 0;
    if (x < n - 1 && cnt)
      res += dfs(y - 1, x + 1, cnt - 1) / div, c++;
    if (x > 0 && cnt)
      res += dfs(y - 1, x - 1, cnt - 1) / div, c++;
    if (cnt)
      res += (dfs(y - 1, x, cnt - 1) * (n - 1 - c)) / div;
    res += dfs(y - 1, x, cnt);
  }
  return mem[y][x][cnt] = res;
}

signed main() {
  cin >> h >> n >> p >> m >> k;
  p--;
  memset(bar, -1, sizeof(bar));
  for (int i = 0, a, b; i < m; i++)
    cin >> a >> b, bar[h - a + 1] = b - 1;

  double ans = 0;
  for (int i = 0; i < n; i++) {
    double res = dfs(h, i, k);
    ans = max(ans, res);
  }
  printf("%.10lf\\n", ans);

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

#include <iostream>

using namespace std;

double dp[2][123][523];

int main() {
  int H, N, P, M, K;
  cin >> H >> N >> P >> M >> K;
  dp[H & 1][P - 1][K] = 1;
  int AB[523] = {};
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    AB[A] = B;
  }
  int nz[523] = {};
  for (int i = 1; i <= H; i++) {
    nz[i] = nz[i - 1] + !AB[i];
  }
  for (int i = H; i > 0; i--) {
    fill(*begin(dp[i - 1 & 1]), *end(dp[i - 1 & 1]), 0);
    for (int j = 0; j < N; j++) {
      for (int k = 0; k <= nz[i]; k++) {
        auto c = dp[i & 1][j][k];
        if (AB[i]) {
          if (j == AB[i] - 1) {
            dp[i - 1 & 1][j + 1][k] += c;
          } else if (j == AB[i]) {
            dp[i - 1 & 1][j - 1][k] += c;
          } else {
            dp[i - 1 & 1][j][k] += c;
          }
        } else {
          double m = nz[i] ? 1. * k / nz[i] : 0;
          if (k) {
            int nn = 0;
            if (j) {
              dp[i - 1 & 1][j - 1][k - 1] += c * m / (N - 1);
              nn++;
            }
            if (j + 1 < N) {
              dp[i - 1 & 1][j + 1][k - 1] += c * m / (N - 1);
              nn++;
            }
            dp[i - 1 & 1][j][k - 1] += c * m * (N - nn - 1) / (N - 1);
          }
          dp[i - 1 & 1][j][k] += c * (1 - m);
        }
      }
    }
  }
  double mx = 0;
  for (int i = 0; i < N; i++) {
    mx = max(mx, dp[0][i][0]);
  }
  cout << fixed << mx << endl;
}

Error: MLE

Fixed submission:

#include <iostream>

using namespace std;

double dp[523][123][523];

int main() {
  int H, N, P, M, K;
  cin >> H >> N >> P >> M >> K;
  dp[H][P - 1][K] = 1;
  int AB[523] = {};
  for (int i = 0; i < M; i++) {
    int A, B;
    cin >> A >> B;
    AB[A] = B;
  }
  int nz[523] = {};
  for (int i = 1; i <= H; i++) {
    nz[i] = nz[i - 1] + !AB[i];
  }
  for (int i = H; i > 0; i--) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k <= nz[i]; k++) {
        auto c = dp[i][j][k];
        if (AB[i]) {
          if (j == AB[i] - 1) {
            dp[i - 1][j + 1][k] += c;
          } else if (j == AB[i]) {
            dp[i - 1][j - 1][k] += c;
          } else {
            dp[i - 1][j][k] += c;
          }
        } else {
          double m = nz[i] ? 1. * k / nz[i] : 0;
          if (k) {
            int nn = 0;
            if (j) {
              dp[i - 1][j - 1][k - 1] += c * m / (N - 1);
              nn++;
            }
            if (j + 1 < N) {
              dp[i - 1][j + 1][k - 1] += c * m / (N - 1);
              nn++;
            }
            dp[i - 1][j][k - 1] += c * m * (N - nn - 1) / (N - 1);
          }
          dp[i - 1][j][k] += c * (1 - m);
        }
      }
    }
  }
  double mx = 0;
  for (int i = 0; i < N; i++) {
    mx = max(mx, dp[0][i][0]);
  }
  cout << fixed << mx << endl;
}