Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
typedef long long ll;
const int mod = 1000000007;
int n,k;
int a[200005];
ii A[200005];
ll ans = 1;
int take[200005];
bool allneg = true;
bool havezero = false;
int main(){
    cin >> n >> k;
    for (int i = 0; i <n ;i++) {
        cin >> a[i];
        if (a[i] >= 0) A[i] = ii(a[i],0);
        else A[i] = ii(-a[i],1);
        if (a[i] > 0) allneg = false;
        if (a[i] == 0) havezero = true;
    }
    if (allneg){
        for (int i = 0; i < n; i++) {
            if (a[i] == 0){
                printf("0\n");
                return 0;
            }
        }
        if (k & 1){
            sort(a,a+n,greater<int>());
        }
        else{
            sort(a,a+n);
        }
        for (int i = 0; i < k; i++) ans = (ans*a[i])%mod;
        ans = (ans+mod)%mod;
        printf("%lld",ans);
        return 0;
    }
    else if ( k== n){
        for (int i = 0; i < k; i++) ans = (ans*a[i])%mod;
        ans = (ans+mod)%mod;
        printf("%lld",ans);
        return 0;
    }
    sort(A,A+n,greater<ii>());
    int ct = 0;
    for (int i = 0; i < k; i++){
        ct += A[i].second;
        take[i] = 1;
    }
    if (ct & 1) {
        int P = -1,Q = -1,R = -1,S = -1;
        for (int i = k-1; i >= 0;i--){
            if (A[i].second && P == -1){
                P = i;
            }
            if (!A[i].second && Q == -1){
                Q = i;
            }
        }
        for (int i = k; i < n; i++){
            if (A[i].second && R == -1){
                R = i;
            }
            if (!A[i].second && S == -1){
                S = i;
            }
        }
        if (S == -1){
            ///force to remove pos, add neg
            if (R == -1 || Q == -1){

            }
            take[R] = 1;
            take[Q] = 0;
        }
        else if (R == -1 || Q == -1){
            ///force to remove neg, add pos
            take[S] = 1;
            take[P] = 0;
        }
        else if ((ll)A[R].first*A[P].first > (ll)A[Q].first*A[S].first){
            ///better to take neg remove pos
            take[R] = 1;
            take[Q] = 0;
        }
        else{
            take[S] = 1;
            take[P] = 0;
        }
    }
    int taken = 0;
    for (int i = 0; i < n; i++){
        int x = A[i].first * (A[i].second?-1:1);
        //printf("%d %d\n",take[i],x);
        if (take[i]) {taken++; ans = (ans*x)%mod;}

    }
    if (taken == k-1){
        bool done =false;
        for (int i = 0; i < n; i++){
            if (taken[i]) continue;
            if (ans < 0 && (A[i].first == 0 || A[i].second)){
                ans = (ans*a[i])%mod;
                done =true;
                break;
            }
            else if (ans > 0 && A[i].second == 0){
                ans = (ans*a[i])%mod;
                done = true;
                break;
            }
        }
        assert(done);
    }
    if (ans < 0 && havezero) ans = 0;
    ans = (ans+mod)%mod;
    printf("%lld",ans);

}


Compiler output:
a.cc: In function 'int main()':
a.cc:103:22: error: invalid types 'int[int]' for array subscript
  103 |             if (taken[i]) continue;
      |                      ^