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

#include <bits/stdc++.h>
int main() {
  int W, H, K, N;
  std::cin >> W >> H >> K >> N;
  using P = std::pair<int, int>;
  std::vector<P> dog;
  for (int i = 0, y, x; i < N; i++) {
    std::cin >> x >> y, dog.push_back({y - 1, x - 1});
  }
  std::sort(dog.begin(), dog.end());
  const int WNUM = (W + 1) / 2;
  auto ok = [&](const int y, const int x) {
    return std::upper_bound(dog.begin(), dog.end(), P{y, x}) ==
           std::lower_bound(dog.begin(), dog.end(), P{y, x});
  };
  int two = 0;
  int ans = 0;
  for (int y = 2; y < H; y += 2) {
    std::vector<int> memo(W, -1);
    auto rec = [&](auto &&self, const int x) {
      if (x == -2) {
        return 0;
      }
      if (memo[x] != -1) {
        return memo[x];
      }
      if (x == 0) {
        return memo[x] = (ok(y - 1, x) ? 1 : 2);
      }
      return memo[x] = std::min((ok(y - 1, x - 1) ? 2 : 4) + self(self, x - 4),
                                (ok(y - 1, x) ? 1 : 2) + self(self, x - 2));
    };
    const int score = rec(rec, W - 1);
    ans += score, two += score - WNUM;
  }
  if (K < WNUM) {
    return std::cout << -1 << std::endl, 0;
  }
  K -= WNUM;
  const int t = std::min(K, two);
  ans -= 2 * t, K -= t, ans -= K;
  std::cout << std::max(0, ans) << std::endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);
  int W, H, K, N;
  std::cin >> W >> H >> K >> N;
  using P = std::pair<int, int>;
  std::set<P> dog;
  for (int i = 0, y, x; i < N; i++) {
    std::cin >> x >> y, dog.insert({y - 1, x - 1});
  }
  const int WNUM = (W + 1) / 2;
  auto ok = [&](const int y, const int x) {
    return dog.find({y, x}) == dog.end();
  };
  int two = 0;
  int ans = 0;
  for (int y = 2; y < H; y += 2) {
    std::vector<int> memo(W, -1);
    auto rec = [&](auto &&self, const int x) {
      if (x == -2) {
        return 0;
      }
      if (memo[x] != -1) {
        return memo[x];
      }
      if (x == 0) {
        return memo[x] = (ok(y - 1, x) ? 1 : 2);
      }
      return memo[x] = std::min((ok(y - 1, x - 1) ? 2 : 4) + self(self, x - 4),
                                (ok(y - 1, x) ? 1 : 2) + self(self, x - 2));
    };
    const int score = rec(rec, W - 1);
    ans += score, two += score - WNUM;
  }
  if (K < WNUM) {
    return std::cout << -1 << std::endl, 0;
  }
  K -= WNUM;
  const int t = std::min(K, two);
  ans -= 2 * t, K -= t, ans -= K;
  std::cout << std::max(0, ans) << std::endl;
  return 0;
}
Problem p01900 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
priority_queue<int> Q[3];
int a[500010];

int F(int bef, int aft) {
  int cnt = 0;
  while (true) {
    int pnt = (3 + aft - bef) % 3;
    if (Q[pnt].empty())
      break;
    cnt++;
    Q[pnt].pop();
    if (!Q[0].empty()) {
      while (!Q[0].empty()) {
        cnt++;
        Q[0].pop();
      }
    }
    bef = aft;
    if (aft == 1)
      aft = 2;
    else if (aft == 2)
      aft = 1;
  }
  if (!Q[0].empty() || !Q[1].empty() || !Q[2].empty())
    cnt++;
  return cnt;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    Q[a[i] % 3].push(a[i]);
  }
  int ans = F(0, 1);
  for (int i = 0; i < 3; i++) {
    while (!Q[i].empty())
      Q[i].pop();
  }
  for (int i = 0; i < n; i++) {
    Q[a[i] % 3].push(a[i]);
  }
  ans = max(ans, F(0, 2));
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
priority_queue<int> Q[3];
int a[100010];

int F(int bef, int aft) {
  int cnt = 0;
  while (true) {
    int pnt = (3 + aft - bef) % 3;
    if (Q[pnt].empty())
      break;
    cnt++;
    Q[pnt].pop();
    if (!Q[0].empty()) {
      while (!Q[0].empty()) {
        cnt++;
        Q[0].pop();
      }
    }
    bef = aft;
    if (aft == 1)
      aft = 2;
    else if (aft == 2)
      aft = 1;
  }
  if (!Q[0].empty() || !Q[1].empty() || !Q[2].empty())
    cnt++;
  return cnt;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    Q[a[i] % 3].push(a[i]);
  }
  int ans = F(0, 1);
  for (int i = 0; i < 3; i++) {
    while (!Q[i].empty())
      Q[i].pop();
  }
  for (int i = 0; i < n; i++) {
    Q[a[i] % 3].push(a[i]);
  }
  ans = max(ans, F(0, 2));
  cout << ans << endl;
  return 0;
}
Problem p01900 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < a; i++)
#define s second
#define f first
using namespace std;

int main(void) {
  int n, ary[3] = {0}, in, ret = 0;
  cin >> n;
  rep(i, n) {
    cin >> in;
    ary[in % 3]++;
  }
  if (ary[1] || ary[2])
    ret = ary[0];
  int mi = min(ary[1], ary[2]);
  int diff = max(ary[1], ary[2]) - mi;
  // cout<<diff<<endl;
  ret = ret + mi * 2 + min(3, diff);
  // cout<< min(3,diff)<<endl;

  cout << (ret == 0 && ary[0] ? 1 : ret) << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a) for (int i = 0; i < a; i++)
#define s second
#define f first
using namespace std;

int main(void) {
  int n, ary[3] = {0}, in, ret = 0;
  cin >> n;
  rep(i, n) {
    cin >> in;
    ary[in % 3]++;
  }
  if (ary[1] || ary[2])
    ret = ary[0];
  int h = 1, now = 0;
  ;
  while (true) {
    if (h == 2) { // h = 2
      int use = min(2, ary[2]);
      ret += use;
      ary[2] -= use;
      if (ary[1] != 0)
        h = 1;
      now = (use * 2 + now) % 3;
      if (now == 0)
        break;
    } else { // h = 1
      int use = min(2, ary[1]);
      ret += use;
      ary[1] -= use;
      if (ary[2] != 0)
        h = 2;
      now = (now + use) % 3;
      if (now == 0)
        break;
    }
  }
  cout << (ret == 0 && ary[0] ? 1 : ret) << endl;
}
Problem p01901 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int main() {
  ll t, s[SIZE], e[SIZE], r[SIZE] = {};
  int n;

  scanf("%lld%d", &t, &n);

  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", s + i, e + i);
  }

  for (int i = 1; i < n; i++) {
    r[i] = s[i] - e[i - 1] + r[i - 1];
  }

  ll ans = 0;

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

    ll *it = upper_bound(s, s + n, s[i] + t);

    if (it == s) {
      ans = max(ans, min(t, e[0] - s[0]));
    } else {
      it--;

      if (s[i] + t >= e[it - s])
        ans = max(ans, e[it - s] - s[i] - (r[it - s] - r[i]));
      else
        ans = max(ans, t - (r[it - s] - r[i]));
    }
  }

  printf("%lld\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << #x << " = " << x << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 10000

int main() {
  ll t, s[SIZE], e[SIZE], r[SIZE] = {};
  int n;

  scanf("%lld%d", &t, &n);

  for (int i = 0; i < n; i++) {
    scanf("%lld%lld", s + i, e + i);
  }

  for (int i = 1; i < n; i++) {
    r[i] = s[i] - e[i - 1] + r[i - 1];
  }

  ll ans = 0;

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

    ll *it = upper_bound(s, s + n, s[i] + t);

    if (it == s) {
      ans = max(ans, min(t, e[0] - s[0]));
    } else {
      it--;

      if (s[i] + t >= e[it - s])
        ans = max(ans, e[it - s] - s[i] - (r[it - s] - r[i]));
      else
        ans = max(ans, t - (r[it - s] - r[i]));
    }
  }

  printf("%lld\\n", ans);

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

#include <iostream>
using namespace std;

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

  for (int i = 0; i < m; i++) {
    int a;
    cin >> a;
  }
  cout << (n - m) - 1 << endl;
}

Error: TLE

Fixed submission:

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

int main() {
  vector<int> v;
  int n, m;
  cin >> n >> m;
  v.assign(n, 1);

  for (int i = 0; i < m; i++) {
    int a;
    cin >> a;
    v[a] = -1;
  }

  int count = 0;
  while (v.size() > 1) {
    int remove = 0;
    for (int i = 0; i < v.size(); i = i + 2) {
      if (v[i] == v[i + 1] && v[i] == -1) {
        remove += 2;
      } else if (v[i] == v[i + 1] && v[i] == 1) {
        remove += 1;
        count++;
      } else if (v[i] != v[i + 1]) {
        remove += 1;
      }
    }
    v.clear();
    v.assign(n - remove, 1);

    if (v.size() != 1 && v.size() % 2 == 1) {
      v.push_back(-1);
    }
  }
  cout << count << endl;
}
Problem p01905 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
#ifdef _DEBUG
#include "dump.hpp"
#else
#define dump(...)
#endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  cout << N - 1 - M << endl;
  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
// #ifdef _DEBUG
// #include "dump.hpp"
// #else
// #define dump(...)
// #endif

// #define int long long
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define rrep(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define all(c) begin(c), end(c)
const int INF =
    sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9) + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

  int N, M;
  cin >> N >> M;
  vector<vector<int>> v(1, vector<int>(N));
  rep(i, 0, N) v[0][i] = i;
  vector<int> alive(N, true);
  int ans(0);
  rep(i, 0, M) {
    int a;
    cin >> a;
    alive[a] = false;
  }

  rep(i, 0, log2(N)) {
    int j(0);
    vector<int> t;
    for (j = 0; j + 1 < N; j += 2) {
      if (alive[v[i][j]] && alive[v[i][j + 1]])
        t.push_back(j), ans++;
      else if (alive[j])
        t.push_back(j);
      else if (alive[j + 1])
        t.push_back(j + 1);
    }
    v.push_back(t);
    N /= 2;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> vec(m);
  rep(i, m) cin >> vec[i];

  vector<bool> b(n, true);
  rep(i, m) b[vec[i]] = false;

  int ans = 0;
  for (int d = 1; d < n; d *= 2) {
    for (int i = 0; i < n; i += d * 2) {
      if (b[i] && b[i + d])
        ans++;
      else if (b[i] || b[i + d])
        b[i] = true;
    }
    //    dbg(b);
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define mp(a, b) make_pair((a), (b))
#define pb(a) push_back((a))
#define all(x) (x).begin(), (x).end()
#define uniq(x) sort(all(x)), (x).erase(unique(all(x)), end(x))
#define fi first
#define se second
#define dbg(x) cout << #x " = " << ((x)) << endl
template <class T, class U>
ostream &operator<<(ostream &o, const pair<T, U> &p) {
  o << "(" << p.fi << "," << p.se << ")";
  return o;
}
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "[";
  for (T t : v) {
    o << t << ",";
  }
  o << "]";
  return o;
}

#define INF 2147483600

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> vec(m);
  rep(i, m) cin >> vec[i];

  vector<bool> b(n, true);
  rep(i, m) b[vec[i] - 1] = false;

  int ans = 0;
  for (int d = 1; d < n; d *= 2) {
    for (int i = 0; i < n; i += d * 2) {
      if (b[i] && b[i + d])
        ans++;
      else if (b[i] || b[i + d])
        b[i] = true;
    }
    //    dbg(b);
  }

  cout << ans << endl;

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

#include <stdio.h>
int main() {
  int N, Y, b, sum;
  scanf("%d %d", &N, &Y);
  for (int i = 0; i < Y; i++) {
    scanf("%d", &b);
  }
  sum = 0;
  for (;;) {
    N = N / 2;
    sum = sum + N;
    if (N == 1)
      break;
  }
  printf("%d\\n", sum - Y);
}

Error: 0

Fixed submission:

#include <stdio.h>
int main() {
  int answer, N, m, a;
  scanf("%d %d", &N, &m);
  int M[m];
  for (int i = 0; i < N; i++) {
    M[i] = i;
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", a);
    M[a] = 1000;
  }
  int L = 0;
  while (N > 1) {
    N /= 2;
    L += N;
  }
  for (int i = 0; i < N; i = i + 2) {
    if (M[i] != i || M[i + 1] != (i + 1)) {
      L = L - 1;
    }
  }
  printf("%d\\n", L);

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
int main() {
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  vector<int> tbl(N);
  REP(i, N) {
    int maxVal = A[i], minVal = A[i];
    REP(j, M) {
      maxVal = max(maxVal, A[(i + j) % N]);
      minVal = min(minVal, A[(i + j) % N]);
    }
    tbl[i] = maxVal - minVal;
  }
  int k = 0;
  int ans = 0;
  while (true) {
    ans += tbl[k];
    k += M;
    k %= N;
    if (k == 0)
      break;
  }
  cout << ans << endl;
}

Error: -6
malloc(): corrupted top size


Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define DEBUG(x) cerr << #x << " = " << x << endl
#define int long long
struct Edge {
  int a, b, s, t, cost;
  Edge() {}
  Edge(int a_, int b_, int s_, int t_, int cost_)
      : a(a_), b(b_), s(s_), t(t_), cost(cost_) {}
};
struct State {
  int v, t, cost;
  State(int v_, int t_, int cost_) : v(v_), t(t_), cost(cost_) {}
  bool operator>(const State &other) const { return cost > other.cost; }
};
signed main() {
  int N, M, S, T;
  cin >> N >> M >> S >> T;
  S--, T--;
  vector<Edge> E(M);
  REP(i, M) {
    cin >> E[i].a >> E[i].b >> E[i].s >> E[i].t;
    E[i].a--;
    E[i].b--;
    E[i].t += E[i].s;
    E[i].cost = 0;
  }
  vector<vector<int>> TIME(N);
  TIME[S].push_back(0);
  REP(i, M) {
    TIME[E[i].a].push_back(E[i].s);
    TIME[E[i].b].push_back(E[i].t);
  }
  vector<map<int, vector<Edge>>> G(N);
  REP(i, N) {
    sort(TIME[i].begin(), TIME[i].end());
    REP(j, (int)TIME[i].size() - 1) {
      if (TIME[i][j] != TIME[i][j + 1]) {
        G[i][TIME[i][j]].emplace_back(i, i, TIME[i][j], TIME[i][j + 1],
                                      TIME[i][j + 1] - TIME[i][j]);
      }
    }
  }
  REP(i, M) { G[E[i].a][E[i].s].push_back(E[i]); }
  // REP(i,N) {
  //   for(const pair<int, vector<Edge>> &pp : G[i]) {
  //     for(const Edge &e : pp.second) {
  //       cerr << e.a << " => " << e.b << " (" << e.s << "," << e.t << ") C="
  //       << e.cost << endl;
  //     }
  //   }
  // }
  vector<map<int, int>> D(N);
  priority_queue<State, vector<State>, greater<State>> Q;
  Q.emplace(S, 0, 0);
  D[S][0] = 0;
  while (!Q.empty()) {
    State s = Q.top();
    Q.pop();
    if (D[s.v].count(s.t) && s.cost > D[s.v][s.t])
      continue;
    for (const Edge &e : G[s.v][s.t]) {
      if ((D[e.b].count(e.t) == 0) || (D[e.b][e.t] > s.cost + e.cost)) {
        D[e.b][e.t] = s.cost + e.cost;
        Q.emplace(e.b, e.t, D[e.b][e.t]);
      }
    }
  }
  int ans = (int)1e18;
  for (pair<int, int> iter : D[T]) {
    ans = min(ans, iter.second);
  }
  cout << ans << endl;
}
Problem p01906 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define lol(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int a[1100];
pair<int, int> F(int left, int right) {
  int p = -1, q = mod;
  for (int i = left; i < right; i++) {
    p = max(p, a[i]);
    q = min(q, a[i]);
  }
  return make_pair(p, q);
}
int main() {
  int n, m;
  cin >> n >> m;
  lol(i, n) cin >> a[i];
  if (n == m) {
    pair<int, int> tmp = F(0, n);
    cout << tmp.first - tmp.second << endl;
    return 0;
  }
  int left = 0, right = m;
  int ans = 0;
  while (true) {
    int p, q;
    if (left < right) {
      pair<int, int> tmp = F(left, right);
      p = tmp.first, q = tmp.second;
    } else {
      pair<int, int> tmp = F(0, right);
      p = tmp.first, q = tmp.second;
      tmp = F(left, n);
      p = max(p, tmp.first);
      q = min(q, tmp.second);
    }
    ans += p - q;
    left = right % n;
    right = (right + m) % n;
    if (right <= 0)
      right += n;
    if (left == 0)
      break;
  }
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define lol(i, n) for (int i = 0; i < n; i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int a[110];
pair<int, int> F(int left, int right) {
  int p = -1, q = mod;
  for (int i = left; i < right; i++) {
    p = max(p, a[i]);
    q = min(q, a[i]);
  }
  return make_pair(p, q);
}
int main() {
  int n, m;
  cin >> n >> m;
  lol(i, n) cin >> a[i];
  if (n == m) {
    pair<int, int> tmp = F(0, n);
    cout << tmp.first - tmp.second << endl;
    return 0;
  }
  int left = 0, right = m;
  int ans = 0;
  while (true) {
    int p, q;
    if (left < right) {
      pair<int, int> tmp = F(left, right);
      p = tmp.first, q = tmp.second;
    } else {
      pair<int, int> tmp = F(0, right);
      p = tmp.first, q = tmp.second;
      tmp = F(left, n);
      p = max(p, tmp.first);
      q = min(q, tmp.second);
    }
    ans += p - q;
    left = right % n;
    right = (right + m) % n;
    if (right <= 0)
      right += n;
    if (left == 0)
      break;
  }
  cout << ans << endl;
  return 0;
}
Problem p01906 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

const int MAX_N = 1000;
int N, M;
int A[MAX_N];

int gcf(int a, int b);

int ans = 0;
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }

  if (N % M == 0) {
    for (int i = 0; i < N; i += M) {
      int max = 0, min = 1000;
      for (int j = 0; j < M; j++) {
        if (A[i + j] < min)
          min = A[i + j];
        if (A[i + j] > max)
          max = A[i + j];
      }
      ans += max - min;
    }
  } else {
    int gnum = 0;
    int gcfn = gcf(N, M);
    gnum = N / gcfn;
    for (int i = 0; i < gnum; i++) {
      int max = 0, min = 1000;
      for (int j = 0; j < M; j++) {
        int k = (i * M + j) % (N);
        if (A[k] < min)
          min = A[k];
        if (A[k] > max)
          max = A[k];
      }
      ans += max - min;
    }
  }

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

  return 0;
}

int gcf(int a, int b) {
  if (a < b) {
    int dum = a;
    a = b;
    b = dum;
  }
  if (a % b == 1)
    return 1;
  else if (a % b == 0)
    return b;
  else
    gcf(b, a % b);
}

Error: 0

Fixed submission:

#include <cstdio>

const int MAX_N = 1000;
int N, M;
int A[MAX_N];

int gcf(int a, int b);

int ans = 0;
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < N; i++) {
    scanf("%d", &A[i]);
  }

  if (N % M == 0) {
    for (int i = 0; i < N; i += M) {
      int max = 0, min = 1000;
      for (int j = 0; j < M; j++) {
        if (A[i + j] < min)
          min = A[i + j];
        if (A[i + j] > max)
          max = A[i + j];
      }
      ans += max - min;
    }
  } else {
    int gnum = 0;
    int gcfn = gcf(N, M);
    gnum = N / gcfn;
    for (int i = 0; i < gnum; i++) {
      int max = 0, min = 1000;
      for (int j = 0; j < M; j++) {
        int dum = 0;
        if (i * M - i * N + j < 0)
          dum = M + 1;
        if (A[i * M - i * N + j + dum] < min)
          min = A[i * M - i * N + j + dum];
        if (A[i * M - i * N + j + dum] > max)
          max = A[i * M - i * N + j + dum];
      }
      ans += max - min;
    }
  }

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

  return 0;
}

int gcf(int a, int b) {
  if (a < b) {
    int dum = a;
    a = b;
    b = dum;
  }
  if (a % b == 1)
    return 1;
  else if (a % b == 0)
    return b;
  else
    gcf(b, a % b);
}
Problem p01907 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <math.h>
#include <vector>

using namespace std;

const int MAX_N = 100001;

struct edge {
  int to, cost;
};
vector<edge> tree[MAX_N];
bool flag[MAX_N] = {false};
int depth[MAX_N];
int s, t, c;
double sum = 0.0;
double ans = 0.0;
double p;
int N;

void dfs(int v, int d) {
  flag[v] = true;
  depth[v] = d;
  for (int i = 0; i < tree[v].size(); i++) {
    if (!flag[tree[v][i].to]) {
      sum += tree[v][i].cost * pow(p, (d + 1.0));
      dfs(tree[v][i].to, d + 1);
    }
  }
}

int main() {
  scanf("%lf %d", &p, &N);

  for (int i = 0; i < N - 1; i++) {
    scanf("%d %d %d", &s, &t, &c);
    s--;
    t--;
    edge e;
    e.to = t;
    e.cost = c;
    tree[s].push_back(e);
    e.to = s;
    tree[t].push_back(e);
  }

  dfs(0, 0);

  ans = 2 * sum;
  for (int i = 1; i < N; i++) {
    ans += sum * pow(p, depth[i]);
  }
  printf("%.10lf\\n", ans);

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <math.h>
#include <vector>

using namespace std;

const int MAX_N = 10000;

struct edge {
  int to, cost;
};
vector<edge> tree[MAX_N];
bool flag[MAX_N] = {false};
int depth[MAX_N];
int s, t, c;
double sum = 0.0;
double ans = 0.0;
double p;
int N;

void dfs(int v, int d) {
  flag[v] = true;
  depth[v] = d;
  for (int i = 0; i < tree[v].size(); i++) {
    if (!flag[tree[v][i].to]) {
      sum += tree[v][i].cost * pow(p, (d + 1.0));
      dfs(tree[v][i].to, d + 1);
    }
  }
}

int main() {
  scanf("%lf %d", &p, &N);

  for (int i = 0; i < N - 1; i++) {
    scanf("%d %d %d", &s, &t, &c);
    s--;
    t--;
    edge e;
    e.to = t;
    e.cost = c;
    tree[s].push_back(e);
    e.to = s;
    tree[t].push_back(e);
  }

  dfs(0, 0);

  ans = 2 * sum;
  for (int i = 1; i < N; i++) {
    ans += sum * pow(p, depth[i]);
  }
  printf("%.10lf\\n", ans);

  return 0;
}