Problem p03716 (C++)

Buggy submission (status: Runtime Error):
/*Bismillahir Rahmanir Rahim*/ //{
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll llinf = (1ll << 61) - 1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
  static constexpr auto &os = cout; // replace by cerr in interactive.
  template <class C> static auto dud(C *x) -> decltype(os << *x, 0);
  template <class C> static char dud(...);
  template <class C>
  typename enable_if<sizeof dud<C>(0) != 1, Dbg &>::type
  operator<<(const C &x) {
    os << x;
    return *this;
  }
  template <class C> Dbg &dump(C b, C e) {
    *this << "\n[";
    int cur = 0;
    for (C i = b; i != e and cur < 26; i++, cur++)
      *this << ", " + 2 * (i == b) << *i;
    return *this << ']';
  }
  template <class C>
  typename enable_if<sizeof dud<C>(0) == 1, Dbg &>::type
  operator<<(const C &x) {
    return dump(begin(x), end(x));
  }
  template <class C, size_t X>
  typename enable_if<sizeof(C) != 1, Dbg &>::type operator<<(C (&x)[X]) {
    return dump(begin(x), end(x));
  }
  template <class B, class C> Dbg &operator<<(const pair<B, C> &x) {
    return *this << '{' << x.first << ", " << x.second << '}';
  }
  template <class C, size_t Y> struct TP {
    void operator()(Dbg &os, const C &t) {
      TP<C, Y - 1>()(os, t);
      os << ", " << get<Y - 1>(t);
    }
  };
  template <class C> struct TP<C, 1> {
    void operator()(Dbg &os, const C &t) { os << get<0>(t); }
  };
  template <class... C> Dbg &operator<<(const tuple<C...> &t) {
    os << '{', TP<decltype(t), sizeof...(C)>()(*this, t);
    return *this << '}';
  }
  Dbg &operator<<(ostream &(*x)(std::ostream &)) {
    os << x;
    return *this;
  }
} dbg;
void err(istringstream *iss) { delete iss; }
template <class C, class... Args>
void err(istringstream *iss, const C &val, const Args &...args) {
  string name;
  *iss >> name;
  if (name.back() == ',')
    name.pop_back();
  dbg << name << " = " << val << "; ", err(iss, args...);
}
#define bug(args...)                                                           \
  dbg << __LINE__ << ": ", err(new istringstream(string(#args)), args),        \
      dbg << endl
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int T, Q, cn; //}
const int inf = 1000000007, mxn = 100005;

template <class key, class cmp = less<key>> class kMinSum {
  multiset<key, cmp> ms1, ms2;
  int k;

public:
  ll sum;

  kMinSum(int _k) : k(_k), sum(0) {}

  void insert(key x) {
    if (ms1.size() < k)
      ms1.insert(x), sum += x;
    else {
      auto it = --ms1.end();
      if (cmp()(x, *it))
        ms2.insert(*it), sum -= *it, ms1.erase(it), ms1.insert(x), sum += x;
      else
        ms2.insert(x);
    }
  }

  bool erase(key x) { // returns 1 if that value actually existed
    if (!ms2.empty()) {
      auto it = ms2.find(x);
      if (it != ms2.end()) {
        ms2.erase(it);
        return 1;
      }
    }
    if (!ms1.empty()) {
      auto it = ms1.find(x);
      if (it != ms1.end()) {
        ms1.erase(it), sum -= x;
        if (!ms2.empty())
          ms1.insert(*ms2.begin()), sum += *ms2.begin(), ms2.erase(ms2.begin());
        return 1;
      }
    }
    return 0;
  }

  key kthMin() {
    if (ms1.size() < k)
      return cout << "vodox, k ta element i nai", -1;
    return *ms1.begin();
  }

  friend Dbg &operator<<(Dbg &os, const kMinSum &rs) {
    return os << "{ sum: " << rs.sum << ", ms1: " << rs.ms1
              << ", ms2: " << rs.ms2 << "}";
  }
};

int n, a[mxn];

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.precision(11);
  /*
  Run a loop over the middle point, take the sum of n max elements to its left
  and n min elements to its right.
  */
  cin >> n;
  kMinSum<int, greater<int>> kms1(n);
  kMinSum<int> kms2(n);
  for (int i = 1; i <= 3 * n; i++) {
    cin >> a[i];
    if (i < n)
      kms1.insert(a[i]);
    else
      kms2.insert(a[i]);
  }
  ll mx = -llinf;
  for (int i = n; i <= (n << 1); i++) {
    kms1.insert(a[i]), kms2.erase(a[i]);
    mx = max(mx, kms1.sum - kms2.sum);
  }
  cout << mx << '\n';
}

Fixed submission:
/*Bismillahir Rahmanir Rahim*/ //{
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll llinf = (1ll << 61) - 1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
  static constexpr auto &os = cout; // replace by cerr in interactive.
  template <class C> static auto dud(C *x) -> decltype(os << *x, 0);
  template <class C> static char dud(...);
  template <class C>
  typename enable_if<sizeof dud<C>(0) != 1, Dbg &>::type
  operator<<(const C &x) {
    os << x;
    return *this;
  }
  template <class C> Dbg &dump(C b, C e) {
    *this << "\n[";
    int cur = 0;
    for (C i = b; i != e and cur < 26; i++, cur++)
      *this << ", " + 2 * (i == b) << *i;
    return *this << ']';
  }
  template <class C>
  typename enable_if<sizeof dud<C>(0) == 1, Dbg &>::type
  operator<<(const C &x) {
    return dump(begin(x), end(x));
  }
  template <class C, size_t X>
  typename enable_if<sizeof(C) != 1, Dbg &>::type operator<<(C (&x)[X]) {
    return dump(begin(x), end(x));
  }
  template <class B, class C> Dbg &operator<<(const pair<B, C> &x) {
    return *this << '{' << x.first << ", " << x.second << '}';
  }
  template <class C, size_t Y> struct TP {
    void operator()(Dbg &os, const C &t) {
      TP<C, Y - 1>()(os, t);
      os << ", " << get<Y - 1>(t);
    }
  };
  template <class C> struct TP<C, 1> {
    void operator()(Dbg &os, const C &t) { os << get<0>(t); }
  };
  template <class... C> Dbg &operator<<(const tuple<C...> &t) {
    os << '{', TP<decltype(t), sizeof...(C)>()(*this, t);
    return *this << '}';
  }
  Dbg &operator<<(ostream &(*x)(std::ostream &)) {
    os << x;
    return *this;
  }
} dbg;
void err(istringstream *iss) { delete iss; }
template <class C, class... Args>
void err(istringstream *iss, const C &val, const Args &...args) {
  string name;
  *iss >> name;
  if (name.back() == ',')
    name.pop_back();
  dbg << name << " = " << val << "; ", err(iss, args...);
}
#define bug(args...)                                                           \
  dbg << __LINE__ << ": ", err(new istringstream(string(#args)), args),        \
      dbg << endl
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int T, Q, cn; //}
const int inf = 1000000007, mxn = 100005;

template <class key, class cmp = less<key>> class kMinSum {
  multiset<key, cmp> ms1, ms2;
  int k;

public:
  ll sum;

  kMinSum(int _k) : k(_k), sum(0) {}

  void insert(key x) {
    if (ms1.size() < k)
      ms1.insert(x), sum += x;
    else {
      auto it = --ms1.end();
      if (cmp()(x, *it))
        ms2.insert(*it), sum -= *it, ms1.erase(it), ms1.insert(x), sum += x;
      else
        ms2.insert(x);
    }
  }

  bool erase(key x) { // returns 1 if that value actually existed
    if (!ms2.empty()) {
      auto it = ms2.find(x);
      if (it != ms2.end()) {
        ms2.erase(it);
        return 1;
      }
    }
    if (!ms1.empty()) {
      auto it = ms1.find(x);
      if (it != ms1.end()) {
        ms1.erase(it), sum -= x;
        if (!ms2.empty())
          ms1.insert(*ms2.begin()), sum += *ms2.begin(), ms2.erase(ms2.begin());
        return 1;
      }
    }
    return 0;
  }

  key kthMin() {
    if (ms1.size() < k)
      return cout << "vodox, k ta element i nai", -1;
    return *ms1.begin();
  }

  friend Dbg &operator<<(Dbg &os, const kMinSum &rs) {
    return os << "{ sum: " << rs.sum << ", ms1: " << rs.ms1
              << ", ms2: " << rs.ms2 << "}";
  }
};

int n, a[mxn * 3];

signed main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.precision(11);
  /*
  Run a loop over the middle point, take the sum of n max elements to its left
  and n min elements to its right.
  */
  cin >> n;
  kMinSum<int, greater<int>> kms1(n);
  kMinSum<int> kms2(n);
  for (int i = 1; i <= 3 * n; i++) {
    cin >> a[i];
    if (i < n)
      kms1.insert(a[i]);
    else
      kms2.insert(a[i]);
  }
  ll mx = -llinf;
  for (int i = n; i <= (n << 1); i++) {
    kms1.insert(a[i]), kms2.erase(a[i]);
    mx = max(mx, kms1.sum - kms2.sum);
  }
  cout << mx << '\n';
}