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

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

int main(void) {
  int N;
  int left, right;
  int water = 0;
  cin >> N;

  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }

  left = 0;
  while (1) {
    while (h[left] == 0) {
      left++;
      if (left == N)
        break;
    }
    right = left;
    while (h[right]) {
      right++;
    }
    if (right > left)
      water++;
    for (int i = left; i < right; i++) {
      h[i]--;
    }
    if (left == N)
      break;
  }

  cout << water << endl;
  return 0;
}


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

int main(void) {
  int N;
  int left, right;
  int water = 0;
  cin >> N;

  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }

  left = 0;
  while (1) {
    while (h[left] == 0) {
      left++;
      if (left == N)
        break;
    }
    if (left == N)
      break;
    right = left + 1;
    if (right != N) {
      while (h[right]) {
        right++;
        if (right == N)
          break;
      }
    }
    if (right > left)
      water++;
    for (int i = left; i < right; i++) {
      h[i]--;
    }
    if (left == N)
      break;
  }

  cout << water << endl;
  return 0;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> h(N);
  for (int i = 1; i <= N; i++)
    cin >> h[i];
  long long int ans = 0, active = 0;
  for (int i = 1; i <= N; i++) {
    if (active < h[i]) {
      ans += h[i] - active;
      active = h[i];
    } else
      active = h[i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <numeric>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> h(1000);
  for (int i = 1; i <= N; i++)
    cin >> h[i];
  long long int ans = 0, active = 0;
  for (int i = 1; i <= N; i++) {
    if (active < h[i]) {
      ans += h[i] - active;
      active = h[i];
    } else
      active = h[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

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

  int N;

  cin >> N;

  vector<int> h(N);

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

  int c = 0;
  int p = 0;

  while (p < N) {
    if (h[p] > 0) {
      c++;
      int s = p;
      while (h[s] != 0) {
        h[s]--;
        s++;
      }
    } else {
      p++;
    }
  }

  cout << c << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

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

  int N;

  cin >> N;

  vector<int> h(N);

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

  int c = 0;
  int p = 0;

  while (p < N) {
    if (h[p] > 0) {
      c++;
      int s = p;
      while (h[s] > 0 && s < N) {
        h[s]--;
        s++;
      }
    } else {
      p++;
    }
  }

  cout << c << endl;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

int main() {
  int n, l;
  int i = 0;
  cin >> n;
  vector<int> table(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> table[i];
  }

  table[n + 1] = 0;
  int max = 0, count = 0;

  for (i = 0; i < n; ++i) {
    if (max < table[i])
      max = table[i];
  }

  while (max != 0) {
    for (i = 0; i < n; ++i) {
      if (max == table[i]) {
        if (table[i] == table[i + 1])
          table[i]--;
        else {
          table[i]--;
          count++;
        }
      }
    }
    max--;
  }

  cout << count << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

int main() {
  int n, l;
  int i = 0;
  cin >> n;
  int table[n + 1];
  for (int i = 0; i < n; i++) {
    cin >> table[i];
  }

  table[n + 1] = 0;
  int max = 0, count = 0;

  for (i = 0; i < n; ++i) {
    if (max < table[i])
      max = table[i];
  }

  while (max != 0) {
    for (i = 0; i < n; ++i) {
      if (max == table[i]) {
        if (table[i] == table[i + 1])
          table[i]--;
        else {
          table[i]--;
          count++;
        }
      }
    }
    max--;
  }

  cout << count << endl;
  return 0;
}
Problem p03147 (C++)

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

#define rep(i, n) for (ll i = 0; i < (n); i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define ALL(x) (x).begin(), (x).end()
#define Sort(a) sort(ALL(a))
#define Rev(a) reverse(ALL(a))

using namespace std;
// using ll=long long;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
const long double PI = 3.14159265358979323846;

template <class T> bool contain(const std::string &s, const T &v) {
  return s.find(v) != std::string::npos;
}

int main() {

  ll n;
  cin >> n;

  vector<ll> h(n + 1);
  rep(i, n) { cin >> h[i]; }
  h[n] = 0;
  ll ans = 10000000000;

  ll i = 0;
  ll j = 0;
  while (h[j] == 0)
    j++;
  i = j;

  while (j < n) {
    // cout<<j<<h[i]<<endl;
    if (i > n - 1 || h[i] == 0) {
      // cout<<"#"<<ans<<" "<<i<<" "<<j<<endl;
      while (h[j] == 0)
        j++;
      i = j;
      ans++;
    }
    if (h[i] > 0) {
      h[i]--;
      // cout<<i<<endl;
      i++;
    }
  }

  cout << ans << endl;
}


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

#define rep(i, n) for (ll i = 0; i < (n); i++)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define ALL(x) (x).begin(), (x).end()
#define Sort(a) sort(ALL(a))
#define Rev(a) reverse(ALL(a))

using namespace std;
// using ll=long long;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;
const long double PI = 3.14159265358979323846;

template <class T> bool contain(const std::string &s, const T &v) {
  return s.find(v) != std::string::npos;
}

int main() {

  ll n;
  cin >> n;

  vector<ll> h(n + 1);
  rep(i, n) { cin >> h[i]; }
  h[n] = 100000000000;
  ll ans = 0;

  ll i = 0;
  ll j = 0;
  while (h[j] == 0)
    j++;
  i = j;

  while (j < n) {
    // cout<<j<<h[i]<<endl;
    if (i > n - 1 || h[i] == 0) {
      // cout<<"#"<<ans<<" "<<i<<" "<<j<<endl;
      while (h[j] == 0)
        j++;
      i = j;
      ans++;
    }
    if (h[i] > 0) {
      h[i]--;
      // cout<<i<<endl;
      i++;
    }
  }

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

Buggy submission (status: Runtime Error):
// g++でビルド

#include "bits/stdc++.h"

using namespace std;

vector<int> watering(vector<int> vec, int s) {
  while (vec[s] != 0) {
    vec[s]--;
    s++;
  }
  return vec;
}

int main() {
  int N;
  cin >> N;
  vector<int> flowers;
  int result = 0;

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

  for (int i = 0; i < N; i++) {
    while (flowers[i] != 0) {
      flowers = watering(flowers, i);
      result++;
    }
  }

  cout << result << endl;
  return 0;
}

Fixed submission:
// g++でビルド

#include "bits/stdc++.h"

using namespace std;

vector<int> watering(vector<int> vec, int s) {
  while (vec[s] != 0) {
    vec[s]--;
    s++;
    if (vec.size() == s) {
      break;
    }
  }
  return vec;
}

int main() {
  int N;
  cin >> N;
  vector<int> flowers;
  int result = 0;

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

  for (int i = 0; i < N; i++) {
    while (flowers[i] != 0) {
      flowers = watering(flowers, i);
      result++;
    }
  }

  cout << result << endl;
  return 0;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lb;
#define REP(i, j, n) for (int i = j; i < (n); i++)
#define RREP(i, n, j) for (int i = n; j <= i; i--)
#define print(out) cout << out << "\n";
#define all(vec) vec.begin(), vec.end()
#define mod 1000000007
const int INF = 1e9;
typedef pair<ll, ll> P;

int main() {
  int N;
  cin >> N;
  vector<int> H(N);
  REP(i, 0, N) { cin >> H[i]; }

  int give_water = 0, cnt, idx;
  while (true) {
    cnt = idx = 0;
    while (idx < N) {
      cnt += H[idx];
      if (H[idx]) {
        give_water++;
        while (H[idx]) {
          H[idx]--;
          idx++;
        }
      }
      idx++;
    }
    if (cnt == 0) {
      break;
    }
  }
  print(give_water)
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double lb;
#define REP(i, j, n) for (int i = j; i < (n); i++)
#define RREP(i, n, j) for (int i = n; j <= i; i--)
#define print(out) cout << out << "\n";
#define all(vec) vec.begin(), vec.end()
#define mod 1000000007
const int INF = 1e9;
typedef pair<ll, ll> P;

int main() {
  int N;
  cin >> N;
  vector<int> H(N);
  REP(i, 0, N) { cin >> H[i]; }

  int give_water = 0, cnt, idx;
  while (true) {
    cnt = idx = 0;
    while (idx < N) {
      cnt += H[idx];
      if (H[idx]) {
        give_water++;
        while (idx < N && H[idx]) {
          H[idx]--;
          idx++;
        }
      }
      idx++;
    }
    if (cnt == 0) {
      break;
    }
  }
  print(give_water)
}
Problem p03147 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

#ifdef _DEBUG
#define DMP(x) cerr << #x << ": " << x << "\n"
#else
#define DMP(x) ((void)0)
#endif

const int MOD = 1000000007, INF = 1111111111;
using namespace std;
typedef long long lint;

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

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

  int ans = 0, st = 0, minh = 0;
  while (minh != INF) {

    st = 0;
    minh = h[0] > 0 ? h[0] : INF;

    if (N == 0) {
      ans = h[0];
      break;
    }

    for (int i = 1; i < N; i++) {

      minh = (h[i] > 0) && h[i] < minh ? h[i] : minh;

      if (h[i - 1] > 0 && h[i] <= 0 || (i == N - 1) && h[N - 1] > 0) {

        for (int j = st; j <= i; j++)
          h[j] -= minh;

        ans += minh;
        st = i;
        minh = INF - 1;
      }
    }
  }

  cout << ans << "\n";

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <string>
#include <vector>

#ifdef _DEBUG
#define DMP(x) cerr << #x << ": " << x << "\n"
#else
#define DMP(x) ((void)0)
#endif

const int MOD = 1000000007, INF = 1111111111;
using namespace std;
typedef long long lint;

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

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

  int ans = 0, st = 0, minh = 0;
  while (minh != INF) {

    st = 0;
    minh = h[0] > 0 ? h[0] : INF;

    if (N == 1) {
      ans = h[0];
      break;
    }

    for (int i = 1; i < N; i++) {

      minh = (h[i] > 0) && h[i] < minh ? h[i] : minh;

      if (h[i - 1] > 0 && h[i] <= 0 || (i == N - 1) && h[N - 1] > 0) {

        for (int j = st; j <= i; j++)
          h[j] -= minh;

        ans += minh;
        st = i;
        minh = INF - 1;
      }
    }
  }

  cout << ans << "\n";

  return 0;
}
Problem p03147 (C++)

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

int main() {
  int n;
  cin >> n;
  auto h = vector<int>(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  long long ans = 0;
  for (int l = 0; l < n; l++) {
    while (h[l] > 0) {
      int r = l;
      while (h[r] > 0) {
        r++;
      }
      r--;
      auto _min = *min_element(h.begin() + l, h.begin() + r);
      for (int i = l; i <= r; i++) {
        h[i] -= _min;
      }

      ans += _min;
    }
  }

  cout << ans;
}

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

int main() {
  int n;
  cin >> n;
  auto h = vector<int>(n + 1, 0);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  long long ans = 0;
  for (int l = 0; l < n; l++) {
    while (h[l] > 0) {
      int r = l;
      while (h[r] > 0) {
        r++;
      }
      r--;
      auto _min = *min_element(h.begin() + l, h.begin() + r);
      for (int i = l; i <= r; i++) {
        h[i] -= _min;
      }

      ans += _min;
    }
  }

  cout << ans;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INF INT_MAX     // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define per(i, n) for (ll i = ((ll)(n)) - 1; i >= 0; i--)
#define perf(i, n) for (ll i = ((ll)(n)); i > 0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  ll n, cur = -1;
  cin >> n;
  vi h(n);
  rep(i, n) {
    cin >> h[i];
    if (cur == -1 && h[i] > 0)
      cur = i;
  }
  ll ans = 0;
  while (cur < n) {
    if (h[cur] == 0) {
      cur++;
      continue;
    }
    ans++;
    for (ll i = cur; i < n; i++) {
      if (h[i] > 0)
        h[i]--;
      else
        break;
    }
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INF INT_MAX     // 2147483647
#define INFLL LLONG_MAX // 9223372036854775807
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define per(i, n) for (ll i = ((ll)(n)) - 1; i >= 0; i--)
#define perf(i, n) for (ll i = ((ll)(n)); i > 0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(10);
  ll n, cur = -1;
  cin >> n;
  vi h(n);
  rep(i, n) {
    cin >> h[i];
    if (cur == -1 && h[i] > 0)
      cur = i;
  }
  ll ans = 0;
  if (cur == -1) {
    cout << 0 << endl;
    return 0;
  }
  while (cur < n) {
    if (h[cur] == 0) {
      cur++;
      continue;
    }
    ans++;
    for (ll i = cur; i < n; i++) {
      if (h[i] > 0)
        h[i]--;
      else
        break;
    }
  }
  cout << ans << endl;
}
Problem p03147 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 10000000000000009
// #define INF 9223372036854775807
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define ORREP(i, n) for (int i = (n); i >= 1; --i)
#define ZREP(i, n) for (int i = 1; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define rollcall cout << "I'm Sucu." << endl;
#define YES(s) s ? cout << "YES" << endl : cout << "NO" << endl
#define Yes(s) s ? cout << "Yes" << endl : cout << "No" << endl
#define Taka(s) s ? cout << "Takahashi" << endl : cout << "Aoki" << endl
#define out(s, t, u) s ? cout << t << endl : cout << u << endl
#define int ll
#define Endl endl

int h[114];
int Ans = 0;

int kukan(int l, int r) {
  if (l == r) {
    Ans += h[l];
    h[l] = 0;
    return 0;
  }
  if (l > r) {
    return 0;
  }
  int e = INF;
  for (int i = l; i <= r; i++) {
    e = min(e, h[i]);
  }
  Ans += e;
  for (int i = l; i <= r; i++) {
    h[i] -= e;
  }
  for (int i = l; i <= r; i++) {
    if (h[i] == 0) {
      kukan(l, i - 1);
      kukan(i + 1, r);
    }
  }
  return 0;
}

signed main() {
  int N;
  cin >> N;
  REP(i, N) { cin >> h[i]; }
  kukan(0, N - 1);
  cout << Ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define INF 10000000000000009
// #define INF 9223372036854775807
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define OREP(i, n) for (int i = 1; i <= (n); ++i)
#define ORREP(i, n) for (int i = (n); i >= 1; --i)
#define ZREP(i, n) for (int i = 1; i < (n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define rollcall cout << "I'm Sucu." << endl;
#define YES(s) s ? cout << "YES" << endl : cout << "NO" << endl
#define Yes(s) s ? cout << "Yes" << endl : cout << "No" << endl
#define Taka(s) s ? cout << "Takahashi" << endl : cout << "Aoki" << endl
#define out(s, t, u) s ? cout << t << endl : cout << u << endl
#define int ll
#define Endl endl

int h[114];
int Ans = 0;

int kukan(int l, int r) {
  if (l == r) {
    Ans += h[l];
    h[l] = 0;
    return 0;
  }
  if (l > r) {
    return 0;
  }
  int e = INF;
  for (int i = l; i <= r; i++) {
    e = min(e, h[i]);
  }
  Ans += e;
  for (int i = l; i <= r; i++) {
    h[i] -= e;
  }
  for (int i = l; i <= r; i++) {
    if (h[i] == 0) {
      kukan(l, i - 1);
      kukan(i + 1, r);
      break;
    }
  }
  return 0;
}

signed main() {
  int N;
  cin >> N;
  REP(i, N) { cin >> h[i]; }
  kukan(0, N - 1);
  cout << Ans << endl;

  return 0;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define numrep(i, n) for (int i = 1; i <= n; i++)
#define bitrep(n) for (int bit = 0; bit < (1 << n); bit++)
#define ll int64_t
#define all(v) v.begin(), v.end()

const int MOD = 1000000007;

int main() {

  ll N;
  cin >> N;

  vector<int> h(N);

  rep(i, N) cin >> h[i]; // input

  ll ans = 0;

  rep(i, N) {
    while (h[i] > 0) {
      int j = 0, x = 100;
      while (h[i + j] > 0) {
        if (i + j >= N)
          break;
        x = min(x, h[i + j]);
        j++;
      }
      for (int k = i; k <= i + j; k++) {
        h[k] -= x;
      }
      ans += x;
    }
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define numrep(i, n) for (int i = 1; i <= n; i++)
#define bitrep(n) for (int bit = 0; bit < (1 << n); bit++)
#define ll int64_t
#define all(v) v.begin(), v.end()

const int MOD = 1000000007;

int main() {

  ll N;
  cin >> N;

  vector<int> h(N + 2);

  rep(i, N) cin >> h[i]; // input

  ll ans = 0;

  rep(i, N) {
    while (h[i] > 0) {
      int j = 0, x = 100;
      while (h[i + j] > 0) {
        if (i + j >= N)
          break;
        x = min(x, h[i + j]);
        j++;
      }
      for (int k = i; k <= i + j; k++) {
        h[k] -= x;
      }
      ans += x;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03147 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];

  int ans = 0;

  while (1) {
    int mx = 0;

    for (int i = 0; i < n; i++) {
      if (mx < h[i])
        mx = h[i];
    }
    if (mx == 0)
      break;

    int i = 0;
    while (i < n) {
      if (h[i] == 0)
        i++;
      else {
        ans++;
        while (h[i] != 0) {
          h[i]--;
          i++;
        }
      }
    }
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];

  int ans = 0;

  while (1) {
    int mx = 0;

    for (int i = 0; i < n; i++) {
      if (mx < h[i])
        mx = h[i];
    }
    if (mx == 0)
      break;

    int i = 0;
    while (i < n) {
      if (h[i] == 0)
        i++;
      else {
        ans++;
        while (h[i] != 0 && i < n) {
          h[i]--;
          i++;
        }
      }
    }
  }

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

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

int h[200] = {0}, l = 1, cnt = 0;

void mizuyari(int l, int r) {
  if (l > r)
    return;
  for (int i = l; i <= r; i++) {
    h[i]--;
  }
  cnt++;
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];

  while (true) {
    for (int i = 0; h[i] == 0; i++)
      l = i + 1;
    for (int i = 1; i <= n + 1; i++) {
      if (h[i] == 0) {
        mizuyari(l, i - 1);
        for (int j = i; h[j] == 0; j++) {
          l = j + 1;
        }
      }
    }

    for (int i = 1; i <= n; i++) {
      if (h[i] != 0)
        break;
      if (i == n && h[i] == 0) {
        cout << cnt << endl;
        return 0;
      }
    }
  }
  return 0;
}


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

int l = 1, cnt = 0;
vector<int> h(300);

void mizuyari(int l, int r) {
  if (l > r)
    return;
  for (int i = l; i <= r; i++) {
    h[i]--;
  }
  cnt++;
}

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> h[i];

  while (true) {
    for (int i = 0; h[i] == 0; i++)
      l = i + 1;
    for (int i = 1; i <= n + 1; i++) {
      if (h[i] == 0) {
        mizuyari(l, i - 1);
        for (int j = i; h[j] == 0; j++) {
          l = j + 1;
        }
      }
    }

    for (int i = 1; i <= n; i++) {
      if (h[i] != 0)
        break;
      if (i == n && h[i] == 0) {
        cout << cnt << endl;
        return 0;
      }
    }
  }
  return 0;
}
Problem p03147 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> line(N);
  for (int i = 0; i < N; i++)
    cin >> line[i];

  int i = 0;
  int ans = 0;
  while (1) {
    if (i == N - 1 && line[i] == 0)
      break;
    if (line[i] == 0) {
      i++;
      continue;
    }
    ans++;
    int j = 0;
    while (1) {
      if (line[i + j] == 0)
        break;
      line[i + j]--;
      j++;
    }
  }
  cout << ans << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> line(N);
  for (int i = 0; i < N; i++)
    cin >> line[i];

  int i = 0;
  int ans = 0;
  while (1) {
    if (i == N - 1 && line[i] == 0)
      break;
    if (line[i] == 0) {
      i++;
      continue;
    }
    ans++;
    int j = 0;
    while (1) {
      if (line[i + j] == 0)
        break;
      line[i + j]--;
      j++;
      if (j == N)
        break;
    }
  }
  cout << ans << endl;
}
Problem p03148 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> vec;
  for (int i = 1; i <= N; i++) {
    int a, b;
    cin >> a >> b;
    vec.push_back(make_pair(b, a));
  }
  sort(vec.begin(), vec.end(), greater<pair<ll, ll>>());

  priority_queue<ll> pq;
  set<ll> s;
  ll sum = 0;
  for (int i = 0; i < K; i++) {
    sum += vec[i].first;
    if (s.count(vec[i].second)) {
      pq.push(-vec[i].first);
    } else {
      s.insert(vec[i].second);
    }
  }

  ll ans = sum + s.size() * s.size();
  for (int i = K; i < N; i++) {
    if (s.count(vec[i].second)) {
      continue;
    }
    sum += pq.top();
    pq.pop();
    sum += vec[i].first;
    s.insert(vec[i].second);
    ll tmp = sum + s.size() * s.size();
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<ll, ll>> vec;
  for (int i = 1; i <= N; i++) {
    int a, b;
    cin >> a >> b;
    vec.push_back(make_pair(b, a));
  }
  sort(vec.begin(), vec.end(), greater<pair<ll, ll>>());

  priority_queue<ll> pq;
  set<ll> s;
  ll sum = 0;
  for (int i = 0; i < K; i++) {
    sum += vec[i].first;
    if (s.count(vec[i].second)) {
      pq.push(-vec[i].first);
    } else {
      s.insert(vec[i].second);
    }
  }

  ll ans = sum + s.size() * s.size();
  for (int i = K; i < N && !pq.empty(); i++) {
    if (s.count(vec[i].second)) {
      continue;
    }
    sum += pq.top();
    pq.pop();
    sum += vec[i].first;
    s.insert(vec[i].second);
    ll tmp = sum + s.size() * s.size();
    ans = max(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
Problem p03148 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  const long long int mod = 1000000007, INF = 50000000000000;
  typedef pair<long long int, long long int> P;
  map<long long int, long long int> mp;
  priority_queue<P, vector<P>, greater<P>> mazuisushi;

  priority_queue<P> umaisushi;
  cout << fixed << setprecision(10);
  string str;
  long long int n, k, t, d, a, b, now = -1, tmp = 0, typ = 0;
  long long int res = 0, ng[200005] = {};
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> t >> d;
    umaisushi.push({d, t});
  }
  for (int i = 0; i < k; i++) {
    if (ng[umaisushi.top().second] == 0) {
      ng[umaisushi.top().second] = 1;
      typ++;
      tmp += umaisushi.top().first;
    } else {
      mazuisushi.push(umaisushi.top());
      tmp += umaisushi.top().first;
    }
    umaisushi.pop();
  }
  res = tmp + typ * typ;
  while (!umaisushi.empty()) {
    if (ng[umaisushi.top().second] == 0) {
      ng[umaisushi.top().second] = 1;
      typ++;
      tmp += umaisushi.top().first;
      tmp -= mazuisushi.top().first;
      mazuisushi.pop();
      res = max(res, tmp + typ * typ);
    }
    umaisushi.pop();
  }
  cout << res;
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int main() {
  const long long int mod = 1000000007, INF = 50000000000000;
  typedef pair<long long int, long long int> P;
  map<long long int, long long int> mp;
  priority_queue<P, vector<P>, greater<P>> mazuisushi;

  priority_queue<P> umaisushi;
  cout << fixed << setprecision(10);
  string str;
  long long int n, k, t, d, a, b, now = -1, tmp = 0, typ = 0;
  long long int res = 0, ng[200005] = {};
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> t >> d;
    umaisushi.push({d, t});
  }
  for (int i = 0; i < k; i++) {
    if (ng[umaisushi.top().second] == 0) {
      ng[umaisushi.top().second] = 1;
      typ++;
      tmp += umaisushi.top().first;
    } else {
      mazuisushi.push(umaisushi.top());
      tmp += umaisushi.top().first;
    }
    umaisushi.pop();
  }
  res = tmp + typ * typ;
  while (!umaisushi.empty() && !mazuisushi.empty()) {
    if (ng[umaisushi.top().second] == 0) {
      ng[umaisushi.top().second] = 1;
      typ++;
      tmp += umaisushi.top().first;
      tmp -= mazuisushi.top().first;
      mazuisushi.pop();
      res = max(res, tmp + typ * typ);
    }
    umaisushi.pop();
  }
  cout << res;
}
Problem p03148 (C++)

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

using namespace std;

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

const ll mod = 1000000007;

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

  int n, k;
  cin >> n >> k;
  vector<pair<int, ll>> td(n);
  for (auto &x : td)
    cin >> x.first >> x.second;

  vector<vector<ll>> c(n);
  for (int i = 0; i < n; i++) {
    c[td[i].first - 1].push_back(td[i].second);
  }

  for (int i = 0; i < n; i++)
    sort(c[i].rbegin(), c[i].rend());
  sort(c.rbegin(), c.rend(), [](auto &a, auto &b) {
    if (a.size() == 0)
      return true;
    else if (b.size() == 0)
      return false;
    else
      return a[0] < b[0];
  });

  ll ans = 0;
  ll varCount = 0;
  priority_queue<ll, vector<ll>, greater<ll>> noPenalty, penalty;
  for (int i = 0; i < n; i++) {
    if (c[i].size() == 0)
      continue;
    varCount++;
    penalty.push(c[i][0]);
    for (int j = 1; j < (int)c[i].size(); j++)
      noPenalty.push(c[i][j]);
    for (auto &x : c[i])
      ans += x;
  }
  ans += varCount * varCount;
  for (int i = n; i > k; i--) {
    ll p, np;
    p = penalty.top();
    if (!noPenalty.empty())
      np = noPenalty.top();
    else
      np = 9e18;
    if (p + 2 * varCount - 1 <= np) {
      penalty.pop();
      ans -= p + 2 * varCount - 1;
      varCount--;
    } else {
      noPenalty.pop();
      ans -= np;
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

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

const ll mod = 1000000007;

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

  int n, k;
  cin >> n >> k;
  vector<pair<int, ll>> td(n);
  for (auto &x : td)
    cin >> x.first >> x.second;

  vector<vector<ll>> c(n);
  for (int i = 0; i < n; i++) {
    c[td[i].first - 1].push_back(td[i].second);
  }

  for (int i = 0; i < n; i++)
    sort(c[i].rbegin(), c[i].rend());

  ll ans = 0;
  ll varCount = 0;
  priority_queue<ll, vector<ll>, greater<ll>> noPenalty, penalty;
  for (int i = 0; i < n; i++) {
    if (c[i].size() == 0)
      continue;
    varCount++;
    penalty.push(c[i][0]);
    for (int j = 1; j < (int)c[i].size(); j++)
      noPenalty.push(c[i][j]);
    for (auto &x : c[i])
      ans += x;
  }
  ans += varCount * varCount;
  for (int i = n; i > k; i--) {
    ll p, np;
    p = penalty.top();
    if (!noPenalty.empty())
      np = noPenalty.top();
    else
      np = 9e18;
    if (p + 2 * varCount - 1 <= np) {
      penalty.pop();
      ans -= p + 2 * varCount - 1;
      varCount--;
    } else {
      noPenalty.pop();
      ans -= np;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p03148 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<int, int>> sushi(n);
  for (auto &e : sushi)
    cin >> e.first >> e.second;

  sort(sushi.begin(), sushi.end(), greater<>());
  vector<ll> kt, other;
  kt.push_back(sushi[0].second);
  for (int i = 1; i < n; ++i) {
    if (sushi[i].first != sushi[i - 1].first) {
      kt.push_back(sushi[i].second);
    } else {
      other.push_back(sushi[i].second);
    }
  }
  sort(kt.begin(), kt.end(), greater<>());
  sort(other.begin(), other.end(), greater<>());

  ll kcnt = min(k, (ll)kt.size());
  ll p = accumulate(kt.begin(), kt.begin() + kcnt, 0LL) + kcnt * kcnt;
  p += accumulate(other.begin(), other.begin() + (k - kcnt), 0LL);
  ll ans = p;
  --kcnt;
  for (; k - kcnt <= other.size(); --kcnt) {
    p -= kt[kcnt] + ((kcnt + 1) * (kcnt + 1)) - (kcnt * kcnt);
    p += other[k - kcnt - 1];
    ans = max(ans, p);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

int main() {
  ll n, k;
  cin >> n >> k;
  vector<pair<int, int>> sushi(n);
  for (auto &e : sushi)
    cin >> e.first >> e.second;

  sort(sushi.begin(), sushi.end(), greater<>());
  vector<ll> kt, other;
  kt.push_back(sushi[0].second);
  for (int i = 1; i < n; ++i) {
    if (sushi[i].first != sushi[i - 1].first) {
      kt.push_back(sushi[i].second);
    } else {
      other.push_back(sushi[i].second);
    }
  }
  sort(kt.begin(), kt.end(), greater<>());
  sort(other.begin(), other.end(), greater<>());

  ll kcnt = min(k, (ll)kt.size());
  ll p = accumulate(kt.begin(), kt.begin() + kcnt, 0LL) + kcnt * kcnt;
  p += accumulate(other.begin(), other.begin() + (k - kcnt), 0LL);
  ll ans = p;
  --kcnt;
  for (; kcnt > 0 && k - kcnt <= other.size(); --kcnt) {
    p -= kt[kcnt] + ((kcnt + 1) * (kcnt + 1)) - (kcnt * kcnt);
    p += other[k - kcnt - 1];
    ans = max(ans, p);
  }

  cout << ans << endl;

  return 0;
}
Problem p03148 (C++)

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void solve() {
  ll N, K;
  cin >> N >> K;
  vector<Pll> sushis(N);
  for (ll i = 0; i < N; i++) {
    cin >> sushis[i].second >> sushis[i].first;
  }
  sort(sushis.begin(), sushis.end(), greater<Pll>());
  ll pdeli = 0;
  stack<ll> dups;
  unordered_set<ll> st;
  for (ll i = 0; i < K; i++) {
    ll t, d;
    tie(d, t) = sushis[i];
    if (st.find(t) != st.end()) {
      dups.emplace(d);
    }
    pdeli += d;
    st.emplace(t);
  }

  ll nk = (ll)st.size();
  ll ans = pdeli + nk * nk;

  for (ll i = K; i < N; i++) {
    ll t, d;
    tie(d, t) = sushis[i];
    if (st.find(t) != st.end())
      continue;
    pdeli -= dups.top();
    dups.pop();
    pdeli += d;
    st.emplace(t);
    nk = (ll)st.size();
    ans = max(ans, pdeli + nk * nk);
  }

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

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  solve();
  cout << flush;
  return 0;
}

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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void solve() {
  ll N, K;
  cin >> N >> K;
  vector<Pll> sushis(N);
  for (ll i = 0; i < N; i++) {
    cin >> sushis[i].second >> sushis[i].first;
  }
  sort(sushis.begin(), sushis.end(), greater<Pll>());
  ll pdeli = 0;
  stack<ll> dups;
  unordered_set<ll> st;
  for (ll i = 0; i < K; i++) {
    ll t, d;
    tie(d, t) = sushis[i];
    if (st.find(t) != st.end()) {
      dups.emplace(d);
    }
    pdeli += d;
    st.emplace(t);
  }

  ll nk = (ll)st.size();
  ll ans = pdeli + nk * nk;

  for (ll i = K; i < N; i++) {
    ll t, d;
    tie(d, t) = sushis[i];
    if (st.find(t) != st.end())
      continue;
    if (dups.empty())
      break;
    pdeli -= dups.top();
    dups.pop();
    pdeli += d;
    st.emplace(t);
    nk = (ll)st.size();
    ans = max(ans, pdeli + nk * nk);
  }

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

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

int main() {
#ifdef FASTIO
  cin.tie(0), cout.tie(0);
  ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
  ifstream ifs("./in_out/input.txt");
  cin.rdbuf(ifs.rdbuf());
#endif
#ifdef FILEOUTPUT
  ofstream ofs("./in_out/output.txt");
  cout.rdbuf(ofs.rdbuf());
#endif
  solve();
  cout << flush;
  return 0;
}
Problem p03148 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;

int main() {
  int n, k;
  cin >> n >> k;
  using P = pair<i64, i64>;
  vector<P> td(n);
  for (int i = 0; i < n; ++i) {
    i64 t, d;
    cin >> t >> d;
    td[i] = {t, d};
  }
  sort(begin(td), end(td), [](P p, P q) { return p.second < q.second; });
  vector<bool> ct(n + 1);
  i64 del = 0;
  i64 kin = 0;
  priority_queue<i64, vector<i64>, greater<i64>> que;
  for (int i = 0; i < k; ++i) {
    i64 t, d;
    tie(t, d) = td.back();
    td.pop_back();
    del += d;
    if (!ct[t]) {
      ct[t] = true;
      kin++;
    } else {
      que.push(d);
    }
  }
  i64 ans = del + kin * kin;
  while (!td.empty()) {
    i64 t, d;
    tie(t, d) = td.back();
    td.pop_back();
    if (!ct[t]) {
      ct[t] = true;
      kin++;
      del -= que.top();
      que.pop();
      del += d;
      ans = max(ans, del + kin * kin);
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
using i64 = int64_t;
using i32 = int32_t;

int main() {
  int n, k;
  cin >> n >> k;
  using P = pair<i64, i64>;
  vector<P> td(n);
  for (int i = 0; i < n; ++i) {
    i64 t, d;
    cin >> t >> d;
    td[i] = {t, d};
  }
  sort(begin(td), end(td), [](P p, P q) { return p.second < q.second; });
  vector<bool> ct(n + 1);
  i64 del = 0;
  i64 kin = 0;
  priority_queue<i64, vector<i64>, greater<i64>> que;
  for (int i = 0; i < k; ++i) {
    i64 t, d;
    tie(t, d) = td.back();
    td.pop_back();
    del += d;
    if (!ct[t]) {
      ct[t] = true;
      kin++;
    } else {
      que.push(d);
    }
  }
  i64 ans = del + kin * kin;
  while (!td.empty() && !que.empty()) {
    i64 t, d;
    tie(t, d) = td.back();
    td.pop_back();
    if (!ct[t]) {
      ct[t] = true;
      kin++;
      del -= que.top();
      que.pop();
      del += d;
      ans = max(ans, del + kin * kin);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p03148 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  ll n, k, ans = 0;
  cin >> n >> k;
  ll cnt = k;
  set<ll> c;
  vector<pair<ll, ll>> v(n);
  rep(i, n) {
    ll t, d;
    cin >> t >> d;
    v[i] = mk(d, t);
  }
  sort(al(v));
  reverse(al(v));
  vector<vector<ll>> sushi(n + 1);
  rep(i, k) {
    ans += v[i].first;
    c.insert(v[i].second);
    sushi[v[i].second].push_back(v[i].first);
  }
  vector<ll> l;
  rep(i, n + 1) {
    if ((ll)sushi[i].size() <= 1)
      continue;
    sort(al(sushi[i]));
    rep(j, (ll)sushi[i].size() - 1) { l.push_back(sushi[i][j]); }
  }
  if ((ll)l.size() > 0)
    sort(al(l));
  ans += c.size() * c.size();
  ll cnt2 = 0, cur = ans;
  while (cnt < n) {
    if (c.count(v[cnt].second)) {
      cnt++;
      continue;
    } else {
      c.insert(v[cnt].second);
      cur -= l[cnt2];
      cnt2++;
      cur += v[cnt].first;
      cur += c.size() * 2 - 1;
      cnt++;
      ans = max(ans, cur);
    }
  }
  cout << ans << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  ll n, k, ans = 0;
  cin >> n >> k;
  ll cnt = k;
  set<ll> c;
  vector<pair<ll, ll>> v(n);
  rep(i, n) {
    ll t, d;
    cin >> t >> d;
    v[i] = mk(d, t);
  }
  sort(al(v));
  reverse(al(v));
  vector<vector<ll>> sushi(n + 1);
  rep(i, k) {
    ans += v[i].first;
    c.insert(v[i].second);
    sushi[v[i].second].push_back(v[i].first);
  }
  vector<ll> l;
  rep(i, n + 1) {
    if ((ll)sushi[i].size() <= 1)
      continue;
    sort(al(sushi[i]));
    rep(j, (ll)sushi[i].size() - 1) { l.push_back(sushi[i][j]); }
  }
  if ((ll)l.size() > 0)
    sort(al(l));
  ans += c.size() * c.size();
  ll cnt2 = 0, cur = ans;
  while (cnt < n) {
    if (c.count(v[cnt].second)) {
      cnt++;
      continue;
    } else {
      if (cnt2 == (ll)l.size())
        break;
      c.insert(v[cnt].second);
      cur -= l[cnt2];
      cnt2++;
      cur += v[cnt].first;
      cur += c.size() * 2 - 1;
      cnt++;
      ans = max(ans, cur);
    }
  }
  cout << ans << endl;
}
Problem p03148 (C++)

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> d_t;

  for (int i = 0; i < N; i++) {
    int t, d;
    cin >> t >> d;
    d_t.push_back(make_pair(d, t));
  }
  sort(d_t.begin(), d_t.end(), greater<pair<int, int>>());
  vector<long long> ans(1, 0);
  map<int, bool> mp;
  vector<pair<int, int>> NG;
  int sz = 0;
  for (int i = 0; i < K; i++) {
    ans[0] += d_t[i].first;
    if (mp[d_t[i].second]) {
      NG.push_back(d_t[i]);
    } else {
      mp[d_t[i].second] = true;
      sz++;
    }
  }

  ans[0] += (long long)sz * sz;

  reverse(NG.begin(), NG.end());
  int x = 0;
  for (int i = K; i < N; i++) {
    if (mp[d_t[i].second] == false) {
      long long a = ans[ans.size() - 1];
      a += (long long)2 * sz + 1;
      a -= NG[x].first;
      a += d_t[i].first;
      ans.push_back(a);
      mp[d_t[i].second] = true;
      sz++;
      x++;
    }
    if (x == NG.size())
      break;
  }
  cout << *max_element(ans.begin(), ans.end()) << endl;
  return 0;
}

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

int main() {
  int N, K;
  cin >> N >> K;
  vector<pair<int, int>> d_t;

  for (int i = 0; i < N; i++) {
    int t, d;
    cin >> t >> d;
    d_t.push_back(make_pair(d, t));
  }
  sort(d_t.begin(), d_t.end(), greater<pair<int, int>>());
  vector<long long> ans(1, 0);
  map<int, bool> mp;
  vector<pair<int, int>> NG;
  int sz = 0;
  for (int i = 0; i < K; i++) {
    ans[0] += d_t[i].first;
    if (mp[d_t[i].second]) {
      NG.push_back(d_t[i]);
    } else {
      mp[d_t[i].second] = true;
      sz++;
    }
  }

  ans[0] += (long long)sz * sz;
  if (NG.size() == 0) {
    cout << ans[0] << endl;
    return 0;
  }
  reverse(NG.begin(), NG.end());
  int x = 0;
  for (int i = K; i < N; i++) {
    if (mp[d_t[i].second] == false) {
      long long a = ans[ans.size() - 1];
      a += (long long)2 * sz + 1;
      a -= NG[x].first;
      a += d_t[i].first;
      ans.push_back(a);
      mp[d_t[i].second] = true;
      sz++;
      x++;
    }
    if (x == NG.size())
      break;
  }
  cout << *max_element(ans.begin(), ans.end()) << endl;
  return 0;
}
Problem p03148 (C++)

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

using namespace std;

struct Node {
  int t, d;
  Node() {}
  Node(int _t, int _d) : t(_t), d(_d) {}
  bool operator<(const Node &_r) const { return d > _r.d; }
};

void solve() {
  int n, k;
  cin >> n >> k;
  vector<Node> a(n);
  for (auto &p : a) {
    cin >> p.t >> p.d;
    p.t--;
  }
  sort(a.begin(), a.end());
  vector<bool> trk(k, false);
  vector<int> stk;
  int type = 0;
  long long sum = 0;
  for (int i = 0; i < k; i++) {
    int t = a[i].t, d = a[i].d;
    if (!trk[t]) {
      trk[t] = true;
      type++;
    } else {
      stk.push_back(d);
    }
    sum += d;
  }
  long long res = sum + 1ll * type * type;
  for (int i = k; i < n; i++) {
    int t = a[i].t, d = a[i].d;
    if (stk.empty())
      break;
    if (trk[t])
      continue;
    trk[t] = true;
    type++;
    sum -= stk.back();
    stk.pop_back();
    sum += d;
    res = max(res, sum + 1ll * type * type);
  }
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}


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

using namespace std;

struct Node {
  int t, d;
  Node() {}
  Node(int _t, int _d) : t(_t), d(_d) {}
  bool operator<(const Node &_r) const { return d > _r.d; }
};

void solve() {
  int n, k;
  cin >> n >> k;
  vector<Node> a(n);
  for (auto &p : a) {
    cin >> p.t >> p.d;
    p.t--;
  }
  sort(a.begin(), a.end());
  vector<bool> trk(n, false);
  vector<int> stk;
  int type = 0;
  long long sum = 0;
  for (int i = 0; i < k; i++) {
    int t = a[i].t, d = a[i].d;
    if (!trk[t]) {
      trk[t] = true;
      type++;
    } else {
      stk.push_back(d);
    }
    sum += d;
  }
  long long res = sum + 1ll * type * type;
  for (int i = k; i < n; i++) {
    int t = a[i].t, d = a[i].d;
    if (stk.empty())
      break;
    if (trk[t])
      continue;
    trk[t] = true;
    type++;
    sum -= stk.back();
    stk.pop_back();
    sum += d;
    res = max(res, sum + 1ll * type * type);
  }
  cout << res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
  cout << endl;
}