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

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

#define MAX_N 20

int n, U;
int cs[MAX_N];
int rs[MAX_N];

inline bool check(int S) {
  // 履修に必要な単位をすべて取得しているか
  for (int i = 0; i < n; i++) {
    if (S & (1 << i)) {
      if ((S & rs[i]) != rs[i]) {
        return false;
      }
    }
  }

  // 総単位数がUを越えているか
  int unit = 0;
  for (int i = 0; i < n; i++) {
    if (S & (1 << i)) {
      unit += cs[i];
    }
  }
  if (unit < U)
    return false;

  return true;
}

bool checkAllPattern(int k) {
  int c = (1 << k) - 1;
  while (c < (1 << n)) {
    if (check(c))
      return true;
    int x = c & -c;
    int y = c + x;
    c = ((c & ~y) / x >> 1) | y;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  while (cin >> n >> U, n || U) {
    memset(cs, 0, sizeof(cs));
    memset(rs, 0, sizeof(rs));
    for (int i = 0; i < n; i++) {
      int c, k, r;
      cin >> c >> k;
      r = 0;
      for (int j = 0; j < k; j++) {
        int _r;
        cin >> _r;
        r |= (1 << _r);
      }
      cs[i] = c;
      rs[i] = r;
    }

    for (int i = 1; i <= n; i++) {
      if (checkAllPattern(i)) {
        cout << i << endl;
        break;
      }
    }

    // int ans = (1<<28);
    // for (int S = 1; S < (1<<n); S++) {
    //     if (check(S)) {
    //         ans = min(ans, __builtin_popcount(S));
    //     }
    // }

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

Error: TLE

Fixed submission:

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

#define MAX_N 20

int n, U;
int cs[MAX_N];
int rs[MAX_N];

inline bool check(int S) {
  // 履修に必要な単位をすべて取得しているか
  for (int i = 0; i < n; i++) {
    if (S & (1 << i)) {
      if ((S & rs[i]) != rs[i]) {
        return false;
      }
    }
  }

  // 総単位数がUを越えているか
  int unit = 0;
  for (int i = 0; i < n; i++) {
    if (S & (1 << i)) {
      unit += cs[i];
    }
  }
  if (unit < U)
    return false;

  return true;
}

int main() {
  ios::sync_with_stdio(false);
  int c, k, r;
  int _r;
  while (cin >> n >> U, n || U) {
    memset(cs, 0, sizeof(cs));
    memset(rs, 0, sizeof(rs));
    for (int i = 0; i < n; i++) {
      cin >> c >> k;
      r = 0;
      for (int j = 0; j < k; j++) {
        cin >> _r;
        r |= (1 << _r);
      }
      cs[i] = c;
      rs[i] = r;
    }

    int ans = (1 << 28);
    for (int S = 1; S < (1 << n); S++) {
      if (check(S)) {
        ans = min(ans, __builtin_popcount(S));
      }
    }

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

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

using namespace std;

#define MAX 20
#define INF 1e9

vector<int> vec, v[MAX], a[MAX];

void init(int N) {
  vec.clear();
  for (int i = 0; i < MAX; i++) {
    v[i].clear();
  }
  for (int i = 1; i < 1 << N; i++) {
    int idx = __builtin_popcount(i);
    v[idx - 1].push_back(i);
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < (int)v[i].size(); j++) {
      vec.push_back(v[i][j]);
    }
  }
}

int main() {
  int N, U, c[MAX], k, r;
  bool used[MAX] = {false};

  while (cin >> N >> U, (N | U)) {
    if (!used[N - 1]) {
      init(N);
      a[N - 1] = vec;
      used[N - 1] = true;
    } else {
      vec = a[N - 1];
    }
    int arr[MAX];
    for (int i = 0; i < N; i++) {
      cin >> c[i] >> k;
      int S = 0;
      for (int j = 0; j < k; j++) {
        cin >> r;
        S |= 1 << r;
      }
      arr[i] = S;
    }
    int ans = INF;
    for (int i = 0; i < (int)vec.size(); i++) {
      int S = vec[i];
      int cnt = 0, total = 0;
      bool flg = true;
      for (int j = 0; j < N; j++) {
        if (S >> j & 1) {
          if ((S & arr[j]) == arr[j]) {
            cnt += c[j];
            total++;
          } else {
            flg = false;
            break;
          }
        }
      }
      if (flg && cnt >= U) {
        ans = total;
        break;
      }
    }
    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;

#define MAX 20
#define INF 1e9

struct State {
  int total, sub, S;
  State(int total, int sub, int S) : total(total), sub(sub), S(S) {}
};

int main() {
  int N, U, c[MAX], k, r;

  while (cin >> N >> U, (N | U)) {
    int arr[MAX];
    for (int i = 0; i < N; i++) {
      cin >> c[i] >> k;
      int S = 0;
      for (int j = 0; j < k; j++) {
        cin >> r;
        S |= 1 << r;
      }
      arr[i] = S;
    }

    bool visited[1 << N];
    memset(visited, false, sizeof(visited));
    visited[0] = true;
    queue<State> Q;
    Q.push(State(0, 0, 0));
    while (!Q.empty()) {
      State now = Q.front();
      Q.pop();
      if (now.total >= U) {
        cout << now.sub << endl;
        break;
      }
      for (int i = 0; i < N; i++) {
        int S = now.S;
        S |= 1 << i;
        if (!visited[S] && (S & arr[i] == arr[i])) {
          Q.push(State(now.total + c[i], now.sub + 1, S));
        }
      }
    }
  }

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

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

int n, U, tmp, sum;
int c[30], k[30], r[30];
int ans;
bool f;

int main() {
  while (scanf("%d%d", &n, &U), (n || U)) {
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &c[i], &k[i]);
      r[i] = 0;
      for (int j = 0; j < k[i]; j++) {
        scanf("%d", &tmp);
        r[i] |= 1 << tmp;
      }
    }

    f = false;
    ;
    for (ans = 1; ans < n; ans++) {
      int comb = (1 << ans) - 1;
      while (comb < (1 << n)) {
        sum = 0;
        f = true;
        for (int i = 0; i < n; i++) {
          if ((comb >> i) & 1) {
            if ((comb & r[i]) != r[i]) {
              f = false;
              break;
            }
            sum += c[i];
          }
        }

        if (f && U <= sum) {
          break;
        }
        f = false;

        int x = comb & -comb, y = comb + x;
        comb = (((comb & ~y) / x) >> 1) | y;
      }
      if (f)
        break;
    }

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

Error: TLE

Fixed submission:

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

int n, U, tmp, sum, num;
int c[30], k[30], r[30];
bool f[30][30], flag;

int main() {
  while (scanf("%d%d", &n, &U), (n || U)) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        f[i][j] = (i - j) ? false : true;

    for (int i = 0; i < n; i++) {
      scanf("%d%d", &c[i], &k[i]);
      for (int j = 0; j < k[i]; j++) {
        scanf("%d", &tmp);
        f[i][tmp] = true;
      }
    }

    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          f[i][j] |= f[i][k] && f[k][j];

    for (int i = 0; i < n; i++) {
      r[i] = 0;
      for (int j = 0; j < n; j++) {
        if (f[i][j])
          r[i] |= 1 << j;
      }
    }

    int res = 20;
    for (int i = 0; i < (1 << n); i++) {
      sum = 0, num = 0;
      flag = true;
      for (int j = 0; j < n; j++) {
        if ((i >> j) & 1) {
          if ((i & r[j]) != r[j]) {
            flag = false;
            break;
          }
          sum += c[j];
          num++;
          if (res < num) {
            flag = false;
            break;
          }
        }
      }
      if (flag && num < res && U <= sum)
        res = num;
    }

    printf("%d\\n", res);
  }
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

// 16
#include <algorithm>
#include <iostream>
#include <queue>

using namespace std;

struct S {
  int c, nc, ng;
};

int main() {
  for (int n, u; cin >> n >> u, n | u;) {
    int ng[20], pr[20] = {};
    for (int i = 0; i < n; i++) {
      int k;
      cin >> ng[i] >> k;
      while (k--) {
        int r;
        cin >> r;
        pr[i] |= 1 << r;
      }
    }
    queue<S> que;
    S is = {0, 0, 0};
    que.push(is);
    bool p[1 << 20];
    fill(p, p + (1 << n), false);
    for (;;) {
      S c = que.front();
      if (c.ng >= u)
        break;
      que.pop();
      if (p[c.c]++)
        continue;
      for (int i = 0; i < n; i++) {
        if (!(c.c & 1 << i) && !(pr[i] & ~c.c)) {
          S n = {c.c | 1 << i, c.nc + 1, c.ng + ng[i]};
          que.push(n);
        }
      }
    }
    cout << que.front().nc << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 16
#include <iostream>

using namespace std;

int main() {
  for (int n, u; cin >> n >> u, n | u;) {
    int ng[20], pr[20] = {};
    for (int i = 0; i < n; i++) {
      int k;
      cin >> ng[i] >> k;
      while (k--) {
        int r;
        cin >> r;
        pr[i] |= 1 << r;
      }
    }
    int m = 1 << 30;
    for (int i = 0; i < 1 << n; i++) {
      int gs = 0, ns = 0, np = 0;
      for (int j = 0; j < n; j++) {
        if (i & 1 << j) {
          np |= pr[j];
          gs += ng[j];
          ns++;
        }
      }
      if (!(np & ~i) && u <= gs && ns < m) {
        m = ns;
      }
    }
    cout << m << endl;
  }
  return 0;
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdio.h>
using namespace std;
int g[21];
int t[21];
// pair<pair<int,int>,int>p[21];
// int conv[];
int bp[1 << 20];
int main() {
  int a, b;
  for (int i = 0; i < (1 << 20); i++)
    bp[i] = __builtin_popcount(i);
  while (scanf("%d%d", &a, &b), a) {
    for (int i = 0; i < a; i++)
      g[i] = 0;
    for (int i = 0; i < a; i++) {
      int c;
      scanf("%d%d", t + i, &c);
      for (int j = 0; j < c; j++) {
        int d;
        scanf("%d", &d);
        g[i] += 1 << d;
      }
    }
    //	for(int i=0;i<a;i++)p[i]=make_pair(t[i],g[i]),;
    //	std::sort(p,p+a);
    //	for(int i=0;i<a;i++){
    //		t[i]=p[a-1-i].first;
    //		g[i]=p[a-1-i].second;
    //	}
    int sum = 0;
    for (int i = 0; i < a; i++)
      sum += t[i];
    int ret = 999999999;
    for (int i = 0; i < (1 << a); i++) {
      int q = bp[i];
      if (ret <= q)
        continue;
      int s = sum;
      bool ok = true;
      for (int j = 0; j < a; j++) {
        if (i & (1 << j)) {
          if ((i & g[j]) != g[j]) {
            ok = false;
            break;
          }
        } else {
          s -= t[j];
          if (s < b) {
            ok = false;
            break;
          }
        }
      }
      if (ok)
        ret = q;
    }
    printf("%d\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
using namespace std;
int g[21];
int t[21];
// pair<pair<int,int>,int>p[21];
// int conv[];
int main() {
  int a, b;
  while (scanf("%d%d", &a, &b), a) {
    for (int i = 0; i < a; i++)
      g[i] = 0;
    for (int i = 0; i < a; i++) {
      int c;
      scanf("%d%d", t + i, &c);
      for (int j = 0; j < c; j++) {
        int d;
        scanf("%d", &d);
        g[i] += 1 << d;
      }
    }
    //	for(int i=0;i<a;i++)p[i]=make_pair(t[i],g[i]),;
    //	std::sort(p,p+a);
    //	for(int i=0;i<a;i++){
    //		t[i]=p[a-1-i].first;
    //		g[i]=p[a-1-i].second;
    //	}
    int sum = 0;
    for (int i = 0; i < a; i++)
      sum += t[i];
    int ret = 999999999;
    for (int i = 0; i < (1 << a); i++) {
      int q = __builtin_popcount(i);
      if (ret <= q)
        continue;
      int s = sum;
      bool ok = true;
      for (int j = 0; j < a; j++) {
        if (i & (1 << j)) {
          if ((i & g[j]) != g[j]) {
            ok = false;
            break;
          }
        } else {
          s -= t[j];
          if (s < b) {
            ok = false;
            break;
          }
        }
      }
      if (ok)
        ret = q;
    }
    printf("%d\\n", ret);
  }
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int n, U, a[20], b[20], c, r[20], popcount[1048588], sum[1048588];
vector<pair<int, int>> popcnts[21];
int main() {
  for (int i = 0; i < 20; i++) {
    for (int j = 1 << i; j < 2 << i; j++) {
      popcount[j] = popcount[j - (1 << i)] + 1;
    }
  }
  for (int i = 1; i <= 20; i++) {
    for (int j = 0; j < (1 << i); j++)
      popcnts[i].push_back(make_pair(popcount[j], j));
    sort(popcnts[i].begin(), popcnts[i].end());
  }
  while (scanf("%d%d", &n, &U), n) {
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &a[i], &b[i]);
      r[i] = 0;
      for (int j = 0; j < b[i]; j++) {
        scanf("%d", &c);
        r[i] |= 1 << c;
      }
    }
    sum[0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 1 << i; j < 2 << i; j++) {
        sum[j] = sum[j - (1 << i)] + a[i];
      }
    }
    int minx = 10000000;
    for (int h = 0; h < (1 << n); h++) {
      int i = popcnts[n][h].second;
      if (sum[i] < U)
        continue;
      bool flag = false;
      int G = (1 << n) - i - 1;
      for (int j = 0; j < n; j++) {
        if ((i & (1 << j)) && (G & r[j])) {
          flag = true;
          break;
        }
      }
      if (!flag) {
        minx = min(minx, popcount[i]);
        break;
      }
    }
    cout << minx << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
int n, U, a[20], b[20], c, r[20], popcount[1048588], sum[1048588];
int main() {
  for (int i = 0; i < 20; i++) {
    for (int j = 1 << i; j < 2 << i; j++) {
      popcount[j] = popcount[j - (1 << i)] + 1;
    }
  }
  while (scanf("%d%d", &n, &U), n) {
    for (int i = 0; i < n; i++) {
      scanf("%d%d", &a[i], &b[i]);
      r[i] = 0;
      for (int j = 0; j < b[i]; j++) {
        scanf("%d", &c);
        r[i] |= 1 << c;
      }
    }
    sum[0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 1 << i; j < 2 << i; j++) {
        sum[j] = sum[j - (1 << i)] + a[i];
      }
    }
    int minx = 10000000;
    for (int i = 0; i < (1 << n); i++) {
      if (sum[i] < U)
        continue;
      bool flag = false;
      int G = (1 << n) - 1;
      G -= i;
      for (int j = 0; j < n; j++) {
        if ((i & (1 << j)) && (G & r[j])) {
          flag = true;
          break;
        }
      }
      if (!flag)
        minx = min(minx, popcount[i]);
    }
    cout << minx << endl;
  }
  return 0;
}
Problem p00618 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bitset>
#include <map>
#include <stdio.h>
// BitCount
int F(int x) {
  int r = 0;
  for (; x; x &= x - 1)
    ++r;
  return r;
}
std::bitset<1 << 20> f;
int dp[1 << 20];
int main() {
  int n, u, k, r, i, j, x, y, z;
  while (scanf("%d%d", &n, &u), n) {
    f.reset();
    std::pair<int, int> a[20];
    for (i = 0; i < n; ++i) {
      scanf("%d%d", &a[i].first, &k);
      if (k) {
        while (k--) {
          scanf("%d", &r);
          a[i].second |= (1 << r);
        }
      } else
        f[1 << i] = 1, dp[1 << i] = a[i].first;
    }
    for (i = 1; i <= n; ++i) {
      z = (1 << i) - 1;
      while (z < (1 << n)) {
        if (f[z]) {
          if (dp[z] >= u) {
            printf("%d\\n", F(z));
            goto E;
          }
          for (j = 0; j < n; ++j)
            if (!(z & (1 << j)) && (z & a[j].second) == a[j].second) {
              f[z | (1 << j)] = 1;
              dp[z | (1 << j)] = dp[z] + a[j].first;
            }
        }
        x = z & -z;
        y = z + x;
        z = ((z & ~y) / x / 2) | y;
      }
    }
  E:;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cstdio>
#include <map>
using namespace std;
int F(int x) {
  x = (x & 0x55555555) + (x >> 1 & 0x55555555);
  x = (x & 0x33333333) + (x >> 2 & 0x33333333);
  x = (x & 0x0f0f0f0f) + (x >> 4 & 0x0f0f0f0f);
  x = (x & 0x00ff00ff) + (x >> 8 & 0x00ff00ff);
  return (x & 0x0000ffff) + (x >> 16 & 0x0000ffff);
}
std::bitset<1 << 20> f;
int dp[1 << 20];
int main() {
  int n, u, k, r, i, j;
  while (scanf("%d%d", &n, &u), n) {
    pair<int, int> a[20];
    for (i = 0; i < n; ++i) {
      for (scanf("%d%d", &a[i].first, &k); k--;) {
        scanf("%d", &r);
        a[i].second |= (1 << r);
      }
    }
    f.reset();
    f[0] = 1;
    r = 99;
    dp[0] = 0;
    for (i = 0; i < (1 << n); ++i) {
      if (!f[i])
        continue;
      if (dp[i] >= u)
        r = min(r, F(i));
      for (j = 0; j < n; ++j) {
        if ((i & (a[j].second)) == a[j].second) {
          f[i | (1 << j)] = 1;
          dp[i | (1 << j)] = dp[i] + a[j].first;
        }
      }
    }
    printf("%d\\n", r);
  }
}
Problem p00620 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef pair<int, int> P;

int n;
int t[10][10];
long long v;
map<long long, bool> m;
vector<P> can;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};

bool rec(int id);

bool dfs(int id, int y, int x, int sum) {
  if (sum == 0)
    return rec(id + 1);
  if (sum > 0)
    return false;

  for (int i = 0; i < 4; i++) {
    int ty = y + dy[i], tx = x + dx[i];
    if (ty < 0 || tx < 0 || n <= ty || n <= tx)
      continue;
    if ((v >> (ty * n + tx)) & 1LL || t[ty][tx] < 0)
      continue;
    v |= 1LL << (ty * n + tx);
    if (dfs(id, ty, tx, sum + t[ty][tx]))
      return true;
    v -= 1LL << (ty * n + tx);
  }
  return false;
}

bool rec(int id) {
  if (id == can.size())
    return true;
  if (m[v])
    return false;
  m[v] = true;
  int y = can[id].first, x = can[id].second;
  v |= 1LL << y * n + x;
  return dfs(id, y, x, t[y][x]);
}

int main() {
  while (cin >> n, n) {
    int sum = 0;
    can.clear();
    m.clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        cin >> t[i][j];
        sum += t[i][j];
        if (t[i][j] < 0)
          can.push_back(P(i, j));
      }
    v = 0LL;

    cout << ((!sum && rec(0)) ? "YES\\n" : "NO\\n");
  }
}

Error: TLE

Fixed submission:

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

typedef pair<int, int> P;

int n;
int t[10][10];
bool v[10][10];
vector<P> can;
int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};

bool rec(int id);

bool dfs(int id, int y, int x, int sum) {
  if (sum == 0)
    return rec(id + 1);
  if (sum > 0)
    return false;

  for (int i = 0; i < 4; i++) {
    int ty = y + dy[i], tx = x + dx[i];
    if (ty < 0 || tx < 0 || n <= ty || n <= tx)
      continue;
    if (v[ty][tx])
      continue;
    v[ty][tx] = true;
    if (dfs(id, ty, tx, sum + t[ty][tx]))
      return true;
    v[ty][tx] = false;
  }
  return false;
}

bool rec(int id) {
  if (id == can.size())
    return true;
  int y = can[id].first, x = can[id].second;
  v[y][x] = true;
  return dfs(id, y, x, t[y][x]);
}

int main() {
  while (cin >> n, n) {
    int sum = 0;
    can.clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) {
        cin >> t[i][j];
        sum += t[i][j];
        if (t[i][j] < 0)
          can.push_back(P(i, j));
        v[i][j] = false;
      }

    cout << ((!sum && rec(0)) ? "YES\\n" : "NO\\n");
  }
}
Problem p00620 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>

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

using namespace std;

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

int n, B[8][8];
bool dfs(int y, int x, bool vis[8][8], int sum, int type) {
  if (type == 0) {
    vis[y][x] = true;
    if (dfs(y, x, vis, B[y][x], 1))
      return true;
    vis[y][x] = false;
    return false;
  } else {
    if (sum > 0)
      return false;
    if (sum == 0) {
      int yy = -1, xx;
      bool end = true;
      rep(i, n) rep(j, n) {
        if (!vis[i][j])
          end = false;
        if (!vis[i][j] && B[i][j] < 0) {
          if (yy == -1 || B[i][j] < B[yy][xx])
            yy = i, xx = j;
        }
      }
      if (end)
        return true;
      return dfs(yy, xx, vis, 0, 0);
    }
    rep(k, 4) {
      int yy = y + dy[k], xx = x + dx[k];
      if (0 <= yy && yy < n && 0 <= xx && xx < n && !vis[yy][xx] &&
          B[yy][xx] > 0) {
        vis[yy][xx] = true;
        if (dfs(yy, xx, vis, sum + B[yy][xx], 1))
          return true;
        vis[yy][xx] = false;
      }
    }
    return false;
  }
}

int main() {
  for (; scanf("%d", &n), n;) {
    int sy = -1, sx;
    rep(i, n) rep(j, n) {
      scanf("%d", B[i] + j);
      if (B[i][j] < 0) {
        if (sy == -1 || B[i][j] < B[sy][sx])
          sy = i, sx = j;
      }
    }

    bool vis[8][8] = {};
    puts(dfs(sy, sx, vis, 0, 0) ? "YES" : "NO");
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

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

using namespace std;

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

int n, B[8][8];
bool dfs(int y, int x, bool vis[8][8], int sum, int type) {
  if (type == 0) {
    vis[y][x] = true;
    if (dfs(y, x, vis, B[y][x], 1))
      return true;
    vis[y][x] = false;
    return false;
  } else {
    if (sum > 0)
      return false;
    if (sum == 0) {
      int yy = -1, xx;
      bool end = true;
      rep(i, n) rep(j, n) {
        if (!vis[i][j])
          end = false;
        if (!vis[i][j] && B[i][j] < 0) {
          if (yy == -1 || B[yy][xx] < B[i][j])
            yy = i, xx = j;
        }
      }
      if (end)
        return true;
      return dfs(yy, xx, vis, 0, 0);
    }
    rep(k, 4) {
      int yy = y + dy[k], xx = x + dx[k];
      if (0 <= yy && yy < n && 0 <= xx && xx < n && !vis[yy][xx] &&
          B[yy][xx] > 0) {
        vis[yy][xx] = true;
        if (dfs(yy, xx, vis, sum + B[yy][xx], 1))
          return true;
        vis[yy][xx] = false;
      }
    }
    return false;
  }
}

int main() {
  for (; scanf("%d", &n), n;) {
    int sy = -1, sx;
    rep(i, n) rep(j, n) {
      scanf("%d", B[i] + j);
      if (B[i][j] < 0) {
        if (sy == -1 || B[sy][sx] < B[i][j])
          sy = i, sx = j;
      }
    }

    bool vis[8][8] = {};
    puts(dfs(sy, sx, vis, 0, 0) ? "YES" : "NO");
  }

  return 0;
}