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

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

class Team {
public:
  int id, af, n, pen;
  Team(int id, int af, int n, int pen) : id(id), af(af), n(n), pen(pen) {}
  bool operator<(const Team &t) const {
    if (n != t.n)
      return n < t.n;
    else if (pen != t.pen)
      return pen > t.pen;
    else
      return id > t.id;
  }
};

int main() {
  int N;
  while (cin >> N, N) {
    priority_queue<Team> pq;
    for (int i = 0; i < N; i++) {
      int id, af, n, pen;
      cin >> id >> af >> n >> pen;
      pq.push(Team(id, af, n, pen));
    }

    int pass = 0;
    vector<int> n_af(1001, 0);
    while (!pq.empty()) {
      Team now = pq.top();
      pq.pop();

      if (pass < 10) {
        if (n_af[now.af] < 3) {
          n_af[now.af]++;
          cout << now.id << endl;
          pass++;
        }
      } else if (pass < 20) {
        if (n_af[now.af] < 2) {
          n_af[now.af]++;
          cout << now.id << endl;
          pass++;
        }
      } else if (pass < 26) {
        if (n_af[now.af] < 1) {
          n_af[now.af]++;
          cout << now.id << endl;
          pass++;
        }
      }
    }
  }
}

Error: 0

Fixed submission:

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

class Team {
public:
  int id, af, n, pen;
  Team(int id, int af, int n, int pen) : id(id), af(af), n(n), pen(pen) {}
  bool operator<(const Team &t) const {
    if (n != t.n)
      return n < t.n;
    else if (pen != t.pen)
      return pen > t.pen;
    else
      return id > t.id;
  }
};

int main() {
  int N;
  while (cin >> N, N) {
    priority_queue<Team> pq;
    for (int i = 0; i < N; i++) {
      int id, af, n, pen;
      cin >> id >> af >> n >> pen;
      pq.push(Team(id, af, n, pen));
    }

    int pass = 0;
    vector<int> n_af(20, 0);
    while (!pq.empty()) {
      Team now = pq.top();
      pq.pop();

      if (pass < 10) {
        if (n_af[now.af] < 3) {
          n_af[now.af]++;
          cout << now.id << endl;
          pass++;
        }
      } else if (pass < 20) {
        if (n_af[now.af] < 2) {
          n_af[now.af]++;
          cout << now.id << endl;
          pass++;
        }
      } else {
        if (n_af[now.af] < 1) {
          n_af[now.af]++;
          cout << now.id << endl;
          pass++;
        }
      }
    }
  }
}
Problem p00629 (C++) — buggy submission, verdict: Runtime Error

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

typedef pair<int, int> P;

int main() {
  int N;
  while (cin >> N, N) {
    vector<vector<P>> rank(11);
    vector<int> univ_id(1001);

    for (int c = 0; c < N; c++) {
      int i, u, a, p;
      cin >> i >> u >> a >> p;
      rank[a].push_back(P(p, i));
      univ_id[i] = u;
    }

    for (auto &i : rank)
      sort(i.begin(), i.end());

    int count = 0;
    vector<int> univ_cnt(1001);
    for (int i = 10; i > -1; i--) {
      for (auto team : rank[i]) {
        int I = team.second;
        int pena = team.first;
        int limit = 0;
        if (count < 10)
          limit = 3;
        else if (count < 20)
          limit = 2;
        else if (count < 26)
          limit = 1;
        else
          limit = 0;
        if (univ_cnt[univ_id[I]] < limit) {
          cout << I << endl;
          univ_cnt[univ_id[I]]++;
          count++;
        }
      }
    }
  }

  return 0;
}

Error: -6
munmap_chunk(): invalid pointer


Fixed submission:

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

typedef pair<int, int> P;

int main() {
  int N;
  while (cin >> N, N) {
    vector<vector<P>> rank(11);
    vector<int> univ_id(N);

    for (int c = 0; c < N; c++) {
      int i, u, a, p;
      cin >> i >> u >> a >> p;
      rank[a].push_back(P(p, i));
      univ_id[i] = u;
    }

    for (auto &i : rank)
      sort(i.begin(), i.end());

    int count = 0;
    vector<int> univ_cnt(1001);
    for (int i = 10; i > -1; i--) {
      for (auto team : rank[i]) {
        int I = team.second;
        int pena = team.first;
        int limit;
        if (count < 10)
          limit = 3;
        else if (count < 20)
          limit = 2;
        else if (count < 26)
          limit = 1;
        if (univ_cnt[univ_id[I]] < limit) {
          cout << I << endl;
          univ_cnt[univ_id[I]]++;
        }
      }
    }
  }

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

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

typedef pair<int, pair<int, pair<int, int>>> pp;

const int U_MAX = 1000;
int us[U_MAX];

int main() {
  int n;
  while (cin >> n, n) {
    map<pp, int> ts;
    for (int i = 0; i < n; i++) {
      int id, u, a, p;
      cin >> id >> u >> a >> p;
      ts[make_pair(-a, make_pair(p, make_pair(id, u)))] = id;
    }
    int e = 0;
    fill_n(us, U_MAX, 0);
    for (map<pp, int>::iterator it = ts.begin(); it != ts.end(); it++) {
      int j = it->first.second.second.second - 1;
      if ((e < 10 && us[j] < 3) || (e < 20 && us[j] < 2) ||
          (e < 26 && !us[j])) {
        cout << it->second << endl;
        us[j]++;
        e++;
      }
    }
  }
}

Error: 0

Fixed submission:

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

class Team {
public:
  int id, belong, correct, penalty;
};

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0) {
      break;
    }

    vector<Team> teams(n);
    for (int i = 0; i < n; i++) {
      cin >> teams[i].id >> teams[i].belong >> teams[i].correct >>
          teams[i].penalty;
    }
    for (int i = 0; i < n - 1; i++) {
      for (int j = n - 1; j > i; j--) {
        if (teams[j - 1].correct < teams[j].correct ||
            (teams[j - 1].correct == teams[j].correct &&
             teams[j - 1].penalty > teams[j].penalty) ||
            (teams[j - 1].correct == teams[j].correct &&
             teams[j - 1].penalty == teams[j].penalty &&
             teams[j - 1].id > teams[j].id)) {
          swap(teams[j - 1], teams[j]);
        }
      }
    }
    int selected = 0;
    queue<int> selected_i;
    int belongs[20];
    fill_n(belongs, 20, 0);
    for (int i = 0; i < n; i++) {
      if (selected < 10) {
        if (belongs[teams[i].belong] < 3) {
          selected_i.push(i);
          belongs[teams[i].belong]++;
        }
      } else if (selected < 20) {
        if (belongs[teams[i].belong] < 2) {
          selected_i.push(i);
          belongs[teams[i].belong]++;
        }
      } else if (selected < 26) {
        if (belongs[teams[i].belong] == 0) {
          selected_i.push(i);
          belongs[teams[i].belong]++;
        }
      } else {
        break;
      }
    }
    while (selected_i.empty() == false) {
      cout << teams[selected_i.front()].id << endl;
      selected_i.pop();
    }
  }
}
Problem p00630 (C++) — buggy submission, verdict: Memory 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;

string name;
char type;

int main() {
  while (cin >> name >> type) {
    if (type == 'X')
      break;
    if (name.find("_") != std::string::npos) {
      if (type == 'D')
        goto e;
      if (type == 'U')
        name[0] -= 32;
      rep(i, name.size()) {
        if (name[i] != '_')
          continue;
        name.erase(name.begin() + i);
        name[i] -= 32;
      }
    } else {
      if (type == 'L') {
        if (name[0] < 97)
          name[0] += 32;
      } else if (type == 'U') {
        if (name[0] > 96)
          name[0] -= 32;
      } else {
        if (name[0] < 97)
          name[0] += 32;
        rep(i, name.size()) {
          if (name[i] > 96)
            continue;
          name[i] += 32;
          name.insert(i, "_");
        }
      }
    }
  e:
    cout << name << endl;
  }
  return 0;
}

Error: MLE

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;

string name;
char type;

int main() {
  while (cin >> name >> type && type != 'X') {
    if (type == 'L') {
      if (name[0] >= 'A' && name[0] <= 'Z')
        name[0] += 32;
      rep(i, name.size()) {
        if (name[i] != '_')
          continue;
        name.erase(name.begin() + i);
        name[i] -= 32;
      }
    } else if (type == 'U') {
      if (name[0] >= 'a' && name[0] <= 'z')
        name[0] -= 32;
      rep(i, name.size()) {
        if (name[i] != '_')
          continue;
        name.erase(name.begin() + i);
        name[i] -= 32;
      }
    } else if (type == 'D') {
      if (name[0] >= 'A' && name[0] <= 'Z')
        name[0] += 32;
      rep(i, name.size()) {
        if (name[i] >= 'a' && name[i] <= 'z')
          continue;
        name[i] += 32;
        name.insert(i, "_");
      }
    }

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

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

void UpperCamelCase(string &);
void LowerCamelCase(string &);
void UnderScoreCase(string &);
bool IsUpperCase(char);

int main() {
  char c;
  string str;

  while (1) {
    cin >> str >> c;

    if (c == 'X')
      break;
    else if (c == 'U')
      UpperCamelCase(str);
    else if (c == 'L')
      LowerCamelCase(str);
    else if (c == 'D')
      UnderScoreCase(str);

    cout << str << endl;
  }

  return 0;
}

void UpperCamelCase(string &s) {
  string::iterator i;

  if (s[0] >= 'a' && s[0] <= 'z')
    s[0] = toupper(s[0]);

  i = s.begin();
  while (1) {
    i = find(i, s.end(), '_');
    if (i == s.end())
      break;
    i = s.erase(i);
    *i = toupper(*i);
  }
}

void LowerCamelCase(string &s) {
  string::iterator i;

  if (s[0] >= 'A' && s[0] <= 'Z')
    s[0] = tolower(s[0]);

  i = s.begin();
  while (1) {
    i = find(i, s.end(), '_');
    if (i == s.end())
      break;
    i = s.erase(i);
    *i = toupper(*i);
  }
}

void UnderScoreCase(string &s) {
  string::iterator i;

  if (s[0] >= 'A' && s[0] <= 'Z')
    s[0] = tolower(s[0]);

  i = s.begin();
  while (1) {
    i = find_if(i, s.end(), IsUpperCase);
    if (i == s.end())
      break;
    *i = tolower(*i);
    i = s.insert(i, '_');
    ++i;
  }
}

bool IsUpperCase(char c) {
  if (c >= 'A' && c <= 'Z')
    return true;
  else
    return false;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

void UpperCamelCase(string &);
void LowerCamelCase(string &);
void UnderScoreCase(string &);
bool IsUpperCase(char);

int main() {
  char c;
  string str;

  while (1) {
    cin >> str >> c;

    if (c == 'X')
      break;
    else if (c == 'U')
      UpperCamelCase(str);
    else if (c == 'L')
      LowerCamelCase(str);
    else if (c == 'D')
      UnderScoreCase(str);

    cout << str << endl;
  }

  return 0;
}

void UpperCamelCase(string &s) {
  string::iterator i;

  if (s[0] >= 'a' && s[0] <= 'z')
    s[0] = toupper(s[0]);

  i = s.begin();
  while (1) {
    i = find(i, s.end(), '_');
    if (i == s.end())
      break;
    i = s.erase(i);
    *i = toupper(*i);
  }
}

void LowerCamelCase(string &s) {
  string::iterator i;

  if (s[0] >= 'A' && s[0] <= 'Z')
    s[0] = tolower(s[0]);

  i = s.begin();
  while (1) {
    i = find(i, s.end(), '_');
    if (i == s.end())
      break;
    i = s.erase(i);
    *i = toupper(*i);
  }
}

void UnderScoreCase(string &s) {
  string::iterator i;

  if (s[0] >= 'A' && s[0] <= 'Z')
    s[0] = tolower(s[0]);

  i = s.begin();
  while (1) {
    i = find_if(i, s.end(), IsUpperCase);
    if (i == s.end())
      break;
    *i = tolower(*i);
    s.insert(i, '_');
  }
}

bool IsUpperCase(char c) {
  if (c >= 'A' && c <= 'Z')
    return true;
  else
    return false;
}
Problem p00630 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
bool C(char c) { return isupper(c); }
int main() {
  string s;
  string::iterator i;
  char c;
  int x;
  while (cin >> s >> c, c - 'X') {
    s[0] = toupper(s[0]);
    while (x = s.find('_'), x != string::npos)
      s.erase(x, 1), s[x] = toupper(s[x]);
    if (c - 'U')
      s[0] = tolower(s[0]);
    if (c == 'D')
      for (i = s.begin(); (i = find_if(i, s.end(), ptr_fun(C))) != s.end(); ++i)
        *i = tolower(*i), i = s.insert(i, '_');
    cout << s << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
bool C(char c) { return isupper(c); }
int main() {
  string s;
  string::iterator i;
  char c;
  int x;
  while (cin >> s >> c, c - 'X') {
    s[0] = toupper(s[0]);
    while (x = s.find('_'), x != string::npos)
      s.erase(x, 1), s[x] = toupper(s[x]);
    if (c - 'U')
      s[0] = tolower(s[0]);
    if (c == 'D')
      for (i = s.begin(); (i = find_if(i, s.end(), ptr_fun(C))) != s.end(); ++i)
        *i = tolower(*i), s.insert(i, '_');
    cout << s << endl;
  }
  return 0;
}
Problem p00631 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> as(n);
    int sum = 0;
    for (int i = 0; i < n; i++) {
      cin >> as[i];
      sum += as[i];
    }
    int ans = 1 << 29;
    for (int i = 1, size = 1 << (n - 1); i < size; i++) {
      int a = 0;
      for (int j = 0; j < n - 1; j++) {
        if (i & (1 << j)) {
          a += as[j];
        }
      }
      ans = min(ans, abs(sum - 2 * a));
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> as(n);
    int sum = 0;
    for (int i = 0; i < n; i++) {
      cin >> as[i];
      sum += as[i];
    }
    int ans = 1 << 29;
    for (int i = 0, size = 1 << (n - 1); i < size; i++) {
      vector<int> gs;
      for (int j = 0; j < n - 1; j++) {
        if (i & (1 << j)) {
          gs.push_back(j);
        }
      }
      if (gs.empty()) {
        continue;
      }
      int a = 0;
      for (vector<int>::iterator it = gs.begin(); it != gs.end(); it++) {
        a += as[*it];
      }
      ans = min(ans, abs(sum - 2 * a));
    }
    cout << ans << endl;
  }
}
Problem p00631 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m[20];
vector<int> X1;
vector<int> X2;
int Mid = 0;

int main() {
  while (true) {
    X1.clear();
    X2.clear();
    int sum = 0;
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> m[i];
      sum += m[i];
    }
    Mid = n / 2;
    int s = 0;
    for (int i = 0; i < (1 << Mid); i++) {
      s = 0;
      for (int j = 0; j < Mid; j++) {
        if ((i / (1 << j)) % 2 == 0) {
          s += m[j];
        }
      }
      X1.push_back(s);
    }
    for (int i = 0; i < (1 << (n - Mid)); i++) {
      s = 0;
      for (int j = 0; j < (n - Mid); j++) {
        if ((i / (1 << j)) % 2 == 0) {
          s += m[Mid + j];
        }
      }
      X2.push_back(s);
    }
    int minx = 1000000000;
    sort(X2.begin(), X2.end());
    for (int i = 0; i < X1.size(); i++) {
      if (X1[i] <= sum / 2) {
        int target = sum / 2 - X1[i];
        int p = upper_bound(X2.begin(), X2.end(), target) - X2.begin();
        p--;
        for (int j = 0; j < 3; j++) {
          if (p >= 0 && p < X2.size()) {
            int a = X2[p] + X1[i];
            int b = sum - a;
            minx = min(minx, abs(a - b));
          }
          p++;
        }
      }
    }
    cout << minx << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int n, m[20];

int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    for (int i = 0; i < n; i++) {
      cin >> m[i];
    }
    int a = 1 << n, b, c, d, minx = 1000000000;
    for (int i = 0; i < a; i++) {
      b = 0;
      c = 0;
      d = 0;
      for (int j = 1; j < a; j *= 2) {
        if ((i / j) % 2 == 0) {
          b += m[d];
        } else {
          c += m[d];
        }
        d++;
      }
      minx = min(minx, abs(b - c));
    }
    cout << minx << endl;
  }
  return 0;
}
Problem p00631 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
int n, s, a[20];
int dfs(int i, int sum) {
  if (i == n)
    return abs(s - sum * 2);
  if ((sum + a[i]) * 2 > s)
    return dfs(i + 1, sum);
  return min(dfs(i + 1, sum + a[i]), dfs(i + 1, sum));
}
int main(void) {
  while (cin >> n) {
    if (n == 0)
      break;
    REP(i, n) cin >> a[i];
    s = accumulate(a, a + n, 0);
    cout << dfs(0, 0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
int n;
int dfs(int i, vector<int> a, int sum, int max) {
  if (i == n)
    return abs(max - sum * 2);
  if ((sum + a[i]) * 2 > max)
    return dfs(i + 1, a, sum, max);
  return min(dfs(i + 1, a, sum + a[i], max), dfs(i + 1, a, sum, max));
}
int main(void) {
  while (cin >> n) {
    if (n == 0)
      break;
    vector<int> a(n);
    REP(i, n) cin >> a[i];
    int s = accumulate(a.begin(), a.end(), 0);
    cout << dfs(0, a, 0, s) << endl;
  }
  return 0;
}
Problem p00631 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <tr1/unordered_set>
using namespace std;

int n, v[20], ans = 0;

tr1::unordered_set<int> memo[20];

int X[421], Y[421], top;

int main() {
  while (scanf("%d\\n", &n) && n) {
    for (int i = 0; i < n; i++)
      scanf("%d", &v[i]), memo[i].clear();
    ans = 99999999;
    top = 0;
    X[top] = 0;
    Y[top] = 0;
    while (~top) {
      int pos = X[top], sum = Y[top];
      top--;
      if (pos == n)
        ans = min(ans, sum);
      else {
        if (memo[pos].count(sum))
          continue;
        else
          memo[pos].insert(sum);
        X[++top] = pos + 1;
        Y[top] = sum + v[pos];
        X[++top] = pos + 1;
        Y[top] = abs(sum - v[pos]);
      }
    }
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <tr1/unordered_set>
using namespace std;

int n, v[20], ans = 0;

tr1::unordered_set<int> memo[20];

int X[421], Y[421], top;

int main() {
  while (scanf("%d\\n", &n) && n) {
    for (int i = 0; i < n; i++)
      scanf("%d", &v[i]), memo[i].clear();
    ans = 99999999;
    top = 0;
    X[top] = 0;
    Y[top] = 0;
    while (~top) {
      int pos = X[top], sum = Y[top];
      top--;
      if (memo[pos].count(sum))
        continue;
      else
        memo[pos].insert(sum);
      if (pos == n)
        ans = min(ans, sum);
      else {
        X[++top] = pos + 1;
        Y[top] = sum + v[pos];
        X[++top] = pos + 1;
        Y[top] = abs(sum - v[pos]);
      }
    }
    printf("%d\\n", ans);
  }
}
Problem p00631 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <string.h>
#include <vector>

using namespace std;

int main() {
  int n;
  int a[22];
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    unsigned long long l = 0;
    int mi = INT_MAX;
    while (l < (1 << n)) {
      int p = 0, q = 0;
      for (int i = 0; i < n; i++) {
        if ((l >> i) & 1)
          p += a[i];
        else
          q += a[i];
      }
      mi = min(abs(q - p), mi);
      l++;
    }
    cout << mi << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int abs(int a) { return a < 0 ? -a : a; }
int main() {
  int n;
  int a[22];
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> a[i];
    int mi = 1 << 21;
    for (int l = 0; l < (2 << n); l++) {
      int p = 0, q = 0;
      for (int i = 0; i < n; i++) {
        if ((l >> i) & 1)
          p += a[i];
        else
          q += a[i];
      }
      mi = min(abs(q - p), mi);
    }
    cout << mi << endl;
  }
}
Problem p00631 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#define abs(a) ((a) > 0 ? (a) : -(a))

using namespace std;

long diffmin, n, p[20];
void dfs(int layer, long a, long b) {
  if (layer < n) {
    dfs(layer + 1, a + p[layer], b);
    dfs(layer + 1, a, b + p[layer]);
  } else {
    if (diffmin > abs(a - b)) {
      diffmin = abs(a - b);
    }
    return;
  }
}
int main() {
  long all = 0, party0 = 0, party1 = 0;
  cin >> n;
  while (n) {
    for (int i = 0; i < n; i++) {
      long input;
      cin >> input;
      p[i] = input;
      all += input;
    }
    diffmin = all;
    dfs(0, 0, 0);
    cout << diffmin << endl;
    cin >> n;
    party0 = party1 = 0;
    all = 0;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int n;
  long p[20], all = 0, party1 = 0, min;
  cin >> n;
  while (n) {
    for (int i = 0; i < n; i++) {
      cin >> p[i];
      all += p[i];
    }
    min = all;
    for (long bit = 1; bit < 1 << 20; bit++) {
      for (int i = 0; i < 20; i++) {
        if (bit >> i & 1) {
          party1 += p[i];
        }
      }
      if (min > all - party1) {
        min = all - party1;
      }
    }
    cout << min << endl;
    cin >> n;
  }
  return 0;
}