Problem p02615 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// #include <boost/lexical_cast.hpp>
using namespace std;

#define ll long long int
#define vll vector<ll>
#define vllv vector<vector<ll>>
#define vpii vector<pair<ll, ll>>
#define sz size()
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
ll MOD = 1000000000 + 7;
ll ceil1(ll n, ll m) {
  if (n % m == 0) {
    return n / m;
  }
  return n / m + 1;
}
ll factor(ll n) {
  vll ans;
  if (n == 2)
    ans.pb(2);
  else {
    for (ll i = 2; i * i <= n; i++) {
      if (n % i == 0 && n % i != i) {
        ans.pb(i);
        if (n / i != i)
          ans.pb(n / i);
      }
    }
    ans.pb(n);
  }
  return ans.sz + 1;
}
ll fact(ll n) {
  ll res = 1;
  for (ll i = 2; i <= n; i++)
    res = res * i;
  return res;
}

ll nCr(ll n, ll r) { return fact(n) / (fact(r) * fact(n - r)); }

void dfs(vllv &v, ll k, vll &par, vll &dis, ll p) {
  par[k] = p;
  dis[k] = dis[p] + 1;
  for (ll i = 0; i < v[k].sz; i++) {
    if (v[k][i] != p) {
      dfs(v, v[k][i], par, dis, k);
    }
  }
}
bool sortbysec(const pair<string, int> &a, const pair<string, int> &b) {
  return (a.second < b.second);
}

ll pow2(ll n, ll k) {
  ll ans = 1;
  while (k > 0) {
    if (k % 2 == 1)
      ans = ans * n;
    n = n * n;
    k /= 2;
  }
  return ans;
}
bool prime(int n) {
  int i, j;
  for (i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
bool bserch(vector<string> a, string s) {
  ll l = 0, r = a.sz - 1, mid;
  while (l <= r) {
    mid = (l + r) / 2;
    if (a[mid] == s)
      return true;
    if (a[mid] < s) {
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return false;
}

bool sortcol(const vector<int> &v1, const vector<int> &v2) {
  return v1[0] < v2[0];
}

ll sumdegit(ll n) {
  ll sum = 0;
  while (n > 0) {
    sum += (n % 10);
    n /= 10;
  }
  return sum;
}

void solve() {
  /*ll h,w,k;
  vector<string> s;
  for(ll i=0;i<n;i++)
          cin>>s;*/
  ll n;
  cin >> n;
  vll a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  sort(all(a));
  ll ans = 0;
  ans += a[n - 1];

  ll k = n - 2;

  ll c = n - 2;
  while (1) {
    if (c > 1) {
      // cout<<a[k]<<" ";
      ans += (2 * a[k]);
      k--;
      c -= 2;
      if (c == 0)
        break;
    } else if (c == 1) {
      // cout<<a[k]<<" ";
      ans += a[k];
      k--;
      break;
    }
  }
  cout << ans;
  return;
}
int main() {
  fast;
  ll q;
  q = 1;
  // cin>>q;
  while (q--) {
    solve();
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// #include <boost/lexical_cast.hpp>
using namespace std;

#define ll long long int
#define vll vector<ll>
#define vllv vector<vector<ll>>
#define vpii vector<pair<ll, ll>>
#define sz size()
#define pb push_back
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define fast                                                                   \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
ll MOD = 1000000000 + 7;
ll ceil1(ll n, ll m) {
  if (n % m == 0) {
    return n / m;
  }
  return n / m + 1;
}
ll factor(ll n) {
  vll ans;
  if (n == 2)
    ans.pb(2);
  else {
    for (ll i = 2; i * i <= n; i++) {
      if (n % i == 0 && n % i != i) {
        ans.pb(i);
        if (n / i != i)
          ans.pb(n / i);
      }
    }
    ans.pb(n);
  }
  return ans.sz + 1;
}
ll fact(ll n) {
  ll res = 1;
  for (ll i = 2; i <= n; i++)
    res = res * i;
  return res;
}

ll nCr(ll n, ll r) { return fact(n) / (fact(r) * fact(n - r)); }

void dfs(vllv &v, ll k, vll &par, vll &dis, ll p) {
  par[k] = p;
  dis[k] = dis[p] + 1;
  for (ll i = 0; i < v[k].sz; i++) {
    if (v[k][i] != p) {
      dfs(v, v[k][i], par, dis, k);
    }
  }
}
bool sortbysec(const pair<string, int> &a, const pair<string, int> &b) {
  return (a.second < b.second);
}

ll pow2(ll n, ll k) {
  ll ans = 1;
  while (k > 0) {
    if (k % 2 == 1)
      ans = ans * n;
    n = n * n;
    k /= 2;
  }
  return ans;
}
bool prime(int n) {
  int i, j;
  for (i = 2; i <= sqrt(n); i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}
bool bserch(vector<string> a, string s) {
  ll l = 0, r = a.sz - 1, mid;
  while (l <= r) {
    mid = (l + r) / 2;
    if (a[mid] == s)
      return true;
    if (a[mid] < s) {
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return false;
}

bool sortcol(const vector<int> &v1, const vector<int> &v2) {
  return v1[0] < v2[0];
}

ll sumdegit(ll n) {
  ll sum = 0;
  while (n > 0) {
    sum += (n % 10);
    n /= 10;
  }
  return sum;
}

void solve() {
  /*ll h,w,k;
  vector<string> s;
  for(ll i=0;i<n;i++)
          cin>>s;*/
  ll n;
  cin >> n;
  vll a(n);
  for (ll i = 0; i < n; i++)
    cin >> a[i];
  sort(all(a));
  ll ans = 0;
  ans += a[n - 1];

  ll k = n - 2;

  ll c = n - 2;
  while (1) {
    if (c == 0)
      break;
    if (c > 1) {
      // cout<<a[k]<<" ";
      ans += (2 * a[k]);
      k--;
      c -= 2;
      if (c == 0)
        break;
    } else if (c == 1) {
      // cout<<a[k]<<" ";
      ans += a[k];
      k--;
      break;
    }
  }
  cout << ans;
  return;
}
int main() {
  fast;
  ll q;
  q = 1;
  // cin>>q;
  while (q--) {
    solve();
  }
  return 0;
}