Language: C++

#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <limits>
#include <numeric>
#include <valarray>
#include <fstream>
#include <array>
#include <random>
#include <unordered_set>
#include <unordered_map>

using namespace std;
using uint = uint32_t;
using LL = int64_t;
using ULL = uint64_t;
using PP = pair<LL, LL>;
template <typename T> using PriorityQ = priority_queue<T, vector<T>, greater<T> >;
#define REP(i, a, n) for(LL i = (a), i##_max_ = (n); i < i##_max_; ++i)
#define REM(i, a, n) for(LL i = (LL)(n) - 1, i##_min_ = (a); i >= i##_min_; --i)
#define FLOAT fixed << setprecision(16)
#define SPEEDUP { cin.tie(NULL); ios::sync_with_stdio(false); }
const int INF = 0x3FFFFFFF;
const LL INFLL = 0x3FFFFFFF3FFFFFFF;
const double INFD = 1.0e+308;
const double EPS = 1.0e-9;

void YesNo(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YESNO(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T, class U> istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U> ostream& operator<<(ostream& ost, const pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N> void Fill(T(&dest)[N], const TCompatible& val) { fill(dest, dest + N, val); }
template <class T, class TCompatible, size_t M, size_t N> void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
template <class T> T Next() { T buf; cin >> buf; return buf; }
istream& Ignore(istream& ist) { string s; ist >> s; return ist; }
bool Inside(int i, int j, int h, int w) { return i >= 0 && i < h && j >= 0 && j < w; }

#ifdef ONLY_MY_ENVIR
#include "Accumulator.h"
#include "Algebraic.h"
#include "BinaryMatrix.h"
#include "BinaryTree.h"
#include "Bipartite.h"
#include "BIT.h"
#include "Compressor.h"
#include "Decompositions.h"
#include "DiscreteLog.h"
#include "DynamicMod.h"
#include "Exponential.h"
#include "Factorization.h"
#include "FFT.h"
#include "FlowSolver.h"
#include "Graph.h"
#include "GraphUtil.h"
#include "IntMod.h"
#include "LazySegmentTree.h"
#include "LIS.h"
#include "Math.h"
#include "MathUtil.h"
#include "Matrix.h"
#include "MinCostFlowSolver.h"
#include "MinMax.h"
#include "Numbers.h"
#include "Optimize.h"
#include "Permutation.h"
#include "Polynomial.h"
#include "Position.h"
#include "Random.h"
#include "Range.h"
#include "Rational.h"
#include "RuntimeMod.h"
#include "SegmentTree.h"
#include "SegmentTree2D.h"
#include "Sets.h"
#include "Shortest.h"
#include "SlidingWindow.h"
#include "SpanningTree.h"
#include "StringSearching.h"
#include "SuffixArray.h"
#include "SwitchList.h"
#include "Timer.h"
#include "Tree.h"
#include "TreeUtil.h"
#include "UnionFind.h"
#include "Util.h"
#include "VectorUtil.h"
#endif

#ifdef __GNUC__
typedef __int128 LLL;
istream& operator>>(istream& ist, __int128& val) { LL tmp; ist >> tmp; val = tmp; return ist; }
ostream& operator<<(ostream& ost, __int128 val) { LL tmp = val; ost << tmp; return ost; }
#endif

int N, K;
int A[200000];

MInt sub(int del, int add) {
	MInt ret = 1;
	REP(i, 0, K) {
		if (i == del) continue;
		ret *= A[i];
	}
	return ret * A[add];
}

int main() {
	cin >> N >> K;
	REP(i, 0, N) cin >> A[i];
	sort(A, A + N, [](int a, int b) {
		return abs(a) > abs(b);
	});

	int mcnt = 0;
	REP(i, 0, N) mcnt += A[i] < 0;
	if (mcnt == N && K % 2 == 1) {
		MInt ret = 1;
		REP(i, 0, K) {
			ret *= A[N - 1 - i];
		}
		cout << ret << endl;
		return 0;
	}

	MInt sum = 1;
	int cnt = 0;
	REP(i, 0, K) {
		if (A[i] < 0) ++cnt;
		sum *= abs(A[i]);
	}
	
	if (cnt % 2 == 0) {
		cout << sum << endl;
	} else {
		int lp = K - 1;
		int lm = K - 1;
		int rp = K;
		int rm = K;
		while (lp >= 0 && A[lp] < 0) --lp;
		while (lm >= 0 && A[lm] >= 0) --lm;
		while (rp < N && A[rp] < 0) ++rp;
		while (rm < N && A[rm] >= 0) ++rm;

		MInt ans;
		if (lp < 0 || rm >= N) {
			ans = sub(lm, rp);
		} else if (lm < 0 || rp >= N) {
			ans = sub(lp, rm);
		} else {
			if ((LL)A[rm] * A[lm] >= (LL)A[rp] * A[lp]) {
				ans = sub(lp, rm);
			} else {
				ans = sub(lm, rp);
			}
		}
		cout << ans << endl;
	}

	return 0;
}


Compiler output:
a.cc:116:1: error: 'MInt' does not name a type
  116 | MInt sub(int del, int add) {
      | ^~~~
a.cc: In function 'int main()':
a.cc:135:17: error: 'MInt' was not declared in this scope
  135 |                 MInt ret = 1;
      |                 ^~~~
a.cc:137:25: error: 'ret' was not declared in this scope
  137 |                         ret *= A[N - 1 - i];
      |                         ^~~
a.cc:139:25: error: 'ret' was not declared in this scope
  139 |                 cout << ret << endl;
      |                         ^~~
a.cc:143:9: error: 'MInt' was not declared in this scope
  143 |         MInt sum = 1;
      |         ^~~~
a.cc:147:17: error: 'sum' was not declared in this scope
  147 |                 sum *= abs(A[i]);
      |                 ^~~
a.cc:151:25: error: 'sum' was not declared in this scope
  151 |                 cout << sum << endl;
      |                         ^~~
a.cc:162:21: error: expected ';' before 'ans'
  162 |                 MInt ans;
      |                     ^~~~
      |                     ;
a.cc:164:25: error: 'ans' was not declared in this scope; did you mean 'abs'?
  164 |                         ans = sub(lm, rp);
      |                         ^~~
      |                         abs
a.cc:164:31: error: 'sub' was not declared in this scope; did you mean 'fsub'?
  164 |                         ans = sub(lm, rp);
      |                               ^~~
      |                               fsub
a.cc:166:25: error: 'ans' was not declared in this scope; did you mean 'abs'?
  166 |                         ans = sub(lp, rm);
      |                         ^~~
      |                         abs
a.cc:166:31: error: 'sub' was not declared in this scope; did you mean 'fsub'?
  166 |                         ans = sub(lp, rm);
      |                               ^~~
      |                               fsub
a.cc:169:33: error: 'ans' was not declared in this scope; did you mean 'abs'?
  169 |                                 ans = sub(lp, rm);
      |                                 ^~~
      |                                 abs
a.cc:169:39: error: 'sub' was not declared in this scope; did you mean 'fsub'?
  169 |                                 ans = sub(lp, rm);
      |                                       ^~~
      |                                       fsub
a.cc:171:33: error: 'ans' was not declared in this scope; did you mean 'abs'?
  171 |                                 ans = sub(lm, rp);
      |                                 ^~~
      |                                 abs
a.cc:171:39: error: 'sub' was not declared in this scope; did you mean 'fsub'?
  171 |                                 ans = sub(lm, rp);
      |                                       ^~~
      |                                       fsub
a.cc:174:25: error: 'ans' was not declared in this scope; did you mean 'abs'?
  174 |                 cout << ans << endl;
      |                         ^~~
      |                         abs