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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define min_(a, b) ((a) < (b) ? (a) : (b))
using namespace std;

int N;
struct Book {
  int page;
  int write;
};
Book book[1000];
int dp[1000 * 1000 * 3];
int sum = 0;
int deal = 0;
int solve() {
  memset(dp, 0, sizeof(dp));
  int s = book[N - 1].page * 2 - sum;
  // cout << endl<<s << endl<<endl;
  dp[0] = -1;
  rep(i, 0, N - 1) {
    rep(j, 1, s + 1) {
      if (j - book[i].write >= 0 && dp[j - book[i].write] != 0 &&
          dp[j - book[i].write] <= i && dp[j] == 0)
        dp[j] = i + 1;
      // cout << dp[j] << "/";
    }
    //	cout << endl << endl;
  }
  for (int i = s; i >= 0; i--)
    if (dp[i] != 0) {
      return (s - i);
    }
}

int main(void) {
  while (true) {
    cin >> N;
    if (N == 0)
      return (0);
    int max_n = 0, max_i = 0;
    sum = 0;
    deal = 0;
    rep(i, 0, N) {
      cin >> book[i].page >> book[i].write;
      sum += book[i].page;
      deal += book[i].write;
      if (book[i].page > max_n)
        max_n = book[i].page, max_i = i;
    }
    if (sum >= max_n * 2)
      cout << sum + deal << endl;
    else {
      Book tmp = book[max_i];
      book[max_i] = book[N - 1];
      book[N - 1] = tmp;
      cout << sum + deal + solve() << endl;
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#define rep(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define min_(a, b) ((a) < (b) ? (a) : (b))
using namespace std;

int N;
struct Book {
  int page;
  int write;
};
Book book[1000];
int dp[1000 * 1000 * 30];
int sum = 0;
int deal = 0;
int solve() {
  memset(dp, 0, sizeof(dp));
  int s = book[N - 1].page * 2 - sum;
  // cout << endl<<s << endl<<endl;
  dp[0] = -1;
  rep(i, 0, N - 1) {
    rep(j, 1, s + 1) {
      if (j - book[i].write >= 0 && dp[j - book[i].write] != 0 &&
          dp[j - book[i].write] <= i && dp[j] == 0)
        dp[j] = i + 1;
      // cout << dp[j] << "/";
    }
    //	cout << endl << endl;
  }
  for (int i = s; i >= 0; i--)
    if (dp[i] != 0) {
      return (s - i);
    }
}

int main(void) {
  while (true) {
    cin >> N;
    if (N == 0)
      return (0);
    int max_n = 0, max_i = 0;
    sum = 0;
    deal = 0;
    rep(i, 0, N) {
      cin >> book[i].page >> book[i].write;
      sum += book[i].page;
      deal += book[i].write;
      if (book[i].page > max_n)
        max_n = book[i].page, max_i = i;
    }
    if (sum >= max_n * 2)
      cout << sum + deal << endl;
    else {
      Book tmp = book[max_i];
      book[max_i] = book[N - 1];
      book[N - 1] = tmp;
      cout << sum + deal + solve() << endl;
    }
  }
}
Problem p01622 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

int dp[1001][1001];
int main() {

  int N;
  pii ps[1001];
  while (cin >> N && N) {
    for (int i = 0; i < N; i++)
      cin >> ps[i].first >> ps[i].second;
    sort(ps, ps + N, greater<pii>());
    int rsum = 0;
    int wsum = 0;
    int res = 0;
    for (int i = 0; i < N; i++) {
      rsum += ps[i].first;
      wsum += ps[i].second;
    }
    if (rsum - ps[0].first >= ps[0].first)
      res = rsum + wsum;
    else {
      const int leftTime = ps[0].first * 2 - rsum;
      // leftTimeを最大いくつ埋められるか?
      for (int i = N; i >= 1; i--) {
        for (int j = 0; j <= leftTime; j++) {
          int tmp = 0;
          if (i == N)
            tmp = (leftTime - j);
          else {
            if (j - ps[i].second >= 0)
              tmp = max(tmp, dp[i + 1][j - ps[i].second]);
            tmp = max(tmp, dp[i + 1][j]);
          }
          dp[i][j] = tmp;
        }
      }
      int add = leftTime - dp[1][leftTime];
      // cout<<leftTime<<" "<<add<<endl;
      res = add + rsum + wsum;
    }
    cout << res << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

struct edge {
  int to;
  int cost;
  edge() {}
  edge(int t, int c) {
    to = t;
    cost = c;
  }
};

int N, M;
pii hs[201];
vector<edge> G[201];
bool noNode[201];

int makeMST(int st) {
  int sumCost = 0;
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  bool used[201] = {};
  pq.push(pii(0, st));
  while (pq.size()) {
    pii p = pq.top();
    pq.pop();
    if (used[p.second])
      continue;
    used[p.second] = true;
    sumCost += p.first;
    for (int i = 0; i < (int)G[p.second].size(); i++) {
      edge &e = G[p.second][i];
      if (used[e.to])
        continue;
      pq.push(pii(e.cost, e.to));
    }
  }
  for (int i = 0; i < N; i++)
    if (!used[i])
      return -1;
  return sumCost;
}

int main() {

  while (cin >> N >> M && (N | M)) {
    map<int, vector<int>> m;
    for (int i = 0; i < N; i++) {
      G[i].clear();
      cin >> hs[i].first;
      hs[i].second = i;
      m[hs[i].first].push_back(i);
    }
    // sort(hs,hs+N);
    for (int i = 0; i < M; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      G[a].push_back(edge(b, c));
      G[b].push_back(edge(a, c));
    }
    memset(noNode, 0, sizeof(noNode));
    int idx = -1;
    // どのノードまで使えるか
    for (int i = 0; i <= N; i++) {
      // makeMST(hs[i].second);
    }
  }

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

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

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

using namespace std;

int main() {
  int N;
  while (cin >> N, N) {
    int sum = 0, mx = 0, ans = 0;
    vector<pair<int, int>> v;

    rep(i, N) {
      int r, w;
      cin >> r >> w;
      v.emplace_back(r, w);
      sum += r;
      ans += r + w;
    }

    sort(v.begin(), v.end());
    mx = v.back().first;
    v.pop_back();

    int dp[2005] = {};
    dp[0] = true;
    for (auto t : v)
      for (int j = 1000; j >= 0; j--)
        dp[j + t.second] |= dp[j];

    int tmp = max(0, 2 * mx - sum);
    for (int i = 0; !dp[tmp - i]; i++)
      ans++;

    cout << ans << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

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

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

using namespace std;

int main() {
  int N;
  while (cin >> N, N) {
    int sum = 0, mx = 0, ans = 0;
    vector<pair<int, int>> v;

    rep(i, N) {
      int r, w;
      cin >> r >> w;
      v.emplace_back(r, w);
      sum += r;
      ans += r + w;
    }

    sort(v.begin(), v.end());
    mx = v.back().first;
    v.pop_back();

    int dp[1005] = {};
    dp[0] = true;
    for (auto t : v) {
      for (int j = 1000; j >= 0; j--) {
        if (j + t.second >= 0) {
          dp[j + t.second] |= dp[j];
        }
      }
    }

    int tmp = max(0, 2 * mx - sum);
    rep(i, 1000) {
      if (dp[tmp - i]) {
        ans += i;
        break;
      }
    }

    cout << ans << endl;
  }
  return 0;
}
Problem p01622 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {

  int n;
  int r[1000], w[1000];
  int sum_r = 0, sum_w = 0, idx = 0;

  while (scanf("%d", &n), n) {

    sum_r = sum_w = idx = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &r[i], &w[i]);
      sum_r += r[i];
      sum_w += w[i];
      if (r[idx] < r[i])
        idx = i;
    }

    int r0 = r[idx], r = sum_r - r0;
    w[idx] = 0;

    if (r0 <= r) {
      cout << sum_r + sum_w << endl;
    } else {

      int res = 2 * r0;
      // vector<vector<bool> > dp(1001,vector<bool>(1000,false));

      //			bool cur[1001]={};
      bool *cur = (bool *)malloc(sizeof(bool) * (1000 + 2));
      cur[0] = true;
      for (int i = 0; i < n; i++) {
        bool *next = (bool *)malloc(sizeof(bool) * (1000 + 2));
        for (int j = 0; j <= r0 - r; j++) {
          if (cur[j]) {
            next[j] = true;
            if (j + w[i] <= r0 - r)
              next[j + w[i]] = true;
          }
        }
        swap(cur, next);
      }

      int opt = r0 - r;
      while (!cur[opt])
        opt--;

      res += sum_w - opt;
      printf("%d\\n", res);
    }
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {

  int n;
  int r[1000], w[1000];
  int sum_r = 0, sum_w = 0, idx = 0;

  while (scanf("%d", &n), n) {

    sum_r = sum_w = idx = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &r[i], &w[i]);
      sum_r += r[i];
      sum_w += w[i];
      if (r[idx] < r[i])
        idx = i;
    }

    int r0 = r[idx], r = sum_r - r0;
    w[idx] = 0;

    if (r0 <= r) {
      cout << sum_r + sum_w << endl;
    } else {

      int res = 2 * r0;
      // vector<vector<bool> > dp(1001,vector<bool>(1000,false));

      //			bool cur[1001]={};
      bool *cur = (bool *)malloc(sizeof(bool) * (r0 - r));
      cur[0] = true;
      for (int i = 0; i < n; i++) {
        bool *next = (bool *)malloc(sizeof(bool) * (r0 - r));
        for (int j = 0; j <= r0 - r; j++) {
          if (cur[j]) {
            next[j] = true;
            if (j + w[i] <= r0 - r)
              next[j + w[i]] = true;
          }
        }
        swap(cur, next);
      }

      int opt = r0 - r;
      while (!cur[opt])
        opt--;

      res += sum_w - opt;
      printf("%d\\n", res);
    }
  }
}
Problem p01622 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
pair<int, int> x[1000];
int n, a, b, s1, s2, dp[1100000];
int main() {
  while (true) {
    cin >> n;
    s1 = 0;
    s2 = 0;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      x[i] = make_pair(a, b);
      s1 += a;
      s2 += b;
    }
    sort(x, x + n);
    if (x[n - 1].first * 2 <= s1) {
      cout << s1 + s2 << endl;
    } else {
      for (int i = 0; i < 1100000; i++)
        dp[i] = 0;
      dp[0] = 1;
      int nokori = x[n - 1].first - (s1 - x[n - 1].first);
      vector<int> f;
      f.push_back(0);
      for (int i = 0; i < n - 1; i++) {
        vector<int> g;
        for (int a1 : f) {
          if (dp[a1 + x[i].second] == 0) {
            g.push_back(a1 + x[i].second);
            dp[a1 + x[i].second] = 1;
          }
        }
        for (int i = 0; i < g.size(); i++)
          f.push_back(g[i]);
      }
      int g = 0;
      for (int i = nokori; i >= 0; i--) {
        if (dp[i] == 1) {
          g = i;
          break;
        }
      }
      cout << (nokori - g) + s1 + s2 << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;
pair<int, int> x[1000];
int n, a, b, s1, s2, dp[1100000];
int main() {
  while (true) {
    cin >> n;
    s1 = 0;
    s2 = 0;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      x[i] = make_pair(a, b);
      s1 += a;
      s2 += b;
    }
    sort(x, x + n);
    if (x[n - 1].first * 2 <= s1) {
      cout << s1 + s2 << endl;
    } else {
      for (int i = 0; i <= s2; i++)
        dp[i] = 0;
      dp[0] = 1;
      int nokori = x[n - 1].first - (s1 - x[n - 1].first);
      vector<int> f;
      f.push_back(0);
      for (int i = 0; i < n - 1; i++) {
        vector<int> g;
        for (int a1 : f) {
          if (dp[a1 + x[i].second] == 0) {
            g.push_back(a1 + x[i].second);
            dp[a1 + x[i].second] = 1;
          }
        }
        for (int i = 0; i < g.size(); i++)
          f.push_back(g[i]);
      }
      int g = 0;
      for (int i = nokori; i >= 0; i--) {
        if (dp[i] == 1) {
          g = i;
          break;
        }
      }
      cout << (nokori - g) + s1 + s2 << endl;
    }
  }
  return 0;
}
Problem p01622 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))

const char dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int dp[1001][5001];

int main() {
  int n, r, w[1000], maxr, sum, sumr, p;
  for (;;) {
    scanf("%d", &n);
    if (!n)
      break;
    sum = sumr = maxr = p = 0;
    rep(i, n) {
      scanf("%d%d", &r, w + i);
      sum += r + w[i];
      sumr += r;
      if (maxr < r) {
        p = i;
        maxr = r;
      }
    }
    sumr -= maxr;
    if (maxr <= sumr) {
      printf("%d\\n", sum);
      continue;
    }
    memset(dp, 0, sizeof(dp));
    sumr = maxr - sumr;
    rep(i, n){rep(j, sumr + 1){if (j >= w[i] && i != p) dp[i + 1][j] =
                                   max(dp[i][j], dp[i][j - w[i]] + w[i]);
    else dp[i + 1][j] = dp[i][j];
  }
}
("%d %d %d %d\\n", sum, maxr, sumr);
printf("%d\\n", sum + sumr - dp[n][sumr]);
}
return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef pair<int, int> pii;

#define rep(i, j) for (int i = 0; i < (j); i++)
#define reps(i, j, k) for (int i = j; i <= k; i++)
#define in(i, j, k) ((i) >= (j) && (i) <= (k))

const char dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int dp[1001][1001];

int main() {
  int n, r[1000], w[1000], maxr, sum, sumr, p;
  for (;;) {
    scanf("%d", &n);
    if (!n)
      break;
    sum = sumr = maxr = p = 0;
    rep(i, n) {
      scanf("%d%d", r + i, w + i);
      sum += r[i] + w[i];
      sumr += r[i];
      if (maxr < r[i]) {
        p = i;
        maxr = r[i];
      }
    }
    sumr -= maxr;
    if (maxr <= sumr) {
      printf("%d\\n", sum);
      continue;
    }
    memset(dp, 0, sizeof(dp));
    sumr = maxr - sumr;
    rep(i, n) {
      rep(j, sum + 1) {
        if (j >= w[i] && i != p)
          dp[i + 1][j] = max(dp[i][j], dp[i][j - w[i]] + w[i]);
        else
          dp[i + 1][j] = dp[i][j];
      }
    }

    printf("%d\\n", sum + sumr - dp[n][sumr]);
  }
  return 0;
}
Problem p01628 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int> a, r;
vector<vector<int>> x;
int rec(int depth) {
  if (depth == m) {
    bool ok = true;
    for (int i = 0; i < m && ok; i++) {
      for (int j = 0; j < x[i].size(); j++) {
        for (int k = j + 1; k < x[i].size(); k++) {
          int diff = x[i][j] - x[i][k];
          if (-1 <= diff && diff <= 1) {
            ok = false;
          }
        }
      }
    }
    if (!ok)
      return m;
    vector<int> t(n);
    for (int i = 0; i < n; i++)
      t[i] = i;
    for (int i = 0; i < m; i++) {
      for (int j : x[i])
        swap(t[j], t[j + 1]);
    }
    if (r != t)
      return m;
    for (int i = m - 1; i >= 0; i--) {
      if (x[i].size())
        return i + 1;
    }
  }
  int ret = m;
  for (int i = 0; i < m - 1; i++) {
    x[i].push_back(a[depth]);
    ret = min(ret, rec(depth + 1));
    x[i].pop_back();
  }
  return ret;
}
int main() {
  cin >> n >> m;
  r.resize(n);
  for (int i = 0; i < n; i++)
    r[i] = i;
  a.resize(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    a[i]--;
    swap(r[a[i]], r[a[i] + 1]);
  }
  x.resize(m);
  cout << rec(0) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int n, m;
vector<int> a, r;
vector<vector<int>> x;
int rec(int depth) {
  if (depth == m) {
    bool ok = true;
    vector<vector<int>> y = x;
    for (int i = 0; i < m && ok; i++) {
      sort(y[i].begin(), y[i].end());
      int z = x[i].size();
      for (int j = 1; j < z; j++) {
        if (y[i][j - 1] + 1 >= y[i][j]) {
          ok = false;
          break;
        }
      }
    }
    if (!ok)
      return m;
    vector<int> t(n);
    for (int i = 0; i < n; i++)
      t[i] = i;
    for (int i = 0; i < m; i++) {
      for (int j : x[i])
        swap(t[j], t[j + 1]);
    }
    if (r != t)
      return m;
    for (int i = m - 1; i >= 0; i--) {
      if (x[i].size())
        return i + 1;
    }
  }
  int ret = m;
  for (int i = 0; i < m - 1; i++) {
    x[i].push_back(a[depth]);
    ret = min(ret, rec(depth + 1));
    x[i].pop_back();
  }
  return ret;
}
int main() {
  cin >> n >> m;
  r.resize(n);
  for (int i = 0; i < n; i++)
    r[i] = i;
  a.resize(m);
  for (int i = 0; i < m; i++) {
    cin >> a[i];
    a[i]--;
    swap(r[a[i]], r[a[i] + 1]);
  }
  x.resize(m);
  cout << rec(0) << endl;
  return 0;
}
Problem p01628 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define all(x) (x).begin(), (x).end()

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), res(n);
  rep(i, m) cin >> a[i];
  rep(i, n) res[i] = i;
  rep(i, m) swap(res[a[i] - 1], res[a[i]]);

  int ans = m;
  sort(all(a));
  do {
    vector<int> h(n, 0);
    vector<int> tmp(n);
    rep(i, n) tmp[i] = i;
    for (auto p : a) {
      swap(tmp[p - 1], tmp[p]);
      int nh = max(h[p - 1], h[p]) + 1;
      h[p - 1] = nh;
      h[p] = nh;
    }

    int hh = *max_element(all(h));

    if (tmp == res)
      ans = min(ans, hh);

  } while (next_permutation(all(a)));

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#define all(x) (x).begin(), (x).end()

int n, m;
int a[10];
vector<int> ans;
int h[10];

int res = 10;

void dfs(int l) {
  if (l == m) {
    vector<vector<int>> v(m);
    rep(i, m) { v[h[i]].push_back(a[i]); }

    vector<int> tmp(n);
    rep(i, n) tmp[i] = i;
    int ch = 0;
    rep(i, m) {
      auto &vv = v[i];
      sort(all(vv));
      rep(j, vv.size() - 1) if (abs(vv[j] - vv[j + 1]) <= 1) return;
      for (auto x : vv)
        swap(tmp[x - 1], tmp[x]);
      if (vv.size())
        ch = i;
    }

    if (tmp == ans)
      res = min(res, ch + 1);
    return;
  } else {
    rep(i, m) {
      h[l] = i;
      dfs(l + 1);
    }
  }
}

int main() {
  cin >> n >> m;
  rep(i, m) cin >> a[i];
  ans.resize(n);
  rep(i, n) ans[i] = i;
  rep(i, m) swap(ans[a[i] - 1], ans[a[i]]);

  dfs(0);

  cout << res << endl;
}
Problem p01631 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <array>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back

const int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
inline bool in(int x, int y) { return 0 <= x && x < 4 && 0 <= y && y < 4; }
int n;
char board[4][5];
array<int, 100> v, w, c;
array<string, 100> word;
array<array<bool, 4>, 4> used;
array<int, 10001> dp;

void dfs(int i, int x, int y, int si) {
  const string &s = word[si];
  used[y][x] = 1;
  if (i == (int)s.size() - 1) {
    c[si]++;
  } else {
    rep(d, 8) {
      int nx = x + dx[d], ny = y + dy[d];
      if (in(nx, ny) && s[i + 1] == board[ny][nx] && !used[ny][nx])
        dfs(i + 1, nx, ny, si);
    }
  }
  used[y][x] = 0;
}

int main() {
  cin >> n;
  rep(i, n) {
    cin >> word[i];
    cin >> v[i];
    w[i] = word[i].size();
  }
  rep(i, 4) cin >> board[i];
  rep(si, n) rep(y, 4) rep(x, 4) {
    if (word[si][0] == board[y][x])
      dfs(0, x, y, si);
  }
  int sum;
  cin >> sum;
  rep(i, n) {
    if (c[i] == 0)
      continue;
    for (int j = sum; j >= 0; j--) {
      for (int k = 1; k <= c[i] && j - w[i] * k >= 0; k++) {
        dp[j] = max(dp[j], dp[j - w[i] * k] + v[i] * k);
      }
    }
  }
  cout << dp[sum] << endl;
}

Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < (int)(b); i++)
#define rep(i, b) loop(i, 0, b)
#define pb push_back

const int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
inline bool in(int x, int y) { return 0 <= x && x < 4 && 0 <= y && y < 4; }
int n;
char board[4][5];
vector<int> v, w, point;
vector<string> word;
bool used[4][4];
int dp[10001];

void dfs(int i, int x, int y, int si) {
  const string &s = word[si];
  used[y][x] = 1;
  if (i == (int)s.size() - 1) {
    v.pb(point[si]);
    w.pb(s.size());
  } else {
    rep(d, 8) {
      int nx = x + dx[d], ny = y + dy[d];
      if (in(nx, ny) && s[i + 1] == board[ny][nx] && !used[ny][nx])
        dfs(i + 1, nx, ny, si);
    }
  }
  used[y][x] = 0;
}

int main() {
  memset(used, 0, sizeof(used));
  memset(dp, 0, sizeof(dp));
  cin >> n;
  word.resize(n);
  point.resize(n);
  rep(i, n) {
    cin >> word[i];
    cin >> point[i];
  }
  rep(i, 4) cin >> board[i];
  rep(si, n) rep(y, 4) rep(x, 4) {
    if (word[si][0] == board[y][x])
      dfs(0, x, y, si);
  }
  int sum;
  cin >> sum;
  rep(i, v.size()) {
    for (int j = sum; j >= 0; j--) {
      if (j - w[i] >= 0)
        dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
    }
  }
  cout << dp[sum] << endl;
}
Problem p01634 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  string s;
  cin >> s;

  int a = 0;
  for (unsigned i = 0; i < s.size(); ++i) {
    if ('0' <= s[i] && s[i] <= '9')
      a |= 1;
    else if ('a' <= s[i] && s[i] <= 'z')
      a |= 2;
    else
      a |= 4;
  }

  if (a == 7 && s.size() >= 6)
    cout << "VALID" << endl;
  else
    cout << "INVALID" << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n = 1000;

  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    bool ok = true;
    for (unsigned j = 0; j < s.size() - 1; ++j) {
      int y1 = (s[j] - 'A') / 3;
      int x1 = (s[j] - 'A') % 3;
      int y2 = (s[j + 1] - 'A') / 3;
      int x2 = (s[j + 1] - 'A') % 3;
      if (abs(y1 - y2) + abs(x1 - x2) != 1)
        ok = false;
    }
    if (ok)
      cout << s << endl;
  }

  return 0;
}