Language: C++


int a[N];

long long binpow(long long b, long long e) {
  if (!e) return 1;
  if (e&1) return b*binpow(b, e-1)%mod;
  return binpow(b*b%mod, e/2);
}

void solve() {
  int n, k; scanf("%d%d", &n, &k);
  for (int i = 0; i < n; ++i) {
    scanf("%d", a+i);
  }
  long long ans = 1;
  if (k == n) {
    for (int i = 0; i < n; ++i) {
      ans = (ans * a[i]) % mod;
    }
    printf("%lld", ans);
    return;
  }
  sort(a, a+n);
  if (a[n-1] <= 0 && k%2) {
    for (int i = n-1; i >= 0 && k; --i) {
      ans = (ans * a[i]) % mod;
      --k;
    }
    printf("%lld", (ans+mod)%mod);
    return;
  }
  sort(a, a+n, [](int x, int y){return abs(x) > abs(y);});
  long long neg = -2e9, pos = 2e9;
  for (int i = 0; i < k; ++i) {
    if (a[i] < 0) neg = a[i];
    if (a[i] > 0) pos = a[i];
    ans = (ans * a[i]) % mod;
  }
  if (ans >= 0) {
    printf("%lld", ans);
    return;
  }
  long long neg2 = -2e9, pos2 = 2e9;
  for (int i = k; i < n; ++i) {
    if (a[i] < 0) neg2 = a[i];
    if (a[i] > 0) pos2 = a[i];
  }
  if (neg != -2e9 && neg2 != -2e9 && pos != 2e9 && pos2 != 2e9) {
    if (neg*neg2 > pos*pos2) {
      ans = ans * neg2 % mod * binpow(pos, mod-2) % mod;
    } else {
      ans = ans * pos2 % mod * binpow(neg, mod-2) % mod;
    }
  } else if (neg != -2e9 && neg2 != -2e9) {
    ans = ans * neg2 % mod * binpow(pos, mod-2) % mod;
  } else {
    ans = ans * pos2 % mod * binpow(neg, mod-2) % mod;
  }
  printf("%lld", ans);
}

int main() {
  int t = 1; //scanf("%d", &t);
  while (t--) {
    solve();
  }
  return 0;
}


Compiler output:
a.cc:2:7: error: 'N' was not declared in this scope
    2 | int a[N];
      |       ^
a.cc: In function 'long long int binpow(long long int, long long int)':
a.cc:6:36: error: 'mod' was not declared in this scope
    6 |   if (e&1) return b*binpow(b, e-1)%mod;
      |                                    ^~~
a.cc:7:21: error: 'mod' was not declared in this scope
    7 |   return binpow(b*b%mod, e/2);
      |                     ^~~
a.cc: In function 'void solve()':
a.cc:11:13: error: 'scanf' was not declared in this scope
   11 |   int n, k; scanf("%d%d", &n, &k);
      |             ^~~~~
a.cc:13:17: error: 'a' was not declared in this scope
   13 |     scanf("%d", a+i);
      |                 ^
a.cc:18:20: error: 'a' was not declared in this scope
   18 |       ans = (ans * a[i]) % mod;
      |                    ^
a.cc:18:28: error: 'mod' was not declared in this scope
   18 |       ans = (ans * a[i]) % mod;
      |                            ^~~
a.cc:20:5: error: 'printf' was not declared in this scope
   20 |     printf("%lld", ans);
      |     ^~~~~~
a.cc:1:1: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
  +++ |+#include <cstdio>
    1 | 
a.cc:23:8: error: 'a' was not declared in this scope
   23 |   sort(a, a+n);
      |        ^
a.cc:23:3: error: 'sort' was not declared in this scope; did you mean 'short'?
   23 |   sort(a, a+n);
      |   ^~~~
      |   short
a.cc:26:28: error: 'mod' was not declared in this scope
   26 |       ans = (ans * a[i]) % mod;
      |                            ^~~
a.cc:29:25: error: 'mod' was not declared in this scope
   29 |     printf("%lld", (ans+mod)%mod);
      |                         ^~~
a.cc:29:5: error: 'printf' was not declared in this scope
   29 |     printf("%lld", (ans+mod)%mod);
      |     ^~~~~~
a.cc:29:5: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
a.cc: In lambda function:
a.cc:32:40: error: 'abs' was not declared in this scope; did you mean 'ans'?
   32 |   sort(a, a+n, [](int x, int y){return abs(x) > abs(y);});
      |                                        ^~~
      |                                        ans
a.cc: In function 'void solve()':
a.cc:37:26: error: 'mod' was not declared in this scope
   37 |     ans = (ans * a[i]) % mod;
      |                          ^~~
a.cc:40:5: error: 'printf' was not declared in this scope
   40 |     printf("%lld", ans);
      |     ^~~~~~
a.cc:40:5: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'
a.cc:50:26: error: 'mod' was not declared in this scope
   50 |       ans = ans * neg2 % mod * binpow(pos, mod-2) % mod;
      |                          ^~~
a.cc:52:26: error: 'mod' was not declared in this scope
   52 |       ans = ans * pos2 % mod * binpow(neg, mod-2) % mod;
      |                          ^~~
a.cc:55:24: error: 'mod' was not declared in this scope
   55 |     ans = ans * neg2 % mod * binpow(pos, mod-2) % mod;
      |                        ^~~
a.cc:57:24: error: 'mod' was not declared in this scope
   57 |     ans = ans * pos2 % mod * binpow(neg, mod-2) % mod;
      |                        ^~~
a.cc:59:3: error: 'printf' was not declared in this scope
   59 |   printf("%lld", ans);
      |   ^~~~~~
a.cc:59:3: note: 'printf' is defined in header '<cstdio>'; this is probably fixable by adding '#include <cstdio>'