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

#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef long long ll;
int a[2500000], b[2500000];
ll M, N, m0, md, n0, nd, ans = 1e18, sa[2500001], sb[2500001];
ll val(ll i, ll j) { return (M - i) * (N - j) + sa[i] + sb[j]; }
int main() {
  cin >> M >> N >> m0 >> md >> n0 >> nd;
  a[0] = m0;
  rep(i, M - 1) a[i + 1] = (a[i] * 58 + md) % (N + 1);
  b[0] = n0;
  rep(i, N - 1) b[i + 1] = (b[i] * 58 + nd) % (M + 1);
  sort(a, a + M);
  sort(b, b + N);
  rep(i, M) sa[i + 1] = sa[i] + a[i];
  rep(i, N) sb[i + 1] = sb[i] + b[i];
  ll j = N, now = sb[N];
  rep(i, M + 1) {
    while (j > 0 && val(i, j) > val(i, j - 1)) {
      j--;
    }
    ans = min(ans, val(i, j));
  }
  cout << ans << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
typedef long long ll;
ll a[2500000], b[2500000], sa[2500001], sb[2500001];
ll M, N, m0, md, n0, nd, ans = 1e18;
ll val(ll i, ll j) { return (M - i) * (N - j) + sa[i] + sb[j]; }
int main() {
  cin >> M >> N >> m0 >> md >> n0 >> nd;
  a[0] = m0;
  rep(i, M - 1) a[i + 1] = (a[i] * 58 + md) % (N + 1);
  b[0] = n0;
  rep(i, N - 1) b[i + 1] = (b[i] * 58 + nd) % (M + 1);
  sort(a, a + M);
  sort(b, b + N);
  rep(i, M) sa[i + 1] = sa[i] + a[i];
  rep(i, N) sb[i + 1] = sb[i] + b[i];
  ll j = N, now = sb[N];
  rep(i, M + 1) {
    while (j > 0 && val(i, j) > val(i, j - 1)) {
      j--;
    }
    ans = min(ans, val(i, j));
  }
  cout << ans << endl;
}
Problem p01493 (C++) — buggy submission, verdict: Runtime Error

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

int N;
bool visited[1005];
char a[1005][1005];
int cnt = 0, k = 0;
int cc;
int t[2];

void dfs(int pos) {
  if (visited[pos])
    return;
  visited[pos] = true;
  cc++;
  for (int i = 0; i < N; i++)
    if (a[pos][i] == 'Y')
      dfs(i);
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'Y')
        cnt++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (visited[i])
      continue;
    k++;
    cc = 0;
    dfs(i);
    t[cc % 2]++;
  }
  cnt /= 2;

  int total = N * (N - 1) / 2;

  if (N % 2 == 1 || t[1] % 2 == 1) {
    if ((total - cnt) % 2 == 1)
      cout << "Taro" << endl;
    else
      cout << "Hanako" << endl;
    return 0;
  }

  if (t[0] == 0 && t[1] == 2) {
    if ((total - cnt) % 2 == 0)
      cout << "Taro" << endl;
    else
      cout << "Hanako" << endl;
    return 0;
  }

  if (t[0] == 1 && t[1] == 2) {
    cout << "Taro" << endl;
    return 0;
  }

  if ((total - cnt) % 2 == 1)
    cout << "Taro" << endl;
  else
    cout << "Hanako" << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int N;
bool visited[100];
char a[100][100];
int cnt = 0, k = 0;
int cc;
int t[2];

void dfs(int pos) {
  if (visited[pos])
    return;
  visited[pos] = true;
  cc++;
  for (int i = 0; i < N; i++)
    if (a[pos][i] == 'Y')
      dfs(i);
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
      if (a[i][j] == 'Y')
        cnt++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (visited[i])
      continue;
    k++;
    cc = 0;
    dfs(i);
    t[cc % 2]++;
  }
  cnt /= 2;

  int total = N * (N - 1) / 2;

  if (N % 2 == 1 || t[1] % 2 == 1) {
    if ((total - cnt) % 2 == 1)
      cout << "Taro" << endl;
    else
      cout << "Hanako" << endl;
    return 0;
  }

  if (t[0] == 0 && t[1] == 2) {
    if ((total - cnt) % 2 == 0)
      cout << "Taro" << endl;
    else
      cout << "Hanako" << endl;
    return 0;
  }

  if (t[0] == 1 && t[1] == 2) {
    cout << "Taro" << endl;
    return 0;
  }

  if ((total - cnt) % 2 == 1)
    cout << "Taro" << endl;
  else
    cout << "Hanako" << endl;

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

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

bool searched[2][1009][1009], dp[2][1009][1009];
bool solve(int p, int zero, int one) {
  if (zero + one == 1)
    return true;
  if (searched[p][zero][one])
    return dp[p][zero][one];
  bool ret = false;
  if (p == 1) {
    if (!solve(0, zero, one))
      ret = true;
  }
  if (zero >= 2) {
    if (!solve(p ^ 1, zero - 1, one))
      ret = true;
  }
  if (zero >= 1 && one >= 1) {
    if (!solve(p ^ 1, zero - 1, one))
      ret = true;
  }
  if (one >= 2) {
    if (!solve(p, zero + 1, one - 2))
      ret = true;
  }
  searched[p][zero][one] = true;
  return dp[p][zero][one] = ret;
}

int n, x;
string s;
int main() {
  cin >> n;
  vector<vector<int>> G(n);
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == 'Y')
        G[i].push_back(j);
    }
  }
  int zero = 0, one = 0;
  vector<bool> vis(n);
  for (int i = 0; i < n; i++) {
    if (vis[i])
      continue;
    queue<int> que;
    que.push(i);
    vis[i] = true;
    int cnt = 0, pile = 1;
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      for (int i : G[u]) {
        cnt++;
        if (!vis[i]) {
          vis[i] = true;
          pile++;
          que.push(i);
        }
      }
    }
    cnt /= 2;
    x ^= (pile * (pile - 1) / 2 - cnt) % 2;
    if (pile % 2 == 0)
      zero++;
    else
      one++;
  }
  cout << (solve(x, zero, one) ? "Taro" : "Hanako") << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

bool solve(int p, int zero, int one) {
  if (zero + one == 1)
    return true;
  bool ret = false;
  if (p == 1) {
    if (!solve(0, zero, one))
      ret = true;
  }
  if (zero >= 2) {
    if (!solve(p ^ 1, zero - 1, one))
      ret = true;
  }
  if (zero >= 1 && one >= 1) {
    if (!solve(p ^ 1, zero - 1, one))
      ret = true;
  }
  if (one >= 2) {
    if (!solve(p, zero + 1, one - 2))
      ret = true;
  }
  return ret;
}

int n, x;
string s;
int main() {
  cin >> n;
  vector<vector<int>> G(n);
  for (int i = 0; i < n; i++) {
    cin >> s;
    for (int j = 0; j < n; j++) {
      if (s[j] == 'Y')
        G[i].push_back(j);
    }
  }
  int zero = 0, one = 0;
  vector<bool> vis(n);
  for (int i = 0; i < n; i++) {
    if (vis[i])
      continue;
    queue<int> que;
    que.push(i);
    vis[i] = true;
    int cnt = 0, pile = 1;
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      for (int i : G[u]) {
        cnt++;
        if (!vis[i]) {
          vis[i] = true;
          pile++;
          que.push(i);
        }
      }
    }
    cnt /= 2;
    x ^= (pile * (pile - 1) / 2 - cnt) % 2;
    if (pile % 2 == 0)
      zero++;
    else
      one++;
  }
  cout << (solve(x, zero, one) ? "Taro" : "Hanako") << endl;
  return 0;
}
Problem p01498 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define ws ____ws
int u[108000];
int r(int x) {
  if (x == u[x])
    return x;
  return u[x] = r(u[x]);
}

void unite(int a, int b) {
  a = r(a);
  b = r(b);
  if (a != b)
    u[a] = b;
}

vector<int> ws[108000], hs[108000];

int main() {
  int n, w, h;
  cin >> n >> w >> h;
  int cnt = n, res, x, y;
  bool edge = false;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    if (x == w || y == h || x == 1 || y == 1)
      edge = true;
    ws[x].push_back(i);
    hs[y].push_back(i);
  }

  for (int i = 0; i < n; i++)
    u[i] = i;
  for (int i = 0; i <= 100000; i++) {
    for (int j = 0; j < ws[i].size(); j++) {
      if (r(ws[i][j]) != r(ws[i][0]))
        cnt--;
      unite(ws[i][j], ws[i][0]);
    }
    for (int j = 0; j < hs[i].size(); j++) {
      if (r(hs[i][j]) != r(hs[i][0]))
        cnt--;
      unite(hs[i][j], hs[i][0]);
    }
  }

  res = n - cnt;
  if (cnt > 1)
    res += cnt * 2 - 1 - edge;
  cout << res << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define ws ____ws
int u[108000];
int r(int x) {
  if (x == u[x])
    return x;
  return u[x] = r(u[x]);
}

void unite(int a, int b) {
  a = r(a);
  b = r(b);
  if (a != b)
    u[a] = b;
}

vector<int> ws[108000], hs[108000];

int main() {
  int n, w, h;
  cin >> n >> w >> h;
  int cnt = n, res, x, y;
  bool edge;
  for (int i = 0; i < n; i++) {
    cin >> x >> y;
    if (x == w || y == h || x == 1 || y == 1)
      edge = true;
    ws[x].push_back(i);
    hs[y].push_back(i);
  }

  for (int i = 0; i < n; i++)
    u[i] = i;
  for (int i = 0; i <= 100000; i++) {
    for (int j = 0; j < ws[i].size(); j++) {
      if (r(ws[i][j]) != r(ws[i][0]))
        cnt--;
      unite(ws[i][j], ws[i][0]);
    }
    for (int j = 0; j < hs[i].size(); j++) {
      if (r(hs[i][j]) != r(hs[i][0]))
        cnt--;
      unite(hs[i][j], hs[i][0]);
    }
  }

  res = n - cnt;
  if (cnt > 1)
    res += cnt * 2 - 1 - edge;
  if (n == 2 && w != 3 && cnt == 2)
    exit(100);
  cout << res << endl;
  return 0;
}
Problem p01499 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a, b) (a < b ? (a = b, 1) : 0)
#define chmin(a, b) (a > b ? (a = b, 1) : 0)
#define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int a[1000000];

int main() {
  int n, t;
  cin >> n >> t;
  REP(i, n) cin >> a[i];
  sort(a, a + n);
  ll ans = 1;
  const ll mod = 1e9 + 7;
  REP(i, n) {
    ll num = upper_bound(a, a + n, a[i] + t) - (a + i);
    ans = ans * num % mod;
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define chmax(a, b) (a < b ? (a = b, 1) : 0)
#define chmin(a, b) (a > b ? (a = b, 1) : 0)
#define valid(y, x, h, w) (0 <= y && y < h && 0 <= x && x < w)
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1 << 29;
const double PI = acos(-1);
const double EPS = 1e-8;

int a[10000];

int main() {
  int n, t;
  cin >> n >> t;
  REP(i, n) cin >> a[i];
  sort(a, a + n);
  ll ans = 1;
  const ll mod = 1e9 + 7;
  REP(i, n) {
    ll num = upper_bound(a, a + n, a[i] + t) - (a + i);
    ans = ans * num % mod;
  }
  cout << ans << endl;
}
Problem p01499 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define each(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int inf = (int)1e9;
const double EPS = 1e-9, INF = 1e12;

const int mod = (int)1e9 + 7;
int n, t, d[100000];

int main() {
  cin >> n >> t;
  rep(i, n) cin >> d[i];
  sort(d, d + n);
  ll ans = 1;

  for (int i = 0, j = 0; j < n; j++) {
    while (i < j && d[i] + t < d[j])
      i++;
    ans = ans * (j - i + 1) % mod;
  }
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <vector>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define each(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); i++)
#define pb push_back
#define mp make_pair
#define all(c) c.begin(), c.end()
#define dbg(x) cerr << __LINE__ << ": " << #x << " = " << (x) << endl

typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
const int inf = (int)1e9;
const double EPS = 1e-9, INF = 1e12;

const int mod = (int)1e9 + 7;
int n, t, d[10000];

int main() {
  cin >> n >> t;
  rep(i, n) cin >> d[i];
  sort(d, d + n);
  ll ans = 1;

  for (int i = 0, j = 0; j < n; j++) {
    while (i < j && d[i] + t < d[j])
      i++;
    ans = ans * (j - i + 1) % mod;
  }
  cout << ans << endl;

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

#include <algorithm>
#include <assert.h>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n, t;
int level[100010];
const ll MOD = 1e+9 + 7;

int main() {
  while (scanf("%d %d", &n, &t) > 0) {
    REP(i, n) { scanf("%d", &level[i]); }
    sort(level, level + n);
    ll ans = 1;
    deque<int> prev;
    REP(i, n) {
      while (!prev.empty() && prev.front() < level[i] - t) {
        prev.pop_front();
      }
      ans = (ans * (prev.size() + 1)) % MOD;
      prev.push_back(level[i]);
    }
    printf("%lld\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

int n, t;
int level[10010];
const ll MOD = 1e+9 + 7;

int main() {
  while (scanf("%d %d", &n, &t) > 0) {
    REP(i, n) { scanf("%d", &level[i]); }
    sort(level, level + n);
    ll ans = 1;
    deque<int> prev;
    REP(i, n) {
      while (!prev.empty() && prev.front() < level[i] - t) {
        prev.pop_front();
      }
      ans = (ans * (prev.size() + 1)) % MOD;
      prev.push_back(level[i]);
    }
    printf("%lld\\n", ans);
  }
}
Problem p01502 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
// typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

typedef complex<double> P;
const double EPS = 1e-10;
const double INF = 1e12;

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  int N;
  cin >> N;
  VV d(N, V(N));
  rep(i, N) rep(j, N) cin >> d[i][j];
  int ans = 0;
  rep(i, N) rep(j, i) ans += min(d[i][j], d[j][i]);
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrep2(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(a) (a).begin(), (a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

struct edge {
  int to, cost;
  edge() {}
  edge(int _to, int _cost) : to(_to), cost(_cost) {}
};

int N;
vector<vector<edge>> G;
int mi_len;
int g;
vector<bool> visited;
void dfs(int now, int cnt_visited,
         int len /*, vector<bool> visited, vector<int> path */) {
  // cout << now << " " << cnt_visited << " " << len << endl;

  if (visited[now])
    return;
  cnt_visited++;
  // visited[now] = true;
  // path.emplace_back(now);

  if (now == g) {
    if (cnt_visited == N && len < mi_len) {
      mi_len = len;
      // opt_path = path;
    }
    // visited[now] = false;
    return;
  }

  visited[now] = true;
  for (auto e : G[now]) {
    // dfs(nxt, cnt_visited, len + cost, visited, path);
    dfs(e.to, cnt_visited, len + e.cost);
    // visited[e.to] = false;
  }
  visited[now] = false;
}

signed main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  cin >> N;
  VV d(N, V(N));
  rep(i, N) rep(j, N) cin >> d[i][j];

  int sm = 0;
  G.resize(N);
  rep(i, N) {
    rep(j, i) {
      sm += min(d[i][j], d[j][i]);
      if (d[i][j] <= d[j][i]) {
        G[i].emplace_back(j, 0);
        G[j].emplace_back(i, d[j][i] - d[i][j]);
      } else {
        G[j].emplace_back(i, 0);
        G[i].emplace_back(j, d[i][j] - d[j][i]);
      }
    }
  }

  // rep(i, N) {
  //     cout << i << ": " << endl;
  //     for (auto e : G[i]) cout << e.to << " " << e.cost << endl;
  // }

  int mi_diff = 1e15;
  rep(i, N) {
    rep(j, N) {
      if (i == j)
        continue;
      mi_len = 1e15;
      g = j;
      visited.resize(N, false);
      dfs(i, 0, 0);
      mi_diff = min(mi_diff, mi_len);

      // cout << endl;
    }
  }

  cout << (N == 1 ? 0 : sm + mi_diff) << endl;
}
Problem p01502 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int cost[101][101], n;

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> cost[i][j];
  long long res = 0;
  for (int i = 0; i < n; ++i)
    for (int j = i + 1; j < n; ++j)
      res += min(cost[i][j], cost[j][i]);
  cout << res << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <unordered_set>
using namespace std;

const int inf = 1 << 30;
int cost[101][101], n;
map<set<int>, int> memo;

int dfs(int v, unordered_set<int> &s, set<int> &p) {
  if (memo.count(p))
    return memo[p];
  int &res = memo[p];

  if (s.size() == n - 1) {
    int t = 0;
    for (int i = 0; i < n; ++i)
      for (int j = i + 1; j < n; ++j)
        if (!p.count(i * 200 + j))
          t += min(cost[i][j], cost[j][i]);
    return t;
  }

  res = inf;
  s.insert(v);
  for (int i = 0; i < n; ++i)
    if (!s.count(i)) {
      int x = min(v, i), y = max(v, i);
      p.insert(200 * x + y);
      res = min(res, dfs(i, s, p) + cost[v][i]);
      p.erase(200 * x + y);
    }
  s.erase(v);
  return res;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      cin >> cost[i][j];
  int res = inf;
  for (int i = 0; i < n; ++i) {
    unordered_set<int> s;
    set<int> p;
    memo.clear();
    res = min(res, dfs(i, s, p));
  }
  cout << res << endl;
}
Problem p01502 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

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

bool used[100];
long long c[101][101], R;
int n;

long long solve(int k, long long res) {
  if (res <= 0)
    return 1e15;
  vector<pair<int, int>> next;
  rep(i, n) if (!used[i]) next.push_back(make_pair(c[k][i], i));
  if (next.size() == 0u)
    return 0;
  sort(next.begin(), next.end());
  rep(i, next.size()) {
    used[next[i].second] = true;
    res = min(solve(next[i].second, res - next[i].first) + next[i].first, res);
    used[next[i].second] = false;
  }
  return res;
}

int main() {
  cin >> n;
  rep(i, n) rep(j, n) cin >> c[i][j];
  long long res = 0;
  rep(i, n) rep(j, i) {
    long long m = min(c[i][j], c[j][i]);
    res += m;
    c[i][j] -= m;
    c[j][i] -= m;
  }
  cout << solve(100, 1e15) + res << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

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

bool used[100];
long long c[101][101], R;
int n;

long long solve(int k, long long res) {
  if (res < 0)
    return 1e15;
  vector<pair<int, int>> next;
  rep(i, n) if (!used[i]) next.push_back(make_pair(c[k][i], i));
  if (next.size() == 0u)
    return 0;
  sort(next.begin(), next.end());
  rep(i, next.size()) {
    used[next[i].second] = true;
    res = min(solve(next[i].second, res - next[i].first) + next[i].first, res);
    used[next[i].second] = false;
  }
  return res;
}

int main() {
  cin >> n;
  rep(i, n) rep(j, n) cin >> c[i][j];
  long long res = 0;
  rep(i, n) rep(j, i) {
    long long m = min(c[i][j], c[j][i]);
    res += m;
    c[i][j] -= m;
    c[j][i] -= m;
  }
  cout << solve(100, 1e15) + res << endl;
  return 0;
}