#include #define REP(i,n) for(int i=0;i<(int)(n);i++) #define ALL(x) (x).begin(),(x).end() using namespace std; struct Data { int mi, ma; Data() : mi(0x7FFFFFFF), ma(0x80000000) {;} Data(int n) : mi(n), ma(n) {;} Data(int mi, int ma) : mi(mi), ma(ma) {;} }; inline Data Merge(Data left, Data right) { return Data(min(left.mi, right.mi), max(left.ma, right.ma)); } struct SegmentTree { int n; vector data; SegmentTree(int N) : n(1 << N), data(n * 2) {} void update (int pos, Data value) { data[pos] = value; while (pos < 2*n-1) { int l = pos, r = pos^1; if (l > r) swap(l, r); pos = pos / 2 + n; data[pos] = Merge(data[l], data[r]); } } Data sub(int fr, int to, int node, int la, int ra) { if (ra<=fr || to<=la) return Data(); if (fr<=la && ra<=to) return data[node]; Data vl = sub(fr, to, (node-n)*2+0, la, (la+ra)/2); Data vr = sub(fr, to, (node-n)*2+1, (la+ra)/2, ra); return Merge(vl, vr); } Data query(int fr, int to) { return sub(fr, to, 2*n-2, 0, n); } }; int solve(int N) { SegmentTree seg(10); vector a(N); REP(i,N) { cin >> a[i]; seg.update(i, a[i]); } int res = 0; REP(i,N) REP(j,i) { Data left = seg.query(0, j); Data mid = seg.query(j+1, i); Data right = seg.query(i+1, N); int val = 0; if (left.ma > a[j] && a[j] < a[i]) val = max({val, left.ma, a[i]}); if (left.mi < a[j] && a[j] > a[i]) val = max(val, a[j]); if (a[j] > mid.mi && mid.mi < a[i]) val = max({val, a[j], a[i]}); if (a[j] < mid.ma && mid.ma > a[i]) val = max(val, mid.ma); if (a[j] > a[i] && a[i] < right.ma) val = max({val, a[j], right.ma}); if (a[j] < a[i] && a[i] > right.mi) val = max(val, a[i]); res += val; } return res; } int main() { int N, M; cin >> N >> M; int ma = 0, res = 0; REP(i,M) { int v = solve(N); //cout << v << endl; if (v > ma) { ma = v; res = i; } } cout << res << endl; return 0; }