Problem p02815 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define taskname ""
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef vector<cd> vcd;
typedef vector<int> vi;

template <class T> using v2d = vector<vector<T>>;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

const int maxN = 2e5 + 10;
const ll mod = 1e9 + 7;

int n;
ll a[maxN];
ll fact[1000010], ifact[1000010];

ll power(ll a, ll b) {
  ll r = 1;
  while (b) {
    if (b & 1) {
      r = r * a % mod;
    }
    b /= 2;
    a = a * a % mod;
  }
  return r;
}

void init() {
  fact[0] = 1;
  for (int i = 1; i <= 1e6; ++i) {
    fact[i] = fact[i - 1] * i % mod;
  }
  ifact[1000000] = power(fact[1000000], mod - 2);
  for (int i = 999999; i >= 0; --i) {
    ifact[i] = ifact[i + 1] * (i + 1) % mod;
  }
}

ll nCk(int n, int k) { return fact[n] * ifact[k] % mod * ifact[n - k] % mod; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  init();
  cin >> n;
  for1(i, n) { cin >> a[i]; }
  sort(a + 1, a + n + 1, greater<ll>());
  ll ans = 0;
  for1(i, n) {
    for1(j, i) {
      ans += a[i] * j % mod * nCk(i - 1, j - 1) % mod * power(2, i) % mod *
             power(4, n - i) % mod;
      ans %= mod;
    }
  }
  cout << ans;
  return 0;
}


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

#define taskname ""
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef vector<cd> vcd;
typedef vector<int> vi;

template <class T> using v2d = vector<vector<T>>;
template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

const int maxN = 2e5 + 10;
const ll mod = 1e9 + 7;

int n;
ll a[maxN];
ll fact[1000010], ifact[1000010];

ll power(ll a, ll b) {
  ll r = 1;
  while (b) {
    if (b & 1) {
      r = r * a % mod;
    }
    b /= 2;
    a = a * a % mod;
  }
  return r;
}

void init() {
  fact[0] = 1;
  for (int i = 1; i <= 1e6; ++i) {
    fact[i] = fact[i - 1] * i % mod;
  }
  ifact[1000000] = power(fact[1000000], mod - 2);
  for (int i = 999999; i >= 0; --i) {
    ifact[i] = ifact[i + 1] * (i + 1) % mod;
  }
}

ll nCk(int n, int k) { return fact[n] * ifact[k] % mod * ifact[n - k] % mod; }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  init();
  cin >> n;
  for1(i, n) { cin >> a[i]; }
  sort(a + 1, a + n + 1, greater<ll>());
  ll ans = 0;
  for1(i, n) {
    ans += a[i] * (i + 1) % mod * power(2, i * 2 - 2) % mod * power(4, n - i) %
           mod;
    ans %= mod;
  }
  cout << ans;
  return 0;
}