Problem p02815 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ;
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 fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
// vector<vector<int> > dp;
// vector<vector<vector<int> > > vvvi;
// dp=vector<vector<int> >(N, vector<int>(M,0));
// vector<pair<int,int> > v;
// v.push_back(make_pair(x,y));
// priority_queue<int,vector<int>, greater<int> > q2;

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

  int N;
  cin >> N;
  VI C(N);
  REP(i, N) cin >> C[i];
  SORT(C);

  int ans = 0;
  int bi[100010] = {0};
  int b = 1;

  for (int i = 0; i <= N; i++) {
    bi[i] = b;
    b *= 2;
    b %= mod;
  }

  REP(i, N) {
    int a = C[i];
    int cnt = bi[N - i - 1];

    if (N - i - 2 >= 0)
      cnt += bi[N - i - 2] * (N - i - 1);

    cnt %= mod;
    cnt *= bi[i];
    cnt %= mod;
    // cout << bi[i] << " " << bi[i] * (N - i - 1) / 2 << " " << bi[N - i-1] <<
    // endl;

    // if(a * cnt<0)cout << a << " " << cnt << endl;

    ans += a * cnt;
    ans %= mod;
  }

  ans *= bi[N];
  ans %= mod;
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef priority_queue<int> PQ;
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 fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
// vector<vector<int> > dp;
// vector<vector<vector<int> > > vvvi;
// dp=vector<vector<int> >(N, vector<int>(M,0));
// vector<pair<int,int> > v;
// v.push_back(make_pair(x,y));
// priority_queue<int,vector<int>, greater<int> > q2;

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

  int N;
  cin >> N;
  VI C(N);
  REP(i, N) cin >> C[i];
  SORT(C);

  int ans = 0;
  int bi[200010] = {0};
  int b = 1;

  for (int i = 0; i <= N; i++) {
    bi[i] = b;
    b *= 2;
    b %= mod;
  }

  REP(i, N) {
    int a = C[i];
    int cnt = bi[N - i - 1];

    if (N - i - 2 >= 0)
      cnt += bi[N - i - 2] * (N - i - 1);

    cnt %= mod;
    cnt *= bi[i];
    cnt %= mod;
    // cout << bi[i] << " " << bi[i] * (N - i - 1) / 2 << " " << bi[N - i-1] <<
    // endl;

    // if(a * cnt<0)cout << a << " " << cnt << endl;

    ans += a * cnt;
    ans %= mod;
  }

  ans *= bi[N];
  ans %= mod;
  cout << ans << endl;

  return 0;
}