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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;

  vector<long long> A;
  vector<long long> B;
  int a;
  for (int i = 0; i < N; i++) {
    cin >> a;
    if (a >= 0) {
      A.emplace_back(a);
    } else {
      B.emplace_back(a);
    }
  }

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  long long ans = 1;
  long long mod = 1000000007;

  if (A.size() == 0) {
    if (K % 2 == 1) {
      for (int i = 1; i <= K; i++) {
        ans *= B[N - i];
        ans %= mod;
      }
      if (ans < 0) {
        ans += mod;
        ans %= mod;
      }
    } else {
      for (int i = 0; i < K; i++) {
        ans *= B[i];
        ans %= mod;
      }
      if (ans < 0) {
        ans += mod;
      }
    }
    cout << ans << endl;
    return 0;
  }

  if (N == K) {
    for (auto p : A) {
      ans *= p;
      ans %= mod;
    }
    for (auto p : B) {
      ans *= p;
      ans %= mod;
    }
    if (ans < 0) {
      ans += mod;
      ans %= mod;
    }
    cout << ans << endl;

    return 0;
  }

  // cout<<"hoge"<<endl;
  int pta = A.size() - 1;
  int ptb = 0;

  // cout<<A.size()<<" "<<B.size()<<endl;
  for (int i = 0; i < K; i++) {
    // cout<<pta<<" "<<ptb<<endl;
    if (pta == -1) {
      ptb++;
    } else if (ptb == B.size()) {
      pta--;
    } else if (A[pta] >= (-B[ptb])) {
      pta--;
    } else {
      ptb++;
    }
  }
  /*
  for(auto p:A){
      cout<<p<<" ";
  }
  cout<<endl;

  for(auto p:B){
      cout<<p<<" ";
  }
  cout<<endl;
  cout<<pta<<" "<<ptb<<endl;
  //*/

  // cout<<"hoge"<<endl;
  if (ptb % 2 == 1) {
    if (pta == -1) {
      pta++;
      ptb++;
    } else if (ptb == B.size() || pta == A.size() - 1) {
      pta--;
      ptb--;
    } else {
      long long aa = A[pta + 1] * A[pta];
      long long bb = B[ptb - 1] * B[ptb];
      if (aa > bb) {
        pta--;
        ptb--;
      } else {
        pta++;
        ptb++;
      }
    }
  }

  for (int i = 0; i < ptb; i++) {
    ans *= B[i];
    ans %= mod;
  }
  for (int i = A.size() - 1; i > pta; i--) {
    ans *= A[i];
    ans %= mod;
  }
  if (ans < 0) {
    ans += mod;
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, K;
  cin >> N >> K;

  vector<long long> A;
  vector<long long> B;
  int a;
  for (int i = 0; i < N; i++) {
    cin >> a;
    if (a >= 0) {
      A.emplace_back(a);
    } else {
      B.emplace_back(a);
    }
  }

  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  long long ans = 1;
  long long mod = 1000000007;

  if (B.size() == 0) {
    for (int i = 1; i <= K; i++) {
      ans *= B[N - i];
      ans %= mod;
    }
    if (ans < 0) {
      ans += mod;
      ans %= mod;
    }
    cout << ans << endl;
    return 0;
  }

  if (N == K) {
    for (auto p : A) {
      ans *= p;
      ans %= mod;
    }
    for (auto p : B) {
      ans *= p;
      ans %= mod;
    }
    if (ans < 0) {
      ans += mod;
      ans %= mod;
    }
    cout << ans << endl;

    return 0;
  }

  // cout<<"hoge"<<endl;
  int pta = A.size() - 1;
  int ptb = 0;

  // cout<<A.size()<<" "<<B.size()<<endl;
  for (int i = 0; i < K; i++) {
    // cout<<pta<<" "<<ptb<<endl;
    if (pta == -1) {
      ptb++;
    } else if (ptb == B.size()) {
      pta--;
    } else if (A[pta] > (-B[ptb])) {
      pta--;
    } else {
      ptb++;
    }
  }

  // cout<<"hoge"<<endl;
  if (ptb % 2 == 1) {
    if (pta == -1) {
      pta++;
      ptb++;
    } else if (ptb == B.size()) {
      pta--;
      ptb--;
    } else {
      long long aa = A[pta] * A[pta - 1];
      long long bb = B[ptb] * B[ptb + 1];
      if (aa > bb) {
        pta--;
        ptb--;
      } else {
        pta++;
        ptb++;
      }
    }
  }

  for (int i = 0; i < ptb; i++) {
    ans *= B[i];
    ans %= mod;
  }
  for (int i = A.size() - 1; i > pta; i--) {
    ans *= A[i];
    ans %= mod;
  }
  if (ans < 0) {
    ans += mod;
  }

  cout << ans << endl;

  return 0;
}