/* ID: usaco.t3 TASK: test LANG: C++14 */ #pragma GCC optimize ("O3") /****Author: Barish Namazov****/ #include using namespace std; /***TEMPLATE***/ #define intt long long #define pii pair #define vi vector #define vii vector #define all(v) (v).begin(),(v).end() #define rall(v) (v).rbegin(),(v).rend() #define F first #define S second #define pb push_back #define IO ios_base::sync_with_stdio(false);cin.tie(); #define endl '\n' #define wtfis(x) cerr << "at line " << __LINE__ << ": " << #x << " = " << (x) << endl const intt max3 = 1003; const intt max4 = 10004; const intt maxx = 100005; const intt max6 = 1000006; const intt max7 = 10000007; const intt lg4 = 13; const intt lg5 = 17; const intt lg6 = 20; const intt INF = 2LL * 1000000000; const intt INFLL = 9LL * 1000000000 * 1000000000; /***************/ intt powmod (intt a, intt b, intt mod) { intt res = 1; a %= mod; for (; b; b >>= 1) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; } return res; } intt gcd (intt a, intt b) { while (b > 0) { intt t = a % b; a = b, b = t; } return a; } intt lcm (intt a, intt b) { return (a / gcd (a, b)) * b; } intt is_prime (intt n) { if (n <= 1 || n > 3 && (n % 2 == 0 || n % 3 == 0)) return 0; for (intt i = 5, t = 2; i * i <= n; i += t, t = 6 - t) if (n % i == 0) return 0; return 1; } /**************************/ const intt MOD = 1e9 + 7; intt n, k, arr[maxx]; intt F[maxx]; void update (intt i, intt val) { for (; i > 0; i -= i & (-i)) { F[i] = max (F[i], val); } } intt query (intt i) { intt res = 0; for (; i < maxx; i += i & (-i)) { res = max (res, F[i]); } return res; } intt t[maxx << 2], lazy[maxx << 2]; void relax (intt v, intt l, intt r) { if (l != r) { lazy[v << 1] += lazy[v], lazy[v << 1 | 1] += lazy[v]; t[v << 1] += lazy[v], t[v << 1 | 1] += lazy[v]; } lazy[v] = 0; } void update (intt v, intt l, intt r, intt i, intt j, intt val) { if (i > r || j < l || i > j) { return; } if (i <= l && r <= j) { lazy[v] += val, t[v] += val; return; } if (lazy[v] != 0) { relax (v, l, r); } intt mid = (l + r) >> 1; update (v << 1, l, mid, i, j, val); update (v << 1 | 1, mid + 1, r, i, j, val); t[v] = max (t[v << 1], t[v << 1 | 1]); } intt query (intt v, intt l, intt r, intt i, intt j) { if (i > r || j < l) { return -1; } if (t[v] < k) { return -1; } if (l == r) { return r; } if (lazy[v] != 0) { relax (v, l, r); } intt mid = (l + r) >> 1; intt res = query (v << 1, l, mid, i, j); if (res == -1) { return query (v << 1 | 1, mid + 1, r, i, j); } else { return res; } } intt pref[50], suff[50]; vector g[maxx]; int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); IO; cin >> n >> k; for (intt i = 1; i <= n; i++) { cin >> arr[i]; } stack mxheap, mnheap; mxheap.push (0); mnheap.push (0); intt has, t; for (intt i = 1; i <= n; i++) { has = i - 1, t = 0; while (mxheap.empty() == 0) { update (1, 1, n, mxheap.top() + 1, has, t - arr[i]); if (arr[mxheap.top()] >= arr[i] || mxheap.top() == 0) { break; } has = mxheap.top(), t = arr[has]; mxheap.pop(); } has = i - 1, t = 0; while (mnheap.empty() == 0) { update (1, 1, n, mnheap.top() + 1, has, -t + arr[i]); if (arr[mnheap.top()] <= arr[i] || mnheap.top() == 0) { break; } has = mnheap.top(), t = arr[has]; mnheap.pop(); } for (intt j = 29; j >= 0; j--) { if ((arr[i] >> j) & 1) { update (1, 1, n, suff[j] + 1, i - 1, (1 << j)); pref[j] = (pref[j] == 0 ? i : pref[j]); suff[j] = i; } else if (pref[j] > 0) { update (1, 1, n, pref[j], i - 1, (1 << j)); pref[j] = 0; } } mxheap.push (i), mnheap.push (i); intt ans = query (1, 1, n, 1, i); if (ans != -1) { g[ans].pb (i); } } for (intt i = 1; i <= n; i++) { for (intt j = 0; j < g[i].size(); j++) { update (g[i][j], g[i][j] - i + 1); } intt res = query (i); if (res == 0) { res = -1; } cout << res << endl; } return 0; }