#include <bits/stdc++.h>
#define ld long double
using namespace std;
#define IO ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
typedef long long ll;
char file_name[100];
void set_output_path(string s)
{
strcpy(file_name, s.c_str());
// cout << file_name << '\n';
freopen(file_name, "w", stdout);
}
string get_id(int num)
{
if (num < 10)
return "0" + to_string(num);
return to_string(num);
}
void set_output_in(int num)
{
set_output_path(get_id(num) + ".in");
}
void set_output_ok(int num)
{
set_output_path(get_id(num) + ".ok");
}
const int N = 1001, K = 51, INF = 1e9;
int n, k, a[N], mem[N][N][K][2];
int dp(int i, int pre, int cnt_lm, bool pre_is_greaterThan_prepre)
{
if (cnt_lm > k) return INF;
if (i == n + 1) return cnt_lm == k ? 0 : INF;
auto &ret = mem[i][pre][cnt_lm][pre_is_greaterThan_prepre];
if (~ret) return ret;
int c1 = 1 + dp(i + 1, pre, cnt_lm, pre_is_greaterThan_prepre); // leaave
int c2 = INF; // try to pick
if (a[i] < a[pre] && pre_is_greaterThan_prepre)
{
c2 = dp(i + 1, i, cnt_lm + 1, 0);
}
else
{
c2 = dp(i + 1, i, cnt_lm, a[i] > a[pre]);
}
return ret = min(c1, c2);
}
void solve()
{
a[0] = INF;
memset(mem, -1, sizeof mem);
auto ans = dp(1, 0, 0, 0);
if (ans == INF)
{
ans = -1;
}
cout << ans;
}
void out(int tt, int lim)
{
set_output_in(tt);
cout << lim << ' ';
n = lim;
int k = rand() % 50 + 1;
cout << k << '\n';
int N = 1e9;
for (int tc = 1; tc <= lim; tc++)
{
int z = rand() % N + 1;
a[tc] = z;
cout << z << '\n';
}
set_output_ok(tt);
solve();
// for (int tc = 1; tc <= lim; tc++)
// {
// solve(tc);
// }
}
int main()
{
///cout << fixed << setprecision(10);
srand(0);
// IO;
///int tc = 1, tt = 2;
for (int i = 3; i <= 5; i++)
{
out(i, 10);
}
for (int i = 6; i <= 10; i++)
{
out(i, 100);
}
for (int i = 11; i <= 15; i++)
{
out(i, 1000);
}
for (int i = 15; i <= 20; i++)
{
out(i, 500);
}
// for (int i = 21; i <= 23; i++)
// {
// out(i, 100000);
// }
// out(4, 100);
// out(4, 1000);
// out(6, 100000);
// out(8, 1000000);
// solve();
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgbGQgbG9uZyBkb3VibGUKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBJTyBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKDApLCBjaW4udGllKDApLCBjb3V0LnRpZSgwKQp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKCmNoYXIgZmlsZV9uYW1lWzEwMF07Cgp2b2lkIHNldF9vdXRwdXRfcGF0aChzdHJpbmcgcykKewogICAgc3RyY3B5KGZpbGVfbmFtZSwgcy5jX3N0cigpKTsKICAgIC8vIGNvdXQgPDwgZmlsZV9uYW1lIDw8ICdcbic7CiAgICBmcmVvcGVuKGZpbGVfbmFtZSwgInciLCBzdGRvdXQpOwp9CgpzdHJpbmcgZ2V0X2lkKGludCBudW0pCnsKICAgIGlmIChudW0gPCAxMCkKICAgICAgICByZXR1cm4gIjAiICsgdG9fc3RyaW5nKG51bSk7CiAgICByZXR1cm4gdG9fc3RyaW5nKG51bSk7Cn0KCnZvaWQgc2V0X291dHB1dF9pbihpbnQgbnVtKQp7CiAgICBzZXRfb3V0cHV0X3BhdGgoZ2V0X2lkKG51bSkgKyAiLmluIik7Cn0KCnZvaWQgc2V0X291dHB1dF9vayhpbnQgbnVtKQp7CiAgICBzZXRfb3V0cHV0X3BhdGgoZ2V0X2lkKG51bSkgKyAiLm9rIik7Cn0KCgpjb25zdCBpbnQgTiA9IDEwMDEsIEsgPSA1MSwgSU5GID0gMWU5OwoKaW50IG4sIGssIGFbTl0sIG1lbVtOXVtOXVtLXVsyXTsKCmludCBkcChpbnQgaSwgaW50IHByZSwgaW50IGNudF9sbSwgYm9vbCBwcmVfaXNfZ3JlYXRlclRoYW5fcHJlcHJlKQp7CiAgICBpZiAoY250X2xtID4gaykgcmV0dXJuIElORjsKICAgIGlmIChpID09IG4gKyAxKSByZXR1cm4gY250X2xtID09IGsgPyAwIDogSU5GOwoKICAgIGF1dG8gJnJldCA9IG1lbVtpXVtwcmVdW2NudF9sbV1bcHJlX2lzX2dyZWF0ZXJUaGFuX3ByZXByZV07CiAgICBpZiAofnJldCkgcmV0dXJuIHJldDsKCiAgICBpbnQgYzEgPSAxICsgZHAoaSArIDEsIHByZSwgY250X2xtLCBwcmVfaXNfZ3JlYXRlclRoYW5fcHJlcHJlKTsgLy8gbGVhYXZlCiAgICBpbnQgYzIgPSBJTkY7ICAgLy8gdHJ5IHRvIHBpY2sKCiAgICBpZiAoYVtpXSA8IGFbcHJlXSAmJiBwcmVfaXNfZ3JlYXRlclRoYW5fcHJlcHJlKQogICAgewogICAgICAgIGMyID0gZHAoaSArIDEsIGksIGNudF9sbSArIDEsIDApOwogICAgfQogICAgZWxzZQogICAgewogICAgICAgIGMyID0gZHAoaSArIDEsIGksIGNudF9sbSwgYVtpXSA+IGFbcHJlXSk7CiAgICB9CiAgICByZXR1cm4gcmV0ID0gbWluKGMxLCBjMik7Cn0KCnZvaWQgc29sdmUoKQp7CiAgICBhWzBdID0gSU5GOwogICAgbWVtc2V0KG1lbSwgLTEsIHNpemVvZiBtZW0pOwogICAgYXV0byBhbnMgPSBkcCgxLCAwLCAwLCAwKTsKICAgIGlmIChhbnMgPT0gSU5GKQogICAgewogICAgICAgIGFucyA9IC0xOwogICAgfQogICAgY291dCA8PCBhbnM7Cn0KCnZvaWQgb3V0KGludCB0dCwgaW50IGxpbSkKewogICAgc2V0X291dHB1dF9pbih0dCk7CiAgICBjb3V0IDw8IGxpbSA8PCAnICc7CiAgICBuID0gbGltOwogICAgaW50IGsgPSByYW5kKCkgJSA1MCArIDE7CiAgICBjb3V0IDw8IGsgPDwgJ1xuJzsKICAgIGludCBOID0gMWU5OwogICAgZm9yIChpbnQgdGMgPSAxOyB0YyA8PSBsaW07IHRjKyspCiAgICB7CiAgICAgICAgaW50IHogPSByYW5kKCkgJSBOICsgMTsKICAgICAgICBhW3RjXSA9IHo7CiAgICAgICAgY291dCA8PCB6IDw8ICdcbic7CiAgICB9CiAgICBzZXRfb3V0cHV0X29rKHR0KTsKICAgIHNvbHZlKCk7CiAgICAvLyBmb3IgKGludCB0YyA9IDE7IHRjIDw9IGxpbTsgdGMrKykKICAgIC8vIHsKICAgIC8vICAgc29sdmUodGMpOwogICAgLy8gfQp9CmludCBtYWluKCkKewogICAgLy8vY291dCA8PCBmaXhlZCA8PCBzZXRwcmVjaXNpb24oMTApOwogICAgc3JhbmQoMCk7CiAgICAvLyBJTzsKICAgIC8vL2ludCB0YyA9IDEsIHR0ID0gMjsKICAgIGZvciAoaW50IGkgPSAzOyBpIDw9IDU7IGkrKykKICAgIHsKICAgICAgICBvdXQoaSwgMTApOwogICAgfQogICAgZm9yIChpbnQgaSA9IDY7IGkgPD0gMTA7IGkrKykKICAgIHsKICAgICAgICBvdXQoaSwgMTAwKTsKICAgIH0KICAgIGZvciAoaW50IGkgPSAxMTsgaSA8PSAxNTsgaSsrKQogICAgewogICAgICAgIG91dChpLCAxMDAwKTsKICAgIH0KICAgIGZvciAoaW50IGkgPSAxNTsgaSA8PSAyMDsgaSsrKQogICAgewogICAgICAgIG91dChpLCA1MDApOwogICAgfQovLyAgICBmb3IgKGludCBpID0gMjE7IGkgPD0gMjM7IGkrKykKLy8gICAgewovLyAgICAgICAgb3V0KGksIDEwMDAwMCk7Ci8vICAgIH0KICAgIC8vIG91dCg0LCAxMDApOwogICAgLy8gb3V0KDQsIDEwMDApOwogICAgLy8gb3V0KDYsIDEwMDAwMCk7CiAgICAvLyBvdXQoOCwgMTAwMDAwMCk7CiAgICAvLyBzb2x2ZSgpOwp9Cg==