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

#include <iostream>
#include <string>

using namespace std;

int h, w;
int field[100][100];

void deleteField(int x, int y, int fluit);
void Delete(int x, int y, int f);

int main() {
  int ans;
  string str;

  /*??\\???*/
  cin >> h >> w;

  ans = 0;
  while (h && w) {
    for (int i = 0; i < h; i++) {
      /*????????¨?????\\????????´??°???2?¬???????????????????*/
      cin >> str;

      for (int j = 0; j < w; j++) {
        switch (str[j]) {
        case '@':
          field[i][j] = 1;
          break;
        case '#':
          field[i][j] = 2;
          break;
        default:
          field[i][j] = 3;
        }
      }
    }

    /*???????????????????????????????????????????????????????????????????¶????*/
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (field[i][j] != 0) {
          ans++;
          deleteField(j, i, field[i][j]);

          /* Debug //
          for (int k = 0; k < h; k++) {
                  for (int l = 0; l < w; l++) {
                          cout << field[k][l];
                  }
                  cout << endl;
          }
          cout << "-----\\n";

          getchar();
          //////////*/
        }
      }
    }

    cout << ans << endl;

    ans = 0;
    cin >> h >> w;
  }
}

void deleteField(int x, int y, int fruit) {
  field[y][x] = 0;

  if (x - 1 >= 0) {
    Delete(x - 1, y, fruit);
  }
  if (x + 1 < w) {
    Delete(x + 1, y, fruit);
  }
  if (y - 1 >= 0) {
    Delete(x, y - 1, fruit);
  }
  if (y + 1 < h) {
    Delete(x, y + 1, fruit);
  }
}

void Delete(int x, int y, int f) {
  if (field[y][x] == f) {
    deleteField(x, y, f);
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>

using namespace std;

int fluit[100][100] = {0};

void blow(int i, int j, int w, int h, int n) {
  queue<pair<int, int>> st;

  st.push(make_pair(i, j));

  while (1) {
    if (st.empty())
      break;
    int x = st.front().second, y = st.front().first;

    fluit[y][x] = 0;

    if (y - 1 >= 0) {
      if (fluit[y - 1][x] == n)
        st.push(make_pair(y - 1, x));
    }
    if (y + 1 < h) {
      if (fluit[y + 1][x] == n)
        st.push(make_pair(y + 1, x));
    }
    if (x - 1 >= 0) {
      if (fluit[y][x - 1] == n)
        st.push(make_pair(y, x - 1));
    }
    if (x + 1 < w) {
      if (fluit[y][x + 1] == n)
        st.push(make_pair(y, x + 1));
    }

    st.pop();
  }
}

int main() {
  int h, w, cont = 0;
  string str;

  while (cin >> h >> w) {
    if (h == 0 && w == 0)
      break;

    for (int i = 0; i < h; i++) {
      cin >> str;

      for (int j = 0; j < w; j++) {
        if (str[j] == '@')
          fluit[i][j] = 1;
        else if (str[j] == '#')
          fluit[i][j] = 2;
        else if (str[j] == '*')
          fluit[i][j] = 3;
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (fluit[i][j]) {
          cont++;
          blow(i, j, w, h, fluit[i][j]);
        }
      }
    }

    cout << cont << endl;
  }

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 110 //??方式?宏是一个好?法
int target;
int a;
int test[MAX][MAX] = {0};
int length, width;
int co[4][2] = {{0, -1}, {0, 1}, {-1, 0}, {1, 0}};
int check(int i, int x, int y) {
  if (((x + co[i][0]) >= 0 && (x + co[i][0]) < length) &&
      ((y + co[i][1]) >= 0 && (y + co[i][1]) < width))
    return 1;
  else
    return 0;
}
void run(int x, int y, const int style) {
  for (int i = 0; i < 4; i++) {
    if (check(i, x, y)) {
      if (test[x + co[i][0]][y + co[i][1]] == style) {
        if (test[x + co[i][0]][y + co[i][1]] > 0) {
          test[x + co[i][0]][y + co[i][1]] *= -1; // 火?的map233333

          run(x + co[i][0], y + co[i][1], style);
        }
      }
    }
    // 如果是-1?明之前走?就不用走了，如果是2就没法走了
  }
}
int main() {
  char str[MAX];
  scanf("%d %d", &width, &length);
  while (length != 0 && width != 0) {
    memset(&test[0][0], 0, sizeof(test));
    target = 0;
    for (int j = 0; j < width; j++) {
      scanf("%s", str);
      for (int i = 0; i < length; i++) {
        if (str[i] == '#') {
          test[i][j] = 2;
        } else if (str[i] == '*') {
          test[i][j] = 1;
        } else if (str[i] == '@') {
          test[i][j] = 3;
        }
      }
    }
    for (int j = 0; j < width; j++) {
      for (int i = 0; i < length; i++) {
        if (test[i][j] > 0) {

          run(i, j, test[i][j]); // 走到?就?明走完了一片
          target++;
          // 一株?
        }
      }
    }

    printf("%d\\n", target);
    scanf("%d %d", &width, &length); // 注意???
  }
}

Error: 0

Fixed submission:

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

using namespace std;

#define ll long long
#define clr(a) memset(a, 0, sizeof(a))
char maps[25][25];
int vis[25][25];
int dir[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
int num, n, m;
void dfs(int x, int y, char c) {
  if (maps[x][y] != c || x < 0 || x >= n || y < 0 || y >= m || vis[x][y] == 1)
    return;
  vis[x][y] = 1;
  num++;
  for (int i = 0; i < 4; i++) {
    dfs(x + dir[i][0], y + dir[i][1], c);
  }
}
int main() {
  while (cin >> n >> m && n != 0 && m != 0) {
    int cnt = 0;
    clr(maps);
    clr(vis);
    num = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> maps[i][j];
      }
      //  getchar();
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        num = 0;
        dfs(i, j, maps[i][j]);
        if (num != 0)
          cnt++;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00119 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair

using namespace std;

int main() {
  int n, m;
  cin >> n >> m;

  int a[21];
  memset(a, 0, sizeof(a));

  bool b[21];
  rep(i, 21) b[i] = true;

  vector<vector<int>> v(21);

  rep(i, m) {
    int x, y;
    cin >> x >> y;

    x--;
    y--;
    a[y]++;
    v[x].pb(y);
  }

  int count = 0;
  while (count != n) {
    rep(i, n) {
      if (a[i] == 0 && b[i]) {
        cout << i + 1 << endl;
        b[i] = false;
        count++;

        rep(j, v[i].size()) a[v[i][j]]--;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = 0; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair

using namespace std;

int main() {

  int n, m;
  cin >> n >> m;

  vector<vector<char>> v;
  v.resize(n);
  map<char, int> ma;

  rep(i, n) {
    char c = '1' + i;
    ma[c] = 0;
  }

  bool isSelected[21];
  rep(i, 21) isSelected[i] = false;

  int count = 0;

  rep(i, m) {
    char c1, c2;
    cin >> c1 >> c2;

    v[c1 - '1'].pb(c2);
    ma[c2]++;
  }

  while (count != n) {
    rep(i, n) {
      char c = '1' + i;
      if (ma[c] == 0) {
        if (!isSelected[i]) {
          cout << c << endl;

          rep(j, v[i].size()) { ma[v[i][j]]--; }

          isSelected[i] = true;
          count++;
        } else {
          continue;
        }
      }
    }
  }
  return 0;
}
Problem p00119 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> m >> n;

  vector<int> per;
  vector<vector<int>> G;
  G.resize(m);
  bool used[m];
  per.resize(m);
  for (int i = 0; i < m; i++)
    per[i] = 0, used[i] = false;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;

    x--, y--;
    per[y]++;
    G[x].push_back(y);
  }
  used[1] = true;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      if (used[j])
        continue;
      if (!per[j])
        cout << j + 1 << endl;
    }
    vector<int> pre = per;

    for (int j = 0; j < m; j++) {
      if (used[j])
        continue;
      if (!pre[j]) {
        used[j] = true;
        for (int k = 0; k < G[j].size(); k++)
          per[G[j][k]]--;
      }
    }
  }
  cout << 2 << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main() {
  int n, m;
  cin >> m >> n;
  cerr << m << " " << n << endl;
  vector<int> per;
  vector<vector<int>> G;
  G.resize(m);
  bool used[m];
  per.resize(m);
  for (int i = 0; i < m; i++)
    per[i] = 0, used[i] = false;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    cerr << x << " " << y << endl;
    x--, y--;
    per[y]++;
    G[x].push_back(y);
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (used[j])
        continue;
      if (!per[j]) {
        cout << j + 1 << endl;
        used[j] = true;
        for (int k = 0; k < G[j].size(); k++)
          per[G[j][k]]--;
        break;
      }
    }
  }
  return 0;
}
Problem p00119 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <stack>
#include <stdio.h>

using namespace std;

struct Info {
  int id, num;
};

bool isContain(int array[], int size, int id) {
  for (int i = 0; i < size; i++) {
    if (array[i] == id)
      return true;
  }
  return false;
}

int main() {

  int member_num, n, table[21][21], num_of_smaller[21], first, second,
      fixed[21], fixed_index = 0, label = 100000, data_dealt_num = 0;

  scanf("%d", &member_num);
  Info member[member_num];
  stack<int> tmp_store;

  for (int i = 1; i <= member_num; i++) {
    for (int k = 1; k <= member_num; k++)
      table[i][k] = 0;
    num_of_smaller[i] = 0;
    member[i - 1].id = i;
    fixed[i - 1] = 0;
  }

  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &first, &second);
    table[first][second] = 1;
    num_of_smaller[first]++;
  }

  for (int i = 1; i <= member_num; i++) {
    if (num_of_smaller[i] == 0) {
      fixed[fixed_index++] = i;
      member[i - 1].num = label--;
      break;
    }
  }

  while (data_dealt_num < n) {

    for (int i = 1; i <= member_num; i++) {
      if (isContain(fixed, fixed_index, i) == false) {
        bool flg = true;
        for (int k = 1; k <= member_num; k++) {
          if (table[i][k] == 1 && isContain(fixed, fixed_index, k) == false) {
            flg = false;
            break;
          }
        }

        if (flg) {
          tmp_store.push(i);
          data_dealt_num += num_of_smaller[i];
        }
      }
    }

    while (!tmp_store.empty()) {
      fixed[fixed_index++] = tmp_store.top();
      member[tmp_store.top() - 1].num = label;
      tmp_store.pop();
    }
    label--;
  }

  for (int i = 1; i <= member_num - 1; i++) {
    for (int k = member_num - 1; k >= i; k--) {
      if (member[k].num < member[k - 1].num)
        swap(member[k], member[k - 1]);
    }
  }

  for (int i = 0; i < member_num; i++) {
    printf("%d\\n", member[i].id);
  }

  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stdio.h>

using namespace std;

struct Info {
  int id, num;
};

struct ORDER {
  int fast, slow;
};

int main() {

  int m, n;

  scanf("%d", &m);
  Info member[m];

  for (int i = 0; i < m; i++) {
    member[i].id = i + 1;
    member[i].num = 10000;
  }

  scanf("%d", &n);
  ORDER orders[n];

  for (int i = 0; i < n; i++) {
    scanf("%d %d", &orders[i].fast, &orders[i].slow);
  }

  for (int i = 1; i <= n - 1; i++) {
    for (int k = n - 1; k >= i; k--) {
      if (orders[k].slow < orders[k - 1].slow)
        swap(orders[k],
             orders[k - 1]); //????????¨?????????????????£?¶??????????
    }
  }

  for (int i = 0; i < m; i++) {
    member[orders[i].fast - 1].num = member[orders[i].slow - 1].num - 1;
  }

  for (int i = 1; i <= m - 1; i++) {
    for (int k = m - 1; k >= i; k--) {
      if (member[k].num < member[k - 1].num)
        swap(member[k], member[k - 1]);
    }
  }

  for (int i = 0; i < m; i++) {
    printf("%d\\n", member[i].id);
  }

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

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

#define mp(x, y) make_pair(x, y)
#define X first
#define Y second

using namespace std;

typedef pair<int, int> pii;

int main(void) {
  int m;

  while (cin >> m) {
    vector<pii> v(m);

    for (int i = 0; i < m; ++i) {
      v[i].X = 0;
      v[i].Y = i + 1;
    }

    int n;

    cin >> n;

    vector<pii> t(n);

    for (int i = 0; i < n; ++i) {
      cin >> t[i].X >> t[i].Y;
      --t[i].X;
      --t[i].Y;
    }

    bool hit = true;

    while (hit) {
      hit = false;

      for (int i = 0; i < n; ++i) {
        pii p = t[i];
        if (v[p.X].X < v[p.Y].X + 1) {
          v[p.X].X = v[p.Y].X + 1;
          hit = true;
        }
      }
    }

    sort(v.begin(), v.end(), greater<pii>());

    for (int i = 0; i < m; ++i) {
      cout << v.at(i).Y << endl;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

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

#define mp(x, y) make_pair(x, y)
#define X first
#define Y second

using namespace std;

typedef pair<int, int> pii;

int main(void) {
  int m;

  while (cin >> m) {
    vector<pii> v(m);

    for (int i = 0; i < m; ++i) {
      v[i].X = 0;
      v[i].Y = i + 1;
    }

    int n;

    cin >> n;

    vector<pii> t(n);

    for (int i = 0; i < n; ++i) {
      cin >> t[i].X >> t[i].Y;
      --t[i].X;
      --t[i].Y;
    }

    bool hit = true;

    while (hit) {
      hit = false;

      for (int i = 0; i < m; ++i) {
        pii p = t[i];
        if (v[p.X].X < v[p.Y].X + 1) {
          v[p.X].X = v[p.Y].X + 1;
          hit = true;
        }
      }
    }

    sort(v.begin(), v.end(), greater<pii>());

    for (int i = 0; i < m; ++i) {
      cout << v.at(i).Y << endl;
    }
  }
  return 0;
}
Problem p00120 (C++) — buggy submission, verdict: Runtime Error

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

#define EPS 1e-8

double calcR(double r1, double r2) {
  return sqrt((r1 + r2) * (r1 + r2) - (r1 - r2) * (r1 - r2));
}

class Trl {
public:
  int p, h;
  double c;
  Trl(int p, int h, double c) : p(p), h(h), c(c) {}

  bool operator<(const Trl &t) const { return c > t.c; }
};

bool v[13][1 << 12];

int main() {
  double cst[11][11] = {0};
  for (int i = 0; i <= 10; i++)
    cst[0][i] = 2 * i;

  for (int i = 3; i <= 10; i++)
    for (int j = 3; j <= 10; j++)
      cst[i][j] = calcR(i, j) - i + j;

  int L;
  while (cin >> L) {
    int cake[13], p = 0;
    cake[0] = 0;
    while (cin.peek() != '\\n') {
      cin >> cake[p + 1];
      p++;
    }

    memset(v, 0, sizeof(v));
    priority_queue<Trl> q;
    q.push(Trl(0, 0, 0));
    bool g = false;
    while (!q.empty()) {
      Trl t = q.top();
      q.pop();
      if (v[t.p][t.h])
        continue;
      v[t.p][t.h] = 1;

      if (t.h == (1 << p) - 1) {
        g = true;
        break;
      }

      for (int i = 0; i < p; i++) {
        if (t.h & (1 << i))
          continue;
        double tc = t.c + cst[cake[t.p]][cake[i + 1]];
        int th = t.h + (1 << i);
        if (v[i + 1][th])
          continue;
        if (tc > L + EPS)
          continue;

        q.push(Trl(i + 1, th, tc));
      }
    }
    cout << (g ? "OK" : "NA") << endl;
  }
}

Error: 0

Fixed submission:

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

#define EPS 1e-8

double calcR(double r1, double r2) {
  return sqrt((r1 + r2) * (r1 + r2) - (r1 - r2) * (r1 - r2));
}

class Trl {
public:
  int p, h;
  double c;
  Trl(int p, int h, double c) : p(p), h(h), c(c) {}

  bool operator<(const Trl &t) const { return c > t.c; }
};

int main() {
  double cst[11][11] = {0};
  for (int i = 0; i <= 10; i++)
    cst[0][i] = 2 * i;

  for (int i = 3; i <= 10; i++)
    for (int j = 3; j <= 10; j++)
      cst[i][j] = calcR(i, j) - i + j;

  int L;
  while (cin >> L) {
    int cake[10], p = 0;
    while (cin.peek() != '\\n')
      cin >> cake[p++];

    priority_queue<Trl> q;
    q.push(Trl(0, 0, 0));
    bool g = false;
    bool v[12][1 << 12] = {0};
    while (!q.empty()) {
      Trl t = q.top();
      q.pop();
      if (v[t.p][t.h])
        continue;
      v[t.p][t.h] = 1;

      if (t.h == (1 << p) - 1) {
        g = true;
        break;
      }

      for (int i = 0; i < p; i++) {
        if (t.h & (1 << i))
          continue;
        double tc = t.c + cst[cake[t.p]][cake[i]];
        int th = t.h + (1 << i);
        if (v[i][th])
          continue;
        if (tc > L + EPS)
          continue;

        q.push(Trl(i, th, tc));
      }
    }
    cout << (g ? "OK" : "NA") << endl;
  }
}
Problem p00120 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string str;
  while (getline(cin, str)) {
    vector<double> vec;
    stringstream ss(str);
    int wid, n = 0;
    ss >> wid;
    while (!ss.eof()) {
      double s;
      ss >> s;
      vec.push_back(s);
      n++;
    }
    double dp[1 << n][n];
    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        dp[i][j] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      dp[(1 << n) - 1 - (1 << i)][i] = vec[i];
    }
    for (int i = (1 << n) - 2; i > -1; i--) {
      for (int j = 0; j < n; j++) {
        if (!(i & (1 << j))) {
          for (int k = 0; k < n; k++) {
            dp[i][j] =
                min(dp[i][j], dp[i | (1 << j)][k] + 2 * sqrt(vec[j] * vec[k]));
          }
        }
      }
    }
    double ans = INF;
    for (int i = 0; i < n; i++) {
      ans = min(ans, dp[0][i] + vec[i]);
    }
    if (ans <= wid)
      cout << "OK" << endl;
    else
      cout << "NA" << endl;
  }
}

Error: WA

Fixed submission:

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

int main() {
  string str;
  while (getline(cin, str)) {
    vector<int> vec;
    stringstream ss(str);
    int wid, n = 0;
    ss >> wid;
    while (!ss.eof()) {
      int s;
      ss >> s;
      vec.push_back(s);
      n++;
    }
    double dp[111][111];
    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        dp[i][j] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      dp[(1 << n) - 1 - (1 << i)][i] = vec[i];
    }
    for (int i = (1 << n) - 2; i > -1; i--) {
      for (int j = 0; j < n; j++) {
        if (!(i & (1 << j))) {
          for (int k = 0; k < n; k++) {
            dp[i][j] =
                min(dp[i][j], dp[i | (1 << j)][k] + 2 * sqrt(vec[j] * vec[k]));
          }
        }
      }
    }
    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < n; j++) {
        cout << dp[i][j] << "  ";
      }
      cout << endl;
    }
    double ans = INF;
    for (int i = 0; i < n; i++) {
      ans = min(ans, dp[0][i] + vec[i]);
    }
    cout << ans << endl;
    if (ans <= wid)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  // cout << (1<<3) << endl;
}
Problem p00120 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <list>
#include <vector>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define pf push_front
int main() {
  int a[12], b[12];
  char buf[256];
  while (fgets(buf, 256, stdin)) {
    char *p = strtok(buf, " ");
    int L = atoi(p);
    int n = 0;
    while (p = strtok(NULL, " "))
      a[n++] = atoi(p);
    std::sort(a, a + n);
    bool ok = false;

    b[n / 2] = a[0];
    int i = 0, j = 1, k = 1, l = 1;
    while (i < n) {
      if (i % 4 == 0) {
        b[n / 2 + k] = a[n - l];
        l++;
      }
      if (i % 4 == 1) {
        b[n / 2 - k] = a[n - l];
        l++;
        k++;
      }
      if (i % 4 == 2) {
        b[n / 2 + k] = a[j];
        j++;
      }
      if (i % 4 == 3) {
        b[n / 2 - k] = a[j];
        j++;
        k++;
      }
      i++;
    }
    double t = b[0];
    REP(i, 1, n) t += 2 * sqrt(b[i - 1] * b[i]);
    t += b[n - 1];
    if (!(t > L))
      ok = true;

    /*
    do {
            double t = a[0];
            REP(i, 1, n) t += 2 * sqrt(a[i-1] * a[i]);
            t += a[n-1];
            if (!(t > L)) { ok = true; break; }
    } while (std::next_permutation(a, a+n));
    */
    puts(ok ? "OK" : "NA");
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) REP(i, 0, n)
int main() {
  int a[12];
  char b[256];
  while (fgets(b, 256, stdin)) {
    char *p = strtok(b, " ");
    int L = atoi(p);
    int n = 0;
    while (p = strtok(NULL, " "))
      a[n++] = atoi(p);

    bool ok = false;
    std::sort(a, a + n);
    do {
      double t = a[0];
      REP(i, 1, n) {
        int r1 = a[i - 1];
        int r2 = a[i];
        if (r1 < r2)
          std::swap(r1, r2);
        t += 2 * sqrt(r1 * r2);
      }
      t += a[n - 1];
      if (!(t > L)) {
        ok = true;
        break;
      }
    } while (std::next_permutation(a, a + n));

    puts(ok ? "OK" : "NA");
  }
  return 0;
}