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

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>

#include <algorithm>
#include <array>
#include <limits>
#include <queue>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> l(n);
  vector<tuple<int, int, int>> intervals(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> l[i];
    intervals[i] = make_tuple(i, x[i] - l[i], x[i] + l[i]);
  }
  sort(intervals.begin(), intervals.end(),
       [](tuple<int, int, int> x1, tuple<int, int, int> x2) {
         return get<2>(x1) < get<2>(x2);
       });
  int cnt = 0;
  while (!intervals.empty()) {
    auto interval = intervals[0];
    cnt += 1;
    std::vector<tuple<int, int, int>> new_intervals;
    std::copy_if(intervals.begin(), intervals.end(),
                 std::back_inserter(new_intervals),
                 [interval](tuple<int, int, int> item) {
                   return get<1>(item) >= get<2>(interval);
                 });
    intervals = new_intervals;
  }
  cout << cnt << endl;
}


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

#include <algorithm>
#include <array>
#include <limits>
#include <queue>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> x(n);
  vector<int> l(n);
  vector<tuple<int, int, int>> intervals(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> l[i];
    intervals[i] = make_tuple(i, x[i] - l[i], x[i] + l[i]);
  }
  sort(intervals.begin(), intervals.end(),
       [](tuple<int, int, int> x1, tuple<int, int, int> x2) {
         return get<2>(x1) < get<2>(x2);
       });
  int cnt = 0;
  int idx = 0;
  while (idx < n) {
    auto interval = intervals[idx];
    ++idx;
    ++cnt;
    for (; idx < n;) {
      if (get<2>(interval) > get<1>(intervals[idx])) {
        ++idx;
      } else {
        break;
      }
    }
  }
  cout << cnt << endl;
}
Problem p02797 (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 set2d(array, val, m, n) memset(array, val, sizeof(array[0][0]) * m * n);
#include <bits/stdc++.h>
using namespace std;
ll gcd(ll a, ll b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll po(ll base, ll powerRaised) {
  if (powerRaised != 0)
    return (base * po(base, powerRaised - 1));
  else
    return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif //*/
  ll n, k, s;
  cin >> n >> k >> s;
  for (int i = 1; i <= k; i++)
    cout << s << " ";
  if (s == 1) {
    for (int i = k + 1; i <= n; i++)
      cout << "2 ";
    return 0;
  } else {
    ll start = 2;
    if (s == 2)
      start = 3;
    ll i = k;
    while (i <= n) {
      if (i == n)
        break;
      cout << start << " ";
      i++;
      if (i == n)
        break;
      cout << s - start + 1 << " ";
      i++;
    }
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}

Fixed submission:
#define ll long long
#define MOD 1000000007
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define set2d(array, val, m, n) memset(array, val, sizeof(array[0][0]) * m * n);
#include <bits/stdc++.h>
using namespace std;
ll gcd(ll a, ll b) {
  if (!b)
    return a;
  return gcd(b, a % b);
}
ll power(ll x, ll y, ll p) {
  ll res = 1;
  x %= p;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll po(ll base, ll powerRaised) {
  if (powerRaised != 0)
    return (base * po(base, powerRaised - 1));
  else
    return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  // #ifndef ONLINE_JUDGE
  //     freopen("input.txt", "r", stdin);
  //     freopen("output.txt", "w", stdout);
  // #endif //*/
  ll n, k, s;
  cin >> n >> k >> s;
  for (int i = 1; i <= k; i++)
    cout << s << " ";
  if (s == 1) {
    for (int i = k + 1; i <= n; i++)
      cout << "2 ";
    return 0;
  } else {
    ll start = 2;
    if (s == 2)
      start = 3;
    ll i = k;
    while (i <= n) {
      if (i == n)
        break;
      cout << start << " ";
      i++;
      if (i == n)
        break;
      cout << s - start + 1 << " ";
      i++;
    }
  }
  cerr << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
  return 0;
}
Problem p02797 (C++)

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

using i64 = std::int64_t;

namespace io {
constexpr int is = 1 << 17;
char ib[is], *ip = ib + is, it;
inline char getc() {
  if (ip == ib + is) {
    read(STDIN_FILENO, ib, is);
    ip = ib;
  }
  return *ip++;
}
inline i64 scan() {
  i64 r = 0;
  if (ip + 16 > ib + is)
    while ((it = getc()) & 16)
      r = r * 10 + it - '0';
  else
    while ((it = *ip++) & 16)
      r = r * 10 + it - '0';
  return r;
}
constexpr int os = 1 << 17;
char ob[os], *op = ob, ot;
inline void putc(const char c) {
  *op++ = c;
  if (op == ob + os) {
    write(STDOUT_FILENO, ob, os);
    op = ob;
  }
}
/* x shoud be greater than or equal to zero */
inline void print(i64 x) {
  char d[20];
  int i = 0;
  while (true) {
    d[i] = x % 10 + 48;
    x /= 10;
    ++i;
    if (x <= 0)
      break;
  }
  while (i--) {
    putc(d[i]);
  }
}
inline void flush() { write(STDOUT_FILENO, ob, op - ob); }
} // namespace io

signed main() {
  i64 n = io::scan();
  i64 k = io::scan();
  i64 s = io::scan();
  for (int i = 0; i < n; i++) {
    io::print(i % k ? 7 : s);
    io::putc(' ');
  }
  io::flush();
}

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

using i64 = std::int64_t;

namespace io {
constexpr int is = 1 << 17;
char ib[is], *ip = ib + is, it;
inline char getc() {
  if (ip == ib + is) {
    read(STDIN_FILENO, ib, is);
    ip = ib;
  }
  return *ip++;
}
inline i64 scan() {
  i64 r = 0;
  if (ip + 16 > ib + is)
    while ((it = getc()) & 16)
      r = r * 10 + it - '0';
  else
    while ((it = *ip++) & 16)
      r = r * 10 + it - '0';
  return r;
}
constexpr int os = 1 << 17;
char ob[os], *op = ob, ot;
inline void putc(const char c) {
  *op++ = c;
  if (op == ob + os) {
    write(STDOUT_FILENO, ob, os);
    op = ob;
  }
}
/* x shoud be greater than or equal to zero */
inline void print(i64 x) {
  char d[20];
  int i = 0;
  while (true) {
    d[i] = x % 10 + 48;
    x /= 10;
    ++i;
    if (x <= 0)
      break;
  }
  while (i--) {
    putc(d[i]);
  }
}
inline void flush() { write(STDOUT_FILENO, ob, op - ob); }
} // namespace io

signed main() {
  i64 n = io::scan();
  i64 k = io::scan();
  i64 s = io::scan();
  for (int i = 0; i < n; i++) {
    io::print(i < k ? s : 7);
    io::putc(' ');
  }
  io::flush();
}
Problem p02797 (C++)

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

char buf[1000000];
signed main() {
  int n, k;
  char s[11];
  scanf("%d %d %s", &n, &k, s);
  char *p = buf;
  size_t len = strlen(s);
  s[len++] = ' ';
  int i = 0;
  for (; i < k; i++) {
    strcpy(p, s);
    p += len;
  }
  for (; i < n; i++) {
    strcpy(p, "7 ");
    p += 2;
  }
  write(STDOUT_FILENO, buf, p - buf);
}


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

char buf[1000000];
signed main() {
  int n, k;
  char s[11];
  scanf("%d %d %s", &n, &k, s);
  char *p = buf;
  size_t len = strlen(s);
  s[len++] = ' ';
  int i = 0;
  for (; i < k; i++) {
    strcpy(p, s);
    p += len;
    if (p - buf > 10000) {
      write(STDOUT_FILENO, buf, p - buf);
      p = buf;
    }
  }
  for (; i < n; i++) {
    strcpy(p, "7 ");
    p += 2;
  }
  write(STDOUT_FILENO, buf, p - buf);
}
Problem p02797 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define ifor(n) for (int i = 0; i < n; i++)
#define jfor(n) for (int j = 0; j < n; j++)
#define Ifor(n) for (int i = 1; i < n; i++)
#define Jfor(n) for (int j = 1; j < n; j++)
const int inf = 0x3f3f3f3f;
int main() {
  int n, k, s;
  cin >> n >> k >> s;
  n = n - k;
  while (k--)
    printf("%d ", s);
  if (s > 100000)
    s = 1;
  else
    s++;
  n--;
  while (n--)
    printf("%d ", s);
  cout << s << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = b - 1; i >= a; i--)
#define ifor(n) for (int i = 0; i < n; i++)
#define jfor(n) for (int j = 0; j < n; j++)
#define Ifor(n) for (int i = 1; i < n; i++)
#define Jfor(n) for (int j = 1; j < n; j++)
const int inf = 0x3f3f3f3f;
int main() {
  int n, k, s;
  cin >> n >> k >> s;
  ifor(k) cout << s << " ";
  if (s < 1000000000) {
    n = n - k;
    ifor(n) cout << s + 1 << " ";
  } else {
    n = n - k;
    ifor(n) cout << s - 1 << " ";
  }
  cout << endl;
}
Problem p02797 (C++)

Buggy submission (status: Runtime Error):


#include <bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>
#define IP pair<int, P>
#define PI pair<P, int>
#define PP pair<P, P>
#define Yes(f)                                                                 \
  { cout << (f ? "Yes" : "No") << endl; }
#define YES(f)                                                                 \
  { cout << (f ? "YES" : "NO") << endl; }

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n, k, s;
  cin >> n >> k >> s;

  rep(i, n) { cout << s / k + !!(s % (i + 1)) << ' '; }
  cout << endl;
}

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
#define inf INT_MAX
#define INF LLONG_MAX
#define ll long long
#define ull unsigned long long
#define M (int)(1e9 + 7)
#define P pair<int, int>
#define FOR(i, m, n) for (int i = (int)m; i < (int)n; i++)
#define RFOR(i, m, n) for (int i = (int)m; i >= (int)n; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define all(a) a.begin(), a.end()
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
#define F first
#define S second
#define PB push_back
#define EB emplace_back
#define int ll
#define vi vector<int>
#define IP pair<int, P>
#define PI pair<P, int>
#define PP pair<P, P>
#define Yes(f)                                                                 \
  { cout << (f ? "Yes" : "No") << endl; }
#define YES(f)                                                                 \
  { cout << (f ? "YES" : "NO") << endl; }

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int n, k, s;
  cin >> n >> k >> s;

  rep(i, n) {
    if (i < k) {
      cout << s << ' ';
    } else {
      cout << ((s + 1) % (int)(1e9)) << ' ';
    }
  }

  cout << endl;
}
Problem p02797 (C++)

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

int main() {
  int n, k, s;
  cin >> n >> k >> s;

  // 4 2 3
  // 1 1 1 1

  // 5 3 100
  // 100 100 100 1 1

  vector<int> ans;
  for (int i = 0; i < k; ++i) {
    ans.push_back(s);
  }
  int tmp = s;
  while (!(1 <= tmp && tmp <= 1e9) or tmp == s)
    ++tmp;
  for (int i = k; i < n; ++i) {
    ans.push_back(tmp);
  }

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

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

int main() {
  int n, k, s;
  cin >> n >> k >> s;

  // 4 2 3
  // 1 1 1 1

  // 5 3 100
  // 100 100 100 1 1

  vector<int> ans;
  for (int i = 0; i < k; ++i) {
    ans.push_back(s);
  }
  int tmp = s;
  while (!(1 <= tmp && tmp <= 1e9) or tmp == s)
    tmp = (tmp + 1) % int(1e9);
  for (int i = k; i < n; ++i) {
    ans.push_back(tmp);
  }

  cout << ans[0];
  for (int i = 1; i < n; ++i) {
    cout << " " << ans[i];
  }
  cout << endl;
  return 0;
}
Problem p02797 (Python)

Buggy submission (status: Runtime Error):
def prime_decomposition(n):
    ret = set()

    x = n

    divisor = 2
    while x % divisor == 0:
        x //= divisor
        ret.add(divisor)

    divisor = 3
    while divisor * divisor <= n:
        while x % divisor == 0:
            x //= divisor
            ret.add(divisor)
        divisor += 2

    if x > 1:
        ret.add(x)

    return ret


def main():
    from functools import reduce
    from operator import mul

    n, k, s = map(int, input().split())

    pd = prime_decomposition(s)
    m = reduce(mul, pd) - 1  # 互いに素

    ret = [s] * k + [m] * (n - k)
    print(*ret)


if __name__ == "__main__":
    main()


Fixed submission:
def prime_decomposition(n):
    ret = set()

    x = n

    divisor = 2
    while x % divisor == 0:
        x //= divisor
        ret.add(divisor)

    divisor = 3
    while divisor * divisor <= n:
        while x % divisor == 0:
            x //= divisor
            ret.add(divisor)
        divisor += 2

    if x > 1:
        ret.add(x)

    return ret


def main():
    from functools import reduce
    from operator import mul

    n, k, s = map(int, input().split())

    pd = prime_decomposition(s)
    if not pd:
        m = s + 1
    else:
        m = reduce(mul, pd) - 1  # 互いに素

    ret = [s] * k + [m] * (n - k)
    print(*ret)


if __name__ == "__main__":
    main()
Problem p02797 (Python)

Buggy submission (status: Runtime Error):
from sys import stdin

N, K, S = [int(x) for x in stdin.readline().rstrip().split()]

ans = [S] * K
n = (N - K) // (S - 1)
ans.extend([S + 1] * (N - K))

print(*ans)


Fixed submission:
from sys import stdin

N, K, S = [int(x) for x in stdin.readline().rstrip().split()]

ans = [S] * K
if S == 10**9:
    ans.extend([1] * (N - K))
else:
    ans.extend([S + 1] * (N - K))

print(*ans)
Problem p02797 (Python)

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

M = 1_000_000_000
_S = M - 1 if S == M else M
print(" ".join(list(map(str, [S] * K + [_S] * (N - K)))))


Fixed submission:
N, K, S = map(int, input().split())

M = 1000000000
_S = M - 1 if S == M else M
print(" ".join(list(map(str, [S] * K + [_S] * (N - K)))))
Problem p02797 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define INF (ll)1e9
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, k, s;
  cin >> n >> k >> s;
  vector<ll> ans;
  ll left, right;
  if (k == n) {
    while (k--) {
      ans.pb(s);
    }
    for (ll i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
  } else if (s == 1) {
    while (k--) {
      ans.pb(1);
    }
    ll size = ans.size();
    while (size != n) {
      ans.pb(INF);
      size++;
    }
    for (int i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
  } else {
    left = s / 2;
    // cerr<<left;
    right = s - left;
    k--;
    ans.pb(left);
    ans.pb(right);
    ll i = 0;
    while (k--) {
      if (i & 1) {
        ans.pb(right);
      } else
        ans.pb(left);

      i++;
    }
    ll p = ans.size();
    ll MI = INF;
    if (s == INF) {
      MI--;
    }
    if (p < n) {
      // ll size = ans.size();
      // cerr<<"\n"<<INF-1<<"\n";
      while (p != n) {
        // cerr<<"\n\n";
        ans.pb(MI);
        p++;
      }
    }

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


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define INF (ll)1e9
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll n, k, s;
  cin >> n >> k >> s;
  vector<ll> ans;
  ll left, right;
  if (k == n) {
    while (k--) {
      ans.pb(s);
    }
    for (ll i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
  } else if (s == 1) {
    while (k--) {
      ans.pb(1);
    }
    ll size = ans.size();
    while (size != n) {
      ans.pb(INF);
      size++;
    }
    for (int i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
  } else if (k == 0) {
    ll MI = INF;
    if (s == INF) {
      MI--;
    }
    ans.resize(n, MI);
    for (auto it : ans) {
      cout << it << " ";
    }
  } else {
    left = s / 2;
    // cerr<<left;
    right = s - left;
    k--;
    ans.pb(left);
    ans.pb(right);
    ll i = 0;
    while (k--) {
      if (i & 1) {
        ans.pb(right);
      } else
        ans.pb(left);

      i++;
    }
    ll p = ans.size();
    ll MI = INF;
    if (s == INF) {
      MI--;
    }
    if (p < n) {
      // ll size = ans.size();
      // cerr<<"\n"<<INF-1<<"\n";
      while (p != n) {
        // cerr<<"\n\n";
        ans.pb(MI);
        p++;
      }
    }

    for (i = 0; i < n; i++) {
      cout << ans[i] << " ";
    }
  }
}
Problem p02797 (C++)

Buggy submission (status: Runtime Error):
/**
 *   author  : The San Cao (caothesan@gmail.com)
 *   created : 2020.02.08 11:01:47 +07
 */

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

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n, k, s;
  cin >> n >> k >> s;
  vector<int> result(k - 1, s);
  int m = min(n - k + 1, s);
  for (int i = 0; i < m - 1; ++i)
    result.push_back(s / m);
  result.push_back(s / m + s % m);
  while (result.size() < n)
    result.push_back(s + 1);
  for (auto it : result)
    cout << it << " ";
  return 0;
}


Fixed submission:
/**
 *   author  : The San Cao (caothesan@gmail.com)
 *   created : 2020.02.08 11:01:47 +07
 */

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

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n, k, s;
  cin >> n >> k >> s;
  for (int i = 0; i < n; ++i)
    cout << s + (i >= k) * (s == 1 ? 1 : -1) << " ";
  return 0;
}
Problem p02797 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
mt19937 rng(std::chrono::duration_cast<std::chrono::nanoseconds>(
                chrono::high_resolution_clock::now().time_since_epoch())
                .count());
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 1000005;
#define M 1000000007
#define double long double
#define BINF 10000000000000
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 15000001
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;

/*
Created at 15:28
*/

#undef int
int main() {
#define int long long int
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("ip.txt", "r", stdin);
  freopen("op.txt", "w", stdout);
#endif

  int n, k, s;
  cin >> n >> k >> s;
  int a[n];
  for (int i = 0; i < k; i++) {
    a[i] = s;
  }
  int sum = 0;
  for (int i = k; i < n; i++) {
    sum++;
    if (sum == s) {
      a[i] = s + 1;
      sum = 0;
      continue;
    } else
      a[i] = 1;
  }

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
mt19937 rng(std::chrono::duration_cast<std::chrono::nanoseconds>(
                chrono::high_resolution_clock::now().time_since_epoch())
                .count());
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 1000005;
#define M 1000000007
#define double long double
#define BINF 10000000000000
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 15000001
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;

/*
Created at 15:28
*/

#undef int
int main() {
#define int long long int
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n, k, s;
  cin >> n >> k >> s;
  int a[n];
  for (int i = 0; i < k; i++) {
    a[i] = s;
  }
  int sum = 0;
  for (int i = k; i < n; i++) {
    sum++;
    if (sum == s) {
      a[i] = s + 1;
      sum = 0;
      continue;
    } else
      a[i] = 1;
  }

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

  return 0;
}
Problem p02797 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, k, s;
  cin >> n >> k >> s;
  ll a[n];
  for (int i = 0; i < k; i++) {
    a[i] = s;
  }
  ll temp = s / (n - k);
  if (temp > 1)
    temp = 1;
  else
    temp = 10;
  for (int i = k; i < n; i++) {
    a[i] = temp;
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
  ll n, k, s;
  cin >> n >> k >> s;
  ll a[n];
  for (int i = 0; i < k; i++) {
    a[i] = s;
  }
  ll temp = 1000000000;
  if (s == temp)
    temp = 999999999;

  for (int i = k; i < n; i++) {
    a[i] = temp;
  }
  for (int i = 0; i < n; i++) {
    cout << a[i] << " ";
  }
}
Problem p02797 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define endl "\n"
#define print(x) cout << #x << " = " << x << endl;

#define MOD 1000000007

int main() {
  ll n, k, s;
  cin >> n >> k >> s;
  vector<ll> ans;
  if (s != 1000000000) {
    rep(i, 0, k) { ans.pb(s); }
    rep(i, 0, n - k) { ans.pb(1000000000); }
  } else {
    assert(false);
  }
  rep(i, 0, ans.sz) {
    cout << ans[i];
    if (i != ans.sz - 1)
      cout << " ";
  }
  cout << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long ll;

#define sz size()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(c) (c).begin(), (c).end()
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define per(i, a, b) for (ll i = b - 1LL; i >= (a); --i)
#define clr(a, b) memset((a), (b), sizeof(a))
#define ctos(c) string(1, c)
#define endl "\n"
#define print(x) cout << #x << " = " << x << endl;

#define MOD 1000000007

int main() {
  ll n, k, s;
  cin >> n >> k >> s;
  vector<ll> ans;
  if (s != 1000000000) {
    rep(i, 0, k) { ans.pb(s); }
    rep(i, 0, n - k) { ans.pb(1000000000); }
  } else {
    rep(i, 0, k) { ans.pb(1000000000); }
    rep(i, 0, n - k) { ans.pb(1); }
  }
  rep(i, 0, ans.sz) {
    cout << ans[i];
    if (i != ans.sz - 1)
      cout << " ";
  }
  cout << endl;
  return 0;
}
Problem p02797 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1e9;
const long double EPS = 1e-10;

int main() {
  int n, s, k;
  cin >> n >> s >> k;

  vector<int> a(n, INF);
  if (s == INF) {
    rep(i, n) a[i] = 1;
  }
  rep(i, k) a[i] = s;

  rep(i, n) {
    if (i)
      cout << " ";
    cout << a[i];
  }
  cout << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1e9;
const long double EPS = 1e-10;

int main() {
  int n, k, s;
  cin >> n >> k >> s;

  vector<int> a(n, INF);
  if (s == INF) {
    rep(i, n) a[i] = 1;
  }
  rep(i, k) a[i] = s;

  rep(i, n) {
    if (i)
      cout << " ";
    cout << a[i];
  }
  cout << endl;

  return 0;
}
Problem p02797 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, int> pli;
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;
}

constexpr lint mod = 1e9 + 7;
constexpr lint INF = mod * mod;
constexpr int MAX = 1000010;

int main() {
  int n, K, S;
  scanf("%d%d%d", &n, &K, &S);
  vector<int> ans;
  if (S == 1) {
    rep(i, K) ans.push_back(1);
    rep(i, n - K) ans.push_back(mod - 7);
  } else {
    ans.push_back(1);
    rep(i, K) ans.push_back(i % 2 == 0 ? S - 1 : 1);
    if (S != mod - 7)
      rep(i, n - K - 1) ans.push_back(mod - 7);
    else {
      int dummy = (int)(ans[ans.size() - 1] == 1 ? mod - 9 : mod - 8);
      rep(i, n - K - 1) ans.push_back(dummy);
    }
  }
  assert(ans.size() == n);
  for (int x : ans)
    printf("%d ", x);
}

Fixed submission:
#include <bits/stdc++.h>
#define For(i, a, b) for (int(i) = (a); (i) < (b); ++(i))
#define rFor(i, a, b) for (int(i) = (a)-1; (i) >= (b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<int, lint> pil;
typedef pair<lint, int> pli;
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;
}

constexpr lint mod = 1e9 + 7;
constexpr lint INF = mod * mod;
constexpr int MAX = 1000010;

int main() {
  int n, K, S;
  scanf("%d%d%d", &n, &K, &S);
  vector<int> ans;
  rep(i, K) ans.push_back(S);
  int dummy = (int)(S == mod - 7 ? 1 : mod - 7);
  rep(i, n - K) ans.push_back(dummy);
  for (int x : ans)
    printf("%d ", x);
}
Problem p02797 (Python)

Buggy submission (status: Runtime Error):
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, K, S = map(int, read().split())

S_half = [S // 2, S - S // 2]

ans = []
for _ in range(N // 2 + 1):
    ans += S_half
ans = np.array(ans[:N])
i = 0
cnt = 0
for i in range(N):
    if cnt == K:
        ans[i:N] = S - 1
        idx = i
        break
    if sum(ans[i : i + 2]) == S:
        cnt += 1
        continue

if ans[idx - 1 : idx + 1].sum() != S:
    for s_ in S_half:
        if ans[idx - 1] + s_ == S:
            ans[idx] = s_

print(*ans)


Fixed submission:
import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, K, S = map(int, read().split())

if S != 1:
    ans = [S - 1] * N
else:
    ans = [S + 1] * N
for i in range(K):
    ans[i] = S

print(*ans)
Problem p02797 (C++)

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

signed main(void) {
  int n, m, k, s;
  cin >> n >> k >> s;
  vector<int> v;
  rep(i, k) { v.push_back(s); }
  m = n - k;
  v.erase(v.begin());
  rep(i, m) { v.push_back(1); }
  v.push_back(s - m);
  sort(v.begin(), v.end());
  rep(i, v.size()) printf("%d%c", v[i], i == v.size() - 1 ? ' ' : '\n');
  return 0;
}


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

signed main(void) {
  int n, m, k, s;
  cin >> n >> k >> s;
  vector<int> v;
  rep(i, k) { v.push_back(s); }
  if (s < 1000000000) {
    rep(i, n - k) v.push_back(s + 1);
  } else {
    rep(i, n - k) v.push_back(1);
  }
  rep(i, v.size()) printf("%d%c", v[i], i == v.size() - 1 ? ' ' : '\n');
  return 0;
}
Problem p02798 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int n, a[2][20], ans = 1e9;
int main() {
  scanf("%d", &n);
  rep(o, 2) rep(i, n) scanf("%d", a[o] + i);
  rep(bt, 1 << n) {
    int rev[20] = {}, c[] = {-1, -2};
    bool ok = true;
    vector<int> v[51];
    rep(i, n) v[a[bt >> i & 1][i]].push_back(i);
    rep(i, 51) {
      for (int t : v[i])
        rev[c[abs(c[1] - t) % 2 == (bt >> t) % 2] += 2] = t + 1;
      if (abs(c[0] - c[1]) > 1)
        ok = false;
    }
    if (ok) {
      constexpr int sz = 32;
      int sm = 0;
      vector<int> dat(sz + 1);
      rep(i, n) {
        for (int j = rev[i]; j; j -= j & -j)
          sm += dat[j];
        for (int j = rev[i]; j <= sz; j += j & -j)
          dat[j]++;
      }
      sm = n * (n - 1) / 2 - sm;
      ans = min(ans, sm);
    }
  }
  printf("%d\n", ans == (int)1e9 ? -1 : ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
int n, a[2][20], ans = 1e9;
int main() {
  scanf("%d", &n);
  rep(o, 2) rep(i, n) scanf("%d", a[o] + i);
  rep(bt, 1 << n) {
    int rev[40] = {}, c[] = {-2, -1};
    bool ok = true;
    vector<int> v[51];
    rep(i, n) v[a[bt >> i & 1][i]].push_back(i);
    rep(i, 51) {
      for (int t : v[i])
        rev[c[abs(c[1] - t) % 2 == (bt >> t) % 2] += 2] = t + 1;
      if (abs(c[0] - c[1]) > 1)
        ok = false;
    }
    if (ok) {
      constexpr int sz = 32;
      int sm = 0;
      vector<int> dat(sz + 1);
      rep(i, n) {
        for (int j = rev[i]; j; j -= j & -j)
          sm += dat[j];
        for (int j = rev[i]; j <= sz; j += j & -j)
          dat[j]++;
      }
      sm = n * (n - 1) / 2 - sm;
      ans = min(ans, sm);
    }
  }
  printf("%d\n", ans == (int)1e9 ? -1 : ans);
}
Problem p02798 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define long long long
using namespace std;
const int N = 1e6, inf = 1e9;
int aa[18 + 2], bb[18 + 2], dp[18 + 2][2][(1 << 18) + 2], n,
    pre[18 + 2][(1 << 18) + 2];
inline int dfs(int last, int f, int msk) {
  if (msk == (1 << n) - 1)
    return 0;
  if (dp[last][f][msk] != -1)
    return dp[last][f][msk];
  int l = aa[last];
  if (f)
    l = bb[last];
  int ret = inf;
  int cnt = __builtin_popcount(msk);
  for (int i = 0; i < n; i++) {
    if (msk & (1 << i))
      continue;
    int p = pre[i][msk] + i; // current position of i

    int fl = (pre[i][msk] + p - cnt) % 2;
    int now = aa[i];
    if (fl)
      now = bb[i];
    if (now >= l)
      ret = min(ret, p - cnt + dfs(i, fl, msk | (1 << i)));
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> aa[i];
  for (int i = 0; i < n; i++)
    cin >> bb[i];

  // for(int i=0;i<n;i++)aa[i]=1+rand()%50;
  // for(int i=0;i<n;i++)bb[i]=1+rand()%50;

  for (int i = 0; i < n; i++) {
    for (int msk = 0; msk < (1 << n); msk++) {
      int cnt = 0;
      if (msk & (1 << i))
        continue;
      for (int j = i + 1; j < n; j++)
        cnt += (bool)(msk & (1 << j));

      pre[i][msk] = cnt;
    }
  }
  // cout<<pre[0][14]<<endl;
  memset(dp, -1, sizeof(dp));
  int mn = dfs(0, 0, (1 << 0));
  for (int i = 1; i < n; i++)
    mn = min(mn, i + dfs(i, i % 2, (1 << i)));
  if (mn == inf)
    mn = -1;
  cout << mn << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define long long long
using namespace std;
const int N = 1e6, inf = 1e9;
int aa[18 + 2], bb[18 + 2], dp[18 + 2][2][(1 << 18) + 2], n,
    pre[18 + 2][(1 << 18) + 2];
inline int dfs(int last, int f, int msk) {
  if (msk == (1 << n) - 1)
    return 0;
  if (dp[last][f][msk] != -1)
    return dp[last][f][msk];
  int l = aa[last];
  if (f)
    l = bb[last];
  int ret = inf;
  int cnt = __builtin_popcount(msk);
  for (int i = 0; i < n; i++) {
    if (msk & (1 << i))
      continue;
    int p = pre[i][msk] + i; // current position of i

    int fl = (pre[i][msk] + p - cnt) % 2;
    int now = aa[i];
    if (fl)
      now = bb[i];
    if (now >= l)
      ret = min(ret, p - cnt + dfs(i, fl, msk | (1 << i)));
  }
  return dp[last][f][msk] = ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> aa[i];
  for (int i = 0; i < n; i++)
    cin >> bb[i];

  // for(int i=0;i<n;i++)aa[i]=1+rand()%50;
  // for(int i=0;i<n;i++)bb[i]=1+rand()%50;

  for (int i = 0; i < n; i++) {
    for (int msk = 0; msk < (1 << n); msk++) {
      int cnt = 0;
      if (msk & (1 << i))
        continue;
      for (int j = i + 1; j < n; j++)
        cnt += (bool)(msk & (1 << j));

      pre[i][msk] = cnt;
    }
  }
  // cout<<pre[0][14]<<endl;
  memset(dp, -1, sizeof(dp));
  int mn = dfs(0, 0, (1 << 0));
  for (int i = 1; i < n; i++)
    mn = min(mn, i + dfs(i, i % 2, (1 << i)));
  if (mn == inf)
    mn = -1;
  cout << mn << endl;

  return 0;
}
Problem p02798 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n;
int a[51], b[51];
int dp[1 << 18][51];
int INF = 1 << 25;

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
  }
  for (int i = 0; i <= (1 << n); i++) {
    for (int j = 0; j <= 50; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < (1 << n); i++) {
    int pos = __builtin_popcount(i);
    for (int j = 0; j <= 50; j++) {
      if (dp[i][j] == INF)
        continue;
      int t_pos = 0;
      // printf("%d %d %d %d\n",i,j,dp[i][j],pos);
      for (int k = 0; k < n; k++) {
        if ((i >> k) & 1)
          continue;
        int flag = (k - pos + 100) % 2;
        // printf("k=%d flag=%d t_pos=%d\n",k,flag,t_pos);
        if (flag == 0) {
          if (j <= a[k]) {
            dp[i + (1 << k)][a[k]] =
                min(dp[i + (1 << k)][a[k]], dp[i][j] + t_pos);
          }
        } else {
          if (j <= b[k]) {
            dp[i + (1 << k)][b[k]] =
                min(dp[i + (1 << k)][b[k]], dp[i][j] + t_pos);
          }
        }
        t_pos++;
      }
    }
  }
  int ans = INF;
  for (int i = 0; i <= 50; i++) {
    ans = min(ans, dp[(1 << n) - 1][i]);
  }
  printf("%d\n", ans == INF ? -1 : ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int, int> P;

int n;
int a[51], b[51];
int dp[1 << 18][51];
int INF = 1 << 25;

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < n; i++) {
    scanf("%d", &b[i]);
  }
  for (int i = 0; i < (1 << n); i++) {
    for (int j = 0; j <= 50; j++) {
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  for (int i = 0; i < (1 << n); i++) {
    int pos = __builtin_popcount(i);
    for (int j = 0; j <= 50; j++) {
      if (dp[i][j] == INF)
        continue;
      int t_pos = 0;
      // printf("%d %d %d %d\n",i,j,dp[i][j],pos);
      for (int k = 0; k < n; k++) {
        if ((i >> k) & 1)
          continue;
        int flag = (k - pos + 100) % 2;
        // printf("k=%d flag=%d t_pos=%d\n",k,flag,t_pos);
        if (flag == 0) {
          if (j <= a[k]) {
            dp[i + (1 << k)][a[k]] =
                min(dp[i + (1 << k)][a[k]], dp[i][j] + t_pos);
          }
        } else {
          if (j <= b[k]) {
            dp[i + (1 << k)][b[k]] =
                min(dp[i + (1 << k)][b[k]], dp[i][j] + t_pos);
          }
        }
        t_pos++;
      }
    }
  }
  int ans = INF;
  for (int i = 0; i <= 50; i++) {
    ans = min(ans, dp[(1 << n) - 1][i]);
  }
  printf("%d\n", ans == INF ? -1 : ans);
  return 0;
}
Problem p02799 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
struct edge {
  ll to;
  ll cost, num;
  edge() {}
  edge(ll to, ll cost, ll num) : to(to), cost(cost), num(num){};
};
int main() {
  ll n, m;
  cin >> n >> m;
  vector<vector<edge>> g(n);
  vector<vector<edge>> gg(n);
  vector<ll> v(n);
  rep(i, n) { cin >> v[i]; }
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    g[a - 1].emplace_back(b - 1, -1, i);
    g[b - 1].emplace_back(a - 1, -1, i);
  }
  rep(i, n) {
    bool ok = 0;
    for (auto r : g[i])
      if (v[i] >= v[r.to])
        ok = 1;
    if (!ok) {
      cout << -1 << endl;
      return 0;
    }
  }
  vector<bool> s(n, 0);
  vector<bool> see(n, 0);
  vector<ll> ans(n, 1000000000);
  rep(i, n) {
    if (see[i])
      continue;
    ll mi = 1LL << 40;
    for (auto r : g[i])
      mi = min(mi, v[r.to]);
    for (auto r : g[i])
      if (mi == v[r.to]) {
        ans[r.num] = v[i];
        gg[r.to].emplace_back(i, -1, i);
        gg[i].emplace_back(r.to, -1, i);
        break;
      }
  }
  rep(i, n) {
    if (see[i])
      continue;
    queue<ll> q;
    q.push(i);
    while (!q.empty()) {
      ll p = q.front();
      see[p] = 1;
      q.pop();
      for (auto r : gg[p]) {
        if (!see[r.to]) {
          s[r.to] = !s[p];
          q.push(r.to);
        }
      }
    }
  }
  string ss = "";
  rep(i, n) {
    if (s[i])
      ss += 'B';
    else
      ss += 'W';
  }
  cout << ss << endl;
  for (auto r : ans)
    cout << r << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
struct edge {
  ll to;
  ll cost, num;
  edge() {}
  edge(ll to, ll cost, ll num) : to(to), cost(cost), num(num){};
};
int main() {
  ll n, m;
  cin >> n >> m;
  vector<vector<edge>> g(n);
  vector<vector<edge>> gg(n);
  vector<ll> v(n);
  rep(i, n) { cin >> v[i]; }
  rep(i, m) {
    ll a, b;
    cin >> a >> b;
    g[a - 1].emplace_back(b - 1, -1, i);
    g[b - 1].emplace_back(a - 1, -1, i);
  }
  rep(i, n) {
    bool ok = 0;
    for (auto r : g[i])
      if (v[i] >= v[r.to])
        ok = 1;
    if (!ok) {
      cout << -1 << endl;
      return 0;
    }
  }
  vector<bool> s(n, 0);
  vector<bool> see(n, 0);
  vector<ll> ans(m, 1000000000);
  rep(i, n) {
    if (see[i])
      continue;
    ll mi = 1LL << 40;
    for (auto r : g[i])
      mi = min(mi, v[r.to]);
    for (auto r : g[i])
      if (mi == v[r.to]) {
        ans[r.num] = v[i];
        gg[r.to].emplace_back(i, -1, i);
        gg[i].emplace_back(r.to, -1, i);
        break;
      }
  }
  rep(i, n) {
    if (see[i])
      continue;
    queue<ll> q;
    q.push(i);
    while (!q.empty()) {
      ll p = q.front();
      see[p] = 1;
      q.pop();
      for (auto r : gg[p]) {
        if (!see[r.to]) {
          s[r.to] = !s[p];
          q.push(r.to);
        }
      }
    }
  }
  string ss = "";
  rep(i, n) {
    if (s[i])
      ss += 'B';
    else
      ss += 'W';
  }
  cout << ss << endl;
  for (auto r : ans)
    cout << r << endl;
  return 0;
}
Problem p02799 (C++)

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

using namespace std;

const int N = 1e6 + 5;

vector<int> g[N];

int d[N];
int vis[N];
bool ok[N];
int a[N], b[N];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i)
    scanf("%d", d + i);
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    --u, --v;
    a[i] = u;
    b[i] = v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vector<int> nodes(n);
  iota(nodes.begin(), nodes.end(), 0);
  sort(nodes.begin(), nodes.end(), [](int l, int r) { return d[l] <= d[r]; });
  for (int v : nodes) {
    if (vis[v])
      continue;
    queue<int> que;
    vis[v] = 1;
    que.push(v);
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      for (int w : g[u]) {
        if (vis[w] || d[w] < d[u])
          continue;
        vis[w] = -vis[u];
        que.push(w);
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    int u = a[i], v = b[i];
    if (vis[u] != vis[v]) {
      if (d[u] == max(d[u], d[v]))
        ok[u] = 1;
      if (d[v] == max(d[u], d[v]))
        ok[v] = 1;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!ok[i]) {
      puts("-1");
      return 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    printf("%c", vis[i] > 0 ? 'B' : 'W');
  }
  printf("\n");
  for (int i = 0; i < m; ++i)
    printf("%d\n", max(d[a[i]], d[b[i]]));
  return 0;
}


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

using namespace std;

const int N = 1e6 + 5;

vector<int> g[N];

int d[N];
int vis[N];
bool ok[N];
int a[N], b[N];

int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i)
    scanf("%d", d + i);
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    --u, --v;
    a[i] = u;
    b[i] = v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  vector<int> nodes(n);
  for (int i = 0; i < n; ++i)
    nodes[i] = i;
  sort(nodes.begin(), nodes.end(), [](int l, int r) { return d[l] < d[r]; });
  for (int v : nodes) {
    if (vis[v])
      continue;
    queue<int> que;
    vis[v] = 1;
    que.push(v);
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      for (int w : g[u]) {
        if (vis[w] || d[w] < d[u])
          continue;
        vis[w] = -vis[u];
        que.push(w);
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    int u = a[i], v = b[i];
    if (vis[u] != vis[v]) {
      if (d[u] == max(d[u], d[v]))
        ok[u] = 1;
      if (d[v] == max(d[u], d[v]))
        ok[v] = 1;
    }
  }
  for (int i = 0; i < n; ++i) {
    if (!ok[i]) {
      puts("-1");
      return 0;
    }
  }
  for (int i = 0; i < n; ++i) {
    printf("%c", vis[i] > 0 ? 'B' : 'W');
  }
  printf("\n");
  for (int i = 0; i < m; ++i)
    printf("%d\n", max(d[a[i]], d[b[i]]));
  return 0;
}
Problem p02799 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = (a); i <= (b); ++i)
#define rv(i, a, b) for (int i = (a); i >= (b); --i)
#define P pair<int, int>
#define pb push_back
#define lb(x) (x & (-x))
#define ALL(x) x.begin(), x.end()
#define SZ(x) ((int)x.size())
using namespace std;
typedef long long LL;
const int M = 120000;
const int inf = 1e9;
int c[M] = {0};
P p[M];
vector<P> g[M];
int n, m, k;
int dd[M];
char s[M];
bool ok(int i) { return s[i] == 'W' || s[i] == 'B'; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  int u, v;
  fo(i, 1, n) {
    cin >> p[i].first;
    dd[i] = p[i].first;
    p[i].second = i;
  }
  sort(p + 1, p + 1 + n);
  fo(i, 1, m) {
    cin >> u >> v;
    g[u].push_back({v, i});
    g[v].push_back({u, i});
  }
  int flag = 1;
  fo(i, 1, n) {
    int id = p[i].second;
    if (ok(id))
      continue;
    int d = p[i].first;
    bool done = 0;
    for (P to : g[id]) {
      if (ok(to.first)) {
        c[to.second] = d;
        s[id] = (s[to.first] == 'W' ? 'B' : 'W');
        done = 1;
        break;
      }
    }
    if (!done) {
      for (P to : g[id]) {
        if (dd[to.first] == d) {
          done = 1;
          s[id] = 'W';
          s[to.first] = 'B';
          c[to.second] = d;
        }
      }
    }
    if (!done) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    s[n + 1] = '\0';
    cout << s + 1 << endl;
    fo(i, 1, m) { cout << (c[i] ? c[i] : inf) << endl; }
  } else
    cout << -1 << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = (a); i <= (b); ++i)
#define rv(i, a, b) for (int i = (a); i >= (b); --i)
#define P pair<int, int>
#define pb push_back
#define lb(x) (x & (-x))
#define ALL(x) x.begin(), x.end()
#define SZ(x) ((int)x.size())
using namespace std;
typedef long long LL;
const int M = 220000;
const int inf = 1e9;
int c[M] = {0};
P p[M];
vector<P> g[M];
int n, m, k;
int dd[M];
char s[M];
bool ok(int i) { return s[i] == 'W' || s[i] == 'B'; }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  int u, v;
  fo(i, 1, n) {
    cin >> p[i].first;
    dd[i] = p[i].first;
    p[i].second = i;
  }
  sort(p + 1, p + 1 + n);
  fo(i, 1, m) {
    cin >> u >> v;
    g[u].push_back({v, i});
    g[v].push_back({u, i});
  }
  int flag = 1;
  fo(i, 1, n) {
    int id = p[i].second;
    if (ok(id))
      continue;
    int d = p[i].first;
    bool done = 0;
    for (P to : g[id]) {
      if (ok(to.first)) {
        c[to.second] = d;
        s[id] = (s[to.first] == 'W' ? 'B' : 'W');
        done = 1;
        break;
      }
    }
    if (!done) {
      for (P to : g[id]) {
        if (dd[to.first] == d) {
          done = 1;
          s[id] = 'W';
          s[to.first] = 'B';
          c[to.second] = d;
        }
      }
    }
    if (!done) {
      flag = 0;
      break;
    }
  }
  if (flag) {
    s[n + 1] = '\0';
    cout << s + 1 << endl;
    fo(i, 1, m) { cout << (c[i] ? c[i] : inf) << endl; }
  } else
    cout << -1 << endl;
  return 0;
}