Problem p02785 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define debug(x) cout << #x << ":" << x << ' ';
#define debugg(x) cout << #x << ":" << x << ' ' << "\n";
#define endl '\n'
#define ff first
#define ss second
#define L(X) ((X) << 1)
#define R(X) (((X) << 1) | 1)
#define M(X, Y) (((X) + (Y)) >> 1)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const long long int LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1);

template <typename T> T add(T x, T y, T m) { return ((x % m) + (y % m)) % m; }
template <typename T> T mul(T x, T y, T m) {
  return ((x % m) * (long long)(y % m)) % m;
}
template <typename T> T sub(T x, T y, T m) { return add(x, -y + m); }
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int lg2(long long x) { return 64 - __builtin_clzll(x) - 1; }
int lg2(int x) { return 32 - __builtin_clzll(x) - 1; }

struct pair_hash {
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto h1 = hash<T1>{}(p.first);
    auto h2 = hash<T2>{}(p.second);
    return h1 ^ h2;
  }
};

ll n, k, h[MAXN];

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

  cin >> n >> k;

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

  sort(h, h + n);

  ll sum = 0;
  for (int i = 0; i < n - k; ++i) {
    sum += h[i];
  }

  cout << sum << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) int((x).size())
#define debug(x) cout << #x << ":" << x << ' ';
#define debugg(x) cout << #x << ":" << x << ' ' << "\n";
#define endl '\n'
#define ff first
#define ss second
#define L(X) ((X) << 1)
#define R(X) (((X) << 1) | 1)
#define M(X, Y) (((X) + (Y)) >> 1)

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int MAXN = 2e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const long long int LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
const double PI = acos(-1);

template <typename T> T add(T x, T y, T m) { return ((x % m) + (y % m)) % m; }
template <typename T> T mul(T x, T y, T m) {
  return ((x % m) * (long long)(y % m)) % m;
}
template <typename T> T sub(T x, T y, T m) { return add(x, -y + m); }
template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

int lg2(long long x) { return 64 - __builtin_clzll(x) - 1; }
int lg2(int x) { return 32 - __builtin_clzll(x) - 1; }

struct pair_hash {
  template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const {
    auto h1 = hash<T1>{}(p.first);
    auto h2 = hash<T2>{}(p.second);
    return h1 ^ h2;
  }
};

ll n, k, h[MAXN];

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

  cin >> n >> k;

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

  sort(h, h + n);

  ll sum = 0;
  for (int i = 0; i < n - k; ++i) {
    sum += h[i];
  }

  cout << sum << endl;

  return 0;
}