Language: C++


#include <iostream>
#include <iomanip> //! for setprecision(10)
#include <math.h>
#include <algorithm>
#include <functional> 
#include <string>
#include <vector>

#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <utility>

using namespace std;

typedef long long LL;

#define dump(c) { for (auto it = c.begin(); it != c.end(); ++it) if (it == c.begin()) cout << *it; else cout << ' ' << *it; cout << endl; } 
#define rep(i,n) for (int i = 0; i < (n); ++i)

const int MOD =     ;

class Mint {
public:
  typedef long long val_type;

  Mint(val_type x=0, int m=MOD) : val((x%m+m)%m), mod(m) {}
  val_type value() const { return val; }
  Mint& operator+=(const Mint a) {
    if ((val += a.val) >= mod) val -= mod;
    return *this;
  }
  Mint& operator-=(const Mint a) {
    if ((val += mod-a.val) >= mod) val -= mod;
    return *this;
  }
  Mint& operator*=(const Mint a) {
    (val *= a.val) %= mod;
    return *this;
  }
  Mint operator+(const Mint a) const {
    Mint res(*this);
    return res+=a;
  }
  Mint operator-(const Mint a) const {
    Mint res(*this);
    return res-=a;
  }
  Mint operator*(const Mint a) const {
    Mint res(*this);
    return res*=a;
  }
  Mint pow(val_type t) const {
    if (!t) return 1;
    Mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime mod
  Mint inv() const {
    return pow(mod-2);
  }
  Mint& operator/=(const Mint a) {
    return (*this) *= a.inv();
  }
  Mint operator/(const Mint a) const {
    Mint res(*this);
    return res/=a;
  }

private:
  val_type val;
  int mod;
};


int main()
{
	cin.tie(0);
	ios::sync_with_stdio(0);

    int N, K;
    cin >> N >> K;
    vector<int> p; p.reserve(N);
    vector<int> n; n.reserve(N);
    int z = 0;
    rep(i, N) {
        int A;
        cin >> A;
        if (0 < A) {
            p.push_back(A);
        } else if (A < 0) {
            n.push_back(A);
        } else {
            ++z;
        }
    }
    sort(p.begin(), p.end());
    sort(n.rbegin(), n.rend());

    Mint ans(0); 
    if (p.size() + n.size() < K) {
        //! need to include zero
        ans = 0;
    } else if (p.size() == 0 && K % 2 == 1) {
        //! K is odd, no positive number
        if (0 < z) {
            ans = 0;
        } else {
            //! choose firs K from n
            ans = 1;
            rep(i, K) {
                ans *= n[i];
            }
        }
    } else if (p.size() + (n.size()/2)*2 < K) {
        if (0 < z) {
            ans = 0;
        } else {
            //! need to to pick odd number from n
            int pc = 0;
            int nc = 0;
            ans = 1;
            while (pc+nc < K) {
                if (pc == p.size()) {
                    ans *= n[nc];
                    ++nc;
                } else if (nc == n.size()) {
                    ans *= p[pc];
                    ++pc;
                } else if (p[pc] <= -n[nc]) {
                    ans *= p[pc];
                    ++pc;
                } else {
                    ans *= n[nc];
                    ++nc;
                }
            }
        }
    } else {
        //! pick even from n, rest from p
        int pc = 0;
        int nc = 0;
        ans = 1;
        while (pc+nc < K) {
            if (n.size() - nc < 2 || (K-pc-nc == 1)) {
                //! no more number from n
                ans *= p[p.size()-pc-1];
                ++pc;
            } else if (2 <= n.size()-nc && p.size() - pc < 2) {
                //! choose 2 from n
                ans *= -n[n.size()-nc-1];
                ans *= -n[n.size()-nc-2];
                nc += 2;
            } else {
                //! choose 2 from p or n
                LL pp = (LL) p[p.size()-pc-1] * (LL) p[p.size()-pc-2];
                LL np = (LL) n[n.size()-nc-1] * (LL) n[n.size()-nc-2];
                if (pp < np) {
                    ans *= np;
                    nc += 2;
                } else {
                    ans *= pp;
                    pc += 2;
                }
            }
        }
    }
    cout << ans.value() << endl;

    return 0;
}


Compiler output:
a.cc:23:21: error: expected primary-expression before ';' token
   23 | const int MOD =     ;
      |                     ^