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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)

using namespace std;

typedef long long ll;

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

  ll n, k;
  cin >> n >> k;

  // r, s, p
  ll score[3];
  cin >> score[0] >> score[1] >> score[2];

  string s;
  cin >> s;

  string spr = "spr";

  ll ans = 0;
  for (int a = 0; a < k; a++) {
    ll dp[n + 1][3];
    rep(i, n + 1) rep(j, 3) dp[i][j] = 0;
    for (int j = 0; j < 3; j++) {
      if (s[a] == spr[j])
        dp[0][j] += score[j];
    }

    int last = 0;
    int idx = a + k;
    for (; idx < n; idx += k) {
      int i = idx / k;
      last = idx;
      for (int j = 0; j < 3; j++) {
        for (int l = 0; l < 3; l++) {
          if (j == l)
            continue;
          if (s[idx] == spr[j])
            dp[i][j] = max(dp[i][j], dp[i - 1][l] + score[j]);
          else
            dp[i][j] = max(dp[i][j], dp[i - 1][l]);
        }
      }
    }

    ans += max({dp[last / k][0], dp[last / k][1], dp[last / k][2]});
  }

  cout << ans << endl;
}


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

using namespace std;

typedef long long ll;

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

  ll n, k;
  cin >> n >> k;

  // r, s, p
  ll score[3];
  cin >> score[0] >> score[1] >> score[2];

  string s;
  cin >> s;

  string spr = "spr";

  ll ans = 0;
  for (int a = 0; a < k; a++) {
    ll dp[n / k + 10][3];
    rep(i, n / k + 10) rep(j, 3) dp[i][j] = 0;
    for (int j = 0; j < 3; j++) {
      if (s[a] == spr[j])
        dp[0][j] += score[j];
    }

    int last = 0;
    int idx = a + k;
    for (; idx < n; idx += k) {
      int i = idx / k;
      last = idx;
      for (int j = 0; j < 3; j++) {
        for (int l = 0; l < 3; l++) {
          if (j == l)
            continue;
          if (s[idx] == spr[j])
            dp[i][j] = max(dp[i][j], dp[i - 1][l] + score[j]);
          else
            dp[i][j] = max(dp[i][j], dp[i - 1][l]);
        }
      }
    }

    ans += max({dp[last / k][0], dp[last / k][1], dp[last / k][2]});
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
string a;
int cnt;
int ans;
int n, k, r, s, p;
int main() {
  cin >> n >> k;
  cin >> r >> s >> p;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == 'r') {
      cnt = p;
    } else if (a[i] == 's') {
      cnt = r;
    } else if (a[i] == 'p') {
      cnt = s;
    }
    if (i >= k and a[i] == a[i - k]) {
      cnt = 0;
      a[i] = ' ';
    }
    ans += cnt;
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
string a;
int cnt;
int ans;
int n, k, r, s, p;
int main() {
  cin >> n >> k;
  cin >> r >> s >> p;
  cin >> a;
  for (int i = 0; i < n; i++) {
    if (a[i] == 'r') {
      cnt = p;
    } else if (a[i] == 's') {
      cnt = r;
    } else if (a[i] == 'p') {
      cnt = s;
    }
    if (i >= k and a[i] == a[i - k]) {
      cnt = 0;
      a[i] = ' ';
    }
    ans += cnt;
  }
  cout << ans;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch))
    f ^= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
bool isprime(int x) {
  for (int i = 2; i * i <= x; ++i)
    if (x % i == 0)
      return 0;
  return 1;
}
char S[100010];
int main() {
#ifdef XZZSB
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n = gi(), k = gi(), r = gi(), s = gi(), p = gi(), ans = 0;
  scanf("%s", S + 1);
  for (int i = 1; i <= k; ++i)
    for (int j = i; j <= n; j += k) {
      if (S[j] == S[j - k])
        S[j] = '*';
      else {
        if (S[j] == 'r')
          ans += p;
        else if (S[j] == 'p')
          ans += s;
        else
          ans += r;
      }
    }
  printf("%d\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
ll gi() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch))
    f ^= ch == '-', ch = getchar();
  while (isdigit(ch))
    x = x * 10 + ch - '0', ch = getchar();
  return f ? x : -x;
}
bool isprime(int x) {
  for (int i = 2; i * i <= x; ++i)
    if (x % i == 0)
      return 0;
  return 1;
}
char S[100010];
int main() {
#ifdef XZZSB
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif
  int n = gi(), k = gi(), r = gi(), s = gi(), p = gi(), ans = 0;
  scanf("%s", S + 1);
  for (int i = 1; i <= k; ++i)
    for (int j = i; j <= n; j += k) {
      if (j > k && S[j] == S[j - k])
        S[j] = '*';
      else {
        if (S[j] == 'r')
          ans += p;
        else if (S[j] == 'p')
          ans += s;
        else
          ans += r;
      }
    }
  printf("%d\n", ans);
  return 0;
}
Problem p02820 (C++)

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

using namespace std;
int val(char ch, int r, int s, int p) {
  if (ch == 'r')
    return r;
  if (ch == 'p')
    return p;
  if (ch == 's')
    return s;
}
int main() {
  int n, k, r, s, p, ans = 0;
  string t;
  cin >> n >> k >> r >> s >> p >> t;
  for (int i = 0; i < n; i++) {
    if (t[i] == 'r')
      t[i] = 'p';
    else if (t[i] == 'p')
      t[i] = 's';
    else if (t[i] == 's')
      t[i] = 'r';
    ans += val(t[i], r, s, p);
  }

  for (int i = 0; i < n; i++) {
    if (t[i] == t[i + k]) {
      ans -= val(t[i + k], r, s, p);
      t[i + k] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}


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

using namespace std;
int val(char ch, int r, int s, int p) {
  if (ch == 'r')
    return r;
  if (ch == 'p')
    return p;
  if (ch == 's')
    return s;
}
int main() {
  int n, k, r, s, p, ans = 0;
  string t;
  cin >> n >> k >> r >> s >> p >> t;
  for (int i = 0; i < n; i++) {
    if (t[i] == 'r')
      t[i] = 'p';
    else if (t[i] == 'p')
      t[i] = 's';
    else if (t[i] == 's')
      t[i] = 'r';
    ans += val(t[i], r, s, p);
  }

  for (int i = 0; i + k < n; i++) {
    if (t[i] == t[i + k]) {
      ans -= val(t[i + k], r, s, p);
      t[i + k] = 0;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 100000000000000005;
const int maxn = 1e6 + 5;
const int mod = 433494437;
ll n, m;
char str[maxn];
char cop[maxn];
map<char, ll> mp;
int main() {
  ll r, s, p; // rock s paper
  scanf("%lld%lld", &n, &m);
  scanf("%lld%lld%lld%s", &r, &s, &p, str + 1);
  mp['r'] = r;
  mp['s'] = s;
  mp['p'] = p;
  for (int i = 1; i <= n; i++) {
    if (str[i] == 'r')
      cop[i] = 'p';
    if (str[i] == 'p')
      cop[i] = 's';
    if (str[i] == 's')
      cop[i] = 'r';
  }
  ll res = 0;
  for (int i = 1; i <= m; i++) {
    for (int k = i; k <= n; k += m) {
      if (cop[k] != cop[k - m])
        res += mp[cop[k]];
      else
        cop[k] = '.';
    }
  }
  printf("%lld\n", res);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 100000000000000005;
const int maxn = 1e6 + 5;
const int mod = 433494437;
ll n, m;
char str[maxn];
char cop[maxn];
map<char, ll> mp;
int main() {
  ll r, s, p; // rock s paper
  scanf("%lld%lld", &n, &m);
  scanf("%lld%lld%lld%s", &r, &s, &p, str + 1);
  mp['r'] = r;
  mp['s'] = s;
  mp['p'] = p;
  for (int i = 1; i <= n; i++) {
    if (str[i] == 'r')
      cop[i] = 'p';
    if (str[i] == 'p')
      cop[i] = 's';
    if (str[i] == 's')
      cop[i] = 'r';
  }
  ll res = 0;
  for (int i = 1; i <= m; i++) {
    res += mp[cop[i]];
    for (int k = i + m; k <= n; k += m) {
      if (cop[k] != cop[k - m])
        res += mp[cop[k]];
      else
        cop[k] = '.';
    }
  }
  printf("%lld\n", res);
  return 0;
}
Problem p02820 (C++)

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

int Point(char T, int R, int S, int P) {
  switch (T) {
  case 'r':
    return P;
  case 's':
    return R;
  case 'p':
    return S;
  }
}

int main() {
  int N, K, R, S, P, sum;
  string T;
  cin >> N >> K >> R >> S >> P >> T;
  sum = 0;
  bool isWon = true;

  REP(i, 0, K) {
    int j = 0;
    while (i + K * j < N) {
      if (T[i + K * j] != T[i + K * (j + 1)] || !isWon) {
        sum += Point(T[i + K * j], R, S, P);
        if (!isWon) {
          isWon = !isWon;
        }
      } else {
        isWon = !isWon;
      }
      j++;
    }
  }
  cout << sum << endl;
}

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

int Point(char T, int R, int S, int P) {
  switch (T) {
  case 'r':
    return P;
  case 's':
    return R;
  case 'p':
    return S;
  }
}

int main() {
  int N, K, R, S, P, sum;
  string T;
  cin >> N >> K >> R >> S >> P >> T;
  sum = 0;
  bool isWon = true;
  string A(K, 'a');
  T = T + A;
  REP(i, 0, K) {
    int j = 0;
    while (i + K * j < N) {
      if (T[i + K * j] != T[i + K * (j + 1)] || !isWon) {
        sum += Point(T[i + K * j], R, S, P);
        if (!isWon) {
          isWon = !isWon;
        }
      } else {
        isWon = !isWon;
      }
      j++;
    }
  }
  cout << sum << endl;
}
Problem p02820 (C++)

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

int main() {
  int n, k, r, s, p;
  string t;
  cin >> n >> k >> r >> s >> p >> t;
  long long sum = 0;
  for (int i = 0; i < t.size(); i++) {
    if (t[i] == 'r')
      sum += p;
    else if (t[i] == 's')
      sum += r;
    else
      sum += s;
  }
  for (int i = 0; i < t.size(); i++) {
    if (t[i] == 'r' && t[i + k] == 'r') {
      sum -= p;
      t[i + k] = 'a';
    } else if (t[i] == 's' && t[i + k] == 's') {
      sum -= r;
      t[i + k] = 'b';
    } else if (t[i] == 'p' && t[i + k] == 'p') {
      sum -= s;
      t[i + k] = 'c';
    }
  }
  cout << sum << endl;
  return 0;
}


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

int main() {
  int n, k, r, s, p;
  string t;
  cin >> n >> k >> r >> s >> p >> t;
  long long sum = 0;
  for (int i = 0; i < t.size(); i++) {
    if (t[i] == 'r')
      sum += p;
    else if (t[i] == 's')
      sum += r;
    else
      sum += s;
  }
  for (int i = 0; i < t.size() - k; i++) {
    if (t[i] == 'r' && t[i + k] == 'r') {
      sum -= p;
      t[i + k] = 'a';
    } else if (t[i] == 's' && t[i + k] == 's') {
      sum -= r;
      t[i + k] = 'b';
    } else if (t[i] == 'p' && t[i + k] == 'p') {
      sum -= s;
      t[i + k] = 'c';
    }
  }
  cout << sum << endl;
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>

using namespace std;

int main(void) {
  long long N, K, R, S, P, i, i2, count, ans = 0, Rdec = 0, Sdec = 0, Pdec = 0,
                                         check[100000];
  char T[100000], U;

  cin >> N >> K >> R >> S >> P >> T;

  for (i = 0; i < N; i++) {
    check[i] = 0;
  }

  for (i = 0; i < N; i++) {
    i2 = i;

    if (T[i] == T[i + K]) {
      count = 0;

      while (i + K < N && T[i] == T[i + K] && check[i] == 0) {
        U = T[i];

        check[i] = 1;

        i += K;

        count++;
      }

      if (count % 2 == 0) {
        if (U == 'r') {
          ans -= P * count / 2;
        } else if (U == 's') {
          ans -= R * count / 2;
        } else {
          ans -= S * count / 2;
        }
      } else {
        if (U == 'r') {
          ans -= P * (count / 2 + 1);
        } else if (U == 's') {
          ans -= R * (count / 2 + 1);
        } else {
          ans -= S * (count / 2 + 1);
        }
      }
    }

    i = i2;
  }

  for (i = 0; i < N; i++) {
    if (T[i] == 'r') {
      ans += P;
    }

    else if (T[i] == 's') {
      ans += R;
    }

    else {
      ans += S;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>

using namespace std;

int main(void) {
  long long N, K, R, S, P, i, i2, count, ans = 0, Rdec = 0, Sdec = 0, Pdec = 0,
                                         check[1000000];
  char T[1000000], U;

  cin >> N >> K >> R >> S >> P >> T;

  for (i = 0; i < N; i++) {
    check[i] = 0;
  }

  for (i = 0; i < N; i++) {
    i2 = i;

    if (T[i] == T[i + K]) {
      count = 0;

      while (i + K < N && T[i] == T[i + K] && check[i] == 0) {
        U = T[i];

        check[i] = 1;

        i += K;

        count++;
      }

      if (count % 2 == 0) {
        if (U == 'r') {
          ans -= P * count / 2;
        } else if (U == 's') {
          ans -= R * count / 2;
        } else {
          ans -= S * count / 2;
        }
      } else {
        if (U == 'r') {
          ans -= P * (count / 2 + 1);
        } else if (U == 's') {
          ans -= R * (count / 2 + 1);
        } else {
          ans -= S * (count / 2 + 1);
        }
      }
    }

    i = i2;
  }

  for (i = 0; i < N; i++) {
    if (T[i] == 'r') {
      ans += P;
    }

    else if (T[i] == 's') {
      ans += R;
    }

    else {
      ans += S;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02820 (C++)

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }

int main() {
  int N, K;
  cin >> N >> K;
  int R, S, P;
  cin >> R >> S >> P;
  string T;
  cin >> T;

  vector<char> cnt(N);

  ll res = 0;
  for (int i = 0; i < N; ++i) {
    ll ans = 0;
    if (T[i] == 'r') {
      cnt[i] = 'p';
      ans = P;
    } else if (T[i] == 's') {
      cnt[i] = 'r';
      ans = R;
    } else if (T[i] == 'p') {
      cnt[i] = 's';
      ans = S;
    }
    if (cnt[i - K] == cnt[i] && i >= K) {
      cnt[i] = 'a';
      ans = 0;
    }
    res += ans;
    // cout << cnt[i] << " " << cnt[i - K] << " " << (cnt[i] == cnt[i - K]) << "
    // " << T[i] << endl; cout << res << endl;
  }

  cout << res << endl;
}

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

#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define all(x) (x).begin(), (x).end()
using ll = long long;
string char_to_string(char val) { return string(1, val); }

int main() {
  int N, K;
  cin >> N >> K;
  int R, S, P;
  cin >> R >> S >> P;
  string T;
  cin >> T;

  vector<char> cnt(N);

  ll res = 0;
  for (int i = 0; i < N; ++i) {
    ll ans = 0;
    if (T[i] == 'r') {
      cnt[i] = 'p';
      ans = P;
    } else if (T[i] == 's') {
      cnt[i] = 'r';
      ans = R;
    } else if (T[i] == 'p') {
      cnt[i] = 's';
      ans = S;
    }

    if (i >= K) {
      if (cnt[i - K] == cnt[i]) {
        cnt[i] = 'a';
        ans = 0;
      }
    }
    res += ans;
    // cout << cnt[i] << " " << cnt[i - K] << " " << (cnt[i] == cnt[i - K]) << "
    // " << T[i] << endl; cout << res << endl;
  }

  cout << res << endl;
}
Problem p02820 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll n, m, r[3], ans, t[110000];
string s;

int main() {
  cin >> n >> m;
  REP(i, 3) cin >> r[i];
  cin >> s;
  REP(i, n) {
    if (s[i] == 'r')
      t[i] = 1;
    if (s[i] == 's')
      t[i] = 2;
    if (s[i] == 'p')
      t[i] = 3;
  }
  REP(i, m) {
    ll dp[110000][3] = {};
    ll j = 0;
    ll now = i;
    while (now < n) {
      REP(k, 3) {
        REP(l, 3) {
          if (k == l)
            continue;
          if ((l - t[now] + 3) % 3 == 1) {
            dp[j + 1][l] = max(dp[j + 1][l], dp[j][k] + r[l]);
          } else
            dp[j + 1][l] = max(dp[j + 1][l], dp[j][k]);
        }
      }
      now += m;
      j++;
    }

    ll t = 0;
    REP(l, 3) t = max(t, dp[j][l]);
    ans += t;
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n; i >= 0; i--)
#define FOR(i, m, n) for (ll i = m; i < n; i++)
#define FORR(i, m, n) for (ll i = m; i >= n; i--)
#define REPO(i, n) for (ll i = 1; i <= n; i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(), n.end()
#define MOD 1000000007
#define P pair<ll, ll>

ll n, m, r[3], ans, t[110000];
string s;

int main() {
  cin >> n >> m;
  REP(i, 3) cin >> r[i];
  cin >> s;
  REP(i, n) {
    if (s[i] == 'r')
      t[i] = 1;
    if (s[i] == 's')
      t[i] = 2;
    if (s[i] == 'p')
      t[i] = 3;
  }
  REP(i, m) {
    vector<vector<ll>> dp(n / m + 10, vector<ll>(3));
    ll j = 0;
    ll now = i;
    while (now < n) {
      REP(k, 3) {
        REP(l, 3) {
          if (k == l)
            continue;
          if ((l - t[now] + 3) % 3 == 1) {
            dp[j + 1][l] = max(dp[j + 1][l], dp[j][k] + r[l]);
          } else
            dp[j + 1][l] = max(dp[j + 1][l], dp[j][k]);
        }
      }
      now += m;
      j++;
    }

    ll t = 0;
    REP(l, 3) t = max(t, dp[j][l]);
    ans += t;
  }
  cout << ans << endl;
}
Problem p02820 (C++)

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

ll N, K, R, S, P;
string T;

array<vector<ll>, 3> memo = {vector<ll>(100000, -1), vector<ll>(100000, -1),
                             vector<ll>(100000, -1)};
array<char, 256> c2i;

ll dfs(char hand, int index, int a) {
  if (index * K >= N) {
    return 0;
  }

  auto hand_index = index * K + a;
  auto &memo_r = memo[c2i[hand]][hand_index];
  if (memo_r != -1) {
    return memo_r;
  }

  auto enemy_hand = T[hand_index];
  ll score = 0;
  if (hand == 'r') {
    if (enemy_hand == 's') {
      score += R;
    }
    score += max(dfs('s', index + 1, a), dfs('p', index + 1, a));
  }
  if (hand == 's') {
    if (enemy_hand == 'p') {
      score += S;
    }
    score += max(dfs('r', index + 1, a), dfs('p', index + 1, a));
  }
  if (hand == 'p') {
    if (enemy_hand == 'r') {
      score += P;
    }
    score += max(dfs('r', index + 1, a), dfs('s', index + 1, a));
  }

  memo_r = score;
  return score;
}

void main_() {
  cin >> N >> K >> R >> S >> P;
  cin >> T;

  c2i['r'] = 0;
  c2i['s'] = 1;
  c2i['p'] = 2;

  ll ans = 0;
  for (ll i = 0; i < K; ++i) {
    ans += max({
        dfs('r', 0, i),
        dfs('s', 0, i),
        dfs('p', 0, i),
    });
  }
  cout << ans << endl;
}
int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);
  main_();
  return 0;
}


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

ll N, K, R, S, P;
string T;

array<vector<ll>, 3> memo = {vector<ll>(100000, -1), vector<ll>(100000, -1),
                             vector<ll>(100000, -1)};
array<char, 256> c2i;

ll dfs(char hand, int index, int a) {
  if (index * K + a >= N) {
    return 0;
  }

  auto hand_index = index * K + a;
  auto &memo_r = memo[c2i[hand]][hand_index];
  if (memo_r != -1) {
    return memo_r;
  }

  auto enemy_hand = T[hand_index];
  ll score = 0;
  if (hand == 'r') {
    if (enemy_hand == 's') {
      score += R;
    }
    score += max(dfs('s', index + 1, a), dfs('p', index + 1, a));
  }
  if (hand == 's') {
    if (enemy_hand == 'p') {
      score += S;
    }
    score += max(dfs('r', index + 1, a), dfs('p', index + 1, a));
  }
  if (hand == 'p') {
    if (enemy_hand == 'r') {
      score += P;
    }
    score += max(dfs('r', index + 1, a), dfs('s', index + 1, a));
  }

  memo_r = score;
  return score;
}

void main_() {
  cin >> N >> K >> R >> S >> P;
  cin >> T;

  c2i['r'] = 0;
  c2i['s'] = 1;
  c2i['p'] = 2;

  ll ans = 0;
  for (ll i = 0; i < K; ++i) {
    ans += max({
        dfs('r', 0, i),
        dfs('s', 0, i),
        dfs('p', 0, i),
    });
  }
  cout << ans << endl;
}
int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);
  main_();
  return 0;
}
Problem p02820 (C++)

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

const int MAX_N = 100005;

// ll dp[MAX_N][3]={0};
ll subans[MAX_N];

int main(void) {
  int N, K;
  cin >> N >> K;
  int R, S, P;
  cin >> R >> S >> P;
  vector<string> machine(K, "");
  for (int i = 0, j = 0; i < N; i++, j++) {
    char c;
    cin >> c;
    machine[j] += c;
    if (j == K - 1)
      j = -1;
  }

  for (int k = 0; k < K; k++) {
    vector<vector<ll>> dp(N / 3 + 1, vector<ll>(3, 0));
    int last = machine[k].size();
    for (int i = 0; i < last; i++) {
      if (machine[k][i] == 's') {
        dp[i + 1][0] = max(dp[i + 1][0], max(dp[i][1] + R, dp[i][2] + R));
        dp[i + 1][1] = max(dp[i + 1][1], max(dp[i][0], dp[i][2]));
        dp[i + 1][2] = max(dp[i + 1][2], max(dp[i][1], dp[i][0]));
      } else if (machine[k][i] == 'p') {
        dp[i + 1][0] = max(dp[i + 1][0], max(dp[i][1], dp[i][2]));
        dp[i + 1][1] = max(dp[i + 1][1], max(dp[i][0] + S, dp[i][2] + S));
        dp[i + 1][2] = max(dp[i + 1][2], max(dp[i][1], dp[i][0]));
      } else {
        dp[i + 1][0] = max(dp[i + 1][0], max(dp[i][1], dp[i][2]));
        dp[i + 1][1] = max(dp[i + 1][1], max(dp[i][0], dp[i][2]));
        dp[i + 1][2] = max(dp[i + 1][2], max(dp[i][1] + P, dp[i][0] + P));
      }
    }
    subans[k] = max(dp[last][0], dp[last][1]);
    subans[k] = max(subans[k], dp[last][2]);
  }

  ll ans = 0;
  for (int i = 0; i < K; i++) {
    ans += subans[i];
    // cout << subans[i] << endl;
  }
  cout << ans << endl;
}


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

const int MAX_N = 100005;

// ll dp[MAX_N][3]={0};
ll subans[MAX_N];

int main(void) {
  int N, K;
  cin >> N >> K;
  int R, S, P;
  cin >> R >> S >> P;
  vector<string> machine(K, "");
  for (int i = 0, j = 0; i < N; i++, j++) {
    char c;
    cin >> c;
    machine[j] += c;
    if (j == K - 1)
      j = -1;
  }

  for (int k = 0; k < K; k++) {
    vector<vector<ll>> dp(N / K + 3, vector<ll>(3, 0));
    int last = machine[k].size();
    for (int i = 0; i < last; i++) {
      if (machine[k][i] == 's') {
        dp[i + 1][0] = max(dp[i + 1][0], max(dp[i][1] + R, dp[i][2] + R));
        dp[i + 1][1] = max(dp[i + 1][1], max(dp[i][0], dp[i][2]));
        dp[i + 1][2] = max(dp[i + 1][2], max(dp[i][1], dp[i][0]));
      } else if (machine[k][i] == 'p') {
        dp[i + 1][0] = max(dp[i + 1][0], max(dp[i][1], dp[i][2]));
        dp[i + 1][1] = max(dp[i + 1][1], max(dp[i][0] + S, dp[i][2] + S));
        dp[i + 1][2] = max(dp[i + 1][2], max(dp[i][1], dp[i][0]));
      } else {
        dp[i + 1][0] = max(dp[i + 1][0], max(dp[i][1], dp[i][2]));
        dp[i + 1][1] = max(dp[i + 1][1], max(dp[i][0], dp[i][2]));
        dp[i + 1][2] = max(dp[i + 1][2], max(dp[i][1] + P, dp[i][0] + P));
      }
    }
    subans[k] = max(dp[last][0], dp[last][1]);
    subans[k] = max(subans[k], dp[last][2]);
  }

  ll ans = 0;
  for (int i = 0; i < K; i++) {
    ans += subans[i];
    // cout << subans[i] << endl;
  }
  cout << ans << endl;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll cnt[100010];
int main() {
  ll n, k;
  cin >> n >> k;
  ll r, s, p;
  cin >> r >> s >> p;
  string t;
  cin >> t;
  for (ll i = 0; i < n; i++) {
    char now = t[i];
    if (cnt[i - k] != 0 && i - k >= 0) {
      if (t[i - k] == now) {
        continue;
      }
    }
    if (now == 'r') {
      cnt[i] = p;
    } else if (now == 's') {
      cnt[i] = r;
    } else {
      cnt[i] = s;
    }
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ans += cnt[i];
  }
  cout << ans;
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

ll cnt[100010];
int main() {
  ll n, k;
  cin >> n >> k;
  ll r, s, p;
  cin >> r >> s >> p;
  string t;
  cin >> t;
  for (ll i = 0; i < n; i++) {
    char now = t[i];
    if (i - k >= 0) {
      if (cnt[i - k] != 0 && t[i - k] == now) {
        continue;
      }
    }
    if (now == 'r') {
      cnt[i] = p;
    } else if (now == 's') {
      cnt[i] = r;
    } else {
      cnt[i] = s;
    }
  }
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    ans += cnt[i];
  }
  cout << ans;
  // your code goes here
  return 0;
}
Problem p02820 (C++)

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define INF 1000000000

void solve_abc149_d() {
  ll N, K;
  ll p[3] = {0};
  string T;
  ll P, Q, R;
  ll ans = 0;

  cin >> N >> K;
  cin >> p[0] >> p[1] >> p[2];
  cin >> T;

  ll cnt = 0;
  ll v = 0;
  vector<char> L[10001];
  rep(i, N) {
    // 勝ちとなる手
    if (T[i] == 'r')
      v = 2;
    else if (T[i] == 's')
      v = 0;
    else if (T[i] == 'p')
      v = 1;

    L[cnt].push_back(v);
    cnt++;
    if (cnt == K) {
      cnt = 0;
    }
  }

  ans = 0;
  rep(i, K) {
    ans += p[L[i][0]];
    rep2(j, 1, L[i].size()) {
      if ((L[i][j] != L[i][j - 1]) || L[i][j - 1] == -1) {
        ans += p[L[i][j]];
      } else {
        L[i][j] = -1;
      }
    }
  }

  cout << ans << "\n";
}

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

  solve_abc149_d();

  return 0;
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define INF 1000000000

void solve_abc149_d() {
  ll N, K;
  ll p[3] = {0};
  string T;
  ll P, Q, R;
  ll ans = 0;

  cin >> N >> K;
  cin >> p[0] >> p[1] >> p[2];
  cin >> T;

  ll cnt = 0;
  ll v = 0;
  vector<char> L[100001];
  rep(i, N) {
    // 勝ちとなる手
    if (T[i] == 'r')
      v = 2;
    else if (T[i] == 's')
      v = 0;
    else if (T[i] == 'p')
      v = 1;

    L[cnt].push_back(v);
    cnt++;
    if (cnt == K) {
      cnt = 0;
    }
  }

  ans = 0;
  rep(i, K) {
    ans += p[L[i][0]];
    rep2(j, 1, L[i].size()) {
      if ((L[i][j] != L[i][j - 1]) || L[i][j - 1] == -1) {
        ans += p[L[i][j]];
      } else {
        L[i][j] = -1;
      }
    }
  }

  cout << ans << "\n";
}

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

  solve_abc149_d();

  return 0;
}
Problem p02820 (C++)

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

using namespace std;

const int MAXN = 501;
const int MAXM = 3;
int n, k, r, s, p;
int dp[MAXM][MAXN], cost[MAXM];
char arr[MAXN];

int getCost(int a, int b) {
  if ((a + 1) % MAXM == b)
    return cost[a];
  else
    return 0;
}

int getNum(char c) {
  if (c == 'r')
    return 0;
  else if (c == 's')
    return 1;
  else
    return 2;
}

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

  cin >> n >> k;

  for (int i = 0; i < 3; i++) {
    cin >> cost[i];
  }

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

  for (int i = 0; i < n; i++) {
    int tmp = i - k;
    int cur = getNum(arr[i]);

    for (int j = 0; j < 3; j++) {
      if (tmp < 0) {
        dp[j][i] = getCost(j, cur);
      } else {
        for (int t = 0; t < 3; t++) {
          if (j == t)
            continue;
          dp[j][i] = max(dp[j][i], dp[t][i - k] + getCost(j, cur));
        }
      }
    }
  }

  int ans = 0;

  for (int i = n - 1; i >= max(n - k, 0); i--) {
    int sum = 0;

    for (int j = 0; j < 3; j++) {
      sum = max(sum, dp[j][i]);
    }
    ans += sum;
  }

  cout << ans << "\n";
  return 0;
}

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

using namespace std;

const int MAXN = 1e5 + 1;
const int MAXM = 3;
int n, k, r, s, p;
int dp[MAXM][MAXN], cost[MAXM];
char arr[MAXN];

int getCost(int a, int b) {
  if ((a + 1) % MAXM == b)
    return cost[a];
  else
    return 0;
}

int getNum(char c) {
  if (c == 'r')
    return 0;
  else if (c == 's')
    return 1;
  else
    return 2;
}

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

  cin >> n >> k;

  for (int i = 0; i < 3; i++) {
    cin >> cost[i];
  }

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

  for (int i = 0; i < n; i++) {
    int tmp = i - k;
    int cur = getNum(arr[i]);

    for (int j = 0; j < 3; j++) {
      if (tmp < 0) {
        dp[j][i] = getCost(j, cur);
      } else {
        for (int t = 0; t < 3; t++) {
          if (j == t)
            continue;
          dp[j][i] = max(dp[j][i], dp[t][i - k] + getCost(j, cur));
        }
      }
    }
  }

  int ans = 0;

  for (int i = n - 1; i >= max(n - k, 0); i--) {
    int sum = 0;

    for (int j = 0; j < 3; j++) {
      sum = max(sum, dp[j][i]);
    }
    ans += sum;
  }

  cout << ans << "\n";
  return 0;
}
Problem p02820 (C++)

Buggy submission (status: Runtime Error):
// Give me My Shotgun

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

#define all(x) x.begin(), x.end()

#define PB push_back
#define F first
#define S second
#define _sz(x) (int)x.size()

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

typedef long long ll;
typedef pair<int, int> pii;

const int MAX_N = -1;
const int inf = 2e9 + 19;

ll n, k, r, p, s;
string T, S;

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  cin >> r >> s >> p;
  cin >> T;
  ll ans = 0;
  for (int i = 0; i < k; i++) {
    if (T[i] == 'r')
      S[i] = 'p', ans += p;
    if (T[i] == 'p')
      S[i] = 's', ans += s;
    if (T[i] == 's')
      S[i] = 'r', ans += r;
  }
  for (int i = k; i < n; i++) {
    if (T[i] == 'r')
      if (S[i - k] != 'p')
        ans += p, S[i] = 'p';
    if (T[i] == 's')
      if (S[i - k] != 'r')
        ans += r, S[i] = 'r';
    if (T[i] == 'p')
      if (S[i - k] != 's')
        ans += s, S[i] = 's';
  }
  cout << ans;
  return (0);
}


Fixed submission:
// Give me My Shotgun

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

#define all(x) x.begin(), x.end()

#define PB push_back
#define F first
#define S second
#define _sz(x) (int)x.size()

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

typedef long long ll;
typedef pair<int, int> pii;

const int MAX_N = -1;
const int inf = 2e9 + 19;

ll n, k, r, p, s;
string T, S;

int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> k;
  cin >> r >> s >> p;
  cin >> T;
  ll ans = 0;
  string tmp = "x";
  for (int i = 0; i < n; i++)
    S += tmp;
  for (int i = 0; i < k; i++) {
    if (T[i] == 'r')
      S[i] = 'p', ans += p;
    if (T[i] == 'p')
      S[i] = 's', ans += s;
    if (T[i] == 's')
      S[i] = 'r', ans += r;
  }
  for (int i = k; i < n; i++) {
    if (T[i] == 'r')
      if (S[i - k] != 'p')
        ans += p, S[i] = 'p';
    if (T[i] == 's')
      if (S[i - k] != 'r')
        ans += r, S[i] = 'r';
    if (T[i] == 'p')
      if (S[i - k] != 's')
        ans += s, S[i] = 's';
  }
  cout << ans;
  return (0);
}
Problem p02820 (C++)

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

using namespace std;

typedef long long ll;

int N, K;
ll R, S, P;
string T;

ll dp[100000 + 100][3]; // j -> 0->r 1->s 2->p

int main() {
  cin >> N >> K >> R >> S >> P >> T;
  ll ans = 0;
  for (int i = 0; i < K; i++) {
    if (T[i] == 'r') {
      dp[i][2] = P;
    } else if (T[i] == 'p') {
      dp[i][1] = S;
    } else {
      dp[i][0] = R;
    }
    ll tmp = 0;
    for (int j = i + K; j < N; j++) {
      dp[j][0] = max(dp[j - K][1], dp[j - K][2]);
      dp[j][1] = max(dp[j - K][0], dp[j - K][2]);
      dp[j][2] = max(dp[j - K][0], dp[j - K][1]);
      if (T[j] == 'r') {
        dp[j][2] = P + max(dp[j - K][0], dp[j - K][1]);
      } else if (T[j] == 'p') {
        dp[j][1] = S + max(dp[j - K][0], dp[j - K][2]);
      } else {
        dp[j][0] = R + max(dp[j - K][1], dp[j - K][2]);
      }
      tmp = max(tmp, max(dp[j][0], max(dp[j][1], dp[j][2])));
    }
    // ans += tmp;
  }
  for (int i = N - 1; i > N - 1 - K; i--) {
    ans += max(dp[i][0], max(dp[i][1], dp[i][2]));
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

typedef long long ll;

int N, K;
ll R, S, P;
string T;

ll dp[100000 + 100][3]; // j -> 0->r 1->s 2->p

int main() {
  cin >> N >> K >> R >> S >> P >> T;
  ll ans = 0;
  for (int i = 0; i < K; i++) {
    if (T[i] == 'r') {
      dp[i][2] = P;
    } else if (T[i] == 'p') {
      dp[i][1] = S;
    } else {
      dp[i][0] = R;
    }
    ll tmp = 0;
    for (int j = i + K; j < N; j += K) {
      dp[j][0] = max(dp[j - K][1], dp[j - K][2]);
      dp[j][1] = max(dp[j - K][0], dp[j - K][2]);
      dp[j][2] = max(dp[j - K][0], dp[j - K][1]);
      if (T[j] == 'r') {
        dp[j][2] = P + max(dp[j - K][0], dp[j - K][1]);
      } else if (T[j] == 'p') {
        dp[j][1] = S + max(dp[j - K][0], dp[j - K][2]);
      } else {
        dp[j][0] = R + max(dp[j - K][1], dp[j - K][2]);
      }
      tmp = max(tmp, max(dp[j][0], max(dp[j][1], dp[j][2])));
    }
    // ans += tmp;
  }
  for (int i = N - 1; i > N - 1 - K; i--) {
    ans += max(dp[i][0], max(dp[i][1], dp[i][2]));
  }
  cout << ans << endl;
  return 0;
}
Problem p02820 (C++)

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

using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(vr) vr.begin(), vr.end()

typedef long long ll;
ll x, n;
ll r, s, p, k, ans, dem;
string t;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  freopen("input.txt", "r", stdin);
  cin >> n >> k;
  cin >> r >> s >> p;
  cin >> t;
  t = '@' + t;
  for (int i = 1; i <= k; i++)
    t = t + '@';
  dem = 1;
  for (int i = 1; i <= min(n, k); i++)
    for (int j = 1; j <= n && j * k + i <= n + k; j++) {
      if (j * k + i > n + k)
        break;
      if (t[j * k + i] != t[(j - 1) * k + i]) {
        if (dem != 0) {
          dem += (dem % 2 == 1);
          if (t[(j - 1) * k + i] == 'r')
            ans += p * (dem / 2);
          if (t[(j - 1) * k + i] == 's')
            ans += r * (dem / 2);
          if (t[(j - 1) * k + i] == 'p')
            ans += s * (dem / 2);
          dem = 1;
          continue;
        }
        if (t[(j - 1) * k + i] == 'r')
          ans += p;
        if (t[(j - 1) * k + i] == 's')
          ans += r;
        if (t[(j - 1) * k + i] == 'p')
          ans += s;
      } else
        dem++;
    }
  cout << ans << endl;
  return 0;
}


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

using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define all(vr) vr.begin(), vr.end()

typedef long long ll;
ll x, n;
ll r, s, p, k, ans, dem;
string t;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // freopen("input.txt","r",stdin);
  cin >> n >> k;
  cin >> r >> s >> p;
  cin >> t;
  t = '@' + t;
  for (int i = 1; i <= k; i++)
    t = t + '@';
  dem = 1;
  for (int i = 1; i <= min(n, k); i++)
    for (int j = 1; j <= n && j * k + i <= n + k; j++) {
      if (j * k + i > n + k)
        break;
      if (t[j * k + i] != t[(j - 1) * k + i]) {
        if (dem != 0) {
          dem += (dem % 2 == 1);
          if (t[(j - 1) * k + i] == 'r')
            ans += p * (dem / 2);
          if (t[(j - 1) * k + i] == 's')
            ans += r * (dem / 2);
          if (t[(j - 1) * k + i] == 'p')
            ans += s * (dem / 2);
          dem = 1;
          continue;
        }
        if (t[(j - 1) * k + i] == 'r')
          ans += p;
        if (t[(j - 1) * k + i] == 's')
          ans += r;
        if (t[(j - 1) * k + i] == 'p')
          ans += s;
      } else
        dem++;
    }
  cout << ans << endl;
  return 0;
}
Problem p02821 (C++)

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

typedef long long int64;
const int MAX = 1e5 + 5;
int n, a[MAX];
int64 m, t[MAX], sum[MAX];

pair<int64, int64> f(int x) {
  int64 cnt = 0, tot_sum = 0;
  for (int i = 1; i <= n; i++) {
    int64 c = t[max(1, x - a[i])];
    int64 s = sum[max(1, x - a[i])];

    cnt += c;
    tot_sum += s;
    tot_sum += c * a[i];
  }

  return {cnt, tot_sum};
}

int main() {
  scanf("%d%lld", &n, &m);

  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    t[a[i]]++;
    sum[a[i]] += a[i];
  }

  for (int i = 1e5; i > 0; i--) {
    t[i] += t[i + 1];
    sum[i] += sum[i + 1];
  }

  int st = 1, nd = 2e5, x = 0;

  while (st <= nd) {
    int mid = (st + nd) >> 1;

    if (f(mid).first >= m) {
      x = max(x, mid);
      st = mid + 1;
    }

    else
      nd = mid - 1;
  }

  auto o = f(x + 1);
  int64 ans = 1LL * (m - o.first) * x + o.second;

  printf("%lld\n", ans);
  return 0;
}

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

typedef long long int64;
const int MAX = 2e5 + 5;
int n, a[MAX];
int64 m, t[MAX], sum[MAX];

pair<int64, int64> f(int x) {
  int64 cnt = 0, tot_sum = 0;
  for (int i = 1; i <= n; i++) {
    int64 c = t[max(1, x - a[i])];
    int64 s = sum[max(1, x - a[i])];

    cnt += c;
    tot_sum += s;
    tot_sum += c * a[i];
  }

  return {cnt, tot_sum};
}

int main() {
  scanf("%d%lld", &n, &m);

  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    t[a[i]]++;
    sum[a[i]] += a[i];
  }

  for (int i = 1e5; i > 0; i--) {
    t[i] += t[i + 1];
    sum[i] += sum[i + 1];
  }

  int st = 1, nd = 2e5, x = 0;

  while (st <= nd) {
    int mid = (st + nd) >> 1;

    if (f(mid).first >= m) {
      x = max(x, mid);
      st = mid + 1;
    }

    else
      nd = mid - 1;
  }

  auto o = f(x + 1);
  int64 ans = 1LL * (m - o.first) * x + o.second;

  printf("%lld\n", ans);
  return 0;
}
Problem p02821 (C++)

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

using namespace std;
ll n, suff[100100], cnt[100100], suffval[100100], a[100100];

int main() {
  ll m;
  cin >> n >> m;
  for (ll i = 0; i < n; i++)
    cin >> a[i], cnt[a[i]]++;
  sort(a, a + n);
  for (ll i = 1e5; i >= 0; i--)
    suff[i] = cnt[i] + suff[i + 1];
  for (ll i = 1e5; i >= 0; i--)
    suffval[i] = suffval[i + 1] + (cnt[i] * i);
  ll Start = 0, End = 2e5, canSum = 0;
  while (Start <= End) {
    ll mid = (Start + End) / 2;
    ll needSum = mid + 1;
    ll Res = 0;
    for (ll i = 0; i < n; i++) {
      Res += (suff[max((ll)0, needSum - a[i])] -
              (needSum - a[i] <= a[i] ? cnt[a[i]] : 0));
      if (needSum - a[i] <= a[i])
        Res += cnt[a[i]];
    }
    if (Res <= m)
      canSum = mid, End = mid - 1;
    else
      Start = mid + 1;
  }
  ll Ans = 0;
  canSum++;
  for (ll i = 0; i < n; i++) {
    Ans += suffval[max((ll)0, canSum - a[i])];
    ll Ways = 0;
    Ways += (suff[max((ll)0, canSum - a[i])] -
             (canSum - a[i] <= a[i] ? cnt[a[i]] : 0));
    if (canSum - a[i] <= a[i])
      Ways += cnt[a[i]];
    m -= Ways;
    Ans += a[i] * Ways;
  }
  cout << Ans + m * (canSum - 1);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <iostream>
#define ll long long

using namespace std;
ll n, suff[200100], cnt[200100], suffval[200100], a[200100];

int main() {
  ll m;
  cin >> n >> m;
  for (ll i = 0; i < n; i++)
    cin >> a[i], cnt[a[i]]++;
  sort(a, a + n);
  for (ll i = 1e5; i >= 0; i--)
    suff[i] = cnt[i] + suff[i + 1];
  for (ll i = 1e5; i >= 0; i--)
    suffval[i] = suffval[i + 1] + (cnt[i] * i);
  ll Start = 0, End = 2e5, canSum = 0;
  while (Start <= End) {
    ll mid = (Start + End) / 2;
    ll needSum = mid + 1;
    ll Res = 0;
    for (ll i = 0; i < n; i++) {
      Res += (suff[max((ll)0, needSum - a[i])] -
              (needSum - a[i] <= a[i] ? cnt[a[i]] : 0));
      if (needSum - a[i] <= a[i])
        Res += cnt[a[i]];
    }
    if (Res <= m)
      canSum = mid, End = mid - 1;
    else
      Start = mid + 1;
  }
  ll Ans = 0;
  canSum++;
  for (ll i = 0; i < n; i++) {
    Ans += suffval[max((ll)0, canSum - a[i])];
    ll Ways = 0;
    Ways += (suff[max((ll)0, canSum - a[i])] -
             (canSum - a[i] <= a[i] ? cnt[a[i]] : 0));
    if (canSum - a[i] <= a[i])
      Ways += cnt[a[i]];
    m -= Ways;
    Ans += a[i] * Ways;
  }
  cout << Ans + m * (canSum - 1);
  return 0;
}
Problem p02821 (Python)

Buggy submission (status: Runtime Error):
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
s = a[0]
t = 0
j = 1
ret = 0
for i in range(n):
    s -= a[i]
    ret += a[i] * 2
    m -= 1
    while j < n and (i >= n - 1 or a[i + 1] + a[i + 1] <= a[i] + a[j]):
        if m < i * 2:
            for k in range(i):
                if m > 0:
                    ret += a[j] + a[k]
                    m -= 1
                if m > 0:
                    ret += a[j] + a[k]
                    m -= 1
        else:
            ret += a[j] * i * 2 + t * 2
            m -= i * 2
        s += a[j]
        j += 1
    num = (j - i - 1) * 2
    if m <= num:
        for k in range(i + 1, j):
            if m > 0:
                ret += a[i] + a[k]
                m -= 1
            if m > 0:
                ret += a[i] + a[k]
                m -= 1
        break
    m -= num
    ret += s * 2 + a[i] * num
    t += a[i]
    print(a, m, num, s, i, j, ret)
print(ret)


Fixed submission:
n, m = map(int, input().split())
a = sorted(list(map(int, input().split())), reverse=True)
s = [0]
for ai in a:
    s.append(ai + s[-1])


def count(x, accum=False):
    ret = 0
    for ai in a:
        lo, hi = -1, n
        while hi - lo > 1:
            mid = (lo + hi) // 2
            if ai + a[mid] >= x:
                lo = mid
            else:
                hi = mid
        ret += ai * hi + s[hi] if accum else hi
    return ret


lo, hi = 0, 1000000000
while hi - lo > 1:
    mid = (lo + hi) // 2
    if count(mid) >= m:
        lo = mid
    else:
        hi = mid

print(count(lo, accum=True) - (count(lo) - m) * lo)
Problem p02821 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#define SIZE 2010
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define ll long long
#define mp make_pair
#define pi acos(-1.0)
#define eps 1e-6
using namespace std;
void io() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
ll n, k, ans, cnt, res;
ll a[SIZE], pre[SIZE];

bool check(ll x) {
  res = cnt = 0;
  rep(i, 1, n) {
    ll pos = lower_bound(a + 1, a + n + 1, x - a[i]) - a;
    cnt += n - pos + 1ll;
    res += pre[n] - pre[pos - 1] + a[i] * (n - pos + 1);
  }
  return cnt >= k;
}

int main() {
  io();
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  sort(a + 1, a + 1 + n);
  rep(i, 1, n) pre[i] = pre[i - 1] + a[i];
  ll L = 1, R = 2e5 + 10, md;
  while (L <= R) {
    md = (L + R) >> 1;
    if (check(md))
      ans = md, L = md + 1;
    else
      R = md - 1;
  }
  cout << res - (cnt - k) * ans;
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#define SIZE 200010
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define ll long long
#define mp make_pair
#define pi acos(-1.0)
#define eps 1e-6
using namespace std;
void io() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
}
ll n, k, ans, cnt, res;
ll a[SIZE], pre[SIZE];

bool check(ll x) {
  res = cnt = 0;
  rep(i, 1, n) {
    ll pos = lower_bound(a + 1, a + n + 1, x - a[i]) - a;
    cnt += n - pos + 1ll;
    res += pre[n] - pre[pos - 1] + a[i] * (n - pos + 1);
  }
  return cnt >= k;
}

int main() {
  io();
  cin >> n >> k;
  rep(i, 1, n) cin >> a[i];
  sort(a + 1, a + 1 + n);
  rep(i, 1, n) pre[i] = pre[i - 1] + a[i];
  ll L = 1, R = 2e5 + 10, md;
  while (L <= R) {
    md = (L + R) >> 1;
    if (check(md))
      ans = md, L = md + 1;
    else
      R = md - 1;
  }
  cout << res - (cnt - k) * ans;
}
Problem p02821 (C++)

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

using namespace std;

int n = 0, m = 0;
int arr[100000];
int suf[100010];

int get(int l, int r) {
  int res = suf[l];
  res -= suf[r + 1];
  return res;
}

bool check(int mm) {
  int res = 0;
  for (int i = 0; i < n; i++) {
    auto it = lower_bound(arr, arr + n, mm - arr[i]);
    res += (arr + n) - it;
  }
  return (res >= m);
}

void print(int mm) {
  int res = 0, Min = INT_MAX, sum = 0;
  for (int i = 0; i < n; i++) {
    auto it = lower_bound(arr, arr + n, mm - arr[i]);
    if (it != arr + n)
      Min = min(Min, (*it) + arr[i]);
    res += (arr + n) - it;
    sum += suf[it - arr];
    sum += ((arr + n) - it) * arr[i];
  }
  assert((res == m) || (res == m + 1) || (res == m + 2));
  if (res > m) {
    sum -= Min;
  }
  cout << sum << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  int sm = 0;
  for (int i = n - 1; i >= 0; i--) {
    sm += arr[i];
    suf[i] = sm;
  }
  int ll = arr[0] * 2, rr = arr[n - 1] * 2;
  while (ll < rr - 1) {
    int mm = (ll + rr) / 2;
    if (check(mm)) {
      ll = mm;
    } else
      rr = mm;
  }
  if (check(rr)) {
    print(rr);
  } else
    print(ll);
  return 0;
}


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

using namespace std;

int n = 0, m = 0;
int arr[100000];
int suf[100010];

int get(int l, int r) {
  int res = suf[l];
  res -= suf[r + 1];
  return res;
}

bool check(int mm) {
  int res = 0;
  for (int i = 0; i < n; i++) {
    auto it = lower_bound(arr, arr + n, mm - arr[i]);
    res += (arr + n) - it;
  }
  return (res >= m);
}

void print(int mm) {
  int res = 0, Min = INT_MAX, sum = 0;
  for (int i = 0; i < n; i++) {
    auto it = lower_bound(arr, arr + n, mm - arr[i]);
    if (it != arr + n)
      Min = min(Min, (*it) + arr[i]);
    res += (arr + n) - it;
    sum += suf[it - arr];
    sum += ((arr + n) - it) * arr[i];
  }
  sum -= (res - m) * Min;
  cout << sum << endl;
}

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  int sm = 0;
  for (int i = n - 1; i >= 0; i--) {
    sm += arr[i];
    suf[i] = sm;
  }
  int ll = arr[0] * 2, rr = arr[n - 1] * 2;
  while (ll < rr - 1) {
    int mm = (ll + rr) / 2;
    if (check(mm)) {
      ll = mm;
    } else
      rr = mm;
  }
  if (check(rr)) {
    print(rr);
  } else
    print(ll);
  return 0;
}