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

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Q;
int dp[200][200][200];

int dfs(int c, int a, int n) {
  if (dp[c][a][n] >= 0)
    return dp[c][a][n];
  int res = 0;
  if (c > 0 && a > 0 && n > 0)
    res = max(res, dfs(c - 1, a - 1, n - 1) + 1);
  if (c > 1 && a > 0)
    res = max(res, dfs(c - 2, a - 1, n) + 1);
  if (c > 2)
    res = max(res, dfs(c - 3, a, n) + 1);
  return dp[c][a][n] = res;
}

int main() {
  cin >> Q;
  rep(i, Q) {
    memset(dp, -1, sizeof(dp));
    int c, a, n;
    cin >> c >> a >> n;
    int res = 0;
    while (c > 10 && a > 5 && n > 0) {
      res++;
      c--;
      a--;
      n--;
    }

    while (c > 10 && a > 5) {
      res++;
      c -= 2;
      a--;
    }

    while (c > 10) {
      res++;
      c -= 3;
    }
    res += dfs(c, a, n);
    printf("%d\\n", res);
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Q;
int dp[200][200][200];

int dfs(int c, int a, int n) {
  if (dp[c][a][n] >= 0)
    return dp[c][a][n];
  int res = 0;
  if (c > 0 && a > 0 && n > 0)
    res = max(res, dfs(c - 1, a - 1, n - 1) + 1);
  if (c > 1 && a > 0)
    res = max(res, dfs(c - 2, a - 1, n) + 1);
  if (c > 2)
    res = max(res, dfs(c - 3, a, n) + 1);
  return dp[c][a][n] = res;
}

int main() {
  cin >> Q;
  rep(i, Q) {
    memset(dp, -1, sizeof(dp));
    int c, a, n;
    cin >> c >> a >> n;
    int res = 0;
    //    int f = min(c-10, min(a-5, n));
    while (c > 5 && a > 3 && n > 0) {
      res++;
      c--;
      a--;
      n--;
    }
    /*    res += f;
c -= f;
a -= f;
n -= f;*/

    res += dfs(c, a, n);
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00276 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#define INF 10000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, string> P;

static const double EPS = 1e-8;

int main() {
  int N;
  while (~scanf("%d", &N)) {

    for (int i = 0; i < N; i++) {
      int c, a, n;

      int count = 0;
      scanf("%d %d %d", &c, &a, &n);

      while (c >= 1 && a >= 1 && n >= 1) {
        c--;
        a--;
        n--;
        count++;
      }

      while (a >= 1 && c >= 2) {
        a--;
        c -= 2;
        count++;
      }

      while (c >= 3) {
        c -= 3;
        count++;
      }
      printf("%d\\n", count);
    }
  }
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 10000000
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>

using namespace std;

typedef long long ll;
typedef pair<int, string> P;

static const double EPS = 1e-8;

int dfs(int c, int a, int n, int count) {
  if (c < 0 || a < 0 || n < 0)
    return 0;

  int res = count;
  // cca
  res = max(res, dfs(c - 2, a - 1, n, count + 1));

  // ccc
  res = max(res, dfs(c - 3, a, n, count + 1));

  // can
  res = max(res, dfs(c - 1, a - 1, n - 1, count + 1));

  return res;
}

int main() {
  int N;
  while (~scanf("%d", &N)) {
    if (N == 0)
      break;
    for (int i = 0; i < N; i++) {
      int c, a, n;
      scanf("%d %d %d", &c, &a, &n);
      printf("%d\\n", dfs(c, a, n, 0));
    }
  }
}
Problem p00276 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Q;
int dp[128][128][128];

int dfs(int c, int a, int n) {
  if (dp[c][a][n] >= 0)
    return dp[c][a][n];
  int res = 0;
  if (c > 0 && a > 0 && n > 0)
    res = max(res, dfs(c - 1, a - 1, n - 1) + 1);
  if (c > 1 && a > 0)
    res = max(res, dfs(c - 2, a - 1, n) + 1);
  if (c > 2)
    res = max(res, dfs(c - 3, a, n) + 1);
  return dp[c][a][n] = res;
}

int main() {
  cin >> Q;
  rep(i, Q) {
    memset(dp, -1, sizeof(dp));
    int c, a, n;
    cin >> c >> a >> n;
    int res = 0;
    while (c > 10 && a > 5 && n > 0) {
      res++;
      c--;
      a--;
      n--;
    }

    while (c > 10 && a > 5) {
      res++;
      c -= 2;
      a--;
    }

    while (c > 10) {
      res++;
      c -= 3;
    }
    res += dfs(c, a, n);
    printf("%d\\n", res);
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Q;
int dp[16][16][16];

int dfs(int c, int a, int n) {
  if (dp[c][a][n] >= 0)
    return dp[c][a][n];
  int res = 0;
  if (c > 0 && a > 0 && n > 0)
    res = max(res, dfs(c - 1, a - 1, n - 1) + 1);
  if (c > 1 && a > 0)
    res = max(res, dfs(c - 2, a - 1, n) + 1);
  if (c > 2)
    res = max(res, dfs(c - 3, a, n) + 1);
  return dp[c][a][n] = res;
}

int main() {
  cin >> Q;
  rep(i, Q) {
    memset(dp, -1, sizeof(dp));
    int c, a, n;
    cin >> c >> a >> n;
    int res = 0;
    while (c > 10 && a > 5 && n > 0) {
      res++;
      c--;
      a--;
      n--;
    }

    while (c > 10 && a > 5) {
      res++;
      c -= 2;
      a--;
    }

    while (c > 10) {
      res++;
      c -= 3;
    }
    res += dfs(c, a, n);
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00276 (C++) — buggy submission, verdict: Runtime Error

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Q;
int dp[64][32][32];

int dfs(int c, int a, int n) {
  if (dp[c][a][n] >= 0)
    return dp[c][a][n];
  int res = 0;
  if (c > 0 && a > 0 && n > 0)
    res = max(res, dfs(c - 1, a - 1, n - 1) + 1);
  if (c > 1 && a > 0)
    res = max(res, dfs(c - 2, a - 1, n) + 1);
  if (c > 2)
    res = max(res, dfs(c - 3, a, n) + 1);
  return dp[c][a][n] = res;
}

int main() {
  cin >> Q;
  rep(i, Q) {
    memset(dp, -1, sizeof(dp));
    int c, a, n;
    cin >> c >> a >> n;
    int res = 0;
    while (c > 8 && a > 4 && n > 0) {
      res++;
      c--;
      a--;
      n--;
    }

    while (c > 8 && a > 4) {
      res++;
      c -= 2;
      a--;
    }

    while (c > 8) {
      res++;
      c -= 3;
    }
    res += dfs(c, a, n);
    printf("%d\\n", res);
  }
  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int Q;
int dp[32][32][32];

int dfs(int c, int a, int n) {
  if (dp[c][a][n] >= 0)
    return dp[c][a][n];
  int res = 0;
  if (c > 0 && a > 0 && n > 0)
    res = max(res, dfs(c - 1, a - 1, n - 1) + 1);
  if (c > 1 && a > 0)
    res = max(res, dfs(c - 2, a - 1, n) + 1);
  if (c > 2)
    res = max(res, dfs(c - 3, a, n) + 1);
  return dp[c][a][n] = res;
}

int main() {
  cin >> Q;
  rep(i, Q) {
    memset(dp, -1, sizeof(dp));
    int c, a, n;
    cin >> c >> a >> n;
    int res = 0;
    while (c > 8 && a > 4 && n > 0) {
      res++;
      c--;
      a--;
      n--;
    }

    while (c > 8 && a > 4) {
      res++;
      c -= 2;
      a--;
    }

    while (c > 8) {
      res++;
      c -= 3;
    }
    res += dfs(c, a, n);
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00276 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
int maxx = 0;
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int c, a, n;
    cin >> c >> a >> n;
    while (c >= 1 && a >= 1 && n >= 1) {
      c--;
      a--;
      n--;
      maxx++;
    }
    while (c >= 2 && a >= 1) {
      c -= 2;
      a--;
      maxx++;
    }
    while (c >= 3) {
      c -= 3;
      maxx++;
    }
    cout << maxx << endl;
    maxx = 0;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
void akabe(int, int, int, int);
int maxx = 0;
int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int c, a, n;
    cin >> c >> a >> n;
    if (c + a + n >= 3)
      akabe(c, a, n, 0);
    cout << maxx << endl;
    maxx = 0;
  }
}
void akabe(int c, int a, int n, int sum) {
  if (c >= 2 && a >= 1) { // CCA
    akabe(c - 2, a - 1, n, sum + 1);
  }
  if (c >= 3) { // CCC
    akabe(c - 3, a, n, sum + 1);
  }
  if (c >= 1 && a >= 1 && n >= 1) { // CAN
    akabe(c - 1, a - 1, n - 1, sum + 1);
  }
  maxx = max(sum, maxx);
}
Problem p00276 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int n, c, a, m;
  int i;
  int cnt;
  cin >> n;
  for (i = 0; i < n; i++) {
    cnt = 0;
    cin >> c >> a >> m;
    if (c == 0)
      ;
    else if (a > 0) {
      while (c > 0 && a > 0 && m > 0) {
        c--;
        a--;
        m--;
        cnt++;
      }
      while (c > 1 && a > 0) {
        c -= 2;
        a--;
        cnt++;
      }
    }
    cnt += c / 3;
    cout << cnt << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int ma;

int jin(int, int, int, int);

int main() {
  int n, c, a, m;
  int i;
  cin >> n;
  for (i = 0; i < n; i++) {
    ma = 0;
    cin >> c >> a >> m;
    jin(c, a, m, 0);
    cout << ma << endl;
  }
  return 0;
}

int jin(int c, int a, int m, int cnt) {
  bool f = true;
  if (c >= 3) {
    f = false;
    jin(c - 3, a, m, cnt + 1);
  }
  if (c >= 2 && a >= 1) {
    f = false;
    jin(c - 2, a - 1, m, cnt + 1);
  }
  if (c >= 1 && a >= 1 && m >= 1) {
    f = false;
    jin(c - 1, a - 1, m - 1, cnt + 1);
  }

  if (f) {
    if (ma < cnt)
      ma = cnt;
  }

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

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n--) {
    int cnt = 0;
    int a, b, c;
    cin >> a >> b >> c;
    while (a > 0 && b > 0 && c > 0) {
      a--;
      b--;
      c--;
      cnt++;
    }
    while (a > 1 && b > 0) {
      a -= 2;
      b--;
      cnt++;
    }
    while (a > 2) {
      a -= 3;
      cnt++;
    }
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int func(int a, int b, int c, int n) {
  if (a < 0 || b < 0 || c < 0)
    return n - 1;
  return max(max(func(a - 2, b - 1, c, n + 1), func(a - 3, b, c, n + 1)),
             func(a - 1, b - 1, c - 1, n + 1));
}
int main() {
  int n;
  cin >> n;
  while (n--) {
    int a, b, c;
    cin >> a >> b >> c;
    cout << func(a, b, c, 0) << endl;
  }
}
Problem p00276 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int q;
  cin >> q;
  while (q--) {
    int c, a, n;
    cin >> c >> a >> n;
    int tmp, sum = 0;
    tmp = min(c, min(a, n));
    c -= tmp;
    a -= tmp;
    sum = tmp;
    tmp = min(c / 2, a);
    c -= tmp * 2;
    sum += tmp;
    sum += c / 3;
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

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

int solve(int c, int a, int n) {
  int k = 0;
  if (c >= 2 && a >= 1) {
    k = max(k, solve(c - 2, a - 1, n) + 1);
  }
  if (c >= 3) {
    k = max(k, solve(c - 3, a, n) + 1);
  }
  if (c >= 1 && a >= 1 && n >= 1) {
    k = max(k, solve(c - 1, a - 1, n - 1) + 1);
  }
  return k;
}

int main() {
  int y;
  cin >> y;
  rep(i, y) {
    int c, a, n;
    cin >> c >> a >> n;
    cout << solve(c, a, n) << endl;
  }
  return 0;
}
Problem p00276 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int Q;
  cin >> Q;
  for (; Q > 0; Q--) {
    int c, a, n, ans = 0;
    cin >> c >> a >> n;

    for (; c >= 1 && a >= 1 && n >= 1; ans++) {
      c--;
      a--;
      n--;
    }

    for (; c >= 2 && a >= 1; ans++) {
      c -= 2;
      a--;
    }

    for (; c >= 3; ans++) {
      c -= 3;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int Q;

  cin >> Q;

  for (; Q > 0; Q--) {
    int a, n, c, ans = 0;
    cin >> c >> a >> n;

    while (c > 0) {
      if (a > 0) {
        if (n > 0) {
          ans++;
          n--;
          a--;
          c--;
        } else if (c > 1) {
          ans++;
          a--;
          c -= 2;
        }
      } else if (c > 2) {
        ans++;
        c -= 3;
      } else
        break;
    }
    cout << ans << endl;
  }
}
Problem p00276 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int Q;
  cin >> Q;
  while (Q--) {
    int C, A, N;
    cin >> C >> A >> N;
    int ans = 0;
    for (int k = 0; k <= N; k++) {
      for (int i = 0; i <= (A - k); i++) {
        for (int j = 0; j <= (C - k - 2 * i) / 3; j++) {
          int c = i * 2 + j * 3 + k;
          int a = i + k;
          int n = k;
          if (c <= C && a <= A && n <= N)
            ans = max(ans, i + j + k);
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int Q;
  cin >> Q;
  while (Q--) {
    int C, A, N;
    cin >> C >> A >> N;
    int ans = 0;
    for (int k = 0; k <= N; k++) {
      for (int i = 0; i <= A; i++) {
        for (int j = 0; j <= C / 3; j++) {
          int c = i * 2 + j * 3 + k;
          int a = i + k;
          int n = k;
          if (c <= C && a <= A && n <= N)
            ans = max(ans, i + j + k);
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00276 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int MAX_CAN = 1002;

int dp[MAX_CAN][MAX_CAN];

int main() {
  memset(dp, -1, sizeof(dp));
  dp[0][0] = 0;
  REP(i, MAX_CAN) {
    REP(j, MAX_CAN) {
      if (i - 1 >= 0)
        dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      if (j - 1 >= 0)
        dp[i][j] = max(dp[i][j], dp[i][j - 1]);
      if (dp[i][j] < 0)
        continue;
      if (i + 2 < MAX_CAN && j + 1 < MAX_CAN)
        dp[i + 2][j + 1] = max(dp[i + 2][j + 1], dp[i][j] + 1);
      if (i + 3 < MAX_CAN)
        dp[i + 3][j] = max(dp[i + 3][j], dp[i][j] + 1);
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int C, A, N, ans = 0;
    cin >> C >> A >> N;
    REP(i, N + 1)
    if (C - i >= 0 && A - i >= 0) ans = max(ans, dp[C - i][A - i] + i);
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef complex<double> P;
const int N = 1002;

int dp[N][N][N];

int main() {
  memset(dp, -1, sizeof(dp));
  dp[0][0][0] = 0;
  REP(i, N) {
    REP(j, N) {
      REP(k, N) {
        if (i - 1 >= 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i - 1][j][k]);
        if (j - 1 >= 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j - 1][k]);
        if (k - 1 >= 0)
          dp[i][j][k] = max(dp[i][j][k], dp[i][j][k - 1]);
        if (dp[i][j][k] < 0)
          continue;
        if (i + 2 < N && j + 1 < N)
          dp[i + 2][j + 1][k] = max(dp[i + 2][j + 1][k], dp[i][j][k] + 1);
        if (i + 3 < N)
          dp[i + 3][j][k] = max(dp[i + 3][j][k], dp[i][j][k] + 1);
        if (i + 1 < N && j + 1 < N && k + 1 < N)
          dp[i + 1][j + 1][k + 1] =
              max(dp[i + 1][j + 1][k + 1], dp[i][j][k] + 1);
      }
    }
  }
  int Q;
  cin >> Q;
  while (Q--) {
    int C, A, N;
    cin >> C >> A >> N;
    cout << dp[C][A][N] << endl;
  }
  return 0;
}
Problem p00276 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

#define reps(v, f, l) for (int v = (f), v##_ = (l); v < v##_; ++v)
#define rep(v, n) reps(v, 0, n)
#define lep(v, n) reps(v, 1, n + 1)

using namespace std;

typedef long long int lint;

static inline int in() {
  int x;
  scanf("%d", &x);
  return x;
}
static inline lint inl() {
  lint x;
  scanf("%lld", &x);
  return x;
}
template <typename T> void show(T &a, char del = '\\n', char last = '\\n') {
  rep(i, a.size() - 1) cout << a[i] << del;
  cout << a[a.size() - 1] << last;
}

int main() {
  rep(i, in()) {
    int c, a, n;
    int maxi = 0;
    c = in();
    a = in();
    n = in();
    rep(can, 1001) {
      if (c < can || a < can || n < can)
        break;
      int nc, na;
      nc = c - can;
      na = a - can;
      rep(cca, 1001) {
        if (nc < cca + cca || na < cca)
          break;
        int nnc = nc - (cca + cca);
        maxi = max(can + cca + nnc / 3, maxi);
      }
    }
    printf("%d\\n", maxi);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <stdio.h>
#include <string.h>

static inline short Max(short a, short b) {
  if (a < b)
    return b;
  return a;
}

short memo[1001][1001][1001];
short solve(int c, int a, int n, int depth) {
  short r = 0;
  if (memo[c][a][n] != -1)
    return memo[c][a][n];
  if (c == 0)
    return memo[c][a][n] = depth;
  if (a > 0 && n > 0)
    r = Max(r, solve(c - 1, a - 1, n - 1, depth + 1));
  if (c > 1 && a > 0)
    r = Max(r, solve(c - 2, a - 1, n, depth + 1));
  if (c > 2)
    r = Max(r, solve(c - 3, a, n, depth + 1));
  return memo[c][a][n] = r;
}

int main(void) {
  int q, i;
  memset(memo, -1, sizeof(memo));
  scanf("%d", &q);
  for (i = 0; i < q; i++) {
    int c, a, n;
    scanf("%d %d %d", &c, &a, &n);
    printf("%hd\\n", solve(c, a, n, 0));
  }
  return (0);
}
Problem p00276 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

using namespace std;

int q;
int c, a, n;

int main(void) {
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d", &c, &a, &n);
    int res = 0;
    while (c > 0 && a > 0 && n > 0) {
      c--;
      a--;
      n--;
      res++;
    }
    while (c > 1 && a > 0) {
      c -= 2;
      a--;
      res++;
    }
    while (c > 2) {
      c -= 3;
      res++;
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

int q;
int c, a, n;

int main(void) {
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d%d%d", &c, &a, &n);
    int res = 0;
    for (int j = 0; j <= c / 3; j++) {
      for (int k = 0; k <= c / 2; k++) {
        for (int l = 0; l <= min(c, min(a, n)); l++) {
          int use_c = j * 3 + k * 2 + l;
          int use_a = k + l;
          int use_n = l;
          if (use_c <= c && use_a <= a && use_n <= n)
            res = max(res, j + k + l);
        }
      }
    }
    printf("%d\\n", res);
  }
  return 0;
}