#include<iostream>
#include<map>
#include<set>
#include<cstdio>
using namespace std;
const int BUF = 1500005;


int nVal, range;
int val[BUF];

void read() {
    scanf("%d%d", &nVal, &range);
    for (int i = 0; i < nVal; ++i) {
        scanf("%d", &val[i]);
    }
}

void add(int v, int v2cnt[BUF], set<int> &candi) {
    if (++v2cnt[v] == 1) {
        candi.erase(candi.find(v));
    }
}

void remove(int v, int v2cnt[BUF], set<int> &candi) {
    if (--v2cnt[v] == 0) {
        candi.insert(v);
    }
}


void work() {
    set<int> candi;
    for (int i = 0; i < BUF; ++i) {
        candi.insert(i);
    }
    
    static int v2cnt[BUF] = {};
    for (int i = 0; i < range; ++i) {
        add(val[i], v2cnt, candi);
    }

    int minV = *candi.begin();
    for (int i = range; i < nVal; ++i) {
        add(val[i], v2cnt, candi);
        remove(val[i - range], v2cnt, candi);
        minV = min(minV, *candi.begin());
    }

    cout << minV << endl;
}


int main() {
    read();
    work();
    return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

using namespace std;
using namespace __gnu_pbds;

#define endl "\n"
#define ll long long
#define IOS ios_base::sync_with_stdio(false); cin.tie(nullptr);

const int N = 105;

set<int> st;

int main() {
    IOS;
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        int a;
        cin >> a;
        set<int> temp;
        for (int x : st) {
            temp.insert(x + a);
        }
        st.insert(a);
        for (int x : temp) {
            st.insert(x);
        }
    }
    int sum = *st.rbegin();
    int ans = 1e9;
    for (int x : st) {
        ans = min(ans, max(sum - x, x));
    }
    cout << ans << endl;


    return 0;
}
