#include <cstdlib>
#include <iostream>
using namespace std;
const int sz = 1000000;
int arr[sz];
int temp[sz];
void mergeParts(int *arr, int l, int mid, int r) {
int i = l, j = mid + 1;
int k = 0;
while (i <= mid && j <= r) {
int nextValue;
if (arr[i] < arr[j]) nextValue = arr[i++];
else nextValue = arr[j++];
temp[k++] = nextValue;
}
while (i <= mid) temp[k++] = arr[i++];
while (j <= r) temp[k++] = arr[j++];
for (int i=0; i<k; i++) arr[l + i] = temp[i];
}
void mergeSort(int *arr, int l, int r) {
if (l >= r) return;
int mid = (l + r) / 2;
mergeSort(arr, l, mid);
mergeSort(arr, mid + 1, r);
mergeParts(arr, l, mid, r);
}
bool check() {
for (int i=0; i<sz-1; i++){
if (arr[i] > arr[i + 1]) return false;
}
return true;
}
int main(){
for (int i=0; i<sz; i++) {
arr[i] = rand() % sz;
}
mergeSort(arr, 0, sz - 1);
cout<<(check() ? "OK": "FAIL");
return 0;
}
CiNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8aW9zdHJlYW0+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY29uc3QgaW50IHN6ID0gMTAwMDAwMDsKCmludCBhcnJbc3pdOwppbnQgdGVtcFtzel07Cgp2b2lkIG1lcmdlUGFydHMoaW50ICphcnIsIGludCBsLCBpbnQgbWlkLCBpbnQgcikgewogICAgaW50IGkgPSBsLCBqID0gbWlkICsgMTsKICAgIGludCBrID0gMDsKCiAgICB3aGlsZSAoaSA8PSBtaWQgJiYgaiA8PSByKSB7CiAgICAgICAgaW50IG5leHRWYWx1ZTsKCiAgICAgICAgaWYgKGFycltpXSA8IGFycltqXSkgbmV4dFZhbHVlID0gYXJyW2krK107CiAgICAgICAgZWxzZSBuZXh0VmFsdWUgPSBhcnJbaisrXTsKCiAgICAgICAgdGVtcFtrKytdID0gbmV4dFZhbHVlOwogICAgfQoKICAgIHdoaWxlIChpIDw9IG1pZCkgdGVtcFtrKytdID0gYXJyW2krK107CiAgICB3aGlsZSAoaiA8PSByKSB0ZW1wW2srK10gPSBhcnJbaisrXTsKCiAgICBmb3IgKGludCBpPTA7IGk8azsgaSsrKSBhcnJbbCArIGldID0gdGVtcFtpXTsKCn0KCnZvaWQgbWVyZ2VTb3J0KGludCAqYXJyLCBpbnQgbCwgaW50IHIpIHsKICAgIGlmIChsID49IHIpIHJldHVybjsKCiAgICBpbnQgbWlkID0gKGwgKyByKSAvIDI7CgogICAgbWVyZ2VTb3J0KGFyciwgbCwgbWlkKTsKICAgIG1lcmdlU29ydChhcnIsIG1pZCArIDEsIHIpOwoKICAgIG1lcmdlUGFydHMoYXJyLCBsLCBtaWQsIHIpOwp9CgoKYm9vbCBjaGVjaygpIHsKICAgIGZvciAoaW50IGk9MDsgaTxzei0xOyBpKyspewogICAgICAgIGlmIChhcnJbaV0gPiBhcnJbaSArIDFdKSByZXR1cm4gZmFsc2U7CiAgICB9CiAgICByZXR1cm4gdHJ1ZTsKfQoKaW50IG1haW4oKXsKICAgIGZvciAoaW50IGk9MDsgaTxzejsgaSsrKSB7CiAgICAgICAgYXJyW2ldID0gcmFuZCgpICUgc3o7CiAgICB9CgogICAgbWVyZ2VTb3J0KGFyciwgMCwgc3ogLSAxKTsKCiAgICBjb3V0PDwoY2hlY2soKSA/ICJPSyI6ICJGQUlMIik7CgogICAgcmV0dXJuIDA7Cn0K