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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (int i = n; i > -1; --i)
#define ALL(a) (a).begin(), (a).end()
#define FILL(a, n, x)                                                          \\
  ;                                                                            \\
  REP(i, (n)) { (a)[i] = (x); }
#define CINA(a, n)                                                             \\
  ;                                                                            \\
  REP(i, (n)) { cin >> (a)[i]; }
#define FILL2(a, n, m, x)                                                      \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { (a)[i][j] = (x); }                                           \\
  }
#define CINA2(a, n, m)                                                         \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { cin >> (a)[i][j]; }                                          \\
  }
#define Liny "Yes\\n"
#define Linn "No\\n"
#define LINY "YES\\n"
#define LINN "NO\\n"

// cout << setfill('0') << right << setw`(4) << 12; // "0012"
int keta(ll x) {
  if (x < 10) {
    return 1;
  } else {
    return keta(x / 10) + 1;
  }
}
int keta_wa(ll x) {
  if (x < 10) {
    return x;
  } else {
    return keta_wa(x / 10) + x % 10;
  }
}
int ctoi(char c) { return ((c >= '0' && c <= '9') ? c - '0' : 0); }
int __stoi(string s) { return atoi(s.c_str()); }
ll sum(ll a[], ll N) { return accumulate(a, a + N, 0LL); }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }
  return a / gcd(a, b) * b;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> bool isIn(T a, vector<T> v) {
  for (T x : v) {
    if (a == x) {
      return true;
    }
  }
  return false;
}
string strReplace(string s, string target, string replacement) {
  if (!target.empty()) {
    std::string::size_type pos = 0;
    while ((pos = s.find(target, pos)) != std::string::npos) {
      s.replace(pos, target.length(), replacement);
      pos += replacement.length();
    }
  }
  return s;
}
const ll MOD = 1e9 + 7;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MP make_pair

bool DEBUG = 0;
template <class T> void dprint(T s) {
  if (DEBUG) {
    cout << s << "\\n";
  }
}

const ll MAX = MOD;

ll N, K;
vector<ll> a, b;

void solve() {
  cin >> N >> K;
  ll temp = 1LL;
  REP(i, N) {
    ll x;
    cin >> x;
    if (x >= 0) {
      a.push_back(x);
    } else {
      b.push_back(-x);
    }
    temp = (temp * x) % MOD;
    if (temp < 0) {
      temp += MOD;
    }
  }
  if (N == K) {
    cout << temp << "\\n";
    return;
  }
  ll ans = 1LL;
  if (a.size() > 0) {
    sort(ALL(a), greater<ll>());
    sort(ALL(b), greater<ll>());

    int idx_a = 0;
    int idx_b = 0;
    if (K % 2 == 1) {
      ans *= a[0];
      --K;
      ++idx_a;
    }
    while (K > 0) {
      if (idx_a + 1 < a.size() && idx_b + 1 < b.size()) {
        ll temp_a = a[idx_a] * a[idx_a + 1];
        ll temp_b = b[idx_b] * b[idx_b + 1];
        if (temp_a > temp_b) {
          ans = (ans * a[idx_a]) % MOD;
          ans = (ans * a[idx_a + 1]) % MOD;
          idx_a += 2;
        } else {
          ans = (ans * b[idx_b]) % MOD;
          ans = (ans * b[idx_b + 1]) % MOD;
          idx_b += 2;
        }
      } else if (idx_a + 1 < a.size()) {
        ans = (ans * a[idx_a]) % MOD;
        ans = (ans * a[idx_a + 1]) % MOD;
        idx_a += 2;
      } else {
        ans = (ans * b[idx_b]) % MOD;
        ans = (ans * b[idx_b + 1]) % MOD;
        idx_b += 2;
      }
      K -= 2;
    }
  } else {
    if (K % 2 == 0) {
      sort(ALL(b), greater<ll>());
      REP(i, K) { ans = (ans * b[i]) % MOD; }
    } else {
      sort(ALL(b));
      REP(i, K) { ans = (ans * b[i]) % MOD; }
      ans = (-ans + MOD) % MOD;
    }
  }
  cout << ans << "\\n";

  return;
}

int main(int argc, char *argv[]) {
  if (argc > 1 && argv[1][0] == 'D') {
    DEBUG = 1;
  }
  solve();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
#define REP(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define REPR(i, n) for (int i = n; i > -1; --i)
#define ALL(a) (a).begin(), (a).end()
#define FILL(a, n, x)                                                          \\
  ;                                                                            \\
  REP(i, (n)) { (a)[i] = (x); }
#define CINA(a, n)                                                             \\
  ;                                                                            \\
  REP(i, (n)) { cin >> (a)[i]; }
#define FILL2(a, n, m, x)                                                      \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { (a)[i][j] = (x); }                                           \\
  }
#define CINA2(a, n, m)                                                         \\
  ;                                                                            \\
  REP(i, (n)) {                                                                \\
    REP(j, (m)) { cin >> (a)[i][j]; }                                          \\
  }
#define Liny "Yes\\n"
#define Linn "No\\n"
#define LINY "YES\\n"
#define LINN "NO\\n"

// cout << setfill('0') << right << setw`(4) << 12; // "0012"
int keta(ll x) {
  if (x < 10) {
    return 1;
  } else {
    return keta(x / 10) + 1;
  }
}
int keta_wa(ll x) {
  if (x < 10) {
    return x;
  } else {
    return keta_wa(x / 10) + x % 10;
  }
}
int ctoi(char c) { return ((c >= '0' && c <= '9') ? c - '0' : 0); }
int __stoi(string s) { return atoi(s.c_str()); }
ll sum(ll a[], ll N) { return accumulate(a, a + N, 0LL); }
ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return b ? gcd(b, a % b) : a;
}
ll lcm(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }
  return a / gcd(a, b) * b;
}
template <class T> void chmax(T &a, T b) {
  if (a < b) {
    a = b;
  }
}
template <class T> void chmin(T &a, T b) {
  if (a > b) {
    a = b;
  }
}
template <class T> bool isIn(T a, vector<T> v) {
  for (T x : v) {
    if (a == x) {
      return true;
    }
  }
  return false;
}
string strReplace(string s, string target, string replacement) {
  if (!target.empty()) {
    std::string::size_type pos = 0;
    while ((pos = s.find(target, pos)) != std::string::npos) {
      s.replace(pos, target.length(), replacement);
      pos += replacement.length();
    }
  }
  return s;
}
const ll MOD = 1e9 + 7;
#define pii pair<int, int>
#define pll pair<ll, ll>
#define MP make_pair

bool DEBUG = 0;
template <class T> void dprint(T s) {
  if (DEBUG) {
    cout << s << "\\n";
  }
}

const ll MAX = MOD;

ll N, K;
vector<ll> a, b;

void solve() {
  cin >> N >> K;
  ll temp = 0;
  REP(i, N) {
    ll x;
    cin >> x;
    if (x >= 0) {
      a.push_back(x);
    } else {
      b.push_back(-x);
    }
  }
  ll ans = 1LL;
  if (a.size() > 0) {
    sort(ALL(a), greater<ll>());
    sort(ALL(b), greater<ll>());
    if (K % 2 == 1) {
      ans = a[0];
      --K;
      a.erase(a.begin());
    }
    while (K > 0) {
      if (a.size() >= 2 && b.size() >= 2) {
        ll temp_a = a[0] * a[1];
        ll temp_b = b[0] * b[1];
        if (temp_a >= temp_b) {
          ans = (ans * a[0]) % MOD;
          ans = (ans * a[1]) % MOD;
          a.erase(a.begin());
          a.erase(a.begin());
        } else {
          ans = (ans * b[0]) % MOD;
          ans = (ans * b[1]) % MOD;
          b.erase(b.begin());
          b.erase(b.begin());
        }
      } else if (a.size() >= 2) {
        ans = (ans * a[0]) % MOD;
        ans = (ans * a[1]) % MOD;
        a.erase(a.begin());
        a.erase(a.begin());
      } else {
        ans = (ans * b[0]) % MOD;
        ans = (ans * b[1]) % MOD;
        b.erase(b.begin());
        b.erase(b.begin());
      }
      K -= 2;
    }
  } else {
    if (K % 2 == 0) {
      sort(ALL(b), greater<ll>());
      REP(i, K) { ans = (ans * b[i]) % MOD; }
    } else {
      sort(ALL(b));
      REP(i, K) { ans = (ans * b[i]) % MOD; }
      ans = (-ans + MOD) % MOD;
    }
  }
  cout << ans << "\\n";

  return;
}

int main(int argc, char *argv[]) {
  if (argc > 1 && argv[1][0] == 'D') {
    DEBUG = 1;
  }
  solve();
  return 0;
}