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

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
// #include <windows.h>
using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
template <class T1, class T2> using P = pair<T1, T2>;
using I = int;
using D = double;
using B = bool;
using C = char;
using S = string;
using LL = long long;
using LD = long double;
using ULL = unsigned long long;
using PII = P<I, I>;
using VPII = V<PII>;
using PLL = P<LL, LL>;
using VPLL = V<PLL>;
using VI = V<I>;
using VVI = VV<I>;
using VLL = V<LL>;
using VVLL = VV<LL>;
using VC = V<C>;
using VVC = VV<C>;
using VS = V<S>;
using VVS = VV<S>;
using VB = V<B>;
using VVB = VV<B>;
#define REP(type, i, n) for (type i = 0; i < (type)(n); ++i)
#define REP2(type, i, m, n) for (type i = (m); i < (type)(n); ++i)
#define REPR(type, i, n) for (type i = (n)-1; i >= 0; --i)
#define REPR2(type, i, m, n) for (type i = (n)-1; i >= (m); --i)
#define REPx(x, a) for (auto x : a)
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define SORTR(a, type) sort(ALL(a), greater<type>())
#define SORTF(a, comp) sort(ALL(a), comp)
#define REVERSE(a) reverse(ALL(a))
#define SIZE(a, type) ((type)(a).size())
#define bit_search(bit, n) REP(LL, bit, 1LL << (n))
#define bit_check(bit, i) ((bit >> (i)) & 1)
#define setpre(n) fixed << setprecision((n))
#define UNIQUE(a)                                                              \\
  do {                                                                         \\
    SORT(a);                                                                   \\
    (a).erase(unique(ALL(a)), (a).end());                                      \\
  } while (0)
#define MAX(a) *max_element(ALL(a))
#define MIN(a) *min_element(ALL(a))
#define bisect_left(a, x) lower_bound(ALL(a), (x)) - a.begin()
#define bisect_right(a, x) upper_bound(ALL(a), (x)) - a.begin()
#define INPUT(a) REPx(&x, a) cin >> x;
#define INPUTP(a) REPx(&x, a) cin >> x.first >> x.second;
#define OUTPUT_PERMUTATION(n)                                                  \\
  do {                                                                         \\
    VI v(n);                                                                   \\
    iota(ALL(v), 1);                                                           \\
    do {                                                                       \\
      REPx(x, v) cout << x << " ";                                             \\
      ENDL                                                                     \\
    } while (next_permutation(ALL(v)));                                        \\
  } while (0);
#define MAKE_PERMUTATION(n, PER)                                               \\
  do {                                                                         \\
    VVI a(fact(n), VI(n));                                                     \\
    int idx = 0;                                                               \\
    VI v(n);                                                                   \\
    iota(ALL(v), 1);                                                           \\
    do {                                                                       \\
      REP(roop, n) a[idx][roop] = v[roop];                                     \\
      idx++;                                                                   \\
    } while (next_permutation(ALL(v)));                                        \\
    PER = a;                                                                   \\
  } while (0); // int fact(), VVI PERを宣言しておく、n=10が限界(500 ms)
#define ENDL cout << endl;

const LL INF = 1e10;
const LL MOD = 1e9 + 7;

template <class T> using PRIORITY_QUEUE = priority_queue<T, V<T>, greater<T>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void debug1(V<T> A) {
  REP(int, i, SIZE(A, int)) {
    if (A[i] == INF)
      cout << "I ";
    else
      cout << A[i] << " ";
  }
  ENDL
}
template <class T> inline void debug2(VV<T> A) {
  REP(int, i, SIZE(A, int)) {
    REP(int, j, SIZE(A[i], int)) {
      if (A[i][j] == INF)
        cout << "I ";
      else
        cout << A[i][j] << " ";
    }
    ENDL
  }
}

bool comp(const LL a, const LL b) { return abs(a) > abs(b); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  LL n, k, ans = 1LL;
  cin >> n >> k;
  VLL A(n), S1, E1, S2, E2;
  int ct = 0;
  REP(int, i, n) {
    cin >> A[i];
    if (A[i] < 0)
      ct++;
  }
  if (n == k) {
    REP(int, i, n) {
      if (A[i] < 0)
        A[i] += MOD;
      ans *= A[i];
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }
  SORTF(A, comp);
  if (ct == n && k % 2) {
    REP(int, i, k) {
      LL t = A[n - i - 1];
      if (t < 0)
        t += MOD;
      ans *= t;
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }
  ct = 0;
  REP(int, i, n) {
    if (i < k) {
      if (A[i] < 0)
        ct++;
      S1.push_back(A[i]);
    } else
      E1.push_back(A[i]);
  }
  if (ct % 2 == 0) {
    REP(int, i, k) {
      if (S1[i] < 0)
        S1[i] += MOD;
      ans *= S1[i];
      ans %= MOD;
    }
  } else {
    S2 = S1;
    E2 = E1;
    bool flg1 = false;
    LL a, b;
    REPR(int, i, k) {
      if (S1[i] < 0) {
        a = S1[i];
        REP(int, j, n - k) {
          if (E1[j] >= 0) {
            b = E1[j];
            S1[i] = E1[j];
            flg1 = true;
            break;
          }
        }
        break;
      }
    }
    bool flg2 = false;
    LL c, d;
    REPR(int, i, k) {
      if (S2[i] > 0) {
        c = S2[i];
        REP(int, j, n - k) {
          if (E2[j] < 0) {
            d = E2[j];
            S2[i] = E2[j];
            flg2 = true;
            break;
          }
        }
        break;
      }
    }
    if (flg1 && flg2) {
      if (abs(c * b) >= abs(a * d)) {
        REP(int, i, k) {
          if (S1[i] < 0)
            S1[i] += MOD;
          ans *= S1[i];
          ans %= MOD;
        }
      } else {
        REP(int, i, k) {
          if (S2[i] < 0)
            S2[i] += MOD;
          ans *= S2[i];
          ans %= MOD;
        }
      }
    } else if (flg1) {
      REP(int, i, k) {
        if (S1[i] < 0)
          S1[i] += MOD;
        ans *= S1[i];
        ans %= MOD;
      }
    } else if (flg2) {
      REP(int, i, k) {
        if (S2[i] < 0)
          S2[i] += MOD;
        ans *= S2[i];
        ans %= MOD;
      }
    }
  }
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

// #include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <stack>
// #include <windows.h>
using namespace std;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
template <class T1, class T2> using P = pair<T1, T2>;
using I = int;
using D = double;
using B = bool;
using C = char;
using S = string;
using LL = long long;
using LD = long double;
using ULL = unsigned long long;
using PII = P<I, I>;
using VPII = V<PII>;
using PLL = P<LL, LL>;
using VPLL = V<PLL>;
using VI = V<I>;
using VVI = VV<I>;
using VLL = V<LL>;
using VVLL = VV<LL>;
using VC = V<C>;
using VVC = VV<C>;
using VS = V<S>;
using VVS = VV<S>;
using VB = V<B>;
using VVB = VV<B>;
#define REP(type, i, n) for (type i = 0; i < (type)(n); ++i)
#define REP2(type, i, m, n) for (type i = (m); i < (type)(n); ++i)
#define REPR(type, i, n) for (type i = (n)-1; i >= 0; --i)
#define REPR2(type, i, m, n) for (type i = (n)-1; i >= (m); --i)
#define REPx(x, a) for (auto x : a)
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define SORTR(a, type) sort(ALL(a), greater<type>())
#define SORTF(a, comp) sort(ALL(a), comp)
#define REVERSE(a) reverse(ALL(a))
#define SIZE(a, type) ((type)(a).size())
#define bit_search(bit, n) REP(LL, bit, 1LL << (n))
#define bit_check(bit, i) ((bit >> (i)) & 1)
#define setpre(n) fixed << setprecision((n))
#define UNIQUE(a)                                                              \\
  do {                                                                         \\
    SORT(a);                                                                   \\
    (a).erase(unique(ALL(a)), (a).end());                                      \\
  } while (0)
#define MAX(a) *max_element(ALL(a))
#define MIN(a) *min_element(ALL(a))
#define bisect_left(a, x) lower_bound(ALL(a), (x)) - a.begin()
#define bisect_right(a, x) upper_bound(ALL(a), (x)) - a.begin()
#define INPUT(a) REPx(&x, a) cin >> x;
#define INPUTP(a) REPx(&x, a) cin >> x.first >> x.second;
#define OUTPUT_PERMUTATION(n)                                                  \\
  do {                                                                         \\
    VI v(n);                                                                   \\
    iota(ALL(v), 1);                                                           \\
    do {                                                                       \\
      REPx(x, v) cout << x << " ";                                             \\
      ENDL                                                                     \\
    } while (next_permutation(ALL(v)));                                        \\
  } while (0);
#define MAKE_PERMUTATION(n, PER)                                               \\
  do {                                                                         \\
    VVI a(fact(n), VI(n));                                                     \\
    int idx = 0;                                                               \\
    VI v(n);                                                                   \\
    iota(ALL(v), 1);                                                           \\
    do {                                                                       \\
      REP(roop, n) a[idx][roop] = v[roop];                                     \\
      idx++;                                                                   \\
    } while (next_permutation(ALL(v)));                                        \\
    PER = a;                                                                   \\
  } while (0); // int fact(), VVI PERを宣言しておく、n=10が限界(500 ms)
#define ENDL cout << endl;

const LL INF = 1e10;
const LL MOD = 1e9 + 7;

template <class T> using PRIORITY_QUEUE = priority_queue<T, V<T>, greater<T>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline void debug1(V<T> A) {
  REP(int, i, SIZE(A, int)) {
    if (A[i] == INF)
      cout << "I ";
    else
      cout << A[i] << " ";
  }
  ENDL
}
template <class T> inline void debug2(VV<T> A) {
  REP(int, i, SIZE(A, int)) {
    REP(int, j, SIZE(A[i], int)) {
      if (A[i][j] == INF)
        cout << "I ";
      else
        cout << A[i][j] << " ";
    }
    ENDL
  }
}

bool comp(const LL a, const LL b) { return abs(a) >= abs(b); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  LL n, k, ans = 1LL;
  cin >> n >> k;
  VLL A(n), S1, E1, S2, E2;
  int ct = 0;
  REP(int, i, n) {
    cin >> A[i];
    if (A[i] < 0)
      ct++;
  }
  if (n == k) {
    REP(int, i, n) {
      if (A[i] < 0)
        A[i] += MOD;
      ans *= A[i];
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }
  SORTF(A, comp);
  // debug1(A);
  if (ct == n && k % 2) {
    REP(int, i, k) {
      LL t = A[n - i - 1];
      if (t < 0)
        t += MOD;
      ans *= t;
      ans %= MOD;
    }
    cout << ans << endl;
    return 0;
  }
  ct = 0;
  REP(int, i, n) {
    if (i < k) {
      if (A[i] < 0)
        ct++;
      S1.push_back(A[i]);
    } else
      E1.push_back(A[i]);
  }
  if (ct % 2 == 0) {
    REP(int, i, k) {
      if (S1[i] < 0)
        S1[i] += MOD;
      ans *= S1[i];
      ans %= MOD;
    }
  } else {
    S2 = S1;
    E2 = E1;
    // debug1(S2);
    // debug1(E2);
    bool flg1 = false;
    LL a, b;
    REPR(int, i, k) {
      if (S1[i] < 0) {
        a = S1[i];
        REP(int, j, n - k) {
          if (E1[j] >= 0) {
            b = E1[j];
            S1[i] = E1[j];
            flg1 = true;
            break;
          }
        }
        break;
      }
    }
    bool flg2 = false;
    LL c, d;
    REPR(int, i, k) {
      if (S2[i] > 0) {
        c = S2[i];
        REP(int, j, n - k) {
          if (E2[j] < 0) {
            d = E2[j];
            S2[i] = E2[j];
            flg2 = true;
            break;
          }
        }
        break;
      }
    }
    if (flg1 && flg2) {
      if (abs(c * b) >= abs(a * d)) {
        REP(int, i, k) {
          if (S1[i] < 0)
            S1[i] += MOD;
          ans *= S1[i];
          ans %= MOD;
        }
      } else {
        REP(int, i, k) {
          if (S2[i] < 0)
            S2[i] += MOD;
          ans *= S2[i];
          ans %= MOD;
        }
      }
    } else if (flg1) {
      REP(int, i, k) {
        if (S1[i] < 0)
          S1[i] += MOD;
        ans *= S1[i];
        ans %= MOD;
      }
    } else if (flg2) {
      REP(int, i, k) {
        if (S2[i] < 0)
          S2[i] += MOD;
        ans *= S2[i];
        ans %= MOD;
      }
    }
  }
  cout << ans << endl;

  return 0;
}