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

/* src:

N, K = inputInt(), inputInt()
P = vector!(int)()
M = vector!(int)()
for i in range(N):
    if 0 < (a@inputInt()):
        P.emplace_back(a)
    else:
        M.emplace_back(a)
P.sort()
M.rsort()
ret = 1
if P.empty() and K % 2 == 1:
    ret *= M[:K] mod 1e9 + 7
elif N==K:
    ret *= M[:] mod 1e9 + 7
    ret *= P[:] mod 1e9 + 7
else:
    if K % 2 == 1:
        ret = P.pop() mod 1e9 + 7
        --K
    while K:
        if M.size() < 2:
            ret *= P.pop() mod 1e9 + 7
            ret *= P.pop() mod 1e9 + 7
        elif P.size() < 2:
            ret *= -M.pop() mod 1e9 + 7
            ret *= -M.pop() mod 1e9 + 7
        elif int64(M[M.size() - 1]) * M[M.size() - 2] < int64(P[P.size() - 1]) *
P[P.size() - 2]: ret *= P.pop() mod 1e9 + 7 ret *= P.pop() mod 1e9 + 7 else: ret
*= -M.pop() mod 1e9 + 7 ret *= -M.pop() mod 1e9 + 7 K -= 2 print(ret)

*/

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

const char *inputCLineOrWord(int mode) {
  static char buf[65536];
  static int bufLen = sizeof(buf);
  static int bufPos = sizeof(buf);
  static bool canReadFlag = true;
  static bool crFlag = false;
  static bool enterFlag = false;
  if (canReadFlag &&
      (enterFlag ? bufLen <= bufPos : (int)sizeof(buf) <= bufPos + bufPos)) {
    if (0 < bufLen - bufPos) {
      memmove(buf, buf + bufPos, bufLen - bufPos);
      bufLen -= bufPos;
    } else {
      bufLen = 0;
    }
    char *result = fgets(buf + bufLen, sizeof(buf) - bufLen, stdin);
    canReadFlag = (result != NULL);
    if (result != NULL) {
      int n = strlen(result);
      enterFlag = (n != (int)sizeof(buf) - 1 - bufLen ||
                   (1 <= bufLen + n && buf[bufLen + n - 1] == '\\n'));
      bufLen += n;
    }
    bufPos = 0;
  }
  if (mode == 0) {
    int pos = bufPos;
    while (true) {
      char c = buf[pos];
      if (c == 32) {
        buf[pos++] = '\\0';
        break;
      } else if (c == 10) {
        if (crFlag) {
          crFlag = false;
          if (bufPos == pos) {
            pos = ++bufPos;
            continue;
          }
        }
        buf[pos++] = '\\0';
        break;
      } else if (c == 13) {
        crFlag = true;
        buf[pos++] = '\\0';
        break;
      } else if (c == 0) {
        break;
      }
      ++pos;
    }
    const char *ret = buf + bufPos;
    bufPos = pos;
    while (true) {
      char c = buf[bufPos];
      if (c == 32 || c == 10 || c == 13) {
        ++bufPos;
      } else {
        break;
      }
    }
    return ret;
  } else if (mode == 1) {
    int pos = bufPos;
    while (true) {
      char c = buf[pos];
      if (c == 10) {
        if (crFlag) {
          crFlag = false;
          if (bufPos == pos) {
            pos = ++bufPos;
            continue;
          }
        }
        buf[pos++] = '\\0';
        break;
      } else if (c == 13) {
        crFlag = true;
        buf[pos++] = '\\0';
        break;
      } else if (c == 0) {
        break;
      }
      ++pos;
    }
    const char *ret = buf + bufPos;
    bufPos = pos;
    if (crFlag) {
      while (true) {
        char c = buf[bufPos];
        if (c == 13) {
          ++bufPos;
          crFlag = false;
          break;
        } else {
          break;
        }
      }
    }
    return ret;
  } else if (mode == 2) {
    return bufLen <= bufPos ? NULL : buf + bufPos;
  }
  assert(false);
  return NULL;
}

const char *inputCWord() { return inputCLineOrWord(0); }

int inputInt() { return atoi(inputCWord()); }

inline int mod_mul(int x, int y, int p) {
  __asm__ __volatile__("imul %%edx; idiv %2" : "+a"(y), "+d"(x) : "r"(p));
  if (x < 0) {
    x += p;
  }
  return x;
}

inline int mod(int x, int p) {
  if (p <= x) {
    do {
      x -= p;
    } while (p <= x);
  } else if (x < 0) {
    do {
      x += p;
    } while (x < 0);
  }
  return x;
}

void print_unit(bool val) { printf("%s", val ? "true" : "false"); }
void print_unit(int val) { printf("%d", val); }
void print_unit(unsigned int val) { printf("%u", val); }
void print_unit(size_t val) { printf("%zd", val); }
void print_unit(const void *val) { printf("%p", val); }
void print_unit(long long val) { printf("%lld", val); }
void print_unit(double val) { printf("%g", val); }
void print_unit(const char *val) { printf("%s", val); }
void print_unit(const std::string &val) { printf("%s", val.c_str()); }
template <class T> void print_unit(const std::vector<T> &val) {
  printf("vec(%d) {", (int)val.size());
  for (int i = 0; i < (int)val.size(); ++i) {
    fputc(' ', stdout);
    print_unit(val[i]);
  }
  printf(" }");
}

#define _print0()
#define _print1(e) print_unit(e)
#define _print2(e1, e2) _print1(e1), fputc(' ', stdout), _print1(e2)
#define _print3(e1, e2, e3) _print2(e1, e2), fputc(' ', stdout), _print1(e3)
#define _print4(e1, e2, e3, e4)                                                \\
  _print2(e1, e2), fputc(' ', stdout), _print2(e3, e4)
#define _print5(e1, e2, e3, e4, e5)                                            \\
  _print3(e1, e2, e3), fputc(' ', stdout), _print2(e4, e5)
#define _print6(e1, e2, e3, e4, e5, e6)                                        \\
  _print3(e1, e2, e3), fputc(' ', stdout), _print3(e4, e5, e6)
#define _print7(e1, e2, e3, e4, e5, e6, e7)                                    \\
  _print4(e1, e2, e3, e4), fputc(' ', stdout), _print3(e5, e6, e7)
#define _print8(e1, e2, e3, e4, e5, e6, e7, e8)                                \\
  _print4(e1, e2, e3, e4), fputc(' ', stdout), _print4(e5, e6, e7, e8)
#define _print9(e1, e2, e3, e4, e5, e6, e7, e8, e9)                            \\
  _print5(e1, e2, e3, e4, e5), fputc(' ', stdout), _print4(e6, e7, e8, e9)
#define _print10(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10)                      \\
  _print5(e1, e2, e3, e4, e5), fputc(' ', stdout), _print5(e6, e7, e8, e9, e10)
#define _GET_PRINT_MACRO_NAME(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME,   \\
                              ...)                                             \\
  NAME
#define print(...)                                                             \\
  _GET_PRINT_MACRO_NAME(__VA_ARGS__, _print10, _print9, _print8, _print7,      \\
                        _print6, _print5, _print4, _print3, _print2, _print1,  \\
                        _print0)                                               \\
  (__VA_ARGS__),                                                               \\
      fputc('\\n', stdout)
#define print0(...)                                                            \\
  _GET_PRINT_MACRO_NAME(__VA_ARGS__, _print10, _print9, _print8, _print7,      \\
                        _print6, _print5, _print4, _print3, _print2, _print1,  \\
                        _print0)                                               \\
  (__VA_ARGS__)

int N;
int K;
vector<int> P;
vector<int> M;
int ret;

int main() {
  {
    N = inputInt();
    K = inputInt();
  }
  P = vector<int>();
  M = vector<int>();
  for (int i = 0; i < N; ++i) {
    {
      const auto &a = inputInt();
      if (0 < (a)) {
        P.emplace_back(a);
      } else {
        M.emplace_back(a);
      }
    }
  }
  sort(P.begin(), P.end());
  sort(M.rbegin(), M.rend());
  ret = 1;
  if (P.empty() && K % 2 == 1) {
    for (int $1 = 0; $1 < K; ++$1)
      ret = mod_mul(ret, M[$1], 1e9 + 7);
  } else if (N == K) {
    for (int $1 = 0; $1 < (int)M.size(); ++$1)
      ret = mod_mul(ret, M[$1], 1e9 + 7);
    for (int $1 = 0; $1 < (int)P.size(); ++$1)
      ret = mod_mul(ret, P[$1], 1e9 + 7);
  } else {
    if (K % 2 == 1) {
      ret = mod(({
                  auto ret = P.back();
                  P.pop_back();
                  ret;
                }),
                1e9 + 7);
      --K;
    }
    while (K) {
      if (M.size() < 2) {
        ret = mod_mul(ret, ({
                        auto ret = P.back();
                        P.pop_back();
                        ret;
                      }),
                      1e9 + 7);
        ret = mod_mul(ret, ({
                        auto ret = P.back();
                        P.pop_back();
                        ret;
                      }),
                      1e9 + 7);
      } else if (P.size() < 2) {
        ret = mod_mul(ret, -({
                        auto ret = M.back();
                        M.pop_back();
                        ret;
                      }),
                      1e9 + 7);
        ret = mod_mul(ret, -({
                        auto ret = M.back();
                        M.pop_back();
                        ret;
                      }),
                      1e9 + 7);
      } else if ((long long)(M[M.size() - 1]) * M[M.size() - 2] <
                 (long long)(P[P.size() - 1]) * P[P.size() - 2]) {
        ret = mod_mul(ret, ({
                        auto ret = P.back();
                        P.pop_back();
                        ret;
                      }),
                      1e9 + 7);
        ret = mod_mul(ret, ({
                        auto ret = P.back();
                        P.pop_back();
                        ret;
                      }),
                      1e9 + 7);
      } else {
        ret = mod_mul(ret, -({
                        auto ret = M.back();
                        M.pop_back();
                        ret;
                      }),
                      1e9 + 7);
        ret = mod_mul(ret, -({
                        auto ret = M.back();
                        M.pop_back();
                        ret;
                      }),
                      1e9 + 7);
      }
      K -= 2;
    }
  }
  print(ret);
  return 0;
}


Error: 0

Fixed submission:

/* src:

N, K = inputInt(), inputInt()
P = vector!(int)()
M = vector!(int)()
for i in range(N):
    if 0 < (a@inputInt()):
        P.emplace_back(a)
    else:
        M.emplace_back(a)
P.sort()
M.rsort()
ret = 1
if P.empty() and K % 2 == 1 or N==K:
    ret *= M[:K] mod 1e9 + 7
else:
    if K % 2 == 1:
        ret = P.pop() mod 1e9 + 7
        --K
    while K:
        if P.size() < 2:
            ret *= -M.pop() mod 1e9 + 7
            if M.size() < 1:
                ret *= P.pop() mod 1e9 + 7
            else:
                ret *= -M.pop() mod 1e9 + 7
        elif M.size() < 2:
            ret *= P.pop() mod 1e9 + 7
            ret *= P.pop() mod 1e9 + 7
        elif int64(M[M.size() - 1]) * M[M.size() - 2] < int64(P[P.size() - 1]) *
P[P.size() - 2]: ret *= P.pop() mod 1e9 + 7 ret *= P.pop() mod 1e9 + 7 else: ret
*= -M.pop() mod 1e9 + 7 ret *= -M.pop() mod 1e9 + 7 K -= 2 print(ret)

*/

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>

using namespace std;

const char *inputCLineOrWord(int mode) {
  static char buf[65536];
  static int bufLen = sizeof(buf);
  static int bufPos = sizeof(buf);
  static bool canReadFlag = true;
  static bool crFlag = false;
  static bool enterFlag = false;
  if (canReadFlag &&
      (enterFlag ? bufLen <= bufPos : (int)sizeof(buf) <= bufPos + bufPos)) {
    if (0 < bufLen - bufPos) {
      memmove(buf, buf + bufPos, bufLen - bufPos);
      bufLen -= bufPos;
    } else {
      bufLen = 0;
    }
    char *result = fgets(buf + bufLen, sizeof(buf) - bufLen, stdin);
    canReadFlag = (result != NULL);
    if (result != NULL) {
      int n = strlen(result);
      enterFlag = (n != (int)sizeof(buf) - 1 - bufLen ||
                   (1 <= bufLen + n && buf[bufLen + n - 1] == '\\n'));
      bufLen += n;
    }
    bufPos = 0;
  }
  if (mode == 0) {
    int pos = bufPos;
    while (true) {
      char c = buf[pos];
      if (c == 32) {
        buf[pos++] = '\\0';
        break;
      } else if (c == 10) {
        if (crFlag) {
          crFlag = false;
          if (bufPos == pos) {
            pos = ++bufPos;
            continue;
          }
        }
        buf[pos++] = '\\0';
        break;
      } else if (c == 13) {
        crFlag = true;
        buf[pos++] = '\\0';
        break;
      } else if (c == 0) {
        break;
      }
      ++pos;
    }
    const char *ret = buf + bufPos;
    bufPos = pos;
    while (true) {
      char c = buf[bufPos];
      if (c == 32 || c == 10 || c == 13) {
        ++bufPos;
      } else {
        break;
      }
    }
    return ret;
  } else if (mode == 1) {
    int pos = bufPos;
    while (true) {
      char c = buf[pos];
      if (c == 10) {
        if (crFlag) {
          crFlag = false;
          if (bufPos == pos) {
            pos = ++bufPos;
            continue;
          }
        }
        buf[pos++] = '\\0';
        break;
      } else if (c == 13) {
        crFlag = true;
        buf[pos++] = '\\0';
        break;
      } else if (c == 0) {
        break;
      }
      ++pos;
    }
    const char *ret = buf + bufPos;
    bufPos = pos;
    if (crFlag) {
      while (true) {
        char c = buf[bufPos];
        if (c == 13) {
          ++bufPos;
          crFlag = false;
          break;
        } else {
          break;
        }
      }
    }
    return ret;
  } else if (mode == 2) {
    return bufLen <= bufPos ? NULL : buf + bufPos;
  }
  assert(false);
  return NULL;
}

const char *inputCWord() { return inputCLineOrWord(0); }

int inputInt() { return atoi(inputCWord()); }

inline int mod_mul(int x, int y, int p) {
  __asm__ __volatile__("imul %%edx; idiv %2" : "+a"(y), "+d"(x) : "r"(p));
  if (x < 0) {
    x += p;
  }
  return x;
}

inline int mod(int x, int p) {
  if (p <= x) {
    do {
      x -= p;
    } while (p <= x);
  } else if (x < 0) {
    do {
      x += p;
    } while (x < 0);
  }
  return x;
}

void print_unit(bool val) { printf("%s", val ? "true" : "false"); }
void print_unit(int val) { printf("%d", val); }
void print_unit(unsigned int val) { printf("%u", val); }
void print_unit(size_t val) { printf("%zd", val); }
void print_unit(const void *val) { printf("%p", val); }
void print_unit(long long val) { printf("%lld", val); }
void print_unit(double val) { printf("%g", val); }
void print_unit(const char *val) { printf("%s", val); }
void print_unit(const std::string &val) { printf("%s", val.c_str()); }
template <class T> void print_unit(const std::vector<T> &val) {
  printf("vec(%d) {", (int)val.size());
  for (int i = 0; i < (int)val.size(); ++i) {
    fputc(' ', stdout);
    print_unit(val[i]);
  }
  printf(" }");
}

#define _print0()
#define _print1(e) print_unit(e)
#define _print2(e1, e2) _print1(e1), fputc(' ', stdout), _print1(e2)
#define _print3(e1, e2, e3) _print2(e1, e2), fputc(' ', stdout), _print1(e3)
#define _print4(e1, e2, e3, e4)                                                \\
  _print2(e1, e2), fputc(' ', stdout), _print2(e3, e4)
#define _print5(e1, e2, e3, e4, e5)                                            \\
  _print3(e1, e2, e3), fputc(' ', stdout), _print2(e4, e5)
#define _print6(e1, e2, e3, e4, e5, e6)                                        \\
  _print3(e1, e2, e3), fputc(' ', stdout), _print3(e4, e5, e6)
#define _print7(e1, e2, e3, e4, e5, e6, e7)                                    \\
  _print4(e1, e2, e3, e4), fputc(' ', stdout), _print3(e5, e6, e7)
#define _print8(e1, e2, e3, e4, e5, e6, e7, e8)                                \\
  _print4(e1, e2, e3, e4), fputc(' ', stdout), _print4(e5, e6, e7, e8)
#define _print9(e1, e2, e3, e4, e5, e6, e7, e8, e9)                            \\
  _print5(e1, e2, e3, e4, e5), fputc(' ', stdout), _print4(e6, e7, e8, e9)
#define _print10(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10)                      \\
  _print5(e1, e2, e3, e4, e5), fputc(' ', stdout), _print5(e6, e7, e8, e9, e10)
#define _GET_PRINT_MACRO_NAME(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, NAME,   \\
                              ...)                                             \\
  NAME
#define print(...)                                                             \\
  _GET_PRINT_MACRO_NAME(__VA_ARGS__, _print10, _print9, _print8, _print7,      \\
                        _print6, _print5, _print4, _print3, _print2, _print1,  \\
                        _print0)                                               \\
  (__VA_ARGS__),                                                               \\
      fputc('\\n', stdout)
#define print0(...)                                                            \\
  _GET_PRINT_MACRO_NAME(__VA_ARGS__, _print10, _print9, _print8, _print7,      \\
                        _print6, _print5, _print4, _print3, _print2, _print1,  \\
                        _print0)                                               \\
  (__VA_ARGS__)

int N;
int K;
vector<int> P;
vector<int> M;
int ret;

int main() {
  {
    N = inputInt();
    K = inputInt();
  }
  P = vector<int>();
  M = vector<int>();
  for (int i = 0; i < N; ++i) {
    {
      const auto &a = inputInt();
      if (0 < (a)) {
        P.emplace_back(a);
      } else {
        M.emplace_back(a);
      }
    }
  }
  sort(P.begin(), P.end());
  sort(M.rbegin(), M.rend());
  ret = 1;
  if (P.empty() && K % 2 == 1 || N == K) {
    for (int $1 = 0; $1 < K; ++$1)
      ret = mod_mul(ret, M[$1], 1e9 + 7);
  } else {
    if (K % 2 == 1) {
      ret = mod(({
                  auto ret = P.back();
                  P.pop_back();
                  ret;
                }),
                1e9 + 7);
      --K;
    }
    while (K) {
      if (P.size() < 2) {
        ret = mod_mul(ret, -({
                        auto ret = M.back();
                        M.pop_back();
                        ret;
                      }),
                      1e9 + 7);
        if (M.size() < 1) {
          ret = mod_mul(ret, ({
                          auto ret = P.back();
                          P.pop_back();
                          ret;
                        }),
                        1e9 + 7);
        } else {
          ret = mod_mul(ret, -({
                          auto ret = M.back();
                          M.pop_back();
                          ret;
                        }),
                        1e9 + 7);
        }
      } else if (M.size() < 2) {
        ret = mod_mul(ret, ({
                        auto ret = P.back();
                        P.pop_back();
                        ret;
                      }),
                      1e9 + 7);
        ret = mod_mul(ret, ({
                        auto ret = P.back();
                        P.pop_back();
                        ret;
                      }),
                      1e9 + 7);
      } else if ((long long)(M[M.size() - 1]) * M[M.size() - 2] <
                 (long long)(P[P.size() - 1]) * P[P.size() - 2]) {
        ret = mod_mul(ret, ({
                        auto ret = P.back();
                        P.pop_back();
                        ret;
                      }),
                      1e9 + 7);
        ret = mod_mul(ret, ({
                        auto ret = P.back();
                        P.pop_back();
                        ret;
                      }),
                      1e9 + 7);
      } else {
        ret = mod_mul(ret, -({
                        auto ret = M.back();
                        M.pop_back();
                        ret;
                      }),
                      1e9 + 7);
        ret = mod_mul(ret, -({
                        auto ret = M.back();
                        M.pop_back();
                        ret;
                      }),
                      1e9 + 7);
      }
      K -= 2;
    }
  }
  print(ret);
  return 0;
}