alexjercan/bugnet huggingface dataset:
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 != 0) {
    int a[20];
    //		int allsum=0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      //			allsum+=a[i];
    }
    sort(a, a + n);

    int ret = 10000000;
    for (int s = 0; s < (1 << n); s++) {
      int sum = 0;
      int left = 0;
      for (int i = 0; i < n; i++) {
        if (1 & (s >> i)) {
          sum += a[i];
        } else
          left += a[i];
      }
      ret = min(ret, abs(sum - left));
      if (ret == 0) {
        break;
      }
    }
    cout << ret << endl;

    // int Asum=0;
    // vector<int>A,B;
    // int minSum;
    // for(int i = 0; i < n; i++){
    //	minSum = abs(Asum-sum);
    //	if(Asum < sum){
    //		A.push_back(a[i]);
    //		Asum+=a[i];
    //		sum-=a[i];
    //	}
    //	else{
    //		for(int j = i; j < n; j++){
    //			B.push_back(a[j]);
    //		}
    //		break;
    //	}
    // }
    // minSum = abs(Asum-sum);
    // for(int i = 0; i < A.size(); i++){
    //	if(Asum <= sum)
    //		break;
    //	Asum-=A[i];
    //	sum+=A[i];
    //	minSum = min(minSum,abs(Asum-sum));
    // }
    // cout << minSum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {

  int n;
  int a[20];
  while (cin >> n && n != 0) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    int minNum = 1000000000;
    for (int s = 0; s < (1 << n); s++) {
      int A, B;
      A = B = 0;
      for (int j = 0; j < n; j++) {
        if (1 & (s >> j)) {
          A += a[j];
        } else {
          B += a[j];
        }
      }
      minNum = min(minNum, abs(A - B));
    }
    cout << minNum << endl;
  }

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

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

int n, ans, a[21];
const int INF = 1e+9;

int solve(int i, int A, int B) {
  if (i == n) {
    ans = min(ans, abs(A - B));
  } else {
    solve(i + 1, A + a[i], B);
    solve(i + 1, A, B + a[i]);
  }
}

int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    ans = INF;
    solve(0, 0, 0);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int n, ans, a[21];
const int INF = 1e+9;

int sum(vector<int> A) {
  int s = 0;
  for (int i = 0; i < A.size(); i++) {
    s += A[i];
  }
  return s;
}

int solve(int i, vector<int> A, vector<int> B) {
  if (i == n) {
    ans = min(ans, abs(sum(A) - sum(B)));
  } else {
    A.push_back(a[i]);
    solve(i + 1, A, B);
    A.pop_back();
    B.push_back(a[i]);
    solve(i + 1, A, B);
    B.pop_back();
  }
}

int main() {
  while (cin >> n, n) {
    vector<int> A, B;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    ans = INF;
    solve(0, A, B);
    cout << ans << endl;
  }
}
Problem p00631 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int n;
int memo = -1;
int *sa, *a;

int bit(int m) {
  if (memo == -1) {
    int ans = 1;
    for (int i = 0; i < m; i++)
      ans *= 2;
    return ans;
  } else
    return memo;
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    sa = new int[bit(n)];
    a = new int[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < bit(n); i++) {
      sa[i] = 0;
      for (int j = 0; j < n; j++) {
        if (i >> j & 1)
          sa[i] += a[j];
        else
          sa[i] -= a[j];
      }
      sa[i] = abs(sa[i]);
    }
    int min = 1000000;
    for (int i = 0; i < bit(n); i++) {
      if (min > sa[i])
        min = sa[i];
    }
    cout << min << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int n;
int memo = -1;
int *sa, *sb, *sc, *a;

int bit(int m) {
  if (memo == -1) {
    int ans = 1;
    for (int i = 0; i < m; i++)
      ans *= 2;
    return ans;
  } else
    return memo;
}

int main() {
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    sa = new int[bit(n)];
    sb = new int[bit(n)];
    sc = new int[bit(n)];
    a = new int[n];
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    for (int i = 0; i < bit(n); i++) {
      for (int j = 0; j < n; j++) {
        if (i >> j & 1)
          sa[i] += a[j];
        else
          sb[i] += a[j];
      }
      sc[i] = abs(sa[i] - sb[i]);
    }
    int min = 1000000;
    for (int i = 0; i < bit(n); i++) {
      if (min > sc[i])
        min = sc[i];
    }
    cout << min << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
typedef set<int> S;

int main() {
  int n;
  while (cin >> n, n) {
    int a[20], sum = 0;
    for (int i = 0; i < n; i++)
      cin >> a[i], sum += a[i];

    S mp[21];
    S::iterator it;
    mp[0].insert(0);
    for (int i = 0; i < n; i++)
      for (it = mp[i].begin(); it != mp[i].end(); it++)
        mp[i + 1].insert((*it) + a[i]), mp[i + 1].insert(*it);

    int ans = 1e9;
    for (it = mp[n].begin(); it != mp[n].end(); it++)
      ans = min(ans, abs(sum - 2 * (*it)));
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef set<int> S;

int main() {
  int n;
  while (cin >> n, n) {
    int a[20], sum = 0;
    for (int i = 0; i < n; i++)
      cin >> a[i], sum += a[i];

    S mp;
    mp.insert(0);
    for (int i = 0; i < n; i++)
      for (S::iterator it = mp.begin(); it != mp.end(); it++)
        mp.insert((*it) + a[i]);

    int ans = 1e9;
    for (S::iterator it = mp.begin(); it != mp.end(); it++)
      ans = min(ans, sum - (*it));
    cout << ans << endl;
  }
  return 0;
}
Problem p00631 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;
int n, a[23];
vector<int> v;
void f(int k, int sum) {
  if (k == n)
    v.push_back(sum);
  else {
    f(k + 1, sum);
    f(k + 1, sum + a[k]);
  }
  return;
}
int main() {
  while (cin >> n, n) {
    v.clear();
    int sum = 0;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      sum += a[i];
    }
    f(0, 0);
    int ans = sum;
    for (size_t i = 0; i < v.size(); i++) {
      int v1 = v[i], v2 = sum - v[i];
      ans = min(ans, abs(v1 - v2));
    }
    cout << ans << endl;
  }
}


Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int n;
long long a[22], ans = 1145141919810, sum = 0;
void f(int i, int s) {
  if (i == n) {
    ans = min(ans, abs((sum - s) - s));
  }
  for (int p = 0; p <= 1; p++) {
    f(i + 1, s + a[i] * p);
  }
  return;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    sum += a[i];
  }
  f(0, 0);
  cout << ans << endl;
}
Problem p00631 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

int main() {
  int n, sum;
  vector<int> a;

  while (true) {
    cin >> n;

    if (n == 0) {
      break;
    }

    a = vector<int>(n);
    sum = 0;

    for (int i = 0; i < n; i++) {
      cin >> a[i];
      sum += a[i];
    }

    int min_ = (1 << 29);

    for (int i = 0; i < (1 << n); i++) {
      int sum2 = 0;

      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          sum2 += a[j];
        }
      }

      min_ = min(abs(2 * sum2 - sum), min_);
    }

    cout << min_ << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

using namespace std;

int main() {
  int n, sum;
  vector<int> a;

  while (true) {
    cin >> n;

    if (n == 0) {
      break;
    }

    a = vector<int>(n);
    sum = 0;

    for (int i = 0; i < n; i++) {
      cin >> a[i];
      sum += a[i];
    }

    int min_ = (1 << 29);

    for (int i = 0; i < (1 << n); i++) {
      int sum2 = 0;

      for (int j = 0; j < n; j++) {
        if (i & (1 << j)) {
          sum2 += a[j];
        }
      }

      cout << sum2 << endl;

      min_ = min(abs(2 * sum2 - sum), min_);
    }

    cout << min_ << endl;
  }

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

#import <queue>
#import <stdio.h>
int main() {
  std::queue<int> q;
  int w, h, f[484], i, j, a, b, t, x, z;
  char p[15];
  for (; scanf("%d%d ", &h, &w), h;) {
    for (j = 0; j < w + 2; j++)
      f[j] = f[j - ~h * 22] = -1;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++)
        f[x = i * 22 + j] = -2, z = getchar(),
                       z - 35 ? z > 65   ? b = x
                                : z > 64 ? a = x
                                         : 1
                              : --f[x];
      f[i * 22] = f[i * 22 - ~w] = -1;
      getchar();
    }
    f[a] = t = 0;
    q.push(0);
    for (q.push(a); a = q.front(), q.size() > 1; q.pop())
      if (!a)
        ++t, q.push(0);
      else
        for (i = 4; i--;)
          if (f[x = a + " 57L"[i] - 54] == -2)
            f[x] = t, q.push(x);
    q.pop();
    scanf("%s", p);
    for (a = t = 0; !a;) {
      x = b;
      z = 0;
      for (i = 0; !a && p[i]; f[b] >= 0 && ++z)
        ++t, ~f[j = b - 54 + "L65676 "[p[i++] - 50]] ? b = j : 0,
                                                       f[b] >= 0 & t >= f[b]
                                                           ? a = b
                                                           : 0;
      !z & !a & !p[i] & b == x & f[b] < 0 && --a;
    }
    printf(~a ? "%d %d %d\\n" : "impossible\\n", t, a / 22 - 1, a % 22 - 1);
  }
}

Error: TLE

Fixed submission:

#import <queue>
#import <stdio.h>
int main() {
  std::queue<int> q;
  int w, h, f[484], i, j, a, b, t, x, z;
  char p[15];
  for (; scanf("%d%d ", &h, &w), h;) {
    for (j = 0; j < w + 2; j++)
      f[j] = f[j - ~h * 22] = -1;
    for (i = 1; i <= h; i++) {
      for (j = 1; j <= w; j++)
        f[x = i * 22 + j] = -2, z = getchar(),
                       z - 35 ? z > 65   ? b = x
                                : z > 64 ? a = x
                                         : 1
                              : --f[x];
      f[i * 22] = f[i * 22 - ~w] = -9;
      getchar();
    }
    f[a] = t = 0;
    q.push(0);
    for (q.push(a); a = q.front(), q.size() > 1; q.pop())
      if (!a)
        ++t, q.push(0);
      else
        for (i = 4; i--;)
          if (f[x = a + " 57L"[i] - 54] == -2)
            f[x] = t, q.push(x);
    q.pop();
    scanf("%s", p);
    for (a = t = 0; !a;) {
      x = b;
      z = 0;
      for (i = 0; !a && p[i]; f[b] >= 0 && ++z)
        ++t, ~f[j = b - 54 + "L65676 "[p[i++] - 50]] ? b = j : 0,
                                                       f[b] >= 0 & t >= f[b]
                                                           ? a = b
                                                           : 0;
      !z & !a & !p[i] & b == x & f[b] < 0 && --a;
    }
    printf(~a ? "%d %d %d\\n" : "impossible\\n", t, a / 22 - 1, a % 22 - 1);
  }
}
Problem p00632 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;

char s[20][21];
int d[20][20];
int dx[]{0, -1, 0, 0, 1}, dy[]{0, 0, 1, -1, 0};

int main() {
  int h, w;
  while (scanf("%d%d", &h, &w), h) {
    int ax, ay, bx, by;
    rep(i, h) {
      scanf("%s", s[i]);
      rep(j, w) {
        if (s[i][j] == 'A')
          ax = i, ay = j;
        if (s[i][j] == 'B')
          bx = i, by = j;
      }
    }
    string p;
    cin >> p;
    for (char &c : p) {
      if (c == '5')
        c = 0;
      if (c == '8')
        c = 1;
      if (c == '6')
        c = 2;
      if (c == '4')
        c = 3;
      if (c == '2')
        c = 4;
    }
    queue<P> que;
    memset(d, -1, sizeof(d));
    d[ax][ay] = 0;
    que.push(P(ax, ay));
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      for (int i = 1; i <= 4; i++) {
        int nx = p.first + dx[i], ny = p.second + dy[i];
        if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != '#' &&
            d[nx][ny] == -1) {
          d[nx][ny] = d[p.first][p.second] + 1;
          que.push(P(nx, ny));
        }
      }
    }
    int x = bx, y = by;
    int t = 0;
    rep(i, 1000) {
      for (char c : p) {
        if (d[x][y] != -1 && d[x][y] <= t) {
          printf("%d %d %d\\n", t, x, y);
          goto g;
        }
        int nx = x + dx[c], ny = y + dy[c];
        t++;
        if (0 <= nx && nx < h && 0 <= ny && ny < w)
          x = nx, y = ny;
      }
    }
    puts("impossible");
  g:;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;

char s[20][21];
int d[20][20];
int dx[]{0, -1, 0, 0, 1}, dy[]{0, 0, 1, -1, 0};

int main() {
  int h, w;
  while (scanf("%d%d", &h, &w), h) {
    int ax, ay, bx, by;
    rep(i, h) {
      scanf("%s", s[i]);
      rep(j, w) {
        if (s[i][j] == 'A')
          ax = i, ay = j;
        if (s[i][j] == 'B')
          bx = i, by = j;
      }
    }
    string p;
    cin >> p;
    for (char &c : p) {
      if (c == '5')
        c = 0;
      if (c == '8')
        c = 1;
      if (c == '6')
        c = 2;
      if (c == '4')
        c = 3;
      if (c == '2')
        c = 4;
    }
    queue<P> que;
    memset(d, -1, sizeof(d));
    d[0][0] = 0;
    que.push(P(ax, ay));
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      for (int i = 1; i <= 4; i++) {
        int nx = p.first + dx[i], ny = p.second + dy[i];
        if (0 <= nx && nx < h && 0 <= ny && ny < w && s[nx][ny] != '#' &&
            d[nx][ny] == -1) {
          d[nx][ny] = d[p.first][p.second] + 1;
          que.push(P(nx, ny));
        }
      }
    }
    int x = bx, y = by;
    int t = 0;
    rep(i, 8000000) {
      for (char c : p) {
        if (d[x][y] != -1 && d[x][y] <= t) {
          printf("%d %d %d\\n", t, x, y);
          goto g;
        }
        int nx = x + dx[c], ny = y + dy[c];
        t++;
        if (0 <= nx && nx < h && 0 <= ny && ny < w)
          x = nx, y = ny;
      }
    }
    puts("impossible");
  g:;
  }
}
Problem p00633 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
double x[100], y[100], r[100];
int size[100];
pair<double, int> e[100][40000];
int d[100];
double DIST[100][100];
inline double HYP(double a, double b) { return sqrt(a * a + b * b); }
double PI = 3.14159265359;
double EPS = 1e-5;
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++)
      scanf("%lf%lf%lf", x + i, y + i, r + i);
    for (int i = 0; i < a; i++) {
      size[i] = 0;
      d[i] = 1;
    }
    for (int i = 0; i < a; i++)
      for (int j = 0; j < a; j++)
        DIST[i][j] = HYP(x[i] - x[j], y[i] - y[j]);
    for (int i = 0; i < a; i++) {
      for (int j = 0; j < a; j++) {
        if (i == j)
          continue;
        if (DIST[i][j] < (double)r[j] - r[i] + EPS)
          d[i] = 0;
        else if (DIST[i][j] < (double)r[i] - r[j] + EPS)
          ;
        else if (DIST[i][j] + EPS < (double)r[i] + r[j]) {
          double c = atan2(y[j] - y[i], x[j] - x[i]);
          double p =
              acos(0.5 * (DIST[i][j] * DIST[i][j] + r[i] * r[i] - r[j] * r[j]) /
                   DIST[i][j] / r[i]);
          double t1 = c - p;
          double t2 = c + p;
          if (t2 < 0) {
            t1 += PI * 2;
            t2 += PI * 2;
          }
          if (t2 > PI * 2) {
            t1 -= PI * 2;
            t2 -= PI * 2;
          }
          if (t1 < 0) {
            e[i][size[i]++] = make_pair(t1 + PI * 2, 1);
            e[i][size[i]++] = make_pair(PI * 2, 2);
            t1 = 0;
          }
          e[i][size[i]++] = (make_pair(t1, 1));
          e[i][size[i]++] = (make_pair(t2, 2));
        }
      }
    }
    for (int i = 0; i < a; i++)
      std::sort(e[i], e[i] + size[i]);
    double ret = 0;
    for (int i = 0; i < a; i++) {
      if (d[i]) {
        int now = 0;
        double at = 0;
        for (int j = 0; j < size[i]; j++) {
          if (!now)
            ret += (e[i][j].first - at) * r[i];
          if (e[i][j].second == 1)
            now++;
          else
            now--;
          at = e[i][j].first;
        }
        ret += (PI * 2 - at) * r[i];
      }
    }
    printf("%.9f\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <stdio.h>
int x[1000], y[1000], r[1000];
int main() {
  int a;
  while (scanf("%d", &a), a) {
    for (int i = 0; i < a; i++)
      scanf("%d%d%d", x + i, y + i, r + i);
    printf("1.0000000000000\\n");
  }
}
Problem p00635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
using namespace std;

int n;

struct NODE {
  int v[10], o[10];
  int depth;
  NODE(){};
};
int sz;
int dist[10][10];
NODE q;
int ans;
void f(int cur) {
  if (cur >= ans)
    return;
  if (sz == n) {
    ans = cur;
    return;
  }
  for (int i = 0; i < n; i++) {
    if (q.o[i] == -1) {
      int mx = 0;
      for (int j = 0; j < sz; j++) {
        mx = max(q.o[q.v[j]] + dist[q.v[j]][i], mx);
      }
      q.v[sz] = i;
      q.o[i] = mx;
      int t = q.depth;
      q.depth = mx;
      sz++;
      f(mx);
      sz--;
      q.depth = t;
      q.o[i] = -1;
    }
  }
}
int main() {
  while (cin >> n && n) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        dist[i][j] = -1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        scanf("%d", &dist[i][j]);
        dist[i][j] = dist[j][i] = max(dist[i][j], dist[j][i]);
      }
    }
    ans = (1 << 30);
    sz = 0;
    for (int i = 0; i < 10; i++)
      q.o[i] = -1;
    f(0);

    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int n;

struct NODE {
  int v[10], o[10];
  int depth, bit, big;
  NODE(){};
};
int sz;
int dist[10][10];
NODE q;
int ans;
void f(int cur) {
  if (cur >= ans)
    return;
  if (sz == n) {
    ans = cur;
    return;
  }
  for (int i = 0; i < n; i++) {
    if (q.o[i] == -1) {
      int mx = 0;
      for (int j = 0; j < sz; j++) {
        mx = max(q.o[q.v[j]] + dist[q.v[j]][i], mx);
      }
      int targ = mx;
      for (int j = 0; j < n; j++) {
        if (q.o[j] == -1)
          targ = mx + dist[i][j];
      }
      q.v[sz] = i;
      q.o[i] = mx;
      q.bit |= (1 << i);
      int t = q.depth;
      int t2 = q.big;
      q.depth = mx;
      q.big = max(targ, q.big);
      // Q.push(q);
      sz++;
      f(mx);
      sz--;
      q.depth = t;
      q.big = t2;
      q.o[i] = -1;
      q.bit &= ~(1 << i);
    }
  }
}
int main() {
  while (cin >> n && n) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        dist[i][j] = -1;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> dist[i][j];
        dist[i][j] = dist[j][i] = max(dist[i][j], dist[j][i]);
      }
    }
    ans = (1 << 30);
    sz = 0;
    for (int i = 0; i < 10; i++)
      q.o[i] = -1;
    f(0);

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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#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;

vector<bool> check;

void dfs(vector<vector<int>> &a, vector<int> pos, int &ret) {
  int n = a.size();

  if (count(pos.begin(), pos.end(), -1) == n - 1) {
    ret = min(ret, *(max_element(pos.begin(), pos.end())));
    return;
  }

  for (int i = 0; i < n; ++i) {
    if (pos[i] == -1)
      continue;

    vector<int> pos2 = pos;
    int maxPos = 0;
    for (int j = 0; j < n; ++j) {
      if (pos2[j] != -1) {
        pos2[j] = max(pos2[j], pos2[i] + a[i][j]);
        maxPos = max(maxPos, pos2[j]);
      }
    }
    pos2[i] = -1;

    if (maxPos >= ret)
      continue;
    dfs(a, pos2, ret);
  }
}

int solve(vector<vector<int>> &a) {
  int n = a.size();
  vector<int> pos(n, 0);
  int ret = INT_MAX;
  dfs(a, pos, ret);
  return ret;
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<vector<int>> a(n, vector<int>(n, 0));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        int tmp;
        cin >> tmp;
        a[i][j] = a[j][i] = max(a[i][j], max(a[j][i], tmp));
      }
    }
    cout << solve(a) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#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 dfs(vector<vector<int>> &a, bitset<10> &bs, vector<int> &pos, int &minX,
        int maxX) {
  int n = a.size();
  int m = bs.count();
  if (n == m)
    return minX = min(minX, maxX);
  if (maxX + (n - m) >= minX)
    return INT_MAX;

  int ret = INT_MAX;
  for (int i = (m == 0 ? 1 : 0); i < n; ++i) {
    if (bs[i])
      continue;
    int tmp = 0;
    for (int j = 0; j < n; ++j) {
      if (bs[j])
        tmp = max(tmp, pos[j] + a[i][j]);
    }
    bs[i] = true;
    pos[i] = tmp;
    ret = min(ret, dfs(a, bs, pos, minX, tmp));
    pos[i] = 0;
    bs[i] = false;
  }
  return ret;
}

int solve(vector<vector<int>> &a) {
  int n = a.size();
  bitset<10> bs(0);
  vector<int> pos(n, 0);
  int minX = INT_MAX;
  return dfs(a, bs, pos, minX, 0);
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<vector<int>> a(n, vector<int>(n, 0));
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        int tmp;
        cin >> tmp;
        a[i][j] = a[j][i] = max(a[i][j], max(a[j][i], tmp));
      }
    }
    cout << solve(a) << endl;
  }
}
Problem p00635 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

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

int g[10][10];
int n;
int ans;
int m[10];
int flag[10];
int p[10];

void solve(int pos, int nn) {
  int now = p[nn - 1];
  if (nn == n) {
    ans = min(ans, now);
  } else if (now < ans) {
    REP(i, n) if (!flag[i]) {
      int dst = 0;
      flag[i] = 1;
      m[nn] = i;
      REP(j, nn) dst = max(dst, p[j] + g[i][m[j]]);
      p[nn] = dst;
      solve(i, nn + 1);
      flag[i] = 0;
    }
  }
}

int main() {
  while (scanf("%d", &n), n) {
    REP(i, n) REP(j, n) scanf("%d", &g[i][j]);
    REP(i, n) REP(j, n) g[i][j] = max(g[i][j], g[j][i]);

    ans = INT_MAX;

    REP(i, n) {
      m[0] = i;
      flag[i] = 1;
      solve(i, 1);
      flag[i] = 0;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

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

int g[10][10];
int n;

int memo[10][1 << 10];

int solve(int pos, int flag) {
  if (flag == (1 << n) - 1)
    return 0;
  // if(memo[pos][flag] != -1) return memo[pos][flag];

  int ret = INT_MAX;
  REP(i, n) if ((flag & (1 << i)) == 0) {
    ret = min(ret, g[pos][i] + solve(i, (flag | 1 << i)));
  }
  // return memo[pos][flag] = ret;
  return ret;
}

int main() {
  while (scanf("%d", &n), n) {
    int ans = INT_MAX;

    REP(i, n) REP(j, n) scanf("%d", &g[i][j]);
    REP(i, n) REP(j, n) g[i][j] = max(g[i][j], g[j][i]);

    memset(memo, -1, sizeof(memo));

    REP(i, n) ans = min(ans, solve(i, (1 << i)));

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