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

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

vector<string> ans;
vector<bool> appear(30);

void enc(string &str, int n, string &itms) {
  if (n < (int)str.length()) {
    if (itms[n] == 'z') {
      enc(str, n + 1, itms);
    } else {
      if (appear[itms[n] - 'a']) {
        enc(str, n + 1, itms);
      }
      if (!appear[itms[n] - 'a' + 1]) {
        itms[n]++;
        appear[itms[n] - 'a'] = true;
        enc(str, n + 1, itms);
        appear[itms[n] - 'a'] = false;
        itms[n]--;
      }
    }

  } else {
    string tmp = itms;
    for (char c = 'b'; c <= 'z'; c++) {
      for (int j = 0; j < (int)tmp.length(); j++) {
        if (tmp[j] == c) {
          tmp[j]--;
          break;
        }
      }
    }
    if (tmp == str) {
      ans.push_back(itms);
    }
  }
}

int main() {
  appear[0] = true;
  while (1) {
    string str;
    cin >> str;
    if (str == "#")
      break;

    ans.clear();
    fill(appear.begin() + 1, appear.end(), false);
    string tmp = str;
    enc(str, 0, tmp);
    sort(ans.begin(), ans.end());

    cout << ans.size() << endl;
    if ((int)ans.size() <= 10) {
      for (int i = 0; i < (int)ans.size(); i++) {
        cout << ans[i] << endl;
      }
    } else {
      for (int i = 0; i < 5; i++) {
        cout << ans[i] << endl;
      }
      for (int i = ans.size() - 5; i < (int)ans.size(); i++) {
        cout << ans[i] << endl;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

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

vector<string> candd;

void enc(string &str, int n, string &itms) {
  if (n < (int)str.length()) {
    if (itms[n] != 'z') {
      itms[n]++;
      enc(str, n + 1, itms);
      itms[n]--;
    }
    enc(str, n + 1, itms);
  } else {
    string tmp = itms;
    for (char c = 'b'; c <= 'z'; c++) {
      for (int j = 0; j < (int)tmp.length(); j++) {
        if (tmp[j] == c) {
          tmp[j]--;
          break;
        }
      }
    }
    if (tmp == str) {
      candd.push_back(itms);
    }
  }
}

int main() {
  while (1) {
    string str;
    cin >> str;
    if (str == "#")
      break;

    candd.clear();
    string tmp = str;
    enc(str, 0, tmp);
    sort(candd.begin(), candd.end());
    cout << candd.size() << endl;
    if ((int)candd.size() <= 10) {
      for (int i = 0; i < (int)candd.size(); i++) {
        cout << candd[i] << endl;
      }
    } else {
      for (int i = 0; i < 5; i++) {
        cout << candd[i] << endl;
      }
      for (int i = candd.size() - 5; i < (int)candd.size(); i++) {
        cout << candd[i] << endl;
      }
    }
  }
  return 0;
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n;
set<vector<char>> st;
string str;
vector<char> res;
bool f[30];

void print(vector<char> vc) {
  for (int i = 0; i < vc.size(); i++)
    cout << vc[i];
  cout << endl;
}

void func(int pos) {
  if (pos >= n) {
    st.insert(res);

  } else {
    if (str[pos] + 1 <= 'z' && !f[str[pos] - 'a']) {
      res.push_back(str[pos] + 1);
      f[str[pos] - 'a'] = true;
      func(pos + 1);
      f[str[pos] - 'a'] = false;
      res.pop_back();
    }

    if (str[pos] == 'a' || f[str[pos] - 1 - 'a']) {
      res.push_back(str[pos]);
      func(pos + 1);
      res.pop_back();
    }
  }
}

void solve() {
  n = str.size();
  st.clear();

  res.clear();
  fill(f, f + 30, false);
  func(0);
  /*
  for(int i=0;i<(1<<n);i++){
    string strf = "";
    for(int j=0;j<n;j++){
      if((i & (1 << j)) && str[j] + 1 <= 'z') strf += str[j] + 1;
      else if(!(i & (1 << j))) strf += str[j];
      else break;
    }
    if(strf.size() != n) continue;

    string strf2 = strf;
    bool f[30];
    fill(f,f+30,false);
    for(int j=0;j<n;j++){
      if(strf2[j] != 'a' && !f[strf2[j]-1-'a']){
        f[strf2[j]-1-'a'] = true;
        strf2[j]--;
      }
    }
    if(strf2 == str) st.insert(strf);
  }
  */

  cout << st.size() << endl;
  if (st.size() <= 10) {
    for (set<vector<char>>::iterator it = st.begin(); it != st.end(); it++) {
      print(*it);
    }
  } else {
    set<vector<char>>::iterator it = st.begin();
    for (int i = 0; i < 5; i++, it++)
      print(*it);
    it = st.end();
    for (int i = 0; i < 5; i++)
      it--;
    for (int i = 0; i < 5; i++, it++)
      print(*it);
  }
}

int main() {
  while (cin >> str && str != "#")
    solve();
}

Error: TLE

Fixed submission:

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

int n;
set<string> st;
string str;

void solve() {
  n = str.size();
  st.clear();

  for (int i = 0; i < (1 << n); i++) {
    string strf = "";
    for (int j = 0; j < n; j++) {
      if ((i & (1 << j)) && str[j] + 1 <= 'z')
        strf += str[j] + 1;
      else if (!(i & (1 << j)))
        strf += str[j];
      else
        break;
    }
    if (strf.size() != n)
      continue;

    string strf2 = strf;
    bool f[30];
    fill(f, f + 30, false);
    for (int j = 0; j < n; j++) {
      if (strf2[j] != 'a' && !f[strf2[j] - 1 - 'a']) {
        f[strf2[j] - 1 - 'a'] = true;
        strf2[j]--;
      }
    }
    if (strf2 == str)
      st.insert(strf);
  }

  cout << st.size() << endl;
  if (st.size() <= 10) {
    for (set<string>::iterator it = st.begin(); it != st.end(); it++) {
      cout << *it << endl;
    }
  } else {
    set<string>::iterator it = st.begin();
    for (int i = 0; i < 5; i++, it++)
      cout << *it << endl;
    it = st.end();
    for (int i = 0; i < 5; i++)
      it--;
    for (int i = 0; i < 5; i++, it++)
      cout << *it << endl;
  }
}

int main() {
  while (cin >> str && str != "#")
    solve();
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

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

string src;
inline bool enc(string s) {
  int done;
  done = 1;
  for (int i = 0; i < s.size(); i++) {
    if (!(done >> (s[i] - 'a') & 1)) {
      done |= 1 << (s[i] - 'a');
      s[i]--;
    }
    if (src[i] != s[i])
      return false;
  }
  return true;
}

int main() {
  while (cin >> src && src != "#") {
    vector<string> v;
    for (int i = 0; i < (1 << src.size()); i++) {
      string dst = src;
      for (int j = 0; j < src.size(); j++)
        if (dst[j] != 'z' && (i >> j & 1)) {
          dst[j]++;
        }
      if (enc(dst)) {
        v.push_back(dst);
      }
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    cout << v.size() << endl;
    if (v.size() <= 10) {
      for (auto x : v)
        cout << x << endl;
    } else {
      for (int j = 0; j < 5; j++)
        cout << v[j] << endl;
      for (int j = 0; j < 5; j++)
        cout << v[v.size() - 5 + j] << endl;
    }
  }
}

Error: TLE

Fixed submission:

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

string src;
inline bool enc(string s) {
  bool chk[26] = {};
  chk[0] = 1;
  for (int i = 0; i < s.size(); i++) {
    if (!chk[s[i] - 'a']) {
      chk[s[i] - 'a'] = true;
      s[i]--;
    }
    if (src[i] != s[i])
      return false;
  }
  return true;
}

int main() {
  while (cin >> src && src != "#") {
    vector<string> v;
    for (int i = 0; i < (1 << src.size()); i++) {
      string dst = src;
      for (int j = 0; j < src.size(); j++)
        if (dst[j] != 'z' && (i >> j & 1)) {
          dst[j]++;
        }
      if (enc(dst)) {
        v.push_back(dst);
      }
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
    cout << v.size() << endl;
    if (v.size() <= 10) {
      for (auto x : v)
        cout << x << endl;
    } else {
      for (int j = 0; j < 5; j++)
        cout << v[j] << endl;
      for (int j = 0; j < 5; j++)
        cout << v[v.size() - 5 + j] << endl;
    }
  }
}
Problem p00776 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;
  while (cin >> s, s != "#") {
    vector<string> v;
    vector<pair<int, int>> same;
    for (int i = 0; i < s.size(); i++)
      for (int j = i + 1; j < s.size(); j++)
        if (s[i] == s[j])
          same.push_back(make_pair(i, j));
    for (unsigned long long S = 0; S < (1 << s.size()); S++) {
      if (s[0] != 'a' && !(S >> s.size() - 1 & 1))
        continue;
      bool possible = true;
      bool used[26] = {0};
      used[0] = true;
      string t = s;

      for (int j = 0; j < s.size(); j++) {
        if (S >> j & 1) {
          if (t[s.size() - j - 1] == 'z') {
            possible = false;
            break;
          }
          t[s.size() - j - 1]++;
        }
      }
      if (!possible)
        continue;
      string p = t;
      for (int j = 0; j < t.size(); j++)
        if (!used[t[j] - 'a']) {
          used[t[j] - 'a'] = true;
          t[j]--;
        }
      if (s == t)
        v.push_back(p);
    }
    cout << v.size() << endl;
    if (v.size() <= 10) {
      for (int i = 0; i < v.size(); i++)
        cout << v[i] << endl;
    } else {
      for (int i = 0; i < 5; i++)
        cout << v[i] << endl;
      for (int i = v.size() - 5; i < v.size(); i++)
        cout << v[i] << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  string s;
  while (cin >> s, s != "#") {
    vector<string> v;
    vector<pair<int, int>> same;
    for (int i = 0; i < s.size(); i++)
      for (int j = i + 1; j < s.size(); j++)
        if (s[i] == s[j])
          same.push_back(make_pair(i, j));
    for (unsigned long long S = 0; S < (1 << s.size()); S++) {
      if (s[0] != 'a' && S >> s.size() - 1 & 0)
        continue;
      bool possible = true;
      for (int i = 0; i < same.size(); i++)
        if (S >> same[i].first & 1 && S >> same[i].second & 1) {
          possible = false;
          break;
        }
      if (!possible)
        continue;
      bool used[26] = {0};
      used[0] = true;
      string t = s;

      for (int j = 0; j < s.size(); j++) {
        if (S >> j & 1) {
          if (t[s.size() - j - 1] == 'z') {
            possible = false;
            break;
          }
          t[s.size() - j - 1]++;
        }
      }
      if (!possible)
        continue;
      string p = t;
      for (int j = 0; j < t.size(); j++)
        if (!used[t[j] - 'a']) {
          used[t[j] - 'a'] = true;
          t[j]--;
        }
      if (s == t)
        v.push_back(p);
    }
    cout << v.size() << endl;
    if (v.size() <= 10) {
      for (int i = 0; i < v.size(); i++)
        cout << v[i] << endl;
    } else {
      for (int i = 0; i < 5; i++)
        cout << v[i] << endl;
      for (int i = v.size() - 5; i < v.size(); i++)
        cout << v[i] << endl;
    }
  }
}
Problem p00776 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

typedef long long ll;

set<string> ans;
string str;

void dfs(string &S, int C, int B) {
  if (C == str.size()) {
    ans.insert(S);
    return;
  }

  if (~B & (1 << ('z' - 'a')) && str[C] == 'z')
    return;
  if (str[C] == 'a') {
    if (~B & (1 << 1)) {
      S[C] = 'b';
      dfs(S, C + 1, B | (1 << 1));
    }
    S[C] = 'a';
    dfs(S, C + 1, B | (1 << 0));

  } else if (B & (1 << (str[C] - 'a'))) {
    if ('z' >= (str[C] + 1) && ~B & (1 << (str[C] - 'a' + 1))) {
      S[C] = (char)(str[C] + 1);
      dfs(S, C + 1, B | (1 << (str[C] + 1 - 'a')));
    }
    S[C] = str[C];
    dfs(S, C + 1, B);

  } else {
    if ('z' >= (str[C] + 1) && ~B & (1 << (str[C] - 'a' + 1))) {
      S[C] = (char)(str[C] + 1);
      dfs(S, C + 1, B | (1 << (str[C] + 1 - 'a')));

    } else
      return;
  }
}

int main() {
  while (cin >> str, str != "#") {
    ans.clear();
    string tmp = str;
    dfs(tmp, 0, 0);
    int s = ans.size();
    cout << s << endl;

    int k = 0;
    if (0 < s && s <= 10) {
      for (auto i : ans) {
        cout << i << endl;
      }
    } else if (s > 10) {
      int k = 0;
      for (auto i : ans) {
        if (k == 5)
          break;
        cout << i << endl;
        k++;
      }
      auto ite = ans.end();
      ite--;
      ite--;
      ite--;
      ite--;
      ite--;
      k = 0;
      while (k++ < 5) {
        cout << *ite << endl;
        ite++;
      }
    }
  }

  return 0;
}

Error: MLE

Fixed submission:

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

typedef long long ll;

set<string> ans;
string str;

void dfs(string &S, int C, int B) {
  if (C == str.size()) {
    ans.insert(S);
    return;
  }

  if (str[C] == 'z')
    return;
  if (str[C] == 'a') {
    if (~B & (1 << 2)) {
      S[C] = 'b';
      dfs(S, C + 1, B | 2);
    }
    S[C] = 'a';
    dfs(S, C + 1, B | 1);

  } else if (B & (1 << (str[C] - 'a'))) {
    if (~B & (1 << (str[C] - 'a' + 1))) {
      S[C] = (char)(str[C] + 1);
      dfs(S, C + 1, B | (1 << (str[C] + 1 - 'a')));
    }
    S[C] = str[C];
    dfs(S, C + 1, B);

  } else {
    if (~B & (1 << (str[C] - 'a' + 1))) {
      S[C] = (char)(str[C] + 1);
      dfs(S, C + 1, B | (1 << (str[C] + 1 - 'a')));

    } else
      return;
  }
}

int main() {
  while (cin >> str, str != "#") {
    ans.clear();
    string tmp = str;
    dfs(tmp, 0, 0);
    int s = ans.size();
    cout << s << endl;

    int k = 0;
    if (0 < s && s <= 10) {
      for (auto i : ans) {
        cout << i << endl;
      }
    } else if (s > 10) {
      int k = 0;
      for (auto i : ans) {
        if (k == 5)
          break;
        cout << i << endl;
        k++;
      }
      auto ite = ans.end();
      ite--;
      ite--;
      ite--;
      ite--;
      ite--;
      k = 0;
      while (k++ < 5) {
        cout << *ite << endl;
        ite++;
      }
    }
  }

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

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

const int INF = 1 << 30;
vector<vector<pair<int, int>>> graph;
int sum, D;

int dfs(int v, int r) {
  vector<int> ch = {0, 0};
  for (auto p : graph[v]) {
    int sv, d;
    tie(sv, d) = p;
    if (sv == r)
      continue;
    int res = dfs(sv, v);
    if (res < 0) {
      ch.push_back(0);
      sum += d;
    } else {
      sum += d * 3;
      ch.push_back(res + d);
    }
  }

  if (ch.size() <= 2)
    return -1;
  sort(ch.rbegin(), ch.rend());
  D = max(D, ch[0] + ch[1]);
  return ch[0];
}

bool solve() {
  int N;
  cin >> N;
  if (N == 0)
    return false;

  vector<int> p(N), d(N);
  for (int i = 1; i < N; ++i) {
    cin >> p[i];
    --p[i];
  }
  for (int i = 1; i < N; ++i) {
    cin >> d[i];
  }

  graph.clear();
  graph.resize(N);
  for (int i = 1; i < N; ++i) {
    graph[i].emplace_back(p[i], d[i]);
    graph[p[i]].emplace_back(i, d[i]);
  }

  sum = 0, D = 0;
  for (int v = 0; v < N; ++v) {
    if (graph[v].size() <= 1)
      continue;
    dfs(v, -1);
    break;
  }

  cout << sum - D << endl;
  return true;
}
int main() {
  while (solve()) {
  }
  return 0;
}


Error: 0

Fixed submission:

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

const int INF = 1 << 30;
vector<vector<pair<int, int>>> graph;
int sum, D;

int dfs(int v, int r) {
  vector<int> ch = {0, 0};
  for (auto p : graph[v]) {
    int sv, d;
    tie(sv, d) = p;
    if (sv == r)
      continue;
    int res = dfs(sv, v);
    if (res < 0) {
      ch.push_back(0);
      sum += d;
    } else {
      sum += d * 3;
      ch.push_back(res + d);
    }
  }

  if (ch.size() <= 2)
    return -1;
  sort(ch.rbegin(), ch.rend());
  D = max(D, ch[0] + ch[1]);
  return ch[0];
}

bool solve() {
  int N;
  cin >> N;
  if (N == 0)
    return false;

  vector<int> p(N - 1), d(N - 1);
  for (int i = 1; i < N; ++i) {
    cin >> p[i];
    --p[i];
  }
  for (int i = 1; i < N; ++i) {
    cin >> d[i];
  }

  graph.clear();
  graph.resize(N);
  for (int i = 1; i < N; ++i) {
    graph[i].emplace_back(p[i], d[i]);
    graph[p[i]].emplace_back(i, d[i]);
  }

  sum = 0, D = 0;
  for (int v = 0; v < N; ++v) {
    if (graph[v].size() <= 1)
      continue;
    dfs(v, -1);
    break;
  }

  cout << sum - D << endl;
  return true;
}
int main() {
  while (solve()) {
  }
  return 0;
}
Problem p00777 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define REPS(i, n) for (int i = 1; i <= (int)n; i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define RREPS(i, n) for (int i = n; i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define pb push_back
#define eb emplace_back
#define endl '\\n'
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

int n;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  while (cin >> n, n) {
    vi deg(n), p(n);
    vector<vector<pii>> g(n);
    REPS(i, n - 1) {
      cin >> p[i];
      p[i]--;
      deg[p[i]]++;
      deg[i]++;
    }
    int ans = 0;
    REPS(i, n - 1) {
      int d;
      cin >> d;
      g[i].eb(p[i], d);
      g[p[i]].eb(i, d);
      if (deg[i] == 1 || deg[p[i]] == 1)
        ans += d;
      else
        ans += 3 * d;
    }
    int ans2 = ans;
    REP(i, n) if (deg[i] != 1) {
      queue<tuple<int, int, int>> q;
      q.emplace(i, 0, -1);
      while (!q.empty()) {
        int u, d, p;
        tie(u, d, p) = q.front();
        q.pop();
        if (deg[u] != 1)
          ans2 = min(ans2, ans - d);
        for (pii e : g[u])
          if (e.first != p)
            q.emplace(e.first, d + e.second, u);
      }
    }
    cout << ans2 << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define REPS(i, n) for (int i = 1; i <= (int)n; i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define RREPS(i, n) for (int i = n; i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define pb push_back
#define eb emplace_back
#define endl '\\n'
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
const int INF = 1000000007;
int n, g[801][801];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  while (cin >> n, n) {
    REP(i, n) REP(j, n) g[i][j] = i == j ? 0 : INF;
    vi deg(n), p(n);
    REPS(i, n - 1) {
      cin >> p[i];
      p[i]--;
      deg[p[i]]++;
      deg[i]++;
    }
    ll ans = 0;
    REPS(i, n - 1) {
      int d;
      cin >> d;
      g[i][p[i]] = d;
      g[p[i]][i] = d;
      if (deg[i] == 1 || deg[p[i]] == 1)
        ans += d;
      else
        ans += 3 * d;
    }
    REP(k, n) REP(i, n) REP(j, n) g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
    int ma = 0;
    REP(i, n) REP(j, i) {
      if (deg[i] != 1 && deg[j] != 1)
        ma = max(ma, g[i][j]);
    }
    cout << ans - ma << endl;
  }
}
Problem p00777 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;

int main() {
  int n;
  while (cin >> n && n > 0) {
    int ans = 0;

    vector<int> p(n - 1);
    vector<int> c(n - 1);
    REP(i, n - 1) cin >> p[i];
    REP(i, n - 1) cin >> c[i];
    REP(i, n - 1) ans += c[i];

    vector<int> deg(n);
    REP(i, n - 1) {
      int a = i + 1, b = p[i] - 1;
      deg[a]++;
      deg[b]++;
    }

    vector<vector<pair<int, int>>> G(n);
    REP(i, n - 1) {
      int a = i + 1, b = p[i] - 1;
      if (deg[a] < 2)
        continue;
      if (deg[b] < 2)
        continue;
      G[a].push_back(make_pair(b, c[i]));
      G[b].push_back(make_pair(a, c[i]));
      ans += 2 * c[i];
    }

    int longest = 0;
    REP(s, n) {
      const int INF = 1 << 29;
      vector<int> dist(n, INF);
      dist[s] = 0;
      queue<int> que;
      que.push(s);
      while (!que.empty()) {
        int u = que.front();
        que.pop();
        for (auto pr : G[u]) {
          int v = pr.first;
          int nd = dist[u] + pr.second;
          if (dist[v] > nd) {
            dist[v] = nd;
            que.push(v);
          }
        }
      }
      REP(g, n) if (dist[g] < INF) longest = max(longest, dist[g]);
    }

    cout << ans - longest << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;

int main() {
  int n;
  while (cin >> n && n > 0) {
    int ans = 0;

    vector<int> p(n - 1);
    vector<int> c(n - 1);
    REP(i, n - 1) cin >> p[i];
    REP(i, n - 1) cin >> c[i];
    REP(i, n - 1) ans += c[i];

    vector<int> deg(n);
    REP(i, n - 1) {
      int a = i + 1, b = p[i] - 1;
      deg[a]++;
      deg[b]++;
    }

    int d[808][808] = {};
    const int INF = 1 << 29;
    REP(i, n) REP(j, n) d[i][j] = (i == j ? 0 : INF);
    REP(i, n - 1) {
      int a = i + 1, b = p[i] - 1;
      if (deg[a] < 2)
        continue;
      if (deg[b] < 2)
        continue;
      d[a][b] = d[b][a] = c[i];
      ans += 2 * c[i];
    }

    REP(k, n) REP(i, n) REP(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
    int longest = 0;
    REP(i, n) REP(j, n) if (d[i][j] < INF) longest = max(longest, d[i][j]);

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

#include <algorithm>
#include <cstdio>
using namespace std;
bool num[1 << 15] = {};
int main() {
  for (int i = 2; i < (1 << 15); i++) {
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        num[i] = true;
        break;
      }
    }
  }
  int n;
  while (scanf("%d", &n) && n) {
    int ans = 0;
    for (int i = 2; i * 2 <= n; i++) {
      if (!num[i] && !num[n - i]) {
        ans++;
      }
    }
    printf("%d\\n", ans);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
bool num[1 << 14] = {};
int main() {
  for (int i = 2; i < (1 << 14); i++) {
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        num[i] = true;
        break;
      }
    }
  }
  int n;
  while (scanf("%d", &n) && n) {
    int ans = 0;
    for (int i = 2; i * 2 <= n; i++) {
      if (!num[i] && !num[n - i]) {
        ans++;
      }
    }
    printf("%d\\n", ans);
  }
}
Problem p00780 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int d[40000];

int main() {
  d[0] = 2;
  d[1] = 3;
  int count = 2;
  for (int i = 4; i <= 40000; i++) {
    bool flag = true;
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        flag = false;
        break;
      }
    }
    if (flag) {
      d[count] = i;
      count++;
    }
  }
  d[count] = 9999;

  int n;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    int ans = 0;
    for (int i = 0; d[i + 1] < n; i++) {
      for (int j = i; d[j] < n; j++) {
        if (d[i] + d[j] == n) {
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int d[256];

int main() {
  d[0] = 2;
  d[1] = 3;
  int count = 2;
  for (int i = 4; i <= 216; i++) {
    bool flag = true;
    for (int j = 2; j * j <= i; j++) {
      if (i % j == 0) {
        flag = false;
        break;
      }
    }
    if (flag) {
      d[count] = i;
      count++;
    }
  }
  d[count] = 9999;

  int n;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    int ans = 0;
    for (int i = 0; d[i + 1] < n; i++) {
      for (int j = i; d[j] < n; j++) {
        if (d[i] + d[j] == n) {
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}