Language: C++

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD 998244353
template<class T>inline bool chmax(T& a,T b){if (a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T& a,T b){if (a>b){a=b;return 1;}return 0;}
const long long INF = 1LL << 60;
bool pairCompare(const pair<double,ll>& firstElof, const pair<double,ll>& secondElof){
    return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double,ll>& firstElof, const pair<double,ll>& secondElof){
    return firstElof.second < secondElof.second;
}
//四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge{//グラフに使うヤツ
    ll from,to,cost;
};

typedef vector<vector<edge> > G;
ll gcd(ll a,ll b){
    if (a%b==0)return(b);
    else return(gcd(b,a%b));
}



int main() {
    ll n,k;
    cin >> n >> k;
    ll a[n];
    for (ll i=0;i<n;i++){
        cin >> a[i];
    }
    vector<ll> p;
    vector<ll> m;
    for (ll i=0;i<n;i++){
        if (a[i]>0)p.push_back(a[i]);
        else m.push_back(a[i]);
    }
    if (p.size())sort(p.begin(),p.end());
    if (m.size())sort(m.begin(),m.end());
    if (k==1){
        if (p.size()>0){
            cout << p[p.size()-1] << endl;
        }else{
            cout << 1000000007+m[n-1] << endl;
        }
        return 0;
    }
    if (k%2){
        if (p.size()==0){
            if (m[m.size()-1]==0){
                cout << 0 << endl;
                return 0;
            }
            ll ans=1;
            for (ll i=0;i<k;i++){
                ans*=m[n-1-i];
                ans%=1000000007;
            }
            cout << 1000000007+ans << endl;
            return 0;
        }else{
            ll ans=p[p.size()-1];
            vector<ll> p2;
            vector<ll> m2;
            if (p.size()){
                for (ll i=(p.size()-1)-1;i>=0;i-=2){
                    if (i-1<0)break;
                    p2.push_back(p[i]*p[i-1]);
                }
            }
            if (m.size()){}
                for (ll i=0;i<m.size()-1;i+=2){
                    if (i+1>m.size()-1)break;
                    m2.push_back(m[i]*m[i+1]);
                }
            }
            if (p2.size())sort(p2.begin(),p2.end());
            if (m2.size())sort(m2.begin(),m2.end());
            ll x=p2.size()-1,y=m2.size()-1;
            for (ll i=0;i<k/2;i++){
                if (x>=0 and y>=0){
                    if (p2[x]>m2[y]){
                        ll xx=p2[x]%1000000007;
                        ans*=xx;
                        ans%=1000000007;
                        x--;
                    }else{
                        ll xx=m2[y]%1000000007;
                        ans*=xx;
                        ans%=1000000007;
                        y--;
                    }
                }else if (x>=0){
                    ll xx=p2[x]%1000000007;
                    ans*=xx;
                    ans%=1000000007;
                    x--;
                }else{
                    ll xx=m2[y]%1000000007;
                    ans*=xx;
                    ans%=1000000007;
                    x--;
                }
            }
        }
    }else{
        ll ans=1;
        vector<ll> p2;
        vector<ll> m2;
        for (ll i=(p.size()-1);i>=0;i-=2){
            if (i-1<0)break;
            p2.push_back(p[i]*p[i-1]);
        }
        for (ll i=0;i<m.size()-1;i+=2){
            if (i+1>m.size()-1)break;
            m2.push_back(m[i]*m[i+1]);
        }
        if (p2.size())sort(p2.begin(),p2.end());
        if (m2.size())sort(m2.begin(),m2.end());
        ll x=p2.size()-1,y=m2.size()-1;
        for (ll i=0;i<k/2;i++){
            if (x>=0 and y>=0){
                if (p2[x]>m2[y]){
                    ll xx=p2[x]%1000000007;
                    ans*=xx;
                    ans%=1000000007;
                    x--;
                }else{
                    ll xx=m2[y]%1000000007;
                    ans*=xx;
                    ans%=1000000007;
                    y--;
                }
            }else if (x>=0){
                ll xx=p2[x]%1000000007;
                ans*=xx;
                ans%=1000000007;
                x--;
            }else{
                ll xx=m2[y]%1000000007;
                ans*=xx;
                ans%=1000000007;
                x--;
            }
        }
        cout << ans <<  endl;
    }
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:83:17: error: 'p2' was not declared in this scope; did you mean 'p'?
   83 |             if (p2.size())sort(p2.begin(),p2.end());
      |                 ^~
      |                 p
a.cc:84:17: error: 'm2' was not declared in this scope; did you mean 'm'?
   84 |             if (m2.size())sort(m2.begin(),m2.end());
      |                 ^~
      |                 m
a.cc:85:18: error: 'p2' was not declared in this scope; did you mean 'p'?
   85 |             ll x=p2.size()-1,y=m2.size()-1;
      |                  ^~
      |                  p
a.cc:87:30: error: 'y' was not declared in this scope
   87 |                 if (x>=0 and y>=0){
      |                              ^
a.cc:88:31: error: 'm2' was not declared in this scope; did you mean 'm'?
   88 |                     if (p2[x]>m2[y]){
      |                               ^~
      |                               m
a.cc:90:25: error: 'ans' was not declared in this scope; did you mean 'abs'?
   90 |                         ans*=xx;
      |                         ^~~
      |                         abs
a.cc:95:25: error: 'ans' was not declared in this scope; did you mean 'abs'?
   95 |                         ans*=xx;
      |                         ^~~
      |                         abs
a.cc:101:21: error: 'ans' was not declared in this scope; did you mean 'abs'?
  101 |                     ans*=xx;
      |                     ^~~
      |                     abs
a.cc:105:27: error: 'm2' was not declared in this scope; did you mean 'm'?
  105 |                     ll xx=m2[y]%1000000007;
      |                           ^~
      |                           m
a.cc:106:21: error: 'ans' was not declared in this scope; did you mean 'abs'?
  106 |                     ans*=xx;
      |                     ^~~
      |                     abs
a.cc: At global scope:
a.cc:112:6: error: expected unqualified-id before 'else'
  112 |     }else{
      |      ^~~~
a.cc:154:5: error: expected unqualified-id before 'return'
  154 |     return 0;
      |     ^~~~~~
a.cc:155:1: error: expected declaration before '}' token
  155 | }
      | ^