prithivMLmods/Coder-Stat huggingface dataset:
Problem p02935 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

long double v[1000];

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;
  long double ans = 0, val;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  sort(v, v + n);
  do {
    val = v[0];
    for (int i = 1; i < n; ++i) {
      val = (val + v[i]) / 2;
    }
    ans = max(ans, val);
  } while (next_permutation(v, v + n));
  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

long double v[1000];

int main() {

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  int n;
  cin >> n;
  long double ans = 0, val;
  for (int i = 0; i < n; ++i) {
    cin >> v[i];
  }
  sort(v, v + n);
  val = v[0];
  for (int i = 1; i < n; ++i) {
    val = (val + v[i]) / 2;
  }
  ans = max(ans, val);
  cout << ans << '\n';
  return 0;
}
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {

  int n, tmp;
  priority_queue<int, vector<int>, greater<int>> queue;
  std::cin >> n;

  for (int i = 0; i < n; i++) {
    std::cin >> tmp;
    queue.push(tmp * 100000);
  }

  do {
    int now = 0;
    now += queue.top();
    queue.pop();
    now += queue.top();
    queue.pop();
    queue.push(now / 2);
  } while (queue.empty() != 1);

  printf("%5f\n", (double)queue.top() / 100000);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {

  int n, tmp;
  priority_queue<int, vector<int>, greater<int>> queue;
  std::cin >> n;

  for (int i = 0; i < n; i++) {
    std::cin >> tmp;
    queue.push(tmp * 100000);
  }

  do {
    int now = 0;
    now += queue.top();
    queue.pop();
    now += queue.top();
    queue.pop();
    queue.push(now / 2);
  } while (queue.size() != 1);

  printf("%5f\n", (double)queue.top() / 100000);

  return 0;
}
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, ans = 0;
  cin >> N;
  vector<float> v(N);

  for (int i = 0; i < N; ++i)
    cin >> v[i];

  sort(v.begin(), v.end());

  for (int i = 0; i < N; ++i) {
    v[i + 1] = (v[i + 1] + v[i]) / 2;
  }

  cout << v[N - 1] << endl;
}


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

int main() {
  int N, ans = 0;
  cin >> N;
  vector<float> v(N);

  for (int i = 0; i < N; ++i)
    cin >> v[i];

  sort(v.begin(), v.end());

  for (int i = 0; i < N - 1; ++i) {
    v[i + 1] = (v[i + 1] + v[i]) / 2;
  }

  cout << v[N - 1] << endl;
}
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rg register long long
#define inf 21474899993647
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
#define ll long long
#define maxn 100005
#define endl "\n"
#define N 6000
const double eps = 1e-8;
using namespace std;
inline ll read() {
  char ch = getchar();
  ll s = 0, w = 1;
  while (ch < 48 || ch > 57) {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= 48 && ch <= 57) {
    s = (s << 1) + (s << 3) + (ch ^ 48);
    ch = getchar();
  }
  return s * w;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + 48);
}
priority_queue<double, vector<double>, greater<double>> q;
int main() {
  ll n = read();
  for (rg i = 1; i <= n; i++) {
    double x = read();
    q.push(x);
  }
  double ans = 0;
  while (!q.empty()) {
    double a = q.top();
    q.pop();
    double b = q.top();
    q.pop();
    q.push((a + b) / 2.0);
    ans = (a + b) / 2;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rg register long long
#define inf 21474899993647
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
#define ll long long
#define maxn 100005
#define endl "\n"
#define N 6000
const double eps = 1e-8;
using namespace std;
inline ll read() {
  char ch = getchar();
  ll s = 0, w = 1;
  while (ch < 48 || ch > 57) {
    if (ch == '-')
      w = -1;
    ch = getchar();
  }
  while (ch >= 48 && ch <= 57) {
    s = (s << 1) + (s << 3) + (ch ^ 48);
    ch = getchar();
  }
  return s * w;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + 48);
}
priority_queue<double, vector<double>, greater<double>> q;
int main() {
  ll n = read();
  for (rg i = 1; i <= n; i++) {
    double x = read();
    q.push(x);
  }
  double ans = 0;
  while (!q.empty()) {
    double a = q.top();
    q.pop();
    if (!q.empty()) {
      double b = q.top();
      q.pop();
      q.push((a + b) / 2.0);
      ans = (a + b) / 2.0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#define ll long long
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define T                                                                      \
  long long t;                                                                 \
  cin >> t;                                                                    \
  while (t--)
#define foe(i) for (int i = 1; i <= n; i++)
#define fo(i) for (int i = 0; i < n; i++)
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  ll a[55];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  a[1] += a[0];
  long double ans = 0;
  int j = 1;
  for (int i = 1; i <= pow(2, n - 2); i = i * 2) {
    ans += i * a[j];
    j++;
  }
  ans = ans / pow(2, n - 1);
  cout << ans;
}

Fixed submission:
#define ll long long
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define T                                                                      \
  long long t;                                                                 \
  cin >> t;                                                                    \
  while (t--)
#define foe(i) for (int i = 1; i <= n; i++)
#define fo(i) for (int i = 0; i < n; i++)
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ll n;
  cin >> n;
  ll a[55];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  a[1] += a[0];
  long double ans = 0;
  int j = 1;
  for (ll i = 1; i <= pow(2, n - 2); i = i * 2) {
    ans += i * a[j];
    j++;
  }
  ans = ans / pow(2, n - 1);
  cout << ans;
}
Problem p02935 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
V = [float(v) for v in input().split()]
V.sort(revese=True)
for i in range(N):
    if len(V) == 1:
        print(V[0])
        exit()
    v1 = V.pop()
    v2 = V.pop()
    V.append((v1 + v2) / 2)


Fixed submission:
N = int(input())
V = [float(v) for v in input().split()]
V.sort(reverse=True)
for i in range(N):
    if len(V) == 1:
        print(V[0])
        exit()
    v1 = V.pop()
    v2 = V.pop()
    V.append((v1 + v2) / 2)
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<float> v(N);

  for (int i = 0; i < N; ++i)
    cin >> v[i];
  sort(v.begin(), v.end());

  for (int j = 0; j < N; ++j) {
    v[j + 1] = (v[j] + v[j + 1]) / 2;
  }

  cout << v[N - 1] << endl;

  return 0;
}

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

int main() {
  int N;
  cin >> N;
  vector<float> v(N);

  for (int i = 0; i < N; ++i)
    cin >> v[i];
  sort(v.begin(), v.end());

  for (int j = 0; j < N - 1; ++j) {
    v[j + 1] = (v[j] + v[j + 1]) / 2;
  }

  cout << v[N - 1] << endl;

  return 0;
}
Problem p02935 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  double max = 0;
  do {
    double temp = arr[0];
    for (int i = 1; i < n; i++) {
      temp += arr[i];
      temp /= 2;
    }
    if (temp > max) {
      max = temp;
    }
  } while (std::next_permutation(arr, arr + n));
  cout << max;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);

  double temp = arr[0];
  for (int i = 1; i < n; i++) {
    temp += arr[i];
    temp /= 2;
  }
  cout << temp;

  return 0;
}
Problem p02935 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  vector<double> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }

  double max_v = 0;
  sort(v.begin(), v.end());
  do {
    double value = v[0];
    for (int i = 1; i < n; i++) {
      value = (value + v[i]) / 2.0;
    }
    max_v = max(max_v, value);
  } while (next_permutation(v.begin(), v.end()));

  printf("%.7f\n", max_v);
}


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

int main() {
  int n;
  cin >> n;
  vector<double> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }

  double max_v = 0;
  sort(v.begin(), v.end());
  // do {
  double value = v[0];
  for (int i = 1; i < n; i++) {
    value = (value + v[i]) / 2.0;
  }
  max_v = max(max_v, value);
  // }while(next_permutation(v.begin(),v.end()));

  printf("%.7f\n", max_v);
}
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  priority_queue<double, vector<double>, greater<double>> pt;
  double a, b, sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lf", &a);
    pt.push(a);
  }
  while (!pt.empty()) {
    a = pt.top();
    pt.pop();
    b = pt.top();
    pt.pop();
    sum = (a + b) / 2.0;
    pt.push(sum);
  }
  printf("%lf\n", sum);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  priority_queue<double, vector<double>, greater<double>> pt;
  double a, b, sum = 0;
  for (int i = 0; i < n; i++) {
    scanf("%lf", &a);
    pt.push(a);
  }
  while (pt.size() > 1) {
    a = pt.top();
    pt.pop();
    b = pt.top();
    pt.pop();
    sum = (a + b) / 2.0;
    pt.push(sum);
  }
  printf("%lf\n", sum);
  return 0;
}
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = n; i >= 0; i++)
#define REP(i, m, n) for (int i = m; i < n; i++)
#define SORT(v) sort((v).begin(), (v).end())
#define REV(v) sort((v).begin(), (v).end(), greater<int>())
#define INF 1000000007
typedef long long ll;
typedef pair<int, int> P;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; };
int lcm(int a, int b) { return a * b / gcd(a, b); };
int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
};

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<double> v(n);
  rep(i, n) cin >> v[i];
  sort(all(v));
  rep(i, n) { v[i + 1] = (v[i] + v[i + 1]) / 2; }
  cout << fixed << v[n - 1] << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = n; i >= 0; i++)
#define REP(i, m, n) for (int i = m; i < n; i++)
#define SORT(v) sort((v).begin(), (v).end())
#define REV(v) sort((v).begin(), (v).end(), greater<int>())
#define INF 1000000007
typedef long long ll;
typedef pair<int, int> P;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; };
int lcm(int a, int b) { return a * b / gcd(a, b); };
int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
};

int main() {
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  vector<double> v(n);
  rep(i, n) cin >> v[i];
  sort(all(v));
  rep(i, n - 1) { v[i + 1] = (v[i] + v[i + 1]) / 2; }
  cout << fixed << v[n - 1] << endl;
  return 0;
}
Problem p02935 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int a[55];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  double max = 0, s;
  do {
    s = a[0];
    for (int i = 0; i < n; i++)
      s = (s + a[i]) * 1.0 / (2 * 1.0);
    if (s > max)
      max = s;
  } while (next_permutation(a, a + n));
  cout << max;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[55];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  double s;
  s = a[0];
  for (int i = 0; i < n; i++)
    s = (s + a[i]) * 1.0 / (2 * 1.0);
  cout << s;
}
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<double> vec(3);

  for (int i = 0; i < n; i++) {
    double v;
    cin >> v;
    vec.at(i) = v;
  }

  for (int i = 0; i < n - 1; i++) {
    sort(vec.begin(), vec.end());
    reverse(vec.begin(), vec.end());
    double num;
    // 末尾2つを足して2で割る
    num = (vec.at(vec.size() - 1) + vec.at(vec.size() - 2)) / 2;
    // 末尾2つを消す
    vec.pop_back();
    vec.pop_back();
    // 末尾にnumを追加
    vec.push_back(num);
  }

  cout << vec.at(0) << endl;
}

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

int main() {
  int n;
  cin >> n;
  vector<double> vec(n);

  for (int i = 0; i < n; i++) {
    double v;
    cin >> v;
    vec.at(i) = v;
  }

  for (int i = 0; i < n - 1; i++) {
    sort(vec.begin(), vec.end());
    reverse(vec.begin(), vec.end());
    double num;
    // 末尾2つを足して2で割る
    num = (vec.at(vec.size() - 1) + vec.at(vec.size() - 2)) / 2;
    // 末尾2つを消す
    vec.pop_back();
    vec.pop_back();
    // 末尾にnumを追加
    vec.push_back(num);
  }

  cout << vec.at(0) << endl;
}
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
#include <bits/stdc++.h>

void test_data(int N, vector<double> v) {
  N = 2;
  v = {3, 4};
  cout << v[0] << endl;
}

int input_data(int N, vector<double> v) {
  cin >> N;

  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  };
}

double nabe(double x, double y) {
  double ans = (x + y) / 2;
  return ans;
}

int main() {
  // int N = 3;
  // vector<double> v = {3, 4, 5};

  int N;
  vector<double> v;

  cin >> N;

  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    // v.push_back(x);
  };

  for (int i = 0; i < v.size(); i++) {
    // cout << v.at(i) << endl;
  }

  // vector<double> v_s;
  sort(v.begin(), v.end());

  for (int i = 0; i < v.size(); i++) {
    // cout << v.at(i) << endl;
  }

  // double test_ans = nabe(x, y);
  double x = nabe(v.at(0), v.at(1));

  if (N >= 3) {
    for (int i = 2; i < v.size(); i++) {
      // cout << x << "+" << v.at(i) << endl;
      x = nabe(v.at(i), x);
      // cout << i << endl;
    }

    cout << x << endl;
    return 0;
  } else if (N <= 2) {
    cout << x << endl;
    return 0;
  }

  // ans.push_back(nabe(v.at(0), v.at(1)));
  // cout << "ans" << endl;
  // cout << x << endl;
  // ans.push_back( nabe(v.at(1), v.at(2)));

  // cout << test_ans << endl;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
#include <bits/stdc++.h>

void test_data(int N, vector<double> v) {
  N = 2;
  v = {3, 4};
  cout << v[0] << endl;
}

int input_data(int N, vector<double> v) {
  cin >> N;

  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  };
}

double nabe(double x, double y) {
  double ans = (x + y) / 2;
  return ans;
}

int main() {
  // int N = 3;
  // vector<double> v = {3, 4, 5};

  int N;
  vector<double> v;

  cin >> N;

  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    v.push_back(x);
  };

  for (int i = 0; i < v.size(); i++) {
    // cout << v.at(i) << endl;
  }

  // vector<double> v_s;
  sort(v.begin(), v.end());

  for (int i = 0; i < v.size(); i++) {
    // cout << v.at(i) << endl;
  }

  // double test_ans = nabe(x, y);
  double x = nabe(v.at(0), v.at(1));

  if (N >= 3) {
    for (int i = 2; i < v.size(); i++) {
      // cout << x << "+" << v.at(i) << endl;
      x = nabe(v.at(i), x);
      // cout << i << endl;
    }

    cout << x << endl;
    return 0;
  } else if (N <= 2) {
    cout << x << endl;
    return 0;
  }

  // ans.push_back(nabe(v.at(0), v.at(1)));
  // cout << "ans" << endl;
  // cout << x << endl;
  // ans.push_back( nabe(v.at(1), v.at(2)));

  // cout << test_ans << endl;
}
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <numeric>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, x) for (int i = 1; i <= (int)(x); i++)
#define all(x) (x).begin(), (x).end()

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<int> list(N);
  rep(i, N) cin >> list.at(i);
  sort(all(list));
  double ans = list.at(0);
  reps(i, N) { ans = (ans + list.at(i)) / 2; }

  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <numeric>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, x) for (int i = 1; i <= (int)(x); i++)
#define all(x) (x).begin(), (x).end()

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  cin >> N;

  vector<int> list(N);
  rep(i, N) cin >> list.at(i);
  sort(all(list));
  double ans = list.at(0);
  for (int i = 1; i < N; i++) {
    ans = (ans + list.at(i)) / 2;
  }

  cout << ans << endl;
}
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int N;
  cin >> N;
  int array[N];
  for (int i = 0; i < N; i++) {
    cin >> array[i];
  }
  double d = 0.0;
  sort(array, array + N);
  d = array[0];
  for (int i = 1; i < N; i++) {
    d = (d + array[i]) / 2;
  }
  return d;
}

Fixed submission:
#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int N;
  cin >> N;
  int array[N];
  for (int i = 0; i < N; i++) {
    cin >> array[i];
  }
  double d = 0.0;
  sort(array, array + N);
  d = array[0];
  for (int i = 1; i < N; i++) {
    d = (d + array[i]) / 2;
  }
  cout << d << endl;
}
Problem p02935 (Python)

Buggy submission (status: Runtime Error):
def solve(string):
    n, *v = map(int, string.split())
    v = sorted(v, reverse=True)
    return str(
        sum(_v / 2**i for i, _v in enumerate(v[:-1], start=1)) + v[-1] / 2 ** (n - 1)
    )


if __name__ == "__main__":
    print(solve(input()))


Fixed submission:
def solve(string):
    n, *v = map(int, string.split())
    v = sorted(v, reverse=True)
    return str(
        sum(_v / 2**i for i, _v in enumerate(v[:-1], start=1)) + v[-1] / 2 ** (n - 1)
    )


if __name__ == "__main__":
    print(solve("\n".join([input(), input()])))
Problem p02935 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
double a[50 + 5];

int main() {
  int n;
  double sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  for (int i = 0; i < n - 1; i++) {
    a[i + 1] = (a[i] + a[i + 1]) / 2;
    if (i == n - 2)
      break;
    sort(a + i + 1, a + n - i - 1);
  }
  printf("%.1lf", a[n - 1]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
double a[50 + 5];

int main() {
  int n;
  double sum = 0;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  sum = (a[0] + a[1]) / 2;
  for (int i = 2; i < n; i++)
    sum = (sum + a[i]) / 2;
  cout << sum << endl;
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;
#define mk make_pair
#define mkt make_tuple

using namespace std;
int const N = 100000;
int add[N];
bool was[N];
vector<vector<int>> v;

void dfs(int u) {
  was[u] = true;
  for (int i : v[u])
    if (!was[i]) {
      add[i] += add[u];
      dfs(i);
    }
}

int main() {
  ll n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    v.push_back(vector<int>());
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    add[p] += x;
  }

  dfs(0);

  for (int i = 0; i < n; i++)
    cout << add[i] << ' ';
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>

typedef long long ll;
typedef unsigned long long ull;
#define mk make_pair
#define mkt make_tuple

using namespace std;
int const N = 200005;
int add[N];
bool was[N];
vector<vector<int>> v;

void dfs(int u) {
  was[u] = true;
  for (int i : v[u])
    if (!was[i]) {
      add[i] += add[u];
      dfs(i);
    }
}

int main() {
  ll n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    v.push_back(vector<int>());
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }

  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    add[p] += x;
  }

  dfs(0);

  for (int i = 0; i < n; i++)
    cout << add[i] << ' ';
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

vector<int> to[20000];
vector<int> ans;

void dfs(int v, int p = -1) {
  for (int u : to[v]) {
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ans.resize(n);
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  dfs(0);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

vector<int> to[200005];
vector<int> ans;

void dfs(int v, int p = -1) {
  for (int u : to[v]) {
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ans.resize(n);
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  dfs(0);
  rep(i, n) { cout << ans[i] << endl; }
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 100010;
vector<int> G[N];
vector<int> Count(N);
int n, q;

void dfs(int now, int pre) {
  for (int i = 0; i < G[now].size(); i++) {
    int to = G[now][i];
    if (to == pre)
      continue;
    Count[to] += Count[now];
    dfs(to, now);
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> n >> q;

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    Count[p] += x;
  }

  dfs(0, -1);

  for (int i = 0; i < n; i++) {
    cout << Count[i] << ' ';
  }
  cout << endl;

  return 0;
}

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

const int N = 200010;
vector<int> G[N];
vector<int> Count(N);
int n, q;

void dfs(int now, int pre) {
  for (int i = 0; i < G[now].size(); i++) {
    int to = G[now][i];
    if (to == pre)
      continue;
    Count[to] += Count[now];
    dfs(to, now);
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  cin >> n >> q;

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    Count[p] += x;
  }

  dfs(0, -1);

  for (int i = 0; i < n; i++) {
    cout << Count[i] << ' ';
  }
  cout << endl;

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 50;
const int inf = 0x3f3f3f3f;
int n, k;
vector<ll> a(20050, 0);
bool vis[20050];
vector<int> g[200050];
void dfs(int t, ll val) {
  vis[t] = 1;
  a[t] += val;
  for (int i = 0; i < g[t].size(); i++) {
    int v = g[t][i];
    if (!vis[v]) {
      dfs(v, a[t]);
    }
  }
}
int main() {
  // freopen("/Users/zilongzhang/Desktop//l.txt","r",stdin);
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--;
    y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  while (k--) {
    int l, b;
    scanf("%d %d", &l, &b);
    l--;
    a[l] += b;
  }
  dfs(0, 0);
  for (int i = 0; i < n; i++)
    printf("%lld ", a[i]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
// #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 50;
const int inf = 0x3f3f3f3f;
int n, k;
ll a[200050];
bool vis[200050];
vector<int> g[200050];
void dfs(int t, ll val) {
  vis[t] = 1;
  a[t] += val;
  for (int i = 0; i < g[t].size(); i++) {
    int v = g[t][i];
    if (!vis[v]) {
      dfs(v, a[t]);
    }
  }
}
int main() {
  // freopen("/Users/zilongzhang/Desktop//l.txt","r",stdin);
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    x--;
    y--;
    g[x].push_back(y);
    g[y].push_back(x);
  }
  while (k--) {
    int l, b;
    scanf("%d %d", &l, &b);
    l--;
    a[l] += b;
  }
  dfs(0, 0);
  for (int i = 0; i < n; i++)
    printf("%lld ", a[i]);
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

vector<int> to[20005];
vector<int> ans;

void dfs(int v, int p = -1) {
  for (int u : to[v]) {
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  int a, b;

  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  ans.resize(n);

  int p, x;

  for (int i = 0; i < q; i++) {
    cin >> p >> x;
    --p;
    ans[p] += x;
  }
  dfs(0);

  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

vector<int> to[200005];
vector<int> ans;

void dfs(int v, int p = -1) {
  for (int u : to[v]) {
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  int a, b;

  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  ans.resize(n);

  int p, x;

  for (int i = 0; i < q; i++) {
    cin >> p >> x;
    --p;
    ans[p] += x;
  }
  dfs(0);

  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm> // sort
#include <bits/stdc++.h>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map> // pair
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define nin(n)                                                                 \
  int n;                                                                       \
  cin >> n;
#define kin(k)                                                                 \
  int k;                                                                       \
  cin >> k;
vector<int> to[20005];
vector<int> ans;
void dfs(int v, int p = -1) {
  for (int u : to[v]) {
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}
const int INF = 1001001001;
int main() {
  int n, q;
  cin >> n >> q;
  ans.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
    ans.at(i) = 0;
  }
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }

  dfs(0);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}

Fixed submission:
#include <algorithm> // sort
#include <bits/stdc++.h>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map> // pair
#include <math.h>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define nin(n)                                                                 \
  int n;                                                                       \
  cin >> n;
#define kin(k)                                                                 \
  int k;                                                                       \
  cin >> k;
vector<int> to[200005];
vector<int> ans;
void dfs(int v, int p = -1) {
  for (int u : to[v]) {
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}
const int INF = 1001001001;
int main() {
  int n, q;
  cin >> n >> q;
  ans.resize(n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
    ans.at(i) = 0;
  }
  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }

  dfs(0);
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()

vector<vector<int>> to(100100);
vector<int> x(100100);
vector<bool> seen(100100, false);

void dfs(int v) {
  seen[v] = true;
  for (auto nv : to[v]) {
    if (seen[nv])
      continue;
    x[nv] += x[v];
    dfs(nv);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  rep(i, q) {
    int p;
    cin >> p;
    p--;
    int a;
    cin >> a;
    x[p] += a;
  }
  dfs(0);
  rep(i, n) printf("%d ", x[i]);
  cout << endl;
  return 0;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()

const int M = 212345;

vector<vector<int>> to(M);
vector<int> x(M);
vector<bool> seen(M, false);

void dfs(int v) {
  seen[v] = true;
  for (auto nv : to[v]) {
    if (seen[nv])
      continue;
    x[nv] += x[v];
    dfs(nv);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  rep(i, q) {
    int p;
    cin >> p;
    p--;
    int a;
    cin >> a;
    x[p] += a;
  }
  dfs(0);
  rep(i, n) printf("%d ", x[i]);
  cout << endl;
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define INF 2e9
#define ll long long

ll n, q;
ll ans[200001];
queue<ll> que[200001];
ll ad[200001];

void add(ll p, ll x) {
  ans[p] += x;
  int que_s = que[p].size();
  for (int k = 0; k < que_s; k++) {
    int s = que[p].front();
    que[p].pop();
    que[p].push(s);
    add(s, x);
  }
}

int main() {
  cin >> n >> q;

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    que[a].push(b);
  }

  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    ad[p] += x;
  }

  for (int i = 1; i <= n; i++) {
    if (ad[i] > 0) {
      add(i, ad[i]);
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define INF 2e9
#define ll long long

ll n, q;
ll ans[200001];
queue<ll> que[200001];
ll ad[200001];

void add(ll p, ll x) {
  ans[p] += x;
  int que_s = que[p].size();
  for (int k = 0; k < que_s; k++) {
    int s = que[p].front();
    que[p].pop();
    que[p].push(s);
    ad[s] += x;
  }
}

int main() {
  cin >> n >> q;

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    que[a].push(b);
  }

  for (int i = 0; i < q; i++) {
    int p, x;
    cin >> p >> x;
    ad[p] += x;
  }

  for (int i = 1; i <= n; i++) {
    if (ad[i] > 0) {
      add(i, ad[i]);
    }
  }

  for (int i = 1; i <= n; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

vector<vector<int>> to;
vector<int> ans;
void dfs(int v, int p = -1) {
  rep(i, to[v].size()) {
    int u = to[v][i];
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  int a, b;
  rep(i, n - 1) {
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ans.resize(n);
  int p, x;
  rep(i, q) {
    cin >> p >> x;
    --p;
    ans[p] += x;
  }
  dfs(0);
  rep(i, n) cout << ans[i] << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

vector<vector<int>> to;
vector<int> ans;
void dfs(int v, int p = -1) {
  rep(i, to[v].size()) {
    int u = to[v][i];
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}

int main() {
  int n, q;
  cin >> n >> q;
  to.resize(n);
  int a, b;
  rep(i, n - 1) {
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ans.resize(n);
  int p, x;
  rep(i, q) {
    cin >> p >> x;
    --p;
    ans[p] += x;
  }
  dfs(0);
  rep(i, n) cout << ans[i] << endl;

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
using namespace std;
typedef long long ll;

vector<int> to[200005]; // vector の配列
vector<int> ans;
void dfs(int v, int p = -1) { // デフォルト因数
  for (int u : to[v]) {
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}
int main(void) {
  int n, q;
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ans.resize(n);
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    --p;
    ans[p] += x;
  }
  dfs(0);
  rep(i, n) { cout << ans[i] << endl; }

  return 0;
}


Fixed submission:
#include <cmath>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
using namespace std;
typedef long long ll;

vector<int> to[200005]; // vector の配列
vector<int> ans;
void dfs(int v, int p = -1) { // デフォルト因数
  for (int i = 0; i < to[v].size(); i++) {
    int u = to[v][i];
    if (u == p)
      continue;
    ans[u] += ans[v];
    dfs(u, v);
  }
}
int main(void) {
  int n, q;
  cin >> n >> q;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  ans.resize(n);
  rep(i, q) {
    int p, x;
    cin >> p >> x;
    --p;
    ans[p] += x;
  }
  dfs(0);
  rep(i, n) { cout << ans[i] << endl; }

  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxd = 1e5 + 10;
struct {
  int v, net;
} e[maxd << 1];
int f[maxd], eid, v[maxd];
void init() {
  eid = 0;
  memset(f, -1, sizeof(f));
}
void insert(int x, int y) {
  e[eid].v = y;
  e[eid].net = f[x];
  f[x] = eid++;
}
int vis[maxd], ans[maxd];
void dfs(int x, int sum) {
  sum += v[x];
  ans[x] = sum;
  vis[x] = 1;
  for (int i = f[x]; i + 1; i = e[i].net) {
    int v = e[i].v;
    if (vis[v])
      continue;
    dfs(v, sum);
  }
}
int main() {
  // freopen("a.in","r",stdin);
  // freopen("k.out","w",stdout);
  int n, m;
  init();
  scanf("%d %d", &n, &m);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    insert(x, y);
    insert(y, x);
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x] += y;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d ", ans[i]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxd = 2e5 + 10;
struct {
  int v, net;
} e[maxd << 1];
int f[maxd], eid, v[maxd];
void init() {
  eid = 0;
  memset(f, -1, sizeof(f));
}
void insert(int x, int y) {
  e[eid].v = y;
  e[eid].net = f[x];
  f[x] = eid++;
}
int vis[maxd], ans[maxd];
void dfs(int x, int sum) {
  sum += v[x];
  ans[x] = sum;
  vis[x] = 1;
  for (int i = f[x]; i + 1; i = e[i].net) {
    int v = e[i].v;
    if (vis[v])
      continue;
    dfs(v, sum);
  }
}
int main() {
  // freopen("a.in","r",stdin);
  // freopen("k.out","w",stdout);
  int n, m;
  init();
  scanf("%d %d", &n, &m);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    insert(x, y);
    insert(y, x);
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d %d", &x, &y);
    v[x] += y;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    printf("%d ", ans[i]);
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> count(100000, 0);
vector<int> counter(100000, 0);
vector<int> vis(100000, 0);
void dfs(vector<int> v[], int index) {
  queue<int> q;
  q.push(index);
  int sum;
  count[0] = counter[0];
  while (!q.empty()) {
    int t = q.front();
    sum = count[t];
    q.pop();
    vis[t] = 1;
    for (int i = 0; i < v[t].size(); i++) {
      if (!vis[v[t][i]]) {
        vis[v[t][i]] = 1;
        count[v[t][i]] = sum + counter[v[t][i]];
        q.push(v[t][i]);
      }
    }
  }
}
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> v[n];
  int a, b;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int p, val;
  for (int i = 0; i < q; i++) {
    cin >> p;
    p--;
    cin >> val;
    counter[p] += val;
  }
  dfs(v, 0);
  for (int i = 0; i < n; i++)
    cout << count[i] << " ";
}


Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<int> count(300000, 0);
vector<int> counter(300000, 0);
vector<int> vis(300000, 0);
void dfs(vector<int> v[], int index) {
  queue<int> q;
  q.push(index);
  int sum;
  count[0] = counter[0];
  while (!q.empty()) {
    int t = q.front();
    sum = count[t];
    q.pop();
    vis[t] = 1;
    for (int i = 0; i < v[t].size(); i++) {
      if (!vis[v[t][i]]) {
        vis[v[t][i]] = 1;
        count[v[t][i]] = sum + counter[v[t][i]];
        q.push(v[t][i]);
      }
    }
  }
}
int main() {
  int n, q;
  cin >> n >> q;
  vector<int> v[n];
  int a, b;
  for (int i = 0; i < n - 1; i++) {
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  int p, val;
  for (int i = 0; i < q; i++) {
    cin >> p;
    p--;
    cin >> val;
    counter[p] += val;
  }
  dfs(v, 0);
  for (int i = 0; i < n; i++)
    cout << count[i] << " ";
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int N;
vector<int> G[20005];
vector<int> color;
vector<int> ans;
void dfs(int now) {
  color[now] = 1;
  for (int next : G[now]) {
    if (color[next] == 0) {
      ans[next] += ans[now];
      dfs(next);
    }
  }
  color[now] = 2;
}
int main() {
  cin >> N;
  int Q;
  cin >> Q;
  color.resize(N);
  ans.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  dfs(0);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include <iomanip>
#include <math.h>
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int N;
vector<int> G[200005];
vector<int> color;
vector<int> ans;
void dfs(int now) {
  color[now] = 1;
  for (int next : G[now]) {
    if (color[next] == 0) {
      ans[next] += ans[now];
      dfs(next);
    }
  }
  color[now] = 2;
}
int main() {
  cin >> N;
  int Q;
  cin >> Q;
  color.resize(N);
  ans.resize(N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < Q; i++) {
    int p, x;
    cin >> p >> x;
    p--;
    ans[p] += x;
  }
  dfs(0);
  for (int i = 0; i < N; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02936 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define print(n) cout << n << endl
#define rep(i, a, n) for (int i = a; i < n; i++)
#define ALL(s) s.begin(), s.end()
#define P pair<ll, ll>
#define vl vector<ll>
#define vi vector<int>
#define vvl vector<vector<ll>>
#define vvi vector<vector<int>>
const int M = 100111;
const int inf = 1000000007;
const long long INF = 1e18;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
int ddx[8] = {1, -1, 0, 0, 1, -1, -1, 1}, ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

/*
vvl comb(int n, int r) {
        vvl v(n + 1,vl(n + 1, 0));
        for (int i = 0; i < v.size(); i++) {
                v[i][0] = 1;
                v[i][i] = 1;
        }
        for (int j = 1; j < v.size(); j++) {
                for (int k = 1; k < j; k++) {
                        v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]);
                }
        }
        return v;
}
*/

vi v[M];
int n, q, a, b, p, x, c[M] = {};
void dfs(int now, int pre) {
  c[now] += c[pre];
  for (int next : v[now])
    if (next != pre) {
      dfs(next, now);
    }
}

int main() {
  cin >> n >> q;
  rep(i, 0, n - 1) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  rep(i, 0, q) cin >> p >> x, c[p] += x;
  dfs(1, 0);
  rep(i, 1, n + 1) {
    cout << c[i];
    if (i != n)
      cout << " ";
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define print(n) cout << n << endl
#define rep(i, a, n) for (int i = a; i < n; i++)
#define ALL(s) s.begin(), s.end()
#define P pair<ll, ll>
#define vl vector<ll>
#define vi vector<int>
#define vvl vector<vector<ll>>
#define vvi vector<vector<int>>
const int M = 100111;
const int inf = 1000000007;
const long long INF = 1e18;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1};
int ddx[8] = {1, -1, 0, 0, 1, -1, -1, 1}, ddy[8] = {0, 0, 1, -1, 1, -1, 1, -1};

/*
vvl comb(int n, int r) {
        vvl v(n + 1,vl(n + 1, 0));
        for (int i = 0; i < v.size(); i++) {
                v[i][0] = 1;
                v[i][i] = 1;
        }
        for (int j = 1; j < v.size(); j++) {
                for (int k = 1; k < j; k++) {
                        v[j][k] = (v[j - 1][k - 1] + v[j - 1][k]);
                }
        }
        return v;
}
*/

vi v[2 * M];
int n, q, a, b, p, x, c[2 * M] = {};
void dfs(int now, int pre) {
  c[now] += c[pre];
  for (int next : v[now])
    if (next != pre) {
      dfs(next, now);
    }
}

int main() {
  cin >> n >> q;
  rep(i, 0, n - 1) {
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  rep(i, 0, q) cin >> p >> x, c[p] += x;
  dfs(1, 0);
  rep(i, 1, n + 1) {
    cout << c[i];
    if (i != n)
      cout << " ";
  }
}
Problem p02936 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys

sys.setrecursionlimit(10**6)

n, q = map(int, input().split())

edges = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

counter = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    p -= 1
    counter[p] += x

ans = [0] * n


def f(par, node, count):
    count += counter[node]
    ans[node] = count
    for n in edges[node]:
        if n == par:
            continue
        f(node, n, count)


f(-1, 0, 0)
print(*ans)


Fixed submission:
import sys

sys.setrecursionlimit(10**6)
input = sys.stdin.readline

n, q = map(int, input().split())

edges = [[] for _ in range(n)]
for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

counter = [0] * n
for _ in range(q):
    p, x = map(int, input().split())
    p -= 1
    counter[p] += x

ans = [0] * n


def f(par, node, count):
    count += counter[node]
    ans[node] = count
    for n in edges[node]:
        if n == par:
            continue
        f(node, n, count)


f(-1, 0, 0)
print(*ans)
Problem p02936 (Python)

Buggy submission (status: Runtime Error):
N, Q = map(int, input().split())

tree = [[0] * 1 for i in range(N)]


tree_list = [0] * N
end = [0] * N

for index in range(N - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    tree[a].append(b)
    tree[b].append(a)

for index in range(Q):
    a, b = map(int, input().split())
    a -= 1
    tree_list[a] += b


def iter(node):
    end[node] = -1
    child = tree[node]
    for c in child:
        if end[c] == 0:
            tree_list[c] += tree_list[node]
            iter(c)


iter(0)

print(*tree_list)


Fixed submission:
import sys

sys.setrecursionlimit(10000000)
input = sys.stdin.readline

N, Q = map(int, input().split())

tree = [[0] * 1 for i in range(N)]


tree_list = [0] * N
end = [0] * N

for index in range(N - 1):
    a, b = map(lambda x: int(x) - 1, input().split())
    tree[a].append(b)
    tree[b].append(a)

for index in range(Q):
    a, b = map(int, input().split())
    a -= 1
    tree_list[a] += b


def iter(node):
    end[node] = -1
    child = tree[node]
    for c in child:
        if end[c] == 0:
            tree_list[c] += tree_list[node]
            iter(c)


iter(0)

print(*tree_list)