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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#include <cstring>
#include <iostream>
#define pie acos(-1)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d %d", &a, &b)
#define siii(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld %lld", &a, &b)
#define slll(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define ss(st) scanf("%s", st)
#define sch(ch) scanf("%ch", &ch)
#define ps(a) printf("%s", a)
#define newLine() printf("\\n")
#define pi(a) printf("%d", a)
#define pii(a, b) printf("%d %d", a, b)
#define piii(a, b, c) printf("%d %d %d", a, b, c)
#define pl(a) printf("%lld", a)
#define pll(a, b) printf("%lld %lld", a, b)
#define plll(a, b, c) printf("%lld %lld %lld", a, b, c)
#define pd(a) printf("%lf", a)
#define pdd(a, b) printf("%lf %lf", a, b)
#define pddd(a, b, c) printf("%lf %lf %lf", a, b, c)
#define pch(c) printf("%ch", c)
#define debug1(str, a) printf("%s=%d\\n", str, a)
#define debug2(str1, str2, a, b) printf("%s=%d %s=%d\\n", str1, a, str2, b)
#define debug3(str1, str2, str3, a, b, c)                                      \\
  printf("%s=%d %s=%d %s=%d\\n", str1, a, str2, b, str3, c)
#define debug4(str1, str2, str3, str4, a, b, c, d)                             \\
  printf("%s=%d %s=%d %s=%d %s=%d\\n", str1, a, str2, b, str3, c, str4, d)
#define for0(i, n) for (i = 0; i < n; i++)
#define for1(i, n) for (i = 1; i <= n; i++)
#define forab(i, a, b) for (i = a; i <= b; i++)
#define forstl(i, s)                                                           \\
  for (__typeof((s).end()) i = (s).begin(); i != (s).end(); ++i)
#define nl puts("")
#define sd(a) scanf("%lf", &a)
#define sdd(a, b) scanf("%lf %lf", &a, &b)
#define sddd(a, b, c) scanf("%lf %lf %lf", &a, &b, &c)
#define sp printf(" ")
#define ll long long int
#define ull unsigned long long int
#define MOD 1000000007
#define mpr make_pair
#define pub(x) push_back(x)
#define pob(x) pop_back(x)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define INF INT_MAX
#define eps 1e-9
#define checkbit(n, pos) (n & (1 << pos))
#define setbit(n, pos) (n(1 << pos))
#define para(i, a, b, ara)                                                     \\
  for (i = a; i <= b; i++) {                                                   \\
    if (i != 0) {                                                              \\
      printf(" ");                                                             \\
    }                                                                          \\
    cout << ara[i];                                                            \\
  }                                                                            \\
  printf("\\n");
#define pvec(i, vec)                                                           \\
  for (i = 0; i < vec.size(); i++) {                                           \\
    if (i != 0) {                                                              \\
      printf(" ");                                                             \\
    }                                                                          \\
    cout << vec[i];                                                            \\
  }                                                                            \\
  printf("\\n");
#define ppara(i, j, n, m, ara)                                                 \\
  for (i = 0; i < n; i++) {                                                    \\
    for (j = 0; j < m; j++) {                                                  \\
      if (j != 0) {                                                            \\
        printf(" ");                                                           \\
      }                                                                        \\
      cout << ara[i][j];                                                       \\
    }                                                                          \\
    printf("\\n");                                                              \\
  }
#define ppstructara(i, j, n, m, ara)                                           \\
  for (i = 0; i < n; i++) {                                                    \\
    printf("%d:\\n", i);                                                        \\
    for (j = 0; j < m; j++) {                                                  \\
      cout << ara[i][j];                                                       \\
      printf("\\n");                                                            \\
    }                                                                          \\
  }
#define ppvec(i, j, n, vec)                                                    \\
  for (i = 0; i < n; i++) {                                                    \\
    printf("%d:", i);                                                          \\
    for (j = 0; j < vec[i].size(); j++) {                                      \\
      if (j != 0) {                                                            \\
        printf(" ");                                                           \\
      }                                                                        \\
      cout << vec[i][j];                                                       \\
    }                                                                          \\
    printf("\\n");                                                              \\
  }
#define ppstructvec(i, j, n, vec)                                              \\
  for (i = 0; i < n; i++) {                                                    \\
    printf("%d:", i);                                                          \\
    for (j = 0; j < vec[i].size(); j++) {                                      \\
      cout << vec[i][j];                                                       \\
      printf("\\n");                                                            \\
    }                                                                          \\
  }
#define sara(i, a, b, ara)                                                     \\
  for (i = a; i <= b; i++) {                                                   \\
    scanf("%d", &ara[i]);                                                      \\
  }
#define pstructara(i, a, b, ara)                                               \\
  for (i = a; i <= b; i++) {                                                   \\
    cout << ara[i];                                                            \\
    nl;                                                                        \\
  }
#define pstructvec(i, vec)                                                     \\
  for (i = 0; i < vec.size(); i++) {                                           \\
    cout << vec[i];                                                            \\
    nl;                                                                        \\
  }
#define pstructstl(stl, x)                                                     \\
  for (__typeof(stl.begin()) it = stl.begin(); it != stl.end(); ++it) {        \\
    x = *it;                                                                   \\
    cout << x;                                                                 \\
    nl;                                                                        \\
  }                                                                            \\
  nl;
#define pstl(stl)                                                              \\
  for (__typeof(stl.begin()) it = stl.begin(); it != stl.end(); ++it) {        \\
    if (it != stl.begin()) {                                                   \\
      sp;                                                                      \\
    }                                                                          \\
    pi(*it);                                                                   \\
  }                                                                            \\
  nl;
#define ppairvec(i, vec)                                                       \\
  for (i = 0; i < vec.size(); i++) {                                           \\
    cout << vec[i].first;                                                      \\
    sp;                                                                        \\
    cout << vec[i].second;                                                     \\
    printf("\\n");                                                              \\
  }
#define ppairara(i, a, b, ara)                                                 \\
  for (i = a; i <= b; i++) {                                                   \\
    cout << ara[i].first;                                                      \\
    sp;                                                                        \\
    cout << ara[i].second;                                                     \\
    printf("\\n");                                                              \\
  }
#define pppairvec(i, j, n, vec)                                                \\
  for (i = 0; i < n; i++) {                                                    \\
    printf("%d:\\n", i);                                                        \\
    for (j = 0; j < vec[i].size(); j++) {                                      \\
      cout << vec[i][j].first;                                                 \\
      sp;                                                                      \\
      cout << vec[i][j].second;                                                \\
      nl;                                                                      \\
    }                                                                          \\
  }
#define pppairara(i, j, n, m, ara)                                             \\
  for (i = 0; i < n; i++) {                                                    \\
    printf("%d:\\n", i);                                                        \\
    for (j = 0; j < m; j++) {                                                  \\
      cout << ara[i][j].first;                                                 \\
      printf(" ");                                                             \\
      cout << ara[i][j].second;                                                \\
      nl;                                                                      \\
    }                                                                          \\
  }
#define SZ 2 * 100010
#define xx first
#define yy second
using namespace std;
// using namespace __gnu_pbds;
// bool status[100010];
// vector <int> prime;
// void siv(){
//     int N = 100005, i, j; prime.clear();
//     int sq = sqrt(N);
//     for(i = 4; i <= N; i += 2){ status[i] = true; }
//     for(i = 3; i <= sq; i+= 2){
//         if(status[i] == false){
//             for(j = i * i; j <= N; j += i){ status[j] = true; }
//         }
//     }
//     status[1] = true;
//     for1(i, N){ if(!status[i]){ prime.pub(i); } }
// }
// mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
// auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
// std::mt19937 mt(seed);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int add(int _a, int _b) {
  if (_a < 0) {
    _a += MOD;
  }
  if (_b < 0) {
    _b += MOD;
  }
  if (_a + _b >= MOD) {
    return _a + _b - MOD;
  }
  return _a + _b;
}
inline int mul(int _a, int _b) {
  if (_a < 0) {
    _a += MOD;
  }
  if (_b < 0) {
    _b += MOD;
  }
  return ((ll)((ll)_a * (ll)_b)) % MOD;
}
const int N = 2e5;
int n, ara[N + 5], k;
void solve() {
  int i, j, ans = 1;
  vector<pair<int, int>> vec;
  for (i = 0; i < n; ++i) {
    if (ara[i] > 0)
      vec.push_back(mpr(ara[i], +1));
    else
      vec.push_back(mpr(-ara[i], -1));
  }
  sort(vec.begin(), vec.end());
  if (vec.size() >= k) {
    reverse(vec.begin(), vec.end());
    int neg = 0;
    for (i = 0; i < k; ++i)
      neg += (vec[i].yy == -1);
    if (neg % 2 == 0) {
      ans = 1;
      for (i = 0; i < k; ++i)
        ans = mul(ans, vec[i].xx);
      pi(ans), nl;
      return;
    } else {
      int a = -1, b = -1;
      for (i = 0; i < k; ++i) {
        if (vec[i].yy == +1)
          a = i;
        if (vec[i].yy == -1)
          b = i;
      }
      int c = -1, d = -1;
      for (i = (int)vec.size() - 1; i >= k; --i) {
        if (vec[i].yy == +1)
          c = i;
        if (vec[i].yy == -1)
          d = i;
      }
      if (a >= 0 && b >= 0 && c >= 0 && d >= 0) {
        // d / a >= c / b
        if ((ll)vec[b].xx * vec[d].xx >= (ll)vec[a].xx * vec[c].xx) {
          ans = 1;
          for (i = 0; i < k; ++i) {
            if (i == a)
              continue;
            ans = mul(ans, vec[i].xx);
          }
          ans = mul(ans, vec[d].xx);
          pi(ans), nl;
          return;
        } else {
          ans = 1;
          for (i = 0; i < k; ++i) {
            if (i == b)
              continue;
            ans = mul(ans, vec[i].xx);
          }
          ans = mul(ans, vec[c].xx);
          pi(ans), nl;
          return;
        }
      } else if (a >= 0 && d >= 0) {
        ans = 1;
        for (i = 0; i < k; ++i) {
          if (i == a)
            continue;
          ans = mul(ans, vec[i].xx);
        }
        ans = mul(ans, vec[d].xx);
        pi(ans), nl;
        return;
      } else if (b >= 0 && c >= 0) {
        ans = 1;
        for (i = 0; i < k; ++i) {
          if (i == b)
            continue;
          ans = mul(ans, vec[i].xx);
        }
        ans = mul(ans, vec[c].xx);
        pi(ans), nl;
        return;
      } else {
        for (i = 0; i < n; ++i) {
          if (ara[i] == 0) {
            pi(0), nl;
            return;
          }
        }
        reverse(vec.begin(), vec.end());
        ans = 1;
        for (i = 0; i < k; ++i) {
          ans = mul(ans, mul(vec[i].xx, vec[i].yy));
        }
        pi(ans), nl;
        return;
      }
    }
  }
  pi(0), nl;
  return;
}
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt", "w", stdout);
  int i, j;
  sii(n, k);
  for0(i, n) si(ara[i]);
  solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#include <cstring>
#include <iostream>
#define pie acos(-1)
#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d %d", &a, &b)
#define siii(a, b, c) scanf("%d %d %d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld %lld", &a, &b)
#define slll(a, b, c) scanf("%lld %lld %lld", &a, &b, &c)
#define ss(st) scanf("%s", st)
#define sch(ch) scanf("%ch", &ch)
#define ps(a) printf("%s", a)
#define newLine() printf("\\n")
#define pi(a) printf("%d", a)
#define pii(a, b) printf("%d %d", a, b)
#define piii(a, b, c) printf("%d %d %d", a, b, c)
#define pl(a) printf("%lld", a)
#define pll(a, b) printf("%lld %lld", a, b)
#define plll(a, b, c) printf("%lld %lld %lld", a, b, c)
#define pd(a) printf("%lf", a)
#define pdd(a, b) printf("%lf %lf", a, b)
#define pddd(a, b, c) printf("%lf %lf %lf", a, b, c)
#define pch(c) printf("%ch", c)
#define debug1(str, a) printf("%s=%d\\n", str, a)
#define debug2(str1, str2, a, b) printf("%s=%d %s=%d\\n", str1, a, str2, b)
#define debug3(str1, str2, str3, a, b, c)                                      \\
  printf("%s=%d %s=%d %s=%d\\n", str1, a, str2, b, str3, c)
#define debug4(str1, str2, str3, str4, a, b, c, d)                             \\
  printf("%s=%d %s=%d %s=%d %s=%d\\n", str1, a, str2, b, str3, c, str4, d)
#define for0(i, n) for (i = 0; i < n; i++)
#define for1(i, n) for (i = 1; i <= n; i++)
#define forab(i, a, b) for (i = a; i <= b; i++)
#define forstl(i, s)                                                           \\
  for (__typeof((s).end()) i = (s).begin(); i != (s).end(); ++i)
#define nl puts("")
#define sd(a) scanf("%lf", &a)
#define sdd(a, b) scanf("%lf %lf", &a, &b)
#define sddd(a, b, c) scanf("%lf %lf %lf", &a, &b, &c)
#define sp printf(" ")
#define ll long long int
#define ull unsigned long long int
#define MOD 1000000007
#define mpr make_pair
#define pub(x) push_back(x)
#define pob(x) pop_back(x)
#define mem(ara, value) memset(ara, value, sizeof(ara))
#define INF INT_MAX
#define eps 1e-9
#define checkbit(n, pos) (n & (1 << pos))
#define setbit(n, pos) (n(1 << pos))
#define para(i, a, b, ara)                                                     \\
  for (i = a; i <= b; i++) {                                                   \\
    if (i != 0) {                                                              \\
      printf(" ");                                                             \\
    }                                                                          \\
    cout << ara[i];                                                            \\
  }                                                                            \\
  printf("\\n");
#define pvec(i, vec)                                                           \\
  for (i = 0; i < vec.size(); i++) {                                           \\
    if (i != 0) {                                                              \\
      printf(" ");                                                             \\
    }                                                                          \\
    cout << vec[i];                                                            \\
  }                                                                            \\
  printf("\\n");
#define ppara(i, j, n, m, ara)                                                 \\
  for (i = 0; i < n; i++) {                                                    \\
    for (j = 0; j < m; j++) {                                                  \\
      if (j != 0) {                                                            \\
        printf(" ");                                                           \\
      }                                                                        \\
      cout << ara[i][j];                                                       \\
    }                                                                          \\
    printf("\\n");                                                              \\
  }
#define ppstructara(i, j, n, m, ara)                                           \\
  for (i = 0; i < n; i++) {                                                    \\
    printf("%d:\\n", i);                                                        \\
    for (j = 0; j < m; j++) {                                                  \\
      cout << ara[i][j];                                                       \\
      printf("\\n");                                                            \\
    }                                                                          \\
  }
#define ppvec(i, j, n, vec)                                                    \\
  for (i = 0; i < n; i++) {                                                    \\
    printf("%d:", i);                                                          \\
    for (j = 0; j < vec[i].size(); j++) {                                      \\
      if (j != 0) {                                                            \\
        printf(" ");                                                           \\
      }                                                                        \\
      cout << vec[i][j];                                                       \\
    }                                                                          \\
    printf("\\n");                                                              \\
  }
#define ppstructvec(i, j, n, vec)                                              \\
  for (i = 0; i < n; i++) {                                                    \\
    printf("%d:", i);                                                          \\
    for (j = 0; j < vec[i].size(); j++) {                                      \\
      cout << vec[i][j];                                                       \\
      printf("\\n");                                                            \\
    }                                                                          \\
  }
#define sara(i, a, b, ara)                                                     \\
  for (i = a; i <= b; i++) {                                                   \\
    scanf("%d", &ara[i]);                                                      \\
  }
#define pstructara(i, a, b, ara)                                               \\
  for (i = a; i <= b; i++) {                                                   \\
    cout << ara[i];                                                            \\
    nl;                                                                        \\
  }
#define pstructvec(i, vec)                                                     \\
  for (i = 0; i < vec.size(); i++) {                                           \\
    cout << vec[i];                                                            \\
    nl;                                                                        \\
  }
#define pstructstl(stl, x)                                                     \\
  for (__typeof(stl.begin()) it = stl.begin(); it != stl.end(); ++it) {        \\
    x = *it;                                                                   \\
    cout << x;                                                                 \\
    nl;                                                                        \\
  }                                                                            \\
  nl;
#define pstl(stl)                                                              \\
  for (__typeof(stl.begin()) it = stl.begin(); it != stl.end(); ++it) {        \\
    if (it != stl.begin()) {                                                   \\
      sp;                                                                      \\
    }                                                                          \\
    pi(*it);                                                                   \\
  }                                                                            \\
  nl;
#define ppairvec(i, vec)                                                       \\
  for (i = 0; i < vec.size(); i++) {                                           \\
    cout << vec[i].first;                                                      \\
    sp;                                                                        \\
    cout << vec[i].second;                                                     \\
    printf("\\n");                                                              \\
  }
#define ppairara(i, a, b, ara)                                                 \\
  for (i = a; i <= b; i++) {                                                   \\
    cout << ara[i].first;                                                      \\
    sp;                                                                        \\
    cout << ara[i].second;                                                     \\
    printf("\\n");                                                              \\
  }
#define pppairvec(i, j, n, vec)                                                \\
  for (i = 0; i < n; i++) {                                                    \\
    printf("%d:\\n", i);                                                        \\
    for (j = 0; j < vec[i].size(); j++) {                                      \\
      cout << vec[i][j].first;                                                 \\
      sp;                                                                      \\
      cout << vec[i][j].second;                                                \\
      nl;                                                                      \\
    }                                                                          \\
  }
#define pppairara(i, j, n, m, ara)                                             \\
  for (i = 0; i < n; i++) {                                                    \\
    printf("%d:\\n", i);                                                        \\
    for (j = 0; j < m; j++) {                                                  \\
      cout << ara[i][j].first;                                                 \\
      printf(" ");                                                             \\
      cout << ara[i][j].second;                                                \\
      nl;                                                                      \\
    }                                                                          \\
  }
#define SZ 2 * 100010
#define xx first
#define yy second
using namespace std;
// using namespace __gnu_pbds;
// bool status[100010];
// vector <int> prime;
// void siv(){
//     int N = 100005, i, j; prime.clear();
//     int sq = sqrt(N);
//     for(i = 4; i <= N; i += 2){ status[i] = true; }
//     for(i = 3; i <= sq; i+= 2){
//         if(status[i] == false){
//             for(j = i * i; j <= N; j += i){ status[j] = true; }
//         }
//     }
//     status[1] = true;
//     for1(i, N){ if(!status[i]){ prime.pub(i); } }
// }
// mt19937_64 mt(chrono::steady_clock::now().time_since_epoch().count());
// auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
// std::mt19937 mt(seed);
// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int add(int _a, int _b) {
  if (_a < 0) {
    _a += MOD;
  }
  if (_b < 0) {
    _b += MOD;
  }
  if (_a + _b >= MOD) {
    return _a + _b - MOD;
  }
  return _a + _b;
}
inline int mul(int _a, int _b) {
  if (_a < 0) {
    _a += MOD;
  }
  if (_b < 0) {
    _b += MOD;
  }
  return ((ll)((ll)_a * (ll)_b)) % MOD;
}
const int N = 2e5;
int n, ara[N + 5], k;
void solve() {
  int i, j, ans = 1;
  vector<pair<int, int>> vec;
  for (i = 0; i < n; ++i) {
    if (ara[i] > 0)
      vec.push_back(mpr(ara[i], +1));
    else
      vec.push_back(mpr(-ara[i], -1));
  }
  sort(vec.begin(), vec.end());
  if (vec.size() >= k) {
    reverse(vec.begin(), vec.end());
    int neg = 0;
    for (i = 0; i < k; ++i)
      neg += (vec[i].yy == -1);
    if (neg % 2 == 0) {
      ans = 1;
      for (i = 0; i < k; ++i)
        ans = mul(ans, vec[i].xx);
      pi(ans), nl;
      return;
    } else {
      int a = -1, b = -1;
      for (i = 0; i < k; ++i) {
        if (vec[i].yy == +1)
          a = i;
        if (vec[i].yy == -1)
          b = i;
      }
      int c = -1, d = -1;
      for (i = (int)vec.size() - 1; i >= k; ++i) {
        if (vec[i].yy == +1)
          c = i;
        if (vec[i].yy == -1)
          d = i;
      }
      if (a >= 0 && b >= 0 && c >= 0 && d >= 0) {
        // d / a >= c / b
        if ((ll)vec[b].xx * vec[d].xx >= (ll)vec[a].xx * vec[c].xx) {
          ans = 1;
          for (i = 0; i < k; ++i) {
            if (i == a)
              continue;
            ans = mul(ans, vec[i].xx);
          }
          ans = mul(ans, vec[d].xx);
          pi(ans), nl;
          return;
        } else {
          ans = 1;
          for (i = 0; i < k; ++i) {
            if (i == b)
              continue;
            ans = mul(ans, vec[i].xx);
          }
          ans = mul(ans, vec[c].xx);
          pi(ans), nl;
          return;
        }
      } else if (a >= 0 && d >= 0) {
        ans = 1;
        for (i = 0; i < k; ++i) {
          if (i == a)
            continue;
          ans = mul(ans, vec[i].xx);
        }
        ans = mul(ans, vec[d].xx);
        pi(ans), nl;
        return;
      } else if (b >= 0 && c >= 0) {
        ans = 1;
        for (i = 0; i < k; ++i) {
          if (i == b)
            continue;
          ans = mul(ans, vec[i].xx);
        }
        ans = mul(ans, vec[c].xx);
        pi(ans), nl;
        return;
      } else {
        for (i = 0; i < n; ++i) {
          if (ara[i] == 0) {
            pi(0), nl;
            return;
          }
        }
        reverse(vec.begin(), vec.end());
        ans = 1;
        for (i = 0; i < k; ++i) {
          ans = mul(ans, mul(vec[i].xx, vec[i].yy));
        }
        pi(ans), nl;
        return;
      }
    }
  }
  pi(0), nl;
  return;
}
int main() {
  // freopen("input.txt","r",stdin);
  // freopen("output.txt", "w", stdout);
  int i, j;
  sii(n, k);
  for0(i, n) si(ara[i]);
  solve();
}