Problem p02617 (C++) — buggy submission, verdict: Runtime Error

/*input
3
1 3
2 3
*/

// Nihesh Anderson - knandy

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set; // find_by_order, order_of_key
ll MOD = 1000000007;

#define endl '\\n'
#define fast_cin()                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
ll INF = 2000000000000000001;
long double EPS = 1e-9;
ll power(ll a, ll n, ll md) {
  if (n == 0) {
    return 1;
  } else {
    ll res = power(a, n / 2, md);
    res = (res * res) % md;
    if (n % 2 != 0) {
      res = (res * a) % md;
    }
    return res;
  }
}
random_device rndm;
mt19937 grndm(rndm());
void mix(ll *a, ll *b) { shuffle(a, b, grndm); }

const ll MX = 200005;

int main() {
  fast_cin();
  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);
  ll n;
  cin >> n;
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += (i * (n - i + 1));
  }
  for (ll i = 0; i < n - 1; i++) {
    ll u, v;
    cin >> u >> v;
    if (u > v)
      swap(u, v);
    ans -= (u * (n - v + 1));
  }
  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

/*input
4 3
-1 -2 -3 -4
*/

// Nihesh Anderson - knandy

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef long double ld;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set; // find_by_order, order_of_key
ll MOD = 1000000007;

#define endl '\\n'
#define fast_cin()                                                             \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define pb push_back
ll INF = 2000000000000000001;
long double EPS = 1e-9;
ll power(ll a, ll n, ll md) {
  if (n == 0) {
    return 1;
  } else {
    ll res = power(a, n / 2, md);
    res = (res * res) % md;
    if (n % 2 != 0) {
      res = (res * a) % md;
    }
    return res;
  }
}
random_device rndm;
mt19937 grndm(rndm());
void mix(ll *a, ll *b) { shuffle(a, b, grndm); }

int main() {
  fast_cin();
  // freopen("input.in","r",stdin);
  // freopen("output.out","w",stdout);
  ll n, k;
  cin >> n >> k;
  vector<ll> v(n), actpos, actneg;
  vector<long double> pos, neg;
  bool yes = false;
  for (ll i = 0; i < n; i++) {
    cin >> v[i];
    if (v[i] > 0) {
      yes = true;
      pos.push_back(log2(v[i]));
      actpos.push_back(v[i]);
    }
    if (v[i] < 0) {
      neg.push_back(log2(-v[i]));
      actneg.push_back(-v[i]);
    }
  }
  ll f = 1;
  if (!yes) {
    if (k & 1) {
      sort(v.begin(), v.end(), greater<ll>());
      for (ll i = 0; i < k; i++) {
        f = (f * v[i]) % MOD;
      }
      f = (f + (ll)1000000000 * MOD) % MOD;
    } else {
      sort(v.begin(), v.end());
      for (ll i = 0; i < k; i++) {
        f = (f * v[i]) % MOD;
      }
      f = (f + (ll)1000000000 * MOD) % MOD;
    }
  } else if (k == n) {
    for (ll i = 0; i < n; i++) {
      f = (f * v[i]) % MOD;
    }
    f = (f + (ll)1000000000 * MOD) % MOD;
  } else {
    sort(pos.begin(), pos.end(), greater<long double>());
    sort(neg.begin(), neg.end(), greater<long double>());
    sort(actpos.begin(), actpos.end(), greater<ll>());
    sort(actneg.begin(), actneg.end(), greater<ll>());
    vector<long double> prefneg(neg.size() + 1, 0), prefpos(pos.size() + 1, 0);
    for (ll i = 0; i < pos.size(); i++) {
      prefpos[i + 1] = prefpos[i] + pos[i];
    }
    for (ll i = 0; i < neg.size(); i++) {
      prefneg[i + 1] = prefneg[i] + neg[i];
    }
    pair<ll, ll> best;
    long double mx = -1;
    for (ll i = 0; i <= neg.size(); i += 2) {
      ll j = k - i;
      if (j > pos.size())
        continue;
      if (j < 0)
        break;
      if (prefneg[i] + prefpos[j] > mx) {
        mx = prefneg[i] + prefpos[i];
        best.first = i;
        best.second = j;
      }
    }
    if (mx < 0) {
      f = 0;
    } else {
      for (ll i = 0; i < best.first; i++) {
        f = (f * actneg[i]) % MOD;
      }
      for (ll i = 0; i < best.second; i++) {
        f = (f * actpos[i]) % MOD;
      }
      f = (f + (ll)1000000000 * MOD) % MOD;
    }
  }
  cout << f << endl;
  return 0;
}