Problem p03972 (C++)

Buggy submission (status: Runtime Error):


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 1; i <= int(n); i++)
#define MP make_pair
#define PB push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define X first
#define Y second
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}

typedef long long LL;
typedef long double LD;
const int INF = 0x3f3f3f3f;

template <class T> inline bool RD(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1, ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <class T> inline void PT(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    PT(x / 10);
  putchar(x % 10 + '0');
}
typedef pair<LL, LL> pii;

pii a[112345];
int main() {
  LL n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i].X), a[i].Y = 0;
  for (int i = n + 1; i <= n + m; i++)
    scanf("%lld", &a[i].X), a[i].Y = 1;
  sort(a + 1, a + 1 + n + m);
  LL c[2];
  c[0] = m + 1;
  c[1] = n + 1;
  LL ans = 0;
  for (int i = 1; i <= n + m; i++) {
    ans += a[i].X * c[a[i].Y];
    c[a[i].Y ^ 1]--;
  }
  cout << ans << endl;
}


Fixed submission:


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 1; i <= int(n); i++)
#define MP make_pair
#define PB push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()
#define X first
#define Y second
template <typename T> inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}

typedef long long LL;
typedef long double LD;
const int INF = 0x3f3f3f3f;

template <class T> inline bool RD(T &ret) {
  char c;
  int sgn;
  if (c = getchar(), c == EOF)
    return 0;
  while (c != '-' && (c < '0' || c > '9'))
    c = getchar();
  sgn = (c == '-') ? -1 : 1, ret = (c == '-') ? 0 : (c - '0');
  while (c = getchar(), c >= '0' && c <= '9')
    ret = ret * 10 + (c - '0');
  ret *= sgn;
  return 1;
}
template <class T> inline void PT(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    PT(x / 10);
  putchar(x % 10 + '0');
}
typedef pair<LL, LL> pii;

pii a[1123456];
int main() {
  LL n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    scanf("%lld", &a[i].X), a[i].Y = 0;
  for (int i = n + 1; i <= n + m; i++)
    scanf("%lld", &a[i].X), a[i].Y = 1;
  sort(a + 1, a + 1 + n + m);
  LL c[2];
  c[0] = m + 1;
  c[1] = n + 1;
  LL ans = 0;
  for (int i = 1; i <= n + m; i++) {
    ans += a[i].X * c[a[i].Y];
    c[a[i].Y ^ 1]--;
  }
  cout << ans << endl;
}