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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
typedef long long LL;
using Graph = vector<vector<int>>;

static long long INF = (1LL << 62);
static const long long P = 1000000000LL + 7;

int N, K;
vector<long long> A;

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;
  int cplus = 0, czero = 0, cminus = 0;
  int odd = 0;
  if ((K % 2) != 0) {
    odd = 1;
  }
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    A.push_back(a);
    if (a < 0) {
      cminus++;
    } else if (a == 0) {
      czero++;
    } else {
      cplus++;
    }
  }
  sort(A.begin(), A.end());
  if (K == 1) {
    printf("%lld\\n", A[N - 1]);
    return 0;
  }
  long long ans = 1;
  // 以下の場合は大きい方から順にK個の積が答え
  //  - すべて0以上
  //  - 負の数が1個
  //  - すべてが負の数かつKが奇数の場合も同じ
  if (cminus == 0 || cminus == 1 || (cminus == N && (K % 2) == 1)) {
    for (int k = 0; k < K; k++) {
      ans *= A[N - 1 - k];
      ans %= P;
      while (ans < 0) {
        ans += P;
      }
    }
    ans %= P;
    printf("%lld\\n", ans);
    return 0;
  }
  // 0が入らざるを得ない場合は0
  if (cplus + cminus < K) {
    printf("0\\n");
    return 0;
  }
  int c = min(cminus, K);
  if ((c % 2) != 0) {
    c--;
  }
  vector<pair<long long, int>> M;
  long long u = 1;
  ;
  for (int i = 0;; i += 2) {
    u = A[N - 1 - odd - i] * A[N - 1 - odd - (i + 1)];
    if (u <= 0 || i > cplus + czero)
      break;
    M.push_back(make_pair(u, N - 1 - odd - (i + 1)));
  }
  for (int i = 0; i < c; i += 2) {
    u = A[i] * A[i + 1];
    M.push_back(make_pair(u, -1));
  }
  sort(M.begin(), M.end());
  if (odd) {
    ans = A[N - 1];
  } else {
    ans = 1;
  }
  int pindx;
  int j = odd;
  for (int i = M.size() - 1; i >= 0; i--) {
    if (j >= K)
      break;
    long long u = M[i].first;
    int v = M[i].second;
    if (v >= 0) {
      pindx = v;
    }
    ans %= P;
    u %= P;
    ans *= u;
    ans %= P;
    while (ans < 0) {
      ans += P;
    }
    j += 2;
  }
  int e = K - j;
  for (int i = 1; i <= e; i++) {
    ans *= A[pindx - i];
    ans %= P;
    while (ans < 0) {
      ans += P;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

#include <bits/stdc++.h>
#include <cmath>
#include <limits>

using namespace std;
typedef long long LL;
using Graph = vector<vector<int>>;

static long long INF = (1LL << 62);
static const long long P = 1000000000LL + 7;

int N, K;
vector<long long> A;

int main(int argc, char *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> N >> K;
  int cplus = 0, czero = 0, cminus = 0;
  int odd = 0;
  if ((K % 2) != 0) {
    odd = 1;
  }
  for (int i = 0; i < N; i++) {
    long long a;
    cin >> a;
    A.push_back(a);
    if (a < 0) {
      cminus++;
    } else if (a == 0) {
      czero++;
    } else {
      cplus++;
    }
  }
  sort(A.begin(), A.end());
  if (K == 1) {
    printf("%lld\\n", A[N - 1]);
    return 0;
  }
  long long ans = 1;
  if (cminus == 1 || (cminus == N && (K % 2) == 1)) {
    for (int k = 0; k < K; k++) {
      ans *= A[N - 1 - k];
      ans %= P;
      while (ans < 0) {
        ans += P;
      }
    }
    ans %= P;
    printf("%lld\\n", ans);
    return 0;
  }

  if (cplus + cminus < K) {
    printf("0\\n");
    return 0;
  }

  int c = min(cminus, K);
  if ((c % 2) != 0) {
    c--;
  }
  vector<pair<long long, int>> M;
  long long u = 1;
  ;
  for (int i = 0;; i += 2) {
    u = A[N - 1 - odd - i] * A[N - 1 - odd - (i + 1)];
    if (u <= 0 || i > cplus + czero)
      break;
    M.push_back(make_pair(u, N - 1 - odd - (i + 1)));
  }
  for (int i = 0; i < c; i += 2) {
    u = A[i] * A[i + 1];
    M.push_back(make_pair(u, -1));
  }
  sort(M.begin(), M.end());
  if (odd) {
    ans = A[N - 1];
  } else {
    ans = 1;
  }
  int pindx;
  int j = odd;
  for (int i = M.size() - 1; i >= 0; i--) {
    if (j >= K)
      break;
    long long u = M[i].first;
    int v = M[i].second;
    if (v >= 0) {
      pindx = v;
    }
    ans %= P;
    u %= P;
    ans *= u;
    ans %= P;
    while (ans < 0) {
      ans += P;
    }
    j += 2;
  }
  int e = K - j;
  for (int i = 1; i <= e; i++) {
    ans *= A[pindx - i];
    ans %= P;
    while (ans < 0) {
      ans += P;
    }
  }
  printf("%lld\\n", ans);
  return 0;
}