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

#include <bits/stdc++.h>
using namespace std;
int power(int n) {
  int num = 1;
  for (int i = 0; i < n; i++)
    num *= 10;
  return num;
}
int serch(int sum, int n) {
  if (sum > 21)
    return 0;
  else if (n == 0)
    return sum;
  else
    return max(serch(sum + 11, n - 1), serch(sum + 1, n - 1));
}
int main() {
  string str;
  while (getline(cin, str)) {
    if (str[0] == '0')
      break;
    int card[1000] = {}, co = 0, point = 0;
    for (int i = 0;; i++) {
      if (str[i] == ' ' || str[i] == '\\0') {
        for (int j = point; j < i; j++)
          card[co] += (str[j] - '0') * power(i - 1 - j);
        co++;
        point = i + 1;
      }
      if (str[i] == '\\0')
        break;
    }
    int cou = 0, sum = 0;
    for (int i = 0; i < co; i++) {
      if (card[i] == 1)
        cou++;
      else if (card[i] < 10)
        sum += card[i];
      else
        sum += 10;
    }
    cout << serch(sum, cou) << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int power(int n) {
  int num = 1;
  for (int i = 0; i < n; i++)
    num *= 10;
  return num;
}
int serch(int sum, int n) {
  if (sum > 21)
    return 0;
  else if (n == 0)
    return sum;
  else
    return max(serch(sum + 11, n - 1), serch(sum + 1, n - 1));
}
int main() {
  string str;
  while (getline(cin, str)) {
    if (str[0] == '0')
      break;
    int card[20] = {}, co = 0, point = 0;
    for (int i = 0;; i++) {
      if (str[i] == ' ' || str[i] == '\\0') {
        for (int j = point; j < i; j++)
          card[co] += (str[j] - '0') * power(i - 1 - j);
        co++;
        point = i + 1;
      }
      if (str[i] == '\\0')
        break;
    }
    int cou = 0, sum = 0;
    for (int i = 0; i < co; i++) {
      if (card[i] == 1)
        cou++;
      else if (card[i] < 10)
        sum += card[i];
      else
        sum += 10;
    }
    cout << serch(sum, cou) << endl;
  }
}
Problem p00169 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
typedef pair<int, int> Pii;
typedef pair<int, Pii> P;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  string str;
  while (true) {
    getline(cin, str);
    vi num;
    stringstream ss(str);
    int tmp;
    while (ss >> tmp) {
      num.PB(tmp);
    }
    if (num[0] == 0)
      break;
    int one = 0;
    int ans = 0;
    rep(i, num.size()) {
      if (num[i] == 1) {
        ans++;
        one++;
      } else {
        if (num[i] > 9) {
          ans += 10;
        } else {
          ans += num[i];
        }
      }
    }
    while (one--) {
      if (ans + 10 < 22) {
        ans += 10;
      }
    }
    if (ans >= 22)
      ans = 0;
    printf("%d\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
const int dy[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int INF = 1 << 30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i, j, k) for (int i = (j); i < (k); i++)
#define rep(i, j) reps(i, 0, j)
#define MOD 1000000007
typedef pair<int, int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  string str;
  while (getline(cin, str)) {
    stringstream ss(str);
    int tmp;
    vi cards;
    while (ss >> tmp) {
      cards.PB(tmp);
    }
    sort(cards.begin(), cards.end());
    if (cards[0] == 0)
      break;
    stack<int> S[2];
    S[0].push(0);
    rep(i, cards.size()) {
      while (S[i % 2].size()) {
        int x = S[i % 2].top();
        S[i % 2].pop();
        //  cout << x << "\\n";
        if (x > 21)
          x = 0;
        if (cards[i] == 1) {
          S[1 - i % 2].push(x + 1);
          S[1 - i % 2].push(x + 11);
        } else if (cards[i] > 9) {
          S[1 - i % 2].push(x + 10);
        } else {
          S[1 - i % 2].push(x + cards[i]);
        }
      }
    }
    int ans = 0;
    while (S[cards.size() % 2].size()) {
      int ret = S[cards.size() % 2].top();
      S[cards.size() % 2].pop();
      if (ret > 21) {
        continue;
      }
      ans = max(ans, ret);
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p00169 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  cin >> noskipws;
  while (true) {
    int cards[21];
    int card_num;
    int ace_num = 0; //  エースがあるかどうか
    for (card_num = 0;; card_num++) {
      if (card_num == 22) {
        int dummy;
        char ws;
        bool b = true;
        while (b) {
          cin >> dummy >> ws;
          if (ws != ' ') {
            b = false;
          }
        }
        break;
      }
      cin >> cards[card_num];
      if (cards[card_num] == 0)
        return 0;
      if (cards[card_num] == 1)
        ace_num++;
      char ws;
      cin >> ws;
      if (ws != ' ') {
        card_num++;
        break;
      }
    }

    int point = 0;
    for (int i = 0; i < card_num; i++) {
      if (cards[i] < 10) {
        point += cards[i];
      } else {
        point += 10;
      }
    }

    while (ace_num > 0 && point <= 11) {
      point += 10;
      ace_num--;
    }
    if (point > 21) {
      point = 0;
    }

    cout << point << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  cin >> noskipws;
  while (true) {
    int cards[21];
    int card_num;
    int ace_num = 0; //  エースがあるかどうか
    for (card_num = 0;; card_num++) {
      if (card_num == 21) {
        int dummy;
        char ws;
        bool b = true;
        while (b) {
          cin >> dummy >> ws;
          if (ws != ' ') {
            b = false;
          }
        }
      }
      cin >> cards[card_num];
      if (cards[card_num] == 0)
        return 0;
      if (cards[card_num] == 1)
        ace_num++;
      char ws;
      cin >> ws;
      if (ws != ' ') {
        card_num++;
        break;
      }
    }

    int point = 0;
    for (int i = 0; i < card_num; i++) {
      if (cards[i] < 10) {
        point += cards[i];
      } else {
        point += 10;
      }
    }

    while (ace_num > 0 && point <= 11) {
      point += 10;
      ace_num--;
    }
    if (point > 21) {
      point = 0;
    }

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

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
  char c;
  int n, s = 0;
  int A = 0;
  while (1) {
    while (1) {
      scanf("%d%c", &n, &c);

      if (n > 1 && n < 11)
        s += n;
      else if (n > 10)
        s += 10;
      else if (n == 1) {
        s += 1;
        A++;
      }
      if (c == '\\n')
        break;
    }
    if (s == 0)
      break;
    if (s >= 22)
      printf("0\\n");

    else {
      if (A > 0) {
        for (int i = 0; i < A; i++)
          if (s + 10 <= 21)
            s += 10;
      }

      printf("%d\\n", s);
    }
    s = 0;
    A = 0;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
  char c;
  int n, s = 0;
  int A = 0;
  while (1) {
    while (1) {
      scanf("%d%c", &n, &c);

      if (n > 1 && n < 11)
        s += n;
      else if (n > 10)
        s += 10;
      else if (n == 1) {
        s += 1;
        A++;
      }
      if (c == '\\n')
        break;
    }
    if (s == 0)
      break;
    if (s >= 22)
      printf("0\\n");

    else {
      if (A > 0) {
        for (int i = 0; i < A; i++)
          if (s + 10 <= 21)
            s += 10;
      }

      printf("%d\\n", s);
      s = 0;
      A = 0;
    }
  }

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

#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  int n, c, m, sw;
  char w;
  while (1) {
    sw = 0;
    m = 0;
    c = 0;
    while (1) {
      scanf("%d%c", &n, &w);
      if (n == 0) {
        sw = 1;
        break;
      }
      if (n == 1) {
        m += 1;
        c += 11;
      } else if (n > 10)
        c += 10;
      else
        c += n;
      if (w == '\\n')
        break;
    }
    if (sw == 1)
      break;
    while (m != 0) {
      if (c > 21) {
        m--;
        c -= 10;
      } else
        break;
    }
    if (c < 22)
      cout << c << endl;
    else
      cout << "0" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  int n, c, m;
  char w;
  while (1) {
    m = 0;
    c = 0;
    while (1) {
      scanf("%d%c", &n, &w);
      if (n == 0)
        break;
      if (n == 1) {
        m += 1;
        c += 11;
      } else if (n > 10)
        c += 10;
      else
        c += n;
      if (w == '\\n')
        break;
    }

    while (m != 0) {
      if (c > 21) {
        m--;
        c -= 10;
      } else
        break;
    }
    if (c < 22)
      cout << c << endl;
    else
      cout << "0" << endl;
  }
  return 0;
}
Problem p00169 (C++) — buggy submission, verdict: Runtime Error

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

/*int stoi(string a){
        int ret;
        stringstream ss;
        ss<<a;
        ss>>ret;
        return ret;
        }*/

int main() {
  string s;
  while (getline(cin, s) && s != "0") {
    s.push_back(' ');
    int one = 0;
    int sum = 0;

    string a;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == ' ') {
        int b = stoi(a);
        if (b == 1)
          one++;
        else if (b > 10)
          sum += 10;
        else
          sum += b;
        a.clear();
      } else
        a.push_back(s[i]);
    }
    sum += one;
    for (int i = 0; i < one; i++) {
      if (sum < 12)
        sum += 10;
    }

    if (sum > 21)
      sum = 0;
    cout << sum << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  string s;
  while (getline(cin, s) && s != "0") {
    s.push_back(' ');
    int one = 0;
    int sum = 0;

    string a;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == ' ') {
        int b = stoi(a);
        if (b == 1)
          one++;
        else if (b > 10)
          sum += 10;
        else
          sum += b;
      } else
        a.push_back(s[i]);
    }

    int dp[114][23] = {};
    dp[0][max(22, sum)] = 1;
    for (int i = 1; i <= one; i++) {
      for (int j = 0; j < 22; j++) {
        dp[i][j + 1] = dp[i][j];
        dp[i][min(22, j + 11)] = dp[i][j];
      }
    }

    int ans = 0;
    for (int i = 0; i < 22; i++) {
      if (dp[one][i])
        ans = i;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00169 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bitset>
#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int main() {
  string s;
  while (getline(cin, s)) {
    if (s == "0")
      break;
    stringstream ss;
    ss << s;
    vector<int> c;
    int temp;
    while (ss >> temp)
      c.push_back(temp);
    bitset<22> bits;
    bits.set(0);
    for (int i = 0; i < c.size(); i++) {
      bitset<22> newbits;
      int x = c[i];
      for (int j = 0; j < 22; j++) {
        if (!bits[j])
          continue;
        if (x >= 10)
          x = 10;
        if (x != 1 && j + x <= 21)
          newbits.set(j + x);
        if (x == 1) {
          if (j + 11 <= 21)
            newbits.set(j + 11);
          if (j + 1 <= 21)
            newbits.set(j + 1);
        }
      }
      bits = newbits;
    }
    int mm = 0;
    for (int i = 0; i < 22; i++) {
      if (bits[i])
        mm = i;
    }
    cout << mm << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <sstream>
#include <vector>

using namespace std;

int ten(vector<int> &cards, int index, int score) {
  if (index == cards.size()) {
    if (score >= 22)
      return 0;
    return score;
  }
  if (cards[index] >= 10)
    return ten(cards, index + 1, score + 10);
  if (cards[index] >= 2)
    return ten(cards, index + 1, score + cards[index]);
  if (cards[index] == 1) {
    return max(ten(cards, index + 1, score + 11),
               ten(cards, index + 1, score + 1));
  }
}

int main() {
  string s;
  while (getline(cin, s)) {
    if (s == "0")
      break;
    stringstream ss;
    ss << s;
    vector<int> c;
    int temp;
    while (ss >> temp)
      c.push_back(temp);
    cout << ten(c, 0, 0) << endl;
  }
}
Problem p00169 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>

using namespace std;

int rec(int sum, int one) {
  if (sum > 21)
    return -1;
  else if (one == 0 && sum > 21)
    return -1;
  else if (one == 0 && sum <= 21)
    return sum;

  int res = rec(sum + 1, one - 1);
  res = max(res, rec(sum + 11, one - 1));

  return res;
}

bool solve(string &s) {
  int sum = 0, one = 0;
  int index = 0;

  s += " ";
  while (index < s.size()) {
    int num = 0;

    while (index < s.size()) {
      if ('0' <= s[index] && s[index] <= '9') {
        num *= 10;
        num += (int)(s[index] - '0');
        index++;
      } else
        break;
    }

    if (num == 1)
      one++;
    else if (num / 10 == 1)
      sum += 10;
    else
      sum += num;
    index++;
  }

  if (sum == 0 && one == 0)
    return false;

  if (sum > 21) {
    cout << 0 << endl;
    return true;
  } else if (one == 0) {
    cout << sum << endl;
    return true;
  } else {

    int ans = rec(sum, one);
    if (ans >= 0)
      cout << ans << endl;
    else
      cout << 0 << endl;
    return true;
  }
}

int main() {
  string s;

  while (true) {
    getline(cin, s);
    if (!solve(s))
      break;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int rec(int sum, int one) {
  if (one == 0 && sum > 21)
    return -1;
  else if (one == 0 && sum <= 21)
    return sum;

  int res = rec(sum + 1, one - 1);
  res = max(res, rec(sum + 11, one - 1));

  return res;
}

bool solve(string &s) {
  int sum = 0, one = 0;
  int index = 0;

  s += " ";
  while (index < s.size()) {
    int num = 0;

    while (index < s.size()) {
      if ('0' <= s[index] && s[index] <= '9') {
        num *= 10;
        num += (int)(s[index] - '0');
        index++;
      } else
        break;
    }

    if (num == 1)
      one++;
    else if (num / 10 == 1)
      sum += 10;
    else
      sum += num;
    index++;
  }

  if (sum == 0 && one == 0)
    return false;

  if (sum > 21) {
    cout << 0 << endl;
    return true;
  } else if (one == 0) {
    cout << sum << endl;
    return true;
  } else {

    int ans = rec(sum, one);
    if (ans >= 0)
      cout << ans << endl;
    else
      cout << 0 << endl;
    return true;
  }
}

int main() {
  string s;

  while (true) {
    getline(cin, s);
    if (!solve(s))
      break;
  }

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

#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 F first
#define S second
#define INF 1 << 30

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

int a[14], p;

int main() {
  char str[514];
  while (fgets(str, sizeof(str), stdin) && str[0] != '0') {
    memset(a, 0, sizeof(a));
    char *tp;
    tp = strtok(str, " ");
    while (tp != NULL) {
      a[atoi(tp)]++;
      tp = strtok(NULL, " ");
    }

    //    rep(i, p) printf("%d ", a[i]); puts("");
    int res = 0;
    a[10] += a[11];
    a[10] += a[12];
    a[10] += a[13];
    REP(i, 2, 11) res += a[i] * i;
    //    printf("%d\\n", res);
    //    printf("%d\\n", a[1]);

    int one = 11 * a[1];
    while (res + one > 21 && one != a[1]) {
      one -= 10;
    }
    //    printf("%d\\n", res+one);
    res += one;
    if (res > 21)
      res = 0;

    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

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 F first
#define S second
#define INF 1 << 30

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

int a[100], p;

int dfs(int i, int n) {
  if (i == p) {
    if (n > 21)
      return 0;
    return n;
  }

  if (a[i] == 1)
    return max(dfs(i + 1, n + 1), dfs(i + 1, n + 11));
  if (a[i] > 10)
    return dfs(i + 1, n + 10);
  return dfs(i + 1, n + a[i]);
}

int main() {
  char str[128];
  while (fgets(str, sizeof(str), stdin) && str[0] != '0') {
    str[strlen(str)] = 0;
    char *tp;
    p = 0;
    tp = strtok(str, " ");
    while (tp != NULL) {
      a[p++] = atoi(tp);
      tp = strtok(NULL, " ");
    }

    //    rep(i, p) printf("%d ", a[i]); puts("");

    printf("%d\\n", dfs(0, 0));
  }
  return 0;
}
Problem p00169 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  while (getline(cin, s), s != "0") {
    stringstream ss(s);
    int x = 0, i, ans, k;
    int si = 0;
    vector<int> v;
    v.clear();
    v.push_back(0);

    while (ss >> x) {
      si = v.size();
      vector<int> n;
      for (i = 0; i < si; i++) {

        if (x == 1) {
          k = v[i];
          n.push_back(k + 11);
          v[i]++;
        } else if (x > 10) {
          v[i] += 10;
        } else {
          v[i] += x;
        }
      }
      for (i = 0; i < n.size(); i++)
        v.push_back(n[i]);
      n.clear();

      sort(v.begin(), v.end());
      v.erase(unique(v.begin(), v.end()), v.end());
      v.erase(lower_bound(v.begin(), v.end(), 22), v.end());
    }

    sort(v.begin(), v.end());
    ans = 0;
    for (i = 0; i < v.size(); i++) {
      if (v[i] <= 21)
        ans = v[i];
    }
    cout << ans << endl;
    s = "";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  while (getline(cin, s), s != "0") {
    stringstream ss(s);
    int x = 0, i, ans, k;
    int si = 0;
    vector<int> v;
    v.clear();
    v.push_back(0);

    while (ss >> x) {
      si = v.size();
      vector<int> n;
      for (i = 0; i < si; i++) {

        if (x == 1) {
          k = v[i];
          n.push_back(k + 11);
          v[i]++;
        } else if (x > 10) {
          v[i] += 10;
        } else {
          v[i] += x;
        }
      }
      for (i = 0; i < n.size(); i++)
        v.push_back(n[i]);
      n.clear();
    }

    sort(v.begin(), v.end());
    ans = 0;
    for (i = 0; i < v.size(); i++) {
      if (v[i] <= 21)
        ans = v[i];
    }
    cout << ans << endl;
    s = "";
  }
  return 0;
}
Problem p00169 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {

  while (1) {
    int n = 0, card[100] = {};
    string str;
    getline(cin, str);
    str += ' ';
    for (int i = 0, num = 0; i < str.size(); i++) {
      if (isdigit(str[i]))
        num = num * 10 + str[i] - '0';
      else
        card[n++] = num, num = 0;
    }
    if (card[0] == 0)
      break;
    sort(card, card + n, greater<int>());
    int score = 0;
    for (int i = 0; i < n; i++) {
      if (card[i] >= 10)
        score += 10;
      else if (card[i] > 1)
        score += card[i];
      else if (score + 11 + (n - i - 1) <= 21)
        score += 11;
      else
        score++;
    }
    cout << (score <= 21 ? score : 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  while (1) {
    int n = 0, card[100];
    while (cin >> card[n++])
      ;
    if (card[0] == 0)
      break;
    sort(card, card + n, greater<int>());
    int score = 0;
    for (int i = 0; i < n; i++) {
      if (card[i] >= 10)
        score += 10;
      else if (card[i] > 1)
        score += card[i];
      else if (11 + (n - i) <= 21)
        score += 11;
      else
        score++;
    }
    cout << (score <= 21 ? score : 0) << endl;
  }
  return 0;
}
Problem p00169 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int calc(int card[], int num) {
  int sum = 0;
  int A = 0;
  int Ap[12][2] = {{0, 0},  {1, 11}, {2, 12}, {3, 13}, {4, 14},  {5, 15},
                   {6, 16}, {7, 17}, {8, 18}, {9, 19}, {10, 20}, {11, 21}};
  for (int i = 0; i < num; i++) {
    if (card[i] != 1) {
      if (card[i] >= 10) {
        sum += 10;
      } else {
        sum += card[i];
      }
    } else {
      A++;
    }
  }
  int best = -1;
  if (A >= 12) {
    sum += A;
    A = 0;
  }
  for (int i = 0; i < 2; i++) {
    int temp = sum + Ap[A][i];
    if (temp > 21)
      temp = 0;
    if (temp > best)
      best = temp;
  }

  return best;
}

int main() {
  while (1) {
    string str;
    getline(cin, str);
    if (str == "0")
      break;

    stringstream sin;
    sin << str;
    int card[1000];
    fill(&card[0], &card[999] + 1, 0);
    int num;
    for (int i = 0; i < 1000; i++) {
      if (!(sin >> card[i])) {
        num = i;
        break;
      }
      if (i + 1 == 1000) {
        num = 1000;
        break;
      }
    }
    cout << calc(card, num) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int calc(int card[54], int num, int score = 0, int cnt = 0) {
  static int max;
  if (cnt == 0)
    max = -1;

  if (cnt == num) {
    if (score > 21)
      score = 0;
    if (score > max)
      max = score;
    return 0;
  }

  if (card[cnt] == 1) {
    calc(card, num, score + 1, cnt + 1);
    calc(card, num, score + 11, cnt + 1);
  } else if (card[cnt] > 10) {
    calc(card, num, score + 10, cnt + 1);
  } else {
    calc(card, num, score + card[cnt], cnt + 1);
  }

  if (cnt == 0)
    return max;
}

int main() {
  while (1) {
    string str;
    getline(cin, str);
    if (str == "0")
      break;

    stringstream sin;
    sin << str;
    int card[54];
    fill(&card[0], &card[53] + 1, 0);
    int num;
    for (int i = 0; i < 54; i++) {
      if (!(sin >> card[i])) {
        num = i;
        break;
      }
      if (i + 1 == 54) {
        num = 54;
        break;
      }
    }
    cout << calc(card, num) << endl;
  }
  return 0;
}