//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
   // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            } else if (re[*itup]==es.second) {
                es.first=min(es.first,*itup);
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    int zaz=es.first;

    if ((adg[es.first]==0||pas[adg[es.first]]==pas[adg[es.first]-1]+1)&&es.second==1){
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }
        if (zaz==0) cout<<a[0]; else cout<<zaz;

        return 0;
    } else {
        cout<<es.first;
        return 0;
        int zaz = 1000000;
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }
        if (zaz!=1000000) {
            cout<<zaz;
            return 0;
        }

        if (es.first==0) cout<<a[0]; else cout<<es.first;
    }

    return 0;
}


//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
   // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            } else if (re[*itup]==es.second) {
                es.first=min(es.first,*itup);
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    int zaz=es.first;

    if ((adg[es.first]==0||pas[adg[es.first]]==pas[adg[es.first]-1]+1)&&es.second==1){
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }
        if (zaz==0) cout<<a[0]; else cout<<zaz;

        return 0;
    } else {
        cout<<es.first;
    }

    return 0;
}


//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    if (es.second==0) {
        cout<<a[n-1];
        return 0;
    } else {
        cout<<es.first;
    }

    return 0;
}


//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            } else if (re[*itup]==es.second) {
                es.first=min(es.first,*itup);
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    int zaz=es.first;

    if (adg[es.first]==0||pas[adg[es.first]]==pas[adg[es.first]-1]+1){
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }

        cout<<zaz;
        return 0;
    } else {
        cout<<es.first;
    }

    return 0;
}


//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
    //freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            } else if (re[*itup]==es.second) {
                es.first=min(es.first,*itup);
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    int zaz=es.first;

    if (adg[es.first]==0||pas[adg[es.first]]==pas[adg[es.first]-1]+1){
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }
        if (zaz==0) cout<<a[0]; else cout<<zaz;

        return 0;
    } else {
        cout<<es.first;
    }

    return 0;
}


//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
   // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    if (es.second==0) {
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                cout<<a[k+1];
                return 0;
            }
        }
        cout<<a[n-1];

        return 0;
    } else {
        cout<<es.first;
    }

    return 0;
}


//# include "stdafx.h"

# define _CRT_SECURE_NO_DEPRECATE
# include <fstream>
# include <iostream>
# include <stdlib.h>
# include <queue>
# include <string>
# include <cstdio>
# include <stdio.h>
# include <math.h>
# include <algorithm>
# include <vector>
# include <unordered_map>
# include <set>
# include <map>
# include <iomanip>
# include <math.h>
# include <stack>
# include <functional>
# define inf 9223372036854775807
# define mp make_pair
# define pb push_back
# define FOR(k,a,z) for (ll  k=a;k<z;k++)
# define pi 3.14159265359
# define maxlong long 2147483647
# define ll long long

#include <cstdio>
#include <cstring>
#define LSOne(S) (S & (-S))
#define MAXLOG 50
#include <sstream>
#include <utility>

#define P 1000000007
#define INF 1000000000

using namespace std;

vector<int> a;
set<int> gr;
int pas[100001];
unordered_map<int,int> adg;
unordered_map<int,int> re;

int main() {
   // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
    int n;
    cin>>n;
    int x;

    FOR(k,0,n) {
        cin>>x;
        a.pb(x);
        adg[x]=k;
    }

    set<int>::iterator itup,itup2;

    FOR(k,0,n) {
        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup==gr.end()) {
                if (k==0) pas[k]=1; else pas[k]=pas[k-1]+1;
        } else pas[k]=pas[k-1];
        gr.insert(a[k]);
    }

    pair<int,int> es;
    es.second=0;

    gr.clear();

    FOR(k,0,n) {

        itup = gr.upper_bound(a[k]);                 //                   ^
        if (itup!=gr.end()) {
          itup2 = gr.upper_bound(*itup);
          if (itup2==gr.end()) {
            re[*itup]++;
            if (re[*itup]>es.second) {
                es.first = *itup;
                es.second=re[*itup];
            } else if (re[*itup]==es.second) {
                es.first=min(es.first,*itup);
            }
          }
        }
        gr.insert(a[k]);
    }

    int sab;

    int zaz=es.first;

    if (es.second>1) {
        cout<<es.first;
        return 0;
    }

    if ((adg[es.first]==0||pas[adg[es.first]]==pas[adg[es.first]-1]+1)&&es.second==1){
        FOR(k,0,n-1) {
            if (pas[k]==pas[k+1]) {
                zaz=min(zaz,a[k+1]);
            }
        }
        if (zaz==0) cout<<a[0]; else cout<<zaz;

        return 0;
    } else {
         int zaz = 1000000;
            FOR(k,0,n-1) {
                if (pas[k]==pas[k+1]) {
                    zaz=min(zaz,a[k+1]);
                }
            }

        if (zaz!=1000000) {
            cout<<zaz;
            return 0;
        }

        if (es.first==0) cout<<a[0]; else cout<<es.first;
        return 0;

    }

    return 0;
}


/********************************
 *   Author : Vinayak Sachdeva  *
 *       MNIT JAIPUR, INDIA     *
 ********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <limits.h>
#include <vector>
#include <utility>
#include <queue>

using namespace std;

#define ll long long
#define rep(i, a, m) for(i = a; i < m; i++)
#define s(a); scanf("%lld", &a);
#define s2(a, b); s(a); s(b);
#define sd(a); ll a; s(a);
#define sd2(a, b); ll a, b; s2(a, b);
#define test sd(t); while(t--)
#define pin(a); printf("%lld\n", a)
#define pin2(a, b) printf("%lld %lld\n", a, b);
#define ps(s) printf("%s", s);
#define pb push_back
#define mp make_pair
#define M 100000
#define MOD 1000000007

int main() {
//	test {
		sd(n);
		ll a[n], b[n];
		ll i = 0, j = 0, k = 0, cnt = 1, cnt1 = 0, cnt2 = 0, mini = LLONG_MAX, mini2 = LLONG_MAX;
		rep(i, 0, n) {
			s(a[i]);
			mini = min(mini, a[i]);
		}
		if (n == 1) {
			pin(a[0]);
			return 0;
		} else if (n == 2) {
			pin(min(a[0], a[1]));
			return 0;
		}
		i = 0;
		while(i < n - 1 && a[i] < a[i + 1]) {
			i++;
			cnt++;
		}
		if (i == n - 1) {
			pin(a[0]);
		} else {
			rep(j, 0, n) {
				if (j == i) continue;
				b[k++] = a[j];
			}
			rep(j, 0, k - 1) {
				if (b[j] >= b[j + 1]) break;
			}
			cnt1 = j + 1;
			k = 0;
			rep(j, 0, n) {
				if (j == i + 1) continue;
				b[k++] = a[j];
			}
			rep(j, 0, k - 1) {
				if (b[j] >= b[j + 1]) break;
			}
			cnt2 = j + 1;
			if (max(cnt1, cnt2) > cnt) {
				if (cnt1 > cnt2) {
					pin(a[i]);
				} else {
					pin(a[i + 1]);
				}
			} else {
				rep(j, i + 1, n) mini2 = min(mini2, a[j]);
				pin(mini2);
			}
		}
//	}
    // your code goes here
    return 0;
}

/*

*/


/********************************
 *   Author : Vinayak Sachdeva  *
 *       MNIT JAIPUR, INDIA     *
 ********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <limits.h>
#include <vector>
#include <utility>
#include <queue>

using namespace std;

#define ll long long
#define rep(i, a, m) for(i = a; i < m; i++)
#define s(a); scanf("%lld", &a);
#define s2(a, b); s(a); s(b);
#define sd(a); ll a; s(a);
#define sd2(a, b); ll a, b; s2(a, b);
#define test sd(t); while(t--)
#define pin(a); printf("%lld\n", a)
#define pin2(a, b) printf("%lld %lld\n", a, b);
#define ps(s) printf("%s", s);
#define pb push_back
#define mp make_pair
#define M 100000
#define MOD 1000000007

int main() {
//	test {
		sd(n);
		ll a[n], b[n];
		ll i = 0, j = 0, k = 0, cnt = 0, maxcnt = 0, pos = -1, mini = LLONG_MAX, lpos = -1, maxi = 0, sec = 0;
		rep(i, 0, n) {
			s(a[i]);
			if (a[i] > maxi) {
				maxi = a[i];
			} else mini = min(mini, a[i]);
		}
		maxi = 0;
		if (n == 1) {
			pin(a[0]);
			return 0;
		} else {
			rep(i, 0, n) {
				if (a[i] > maxi) {
					cnt = 0;
					sec = maxi;
					maxi = a[i]; if (sec) sec--;
					lpos = i;
				} else if (a[i] > sec) {
					sec = a[i];
					cnt++;
				}
				if (cnt > maxcnt && cnt > 1) {
					maxcnt = cnt;
					pos = lpos;
				}
			}
			if (pos >= 0) printf("%lld\n", a[pos]);
			else printf("%lld\n", mini);
		}
//	}
    // your code goes here
    return 0;
}

/*

*/


/********************************
 *   Author : Vinayak Sachdeva  *
 *       MNIT JAIPUR, INDIA     *
 ********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <limits.h>
#include <vector>
#include <utility>
#include <queue>

using namespace std;

#define ll long long
#define rep(i, a, m) for(i = a; i < m; i++)
#define s(a); scanf("%lld", &a);
#define s2(a, b); s(a); s(b);
#define sd(a); ll a; s(a);
#define sd2(a, b); ll a, b; s2(a, b);
#define test sd(t); while(t--)
#define pin(a); printf("%lld\n", a)
#define pin2(a, b) printf("%lld %lld\n", a, b);
#define ps(s) printf("%s", s);
#define pb push_back
#define mp make_pair
#define M 100000
#define MOD 1000000007

int main() {
//	test {
		sd(n);
		ll a[n];
		ll i = 0, j = 0, mini = LLONG_MAX, mini2 = LLONG_MAX;
		rep(i, 0, n) {
			s(a[i]);
			mini = min(mini, a[i]);
		}
		if (n == 2) {
			pin(min(a[0], a[1]));
			return 0;
		}
		i = 0;
		while(i < n - 1 && a[i] < a[i + 1]) {
			i++;
		}
		if (i == n - 1) {
			pin(a[0]);
		} else if (i == 0) {
			rep(j, i + 1, n) if (a[j] >= a[j + 1]) break;
			if (j - i == 1) {
				pin(mini);
			} else {
				pin(a[0]);
			}
		} else if (i == n - 2) {
			pin(a[n - 1]);
		} else if (a[i - 1] < a[i + 1]) {
			if (a[i + 1] < a[i + 2]) {
				pin(a[i]);
			} else {
				rep(j, i + 1, n) mini2 = min(mini2, a[j]);
				pin(mini2);
			}
		} else {
			rep(j, i + 1, n) mini2 = min(mini2, a[j]);
			pin(mini2);
		}
//	}
    // your code goes here
    return 0;
}

/*

*/


/********************************
 *   Author : Vinayak Sachdeva  *
 *       MNIT JAIPUR, INDIA     *
 ********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <limits.h>
#include <vector>
#include <utility>
#include <queue>

using namespace std;

#define ll long long
#define rep(i, a, m) for(i = a; i < m; i++)
#define s(a); scanf("%lld", &a);
#define s2(a, b); s(a); s(b);
#define sd(a); ll a; s(a);
#define sd2(a, b); ll a, b; s2(a, b);
#define test sd(t); while(t--)
#define pin(a); printf("%lld\n", a)
#define pin2(a, b) printf("%lld %lld\n", a, b);
#define ps(s) printf("%s", s);
#define pb push_back
#define mp make_pair
#define M 100000
#define MOD 1000000007

int main() {
//	test {
		sd(n);
		ll a[n], b[n];
		ll i = 0, j = 0, k = 0, cnt = 0, maxcnt = 0, pos = -1, mini = LLONG_MAX, lpos = -1, maxi = 0, sec = 0;
		rep(i, 0, n) {
			s(a[i]);
			if (a[i] > maxi) {
				maxi = a[i];
			} else mini = min(mini, a[i]);
		}
		maxi = 0;
		if (n == 1) {
			pin(a[0]);
			return 0;
		} else {
			rep(i, 0, n) {
				if (a[i] > maxi) {
					cnt = 0;
					sec = maxi;
					maxi = a[i]; if (sec) sec--;
					lpos = i;
				} else if (a[i] > sec) {
					sec = a[i];
					cnt++;
				}
				if (cnt > maxcnt && cnt > 1) {
					maxcnt = cnt;
					pos = lpos;
				}
			}
			if (pos >= 0) printf("%lld\n", a[pos]);
			else if (mini != LLONG_MAX) printf("%lld\n", mini);
			else printf("%lld\n", a[0]);
		}
//	}
    // your code goes here
    return 0;
}

/*

*/


/********************************
 *   Author : Vinayak Sachdeva  *
 *       MNIT JAIPUR, INDIA     *
 ********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <limits.h>
#include <vector>
#include <utility>
#include <queue>

using namespace std;

#define ll long long
#define rep(i, a, m) for(i = a; i < m; i++)
#define s(a); scanf("%lld", &a);
#define s2(a, b); s(a); s(b);
#define sd(a); ll a; s(a);
#define sd2(a, b); ll a, b; s2(a, b);
#define test sd(t); while(t--)
#define pin(a); printf("%lld\n", a)
#define pin2(a, b) printf("%lld %lld\n", a, b);
#define ps(s) printf("%s", s);
#define pb push_back
#define mp make_pair
#define M 100000
#define MOD 1000000007

int main() {
//	test {
		sd(n);
		ll a[n], b[n];
		ll i = 0, j = 0, k = 0, cnt = 1, cnt1 = 0, cnt2 = 0, mini = LLONG_MAX, mini2 = LLONG_MAX;
		rep(i, 0, n) {
			s(a[i]);
			mini = min(mini, a[i]);
		}
		if (n == 1) {
			pin(a[0]);
			return 0;
		} else if (n == 2) {
			pin(min(a[0], a[1]));
			return 0;
		}
		i = 0;
		while(i < n - 1 && a[i] < a[i + 1]) {
			i++;
			cnt++;
		}
		if (i == n - 1) {
			pin(a[0]);
		} else {
			rep(j, 0, n) {
				if (j == i) continue;
				b[k++] = a[j];
			}
			rep(j, 0, k) {
				if (b[j] >= b[j + 1]) break;
			}
			cnt1 = j;
			k = 0;
			rep(j, 0, n) {
				if (j == i + 1) continue;
				b[k++] = a[j];
			}
			rep(j, 0, k) {
				if (b[j] >= b[j + 1]) break;
			}
			cnt2 = j;
			if (max(cnt1, cnt2) > cnt) {
				if (cnt1 > cnt2) {
					pin(a[i]);
				} else {
					pin(a[i + 1]);
				}
			} else {
				rep(j, i + 1, n) mini2 = min(mini2, a[j]);
				pin(mini2);
			}
		}
//	}
    // your code goes here
    return 0;
}

/*

*/


/********************************
 *   Author : Vinayak Sachdeva  *
 *       MNIT JAIPUR, INDIA     *
 ********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <limits.h>
#include <vector>
#include <utility>
#include <queue>

using namespace std;

#define ll long long
#define rep(i, a, m) for(i = a; i < m; i++)
#define s(a); scanf("%lld", &a);
#define s2(a, b); s(a); s(b);
#define sd(a); ll a; s(a);
#define sd2(a, b); ll a, b; s2(a, b);
#define test sd(t); while(t--)
#define pin(a); printf("%lld\n", a)
#define pin2(a, b) printf("%lld %lld\n", a, b);
#define ps(s) printf("%s", s);
#define pb push_back
#define mp make_pair
#define M 100000
#define MOD 1000000007

int main() {
//	test {
		sd(n);
		ll a[n], b[n];
		ll i = 0, j = 0, k = 0, cnt = 0, cnt1 = 0, cnt2 = 0, mini = LLONG_MAX, mini2 = LLONG_MAX;
		rep(i, 0, n) {
			s(a[i]);
			mini = min(mini, a[i]);
		}
		if (n == 1) {
			pin(a[0]);
			return 0;
		} else if (n == 2) {
			pin(min(a[0], a[1]));
			return 0;
		}
		i = 0;
		while(i < n - 1 && a[i] < a[i + 1]) {
			i++;
			cnt++;
		}
		if (i == n - 1) {
			pin(a[0]);
		} else {
			rep(j, 0, n) {
				if (j == i) continue;
				b[k++] = a[j];
			}
			rep(j, 0, k) {
				if (b[j] >= b[j + 1]) break;
			}
			cnt1 = j;
			k = 0;
			rep(j, 0, n) {
				if (j == i + 1) continue;
				b[k++] = a[j];
			}
			rep(j, 0, k) {
				if (b[j] >= b[j + 1]) break;
			}
			cnt2 = j;
			if (max(cnt1, cnt2) > cnt) {
				if (cnt1 > cnt2) {
					pin(a[i]);
				} else {
					pin(a[i + 1]);
				}
			} else {
				rep(j, i + 1, n) mini2 = min(mini2, a[j]);
				pin(mini2);
			}
		}
//	}
    // your code goes here
    return 0;
}

/*

*/


/********************************
 *   Author : Vinayak Sachdeva  *
 *       MNIT JAIPUR, INDIA     *
 ********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <limits.h>
#include <vector>
#include <utility>
#include <queue>

using namespace std;

#define ll long long
#define rep(i, a, m) for(i = a; i < m; i++)
#define s(a); scanf("%lld", &a);
#define s2(a, b); s(a); s(b);
#define sd(a); ll a; s(a);
#define sd2(a, b); ll a, b; s2(a, b);
#define test sd(t); while(t--)
#define pin(a); printf("%lld\n", a)
#define pin2(a, b) printf("%lld %lld\n", a, b);
#define ps(s) printf("%s", s);
#define pb push_back
#define mp make_pair
#define M 100000
#define MOD 1000000007

int main() {
//	test {
		sd(n);
		ll a[n];
		ll i = 0, j = 0, mini = LLONG_MAX, mini2 = LLONG_MAX;
		rep(i, 0, n) {
			s(a[i]);
			mini = min(mini, a[i]);
		}
		if (n == 2) {
			pin(min(a[0], a[1]));
			return 0;
		}
		i = 0;
		while(i < n - 1 && a[i] < a[i + 1]) {
			i++;
		}
		if (i == n - 1) {
			pin(a[0]);
		} else if (i == 0) {
			rep(j, i + 1, n) if (a[j] >= a[j + 1]) break;
			if (j - i == 1) {
				pin(mini);
			} else {
				if (a[0] < a[2]) {
					pin(a[1]);
				} else {
					pin(a[0]);
				}
			}
		} else if (i == n - 2) {
			pin(a[n - 1]);
		} else if (a[i - 1] < a[i + 1]) {
			if (a[i + 1] < a[i + 2]) {
				if (a[i] < a[i + 2]) {
					pin(a[i + 1]);
				} else {
					pin(a[i]);
				}
			} else {
				rep(j, i + 1, n) mini2 = min(mini2, a[j]);
				pin(mini2);
			}
		} else {
			rep(j, i + 1, n) mini2 = min(mini2, a[j]);
			pin(mini2);
		}
//	}
    // your code goes here
    return 0;
}

/*

*/


/********************************
 *   Author : Vinayak Sachdeva  *
 *       MNIT JAIPUR, INDIA     *
 ********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <limits.h>
#include <vector>
#include <utility>
#include <queue>

using namespace std;

#define ll long long
#define rep(i, a, m) for(i = a; i < m; i++)
#define s(a); scanf("%lld", &a);
#define s2(a, b); s(a); s(b);
#define sd(a); ll a; s(a);
#define sd2(a, b); ll a, b; s2(a, b);
#define test sd(t); while(t--)
#define pin(a); printf("%lld\n", a)
#define pin2(a, b) printf("%lld %lld\n", a, b);
#define ps(s) printf("%s", s);
#define pb push_back
#define mp make_pair
#define M 100000
#define MOD 1000000007

int main() {
//	test {
		sd(n);
		ll a[n];
		ll i = 0, j = 0, mini = LLONG_MAX, mini2 = LLONG_MAX;
		rep(i, 0, n) {
			s(a[i]);
			mini = min(mini, a[i]);
		}
		i = 0;
		while(i < n - 1 && a[i] < a[i + 1]) {
			i++;
		}
		if (i == n - 1) {
			pin(a[0]);
		} else if (i == 0) {
			rep(j, i + 1, n) if (a[j] >= a[j + 1]) break;
			if (j - i == 1) {
				pin(mini);
			} else {
				pin(a[0]);
			}
		} else if (i == n - 2) {
			pin(a[n - 1]);
		} else if (a[i - 1] < a[i + 1]) {
			if (a[i + 1] < a[i + 2]) {
				pin(a[i]);
			} else {
				rep(j, i + 1, n) mini2 = min(mini2, a[j]);
				pin(mini2);
			}
		} else {
			rep(j, i + 1, n) mini2 = min(mini2, a[j]);
			pin(mini2);
		}
//	}
    // your code goes here
    return 0;
}

/*

*/


/********************************
 *   Author : Vinayak Sachdeva  *
 *       MNIT JAIPUR, INDIA     *
 ********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <limits.h>
#include <vector>
#include <utility>
#include <queue>

using namespace std;

#define ll long long
#define rep(i, a, m) for(i = a; i < m; i++)
#define s(a); scanf("%lld", &a);
#define s2(a, b); s(a); s(b);
#define sd(a); ll a; s(a);
#define sd2(a, b); ll a, b; s2(a, b);
#define test sd(t); while(t--)
#define pin(a); printf("%lld\n", a)
#define pin2(a, b) printf("%lld %lld\n", a, b);
#define ps(s) printf("%s", s);
#define pb push_back
#define mp make_pair
#define M 100000
#define MOD 1000000007

int main() {
//	test {
		sd(n);
		ll a[n], b[n];
		ll i = 0, j = 0, k = 0, cnt = 1, cnt1 = 0, cnt2 = 0, mini = LLONG_MAX, mini2 = LLONG_MAX;
		rep(i, 0, n) {
			s(a[i]);
			mini = min(mini, a[i]);
		}
		if (n == 1) {
			pin(a[0]);
			return 0;
		} else if (n == 2) {
			pin(min(a[0], a[1]));
			return 0;
		}
		i = 0;
		while(i < n - 1 && a[i] < a[i + 1]) {
			i++;
			cnt++;
		}
		if (i == n - 1) {
			pin(a[0]);
		} else {
			rep(j, 0, n) {
				if (j == i) continue;
				b[k++] = a[j];
			}
			rep(j, 0, k - 1) {
				if (b[j] >= b[j + 1]) break;
			}
			if (j == k - 1) cnt1 = k;
			else cnt1 = j;
			k = 0;
			rep(j, 0, n) {
				if (j == i + 1) continue;
				b[k++] = a[j];
			}
			rep(j, 0, k - 1) {
				if (b[j] >= b[j + 1]) break;
			}
			if (j == k - 1) cnt2 = k;
			else cnt2 = j;
			if (max(cnt1, cnt2) > cnt) {
				if (cnt1 > cnt2) {
					pin(a[i]);
				} else {
					pin(a[i + 1]);
				}
			} else {
				rep(j, i + 1, n) mini2 = min(mini2, a[j]);
				pin(mini2);
			}
		}
//	}
    // your code goes here
    return 0;
}

/*

*/


/********************************
 *   Author : Vinayak Sachdeva  *
 *       MNIT JAIPUR, INDIA     *
 ********************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <limits.h>
#include <vector>
#include <utility>
#include <queue>

using namespace std;

#define ll long long
#define rep(i, a, m) for(i = a; i < m; i++)
#define s(a); scanf("%lld", &a);
#define s2(a, b); s(a); s(b);
#define sd(a); ll a; s(a);
#define sd2(a, b); ll a, b; s2(a, b);
#define test sd(t); while(t--)
#define pin(a); printf("%lld\n", a)
#define pin2(a, b) printf("%lld %lld\n", a, b);
#define ps(s) printf("%s", s);
#define pb push_back
#define mp make_pair
#define M 100000
#define MOD 1000000007

int main() {
//	test {
		sd(n);
		ll a[n], b[n];
		ll i = 0, j = 0, k = 0, cnt = 0, maxcnt = 0, pos = -1, mini = LLONG_MAX, lpos = -1, maxi = 0, sec = 0;
		rep(i, 0, n) {
			s(a[i]);
			mini = min(mini, a[i]);
		}
		if (n == 1) {
			pin(a[0]);
			return 0;
		} else {
			rep(i, 0, n) {
				if (a[i] > maxi) {
					cnt = 0;
					sec = maxi;
					maxi = a[i]; if (sec) sec--;
					lpos = i;
				} else if (a[i] > sec) {
					sec = a[i];
					cnt++;
				}
				if (cnt > maxcnt && cnt > 1) {
					maxcnt = cnt;
					pos = lpos;
				}
			}
			if (pos >= 0) printf("%lld\n", a[pos]);
			else printf("%lld\n", mini);
		}
//	}
    // your code goes here
    return 0;
}

/*

*/


#include <bits/stdc++.h>

using namespace std;

typedef double db;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll mINF = 1000 * 1000 * 1000 + 9;
const db ebs = 0.000001;

ll f(ll g){
    ll answ = 1;
    while(g > answ){
        answ *= 10;
    }
    return answ/10;
}

int main()
{
    int kol_vo;
    cin >> kol_vo;
    vector<int> v(kol_vo);
    for(int c=0; c<kol_vo; c++){
        cin >> v[c];
    }
    vector<int> dop(kol_vo);
    dop[0] = 0;
    for(int c=1; c<kol_vo; c++){
        int now = max(v[c-1], dop[c-1]);
        if(v[c] < now)dop[c] = now;
        else dop[c] = 0;
    }
    vector<int> used(kol_vo+1);
    for(int c=0; c<kol_vo; c++)
        used[dop[c]]++;
    int now = -1, i=-1;
    for(int c=1; c<kol_vo+1; c++){
        if(now < used[c]){
            now = used[c];
            i= c;
        }
    }
    cout << i;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef double db;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll mINF = 1000 * 1000 * 1000 + 9;
const db ebs = 0.000001;

ll f(ll g){
    ll answ = 1;
    while(g > answ){
        answ *= 10;
    }
    return answ/10;
}

int main()
{
    int kol_vo;
    cin >> kol_vo;
    vector<int> v(kol_vo);
    for(int c=0; c<kol_vo; c++){
        cin >> v[c];
    }
    vector<int> dop(kol_vo);
    dop[0] = v[0];
    for(int c=1; c<kol_vo; c++)
        dop[c] = max(dop[c-1], v[c]);
    int mmax = 1000000;
    int y = -1;
    for(int c=0; c<kol_vo; c++){
        int cur = 0;
        for(int i=c+1; i<kol_vo && v[i] < v[c] && v[i] > dop[i-1]; i++)
            cur++;
        if(mmax > cur){
            mmax = cur;
            y = c;
        }
    }
    cout << v[y];
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef double db;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll mINF = 1000 * 1000 * 1000 + 9;
const db ebs = 0.000001;

int main()
{
    int kol_vo;
    cin >> kol_vo;
    vector<int> v(kol_vo);
    for(int c=0; c<kol_vo; c++)
        cin >> v[c];
    pair<int, int> p;
    p.first = v[0];
    p.second = 0;
    vector<int> used(kol_vo+1);
    used[v[0]] = -1;
    for(int c=1; c<kol_vo; c++){
        if(v[c] > p.second){
            if(v[c] > p.first){
                p.second = p.first;
                p.first = v[c];
                used[v[c]]--;
            }
            else {
                p.second = v[c];
                used[p.first]++;
            }
        }
    }
    int mmax = -2, i = -1;
    for(int c=1; c<=kol_vo; c++){
        if(used[c] > mmax){
            mmax = used[c];
            i = c;
        }
    }
    cout << i;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef double db;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll mINF = 1000 * 1000 * 1000 + 9;
const db ebs = 0.000001;

ll f(ll g){
    ll answ = 1;
    while(g > answ){
        answ *= 10;
    }
    return answ/10;
}

int main()
{
    int kol_vo;
    cin >> kol_vo;
    vector<int> v(kol_vo);
    for(int c=0; c<kol_vo; c++){
        cin >> v[c];
    }
    vector<int> dop(kol_vo+1);
    dop[0] = 0;
    for(int c=1; c<=kol_vo; c++)
        dop[c] = max(dop[c-1], v[c-1]);
    int mmax = -10;
    int y = -1;
    for(int c=0; c<kol_vo; c++){
        int cur = 0;
        for(int i=c+1; i<kol_vo && v[i] < v[c] && v[i] > dop[c]; i++){
            if(i-c-1 && v[i] < v[i-1])break;
            cur++;
        }
        if(v[c] == dop[c+1])cur--;
        if(mmax < cur || (mmax == cur && v[y] > v[c])){
            mmax = cur;
            y = c;
        }
    }
    cout << v[y];
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef double db;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll mINF = 1000 * 1000 * 1000 + 9;
const db ebs = 0.000001;

int main()
{
    int kol_vo;
    cin >> kol_vo;
    vector<int> v(kol_vo);
    for(int c=0; c<kol_vo; c++)
        cin >> v[c];
    pair<int, int> p;
    p.first = v[0];
    p.second = 0;
    vector<int> used(kol_vo+1);
    for(int c=1; c<kol_vo; c++){
        if(v[c] > p.second){
            if(v[c] > p.first){
                p.second = p.first;
                p.first = v[c];
                used[v[c]]--;
            }
            else {
                p.second = v[c];
                used[p.first]++;
            }
        }
    }
    int mmax = -2, i = -1;
    for(int c=0; c<kol_vo+1; c++){
        if(used[c] > mmax){
            mmax = v[c];
            i = c;
        }
    }
    cout << v[i];
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef double db;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll mINF = 1000 * 1000 * 1000 + 9;
const db ebs = 0.000001;

ll f(ll g){
    ll answ = 1;
    while(g > answ){
        answ *= 10;
    }
    return answ/10;
}

int main()
{
    int kol_vo;
    cin >> kol_vo;
    vector<int> v(kol_vo);
    for(int c=0; c<kol_vo; c++){
        cin >> v[c];
    }
    vector<int> dop(kol_vo+1);
    dop[0] = 0;
    for(int c=1; c<kol_vo; c++)
        dop[c] = max(dop[c-1], v[c-1]);
    int mmax = -1;
    int y = -1;
    for(int c=0; c<kol_vo; c++){
        int cur = 0;
        for(int i=c+1; i<kol_vo && v[i] < v[c] && v[i] > dop[c]; i++){
            if(i-c-1 && v[i] < v[i-1])break;
            cur++;
        }
        if(v[c] == dop[c+1])cur--;
        if(mmax < cur || (mmax == cur && v[y] > v[c])){
            mmax = cur;
            y = c;
        }
    }
    cout << v[y];
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int width, length;
int a[100005];
int ans = INT_MAX;

int main()
{
	scanf("%d%d", &width, &length);
	for (int i = 1; i < width; i++) scanf("%d", &a[i]);

	int sum = 0;
	for (int i = 1; i <= length; i++)
	{
		sum += a[i];
	}
	if (sum < ans) ans = sum;

	for (int i = length + 1; i < width; i++)
	{
		sum = sum - a[i-length] + a[i];
		if (sum < ans) ans = sum;
	}

	printf("%d\n", ans);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int width, length;
int a[100005];
int nfmax = 0;

bool isok(int x)
{
	//cout << x << '\n';
	int i1 = 0, i2 = 1;
	int j1 = 0, j2 = 1;
	for (int i = 1; i <= x; i++)
	{
		j2++;
		while (j2 > a[j1]) j1++, j2 = 1;
	}

	while (i1 <= width && j1 <= width)
	{
		if (j1 - i1 > length) return false;

		i2++, j2++;
		while (i2 > a[i1])
		{
			i1++, i2 = 1;
			if (i1 > width) break;
		}
		while (j2 > a[j1])
		{
			j1++, j2 = 1;
			if (j1 > width) break;
		}
	}

	return true;
}

int main()
{
	cin >> width >> length;
	for (int i = 1; i < width; i++)
	{
		cin >> a[i];
		nfmax += a[i];
	}
	a[0] = 1, a[width] = 1;

	int llim = 0, ulim = nfmax, ans = 0;;
	while (llim < ulim)
	{
		int mid = (llim + ulim + 1) / 2;
		if (isok(mid)) { llim = mid; ans = mid; }
		else ulim = mid - 1;
	}

	cout << ans << '\n';

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int width, length;
int a[100005];
int nfmax = 0;

bool isok(int x)
{
	int sum = 0;
	for (int i = 1; i <= length; i++)
	{
		sum += a[i];
	}
	if (sum < x) return false;

	for (int i = length + 1; i < width; i++)
	{
		sum = sum - a[i-length] + a[i];
		if (sum < x) return false;
	}
	return true;
	/*//cout << x << '\n';
	int i1 = 0, i2 = 1;
	int j1 = 0, j2 = 1;
	for (int i = 1; i <= x; i++)
	{
		j2++;
		while (j2 > a[j1]) j1++, j2 = 1;
	}

	while (i1 <= width && j1 <= width)
	{
		if (j1 - i1 > length) return false;

		i2++, j2++;
		while (i2 > a[i1])
		{
			i1++, i2 = 1;
			if (i1 > width) break;
		}
		while (j2 > a[j1])
		{
			j1++, j2 = 1;
			if (j1 > width) break;
		}
	}*/

	return true;
}

int main()
{
	cin >> width >> length;
	for (int i = 1; i < width; i++)
	{
		cin >> a[i];
		nfmax += a[i];
	}
	a[0] = 1, a[width] = 1;

	int llim = 0, ulim = nfmax, ans = 0;;
	while (llim < ulim)
	{
		int mid = (llim + ulim + 1) / 2;
		if (isok(mid)) { llim = mid; ans = mid; }
		else ulim = mid - 1;
	}

	cout << ans << '\n';

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int width, length;
int a[100005];
int ans = INT_MAX;

int main()
{
	cin >> width >> length;
	for (int i = 1; i < width; i++)
	{
		cin >> a[i];
	}
	a[0] = 1, a[width] = 1;

	int sum = 0;
	for (int i = 1; i <= length; i++)
	{
		sum += a[i];
	}
	if (sum < ans) ans = sum;

	for (int i = length + 1; i < width; i++)
	{
		sum = sum - a[i-length] + a[i];
		if (sum < ans) ans = sum;
	}

	cout << ans << '\n';

	return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <unordered_set>

using namespace std;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int, int> int2;
const int Inf = (int)1e9+7;

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define mp make_pair
#define endl "\n"

namespace {

    const int N = (int)1e5 + 7;
    int w, l;
    int a[N];
    int f[N];

    void solve()
    {
        cin >> w >> l;
        for (int i = 1; i < w; i++) cin >> a[i];

        a[w] = (int)1e9 + 7;
        for (int i = 1; i <= l; i++) f[i] = a[i];
        for (int i = l + 1, p = 1; i <= w; i++) {
            while (a[i] > 0 && p < i) {
                int del = min(f[p], a[i]);
                f[p] -= del;
                f[i] += del;
                a[i] -= del;
                if (f[p] > 0) break;
                p++;
            }
        }
        cout << f[w] << endl;
    }

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(9);
    cout << fixed;

    // clock_t ts;
    // ts = clock();
    solve();
    // ts = clock()-ts;
    // cout << (ts*1000.0/CLOCKS_PER_SEC) << " ms" << endl;
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <unordered_set>

using namespace std;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int, int> int2;
const int Inf = (int)1e9+7;

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define mp make_pair
#define endl "\n"

namespace {

    const int N = (int)1e5 + 7;
    int w, l;
    int a[N];
    int f[N];

    void solve()
    {
        cin >> w >> l;
        for (int i = 1; i < w; i++) cin >> a[i];

        a[w] = (int)1e9 + 7;
        for (int i = 1; i <= l; i++) {
            f[i] = a[i];
            a[i] = 0;
        }

        for (int i = l + 1, p = 1; i <= w; i++) {
            while (p + l < i) p++;
            while (a[i] > 0 && p < i) {
                int del = min(f[p], a[i]);
                f[p] -= del;
                f[i] += del;
                a[i] -= del;
                if (f[p] > 0) break;
                p++;
            }
        }
        cout << f[w] << endl;
    }

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(9);
    cout << fixed;

    // clock_t ts;
    // ts = clock();
    solve();
    // ts = clock()-ts;
    // cout << (ts*1000.0/CLOCKS_PER_SEC) << " ms" << endl;
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <unordered_set>

using namespace std;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int, int> int2;
const int Inf = (int)1e9+7;

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define mp make_pair
#define endl "\n"

namespace {

    const int N = (int)1e5 + 7;
    int w, l;
    int a[N];
    int f[N];
    int ps[N];

    void solve()
    {
        cin >> w >> l;
        for (int i = 1; i < w; i++) cin >> a[i];

        f[0] = (int)1e9 + 7;
        ps[0] = f[0];
        a[w] = (int)1e9 + 7;
        for (int i = 1; i <= w; i++) {
            // int cnt = 0;
            // for (int j = 0; j < i; j++) if (i - j <= l)
            //     cnt += f[j];
            int cnt = ps[i - 1] - ps[max(0, i - l - 1)];
            f[i] = min(cnt, a[i]);
            ps[i] = ps[i - 1] + f[i];
        }
        cout << f[w] << endl;
    }

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(9);
    cout << fixed;

    // clock_t ts;
    // ts = clock();
    solve();
    // ts = clock()-ts;
    // cout << (ts*1000.0/CLOCKS_PER_SEC) << " ms" << endl;
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <unordered_set>

using namespace std;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int, int> int2;
const int Inf = (int)1e9+7;

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define mp make_pair
#define endl "\n"

namespace {

    const int N = (int)1e5 + 7;
    int w, l;
    int a[N];
    int f[N];
    int ps[N];

    void solve()
    {
        cin >> w >> l;
        for (int i = 1; i < w; i++) cin >> a[i];

        f[0] = (int)1e9 + 7;
        ps[0] = f[0];
        a[w] = (int)1e9 + 7;
        for (int i = 1; i <= w; i++) {
            // int cnt = 0;
            // for (int j = 0; j < i; j++) if (i - j <= l)
            //     cnt += f[j];
            int cnt = ps[i - 1] - ps[i - l - 1];
            f[i] = min(cnt, a[i]);
            ps[i] = ps[i - 1] + f[i];
        }
        cout << f[w] << endl;
    }

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(9);
    cout << fixed;

    // clock_t ts;
    // ts = clock();
    solve();
    // ts = clock()-ts;
    // cout << (ts*1000.0/CLOCKS_PER_SEC) << " ms" << endl;
    return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <unordered_set>

using namespace std;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int, int> int2;
const int Inf = (int)1e9+7;

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define mp make_pair
#define endl "\n"

namespace {

    const int N = (int)1e5 + 7;
    int w, l;
    int a[N];
    int f[N];
    int ps[N];

    void solve()
    {
        cin >> w >> l;
        for (int i = 1; i < w; i++) cin >> a[i];

        f[0] = (int)1e9 + 7;
        ps[0] = f[0];
        a[w] = (int)1e9 + 7;
        for (int i = 1; i <= w; i++) {
            // int cnt = 0;
            // for (int j = 0; j < i; j++) if (i - j <= l)
            //     cnt += f[j];
            int cnt = ps[i - 1] - (i - l - 1 >= 0 ? ps[i - l - 1] : 0);
            f[i] = min(cnt, a[i]);
            ps[i] = ps[i - 1] + f[i];
        }
        cout << f[w] << endl;
    }

}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(9);
    cout << fixed;

    // clock_t ts;
    // ts = clock();
    solve();
    // ts = clock()-ts;
    // cout << (ts*1000.0/CLOCKS_PER_SEC) << " ms" << endl;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
// #define MOD 1000000007
#define MOD 998244353
#define ll int
#define db double
#define endl "\n"
#define INF 1000000000000000000
#define INF1 1000000000
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define RAND mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define mp make_pair
#define debx cout<<" X "<<endl;
#define deby cout<<" Y "<<endl;
#define debz cout<<" Z "<<endl;
#define deb(x) cout << '>' << #x << ':' << x << endl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
/*
    find_by_order() - Returns an iterator to the k-th largest element (counting from zero)
    order_of_key()  - The number of items in a set that are strictly smaller than our item
*/

ll mpow(ll a, ll b){
    if(!b)
        return 1;
    ll temp=mpow(a,b/2);
    temp=(temp*temp)%MOD;
    if(b%2)
        return (a*temp)%MOD;
    return temp;
}
ll _pow(ll a, ll b){
    if(!b)
        return 1;
    ll temp=_pow(a,b/2);
    temp=(temp*temp);
    if(b%2)
        return (a*temp);
    return temp;
}
ll mod_in(ll n){
    return mpow(n,MOD-2);
}
ll cl(ll a, ll b){
    if(a%b)
        return 1+a/b;
    return a/b;
}
ll dig(ll n){
    if(!n)
        return 0;
    return 1 + dig(n/10);
}
vector<vector<ll> > multiply_matrix(vector<vector<ll> > V1, vector<vector<ll> > V2){
    vector<vector<ll> > V;
    for(ll i=0 ; i<V1.size() ; i++){
        vector<ll> V0;
        for(ll j=0 ; j<V2[0].size() ; j++){
            ll tmp = 0;
            for(ll k=0 ; k<V1.size() ; k++){
                tmp += V1[i][k] * V2[k][j];
                tmp %= MOD;
            }
            V0.pb(tmp);
        }
        V.pb(V0);
    }
    return V;
}
vector<vector<ll> > matrix_exp(vector<vector<ll> > V1, ll n){
    if(n==1)
        return V1;
    vector<vector<ll> > V2 = matrix_exp(V1,n/2);
    V2 = multiply_matrix(V2,V2);
    if(n%2==0)
        return V2;
    return multiply_matrix(V2,V1);
}
int main(){
    fastio;
    ll w,l;
    cin>>w>>l;
    ll a[w+5];
    set<pair<ll,ll> > S;
    priority_queue<pair<ll,ll> > q;
    for(ll i=1 ; i<w ; i++){
        cin>>a[i];
        if(a[i]){
            if(i<=l)
                q.push(mp(-i,a[i]));
            else
                S.insert(mp(-i,a[i]));
        }
    }
    S.insert(mp(0,0));
    ll ans = 0;
    while(!q.empty()){
        pair<ll,ll> y = q.top();
        q.pop();
        if(y.first-l<=-w){
            ans += y.second;
            continue;
        }
        pair<ll,ll> x = *S.lower_bound(mp(y.first-l,y.second));
        if(x.first<y.first){
            S.erase(x);
            ll num = min(y.second,x.second);
            q.push(mp(x.first,num));
            if(num<y.second)
                q.push(mp(y.first, y.second-num));
            if(num<x.second)
                S.insert(mp(x.first, x.second-num));
        }
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
// #define MOD 1000000007
#define MOD 998244353
#define ll int
#define db double
#define endl "\n"
#define INF 1000000000000000000
#define INF1 1000000000
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define RAND mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define mp make_pair
#define debx cout<<" X "<<endl;
#define deby cout<<" Y "<<endl;
#define debz cout<<" Z "<<endl;
#define deb(x) cout << '>' << #x << ':' << x << endl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
/*
    find_by_order() - Returns an iterator to the k-th largest element (counting from zero)
    order_of_key()  - The number of items in a set that are strictly smaller than our item
*/

ll mpow(ll a, ll b){
    if(!b)
        return 1;
    ll temp=mpow(a,b/2);
    temp=(temp*temp)%MOD;
    if(b%2)
        return (a*temp)%MOD;
    return temp;
}
ll _pow(ll a, ll b){
    if(!b)
        return 1;
    ll temp=_pow(a,b/2);
    temp=(temp*temp);
    if(b%2)
        return (a*temp);
    return temp;
}
ll mod_in(ll n){
    return mpow(n,MOD-2);
}
ll cl(ll a, ll b){
    if(a%b)
        return 1+a/b;
    return a/b;
}
ll dig(ll n){
    if(!n)
        return 0;
    return 1 + dig(n/10);
}
vector<vector<ll> > multiply_matrix(vector<vector<ll> > V1, vector<vector<ll> > V2){
    vector<vector<ll> > V;
    for(ll i=0 ; i<V1.size() ; i++){
        vector<ll> V0;
        for(ll j=0 ; j<V2[0].size() ; j++){
            ll tmp = 0;
            for(ll k=0 ; k<V1.size() ; k++){
                tmp += V1[i][k] * V2[k][j];
                tmp %= MOD;
            }
            V0.pb(tmp);
        }
        V.pb(V0);
    }
    return V;
}
vector<vector<ll> > matrix_exp(vector<vector<ll> > V1, ll n){
    if(n==1)
        return V1;
    vector<vector<ll> > V2 = matrix_exp(V1,n/2);
    V2 = multiply_matrix(V2,V2);
    if(n%2==0)
        return V2;
    return multiply_matrix(V2,V1);
}
int main(){
    fastio;
    ll w,l;
    cin>>w>>l;
    ll a[w+5];
    set<pair<ll,ll> > S;
    priority_queue<pair<ll,ll> > q;
    for(ll i=1 ; i<w ; i++){
        cin>>a[i];
        if(a[i]){
            if(i<=l)
                q.push(mp(-i,a[i]));
            else
                S.insert(mp(-i,a[i]));
        }
    }
    ll ans = 0;
    while(!q.empty()){
        pair<ll,ll> y = q.top();
        q.pop();
        if(y.first-l<=-w){
            ans += y.second;
            continue;
        }
        pair<ll,ll> x = *S.lower_bound(mp(y.first-l,y.second));
        if(x.first<y.first){
            S.erase(x);
            ll num = min(y.second,x.second);
            q.push(mp(x.first,num));
            if(num<y.second)
                q.push(mp(y.first, y.second-num));
            if(num<x.second)
                S.insert(mp(x.first, x.second-num));
        }
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#define MOD 1000000007
// #define MOD 998244353
#define ll long long
#define db double
#define endl "\n"
#define INF 1000000000000000000
#define INF1 1000000000
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define RAND mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define mp make_pair
#define debx cout<<" X "<<endl;
#define deby cout<<" Y "<<endl;
#define debz cout<<" Z "<<endl;
#define deb(x) cout << '>' << #x << ':' << x << endl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
/*
    find_by_order() - Returns an iterator to the k-th largest element (counting from zero)
    order_of_key()  - The number of items in a set that are strictly smaller than our item
*/

ll mpow(ll a, ll b){
    if(!b)
        return 1;
    ll temp=mpow(a,b/2);
    temp=(temp*temp)%MOD;
    if(b%2)
        return (a*temp)%MOD;
    return temp;
}
ll _pow(ll a, ll b){
    if(!b)
        return 1;
    ll temp=_pow(a,b/2);
    temp=(temp*temp);
    if(b%2)
        return (a*temp);
    return temp;
}
ll mod_in(ll n){
    return mpow(n,MOD-2);
}
ll cl(ll a, ll b){
    if(a%b)
        return 1+a/b;
    return a/b;
}
ll dig(ll n){
    if(!n)
        return 0;
    return 1 + dig(n/10);
}
vector<vector<ll> > multiply_matrix(vector<vector<ll> > V1, vector<vector<ll> > V2){
    vector<vector<ll> > V;
    for(ll i=0 ; i<V1.size() ; i++){
        vector<ll> V0;
        for(ll j=0 ; j<V2[0].size() ; j++){
            ll tmp = 0;
            for(ll k=0 ; k<V1.size() ; k++){
                tmp += V1[i][k] * V2[k][j];
                tmp %= MOD;
            }
            V0.pb(tmp);
        }
        V.pb(V0);
    }
    return V;
}
vector<vector<ll> > matrix_exp(vector<vector<ll> > V1, ll n){
    if(n==1)
        return V1;
    vector<vector<ll> > V2 = matrix_exp(V1,n/2);
    V2 = multiply_matrix(V2,V2);
    if(n%2==0)
        return V2;
    return multiply_matrix(V2,V1);
}
int main(){
    fastio;
    ll w,l;
    cin>>w>>l;
    ll a[w+5], b[w+5]={0}, dp[w+5]={0},s=0;
    for(ll i=1 ; i<w ; i++){
        cin>>a[i];
        s += a[i];
        if(i<=l)
            b[i] = a[i];
        if(i==l)
            dp[i] = s;
    }
    ll lo = 0;
    for(ll i=l+1 ; i<=w ; i++){
        ll x = 0;
        lo = max(lo, i-l);
        while(a[i]&&lo<i){
            ll y = min(b[lo],a[i]);
            x += y;
            // cout<<i<<" "<<lo<<" "<<x<<endl;
            a[i] -= y;
            b[lo] -= y;
            lo++;
        }
        lo--;
        b[i] = x;
        // for(ll i=1 ; i<w ; i++)
        //     cout<<b[i]<<" ";
        // cout<<endl;
        dp[i] = dp[i-1] - b[i-l];
        // cout<<i<<" "<<dp[i]<<endl;
        if(i==w)
            cout<<dp[i];
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#define MOD 1000000007
// #define MOD 998244353
#define ll long long
#define db double
#define endl "\n"
#define INF 1000000000000000000
#define INF1 1000000000
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define RAND mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define mp make_pair
#define debx cout<<" X "<<endl;
#define deby cout<<" Y "<<endl;
#define debz cout<<" Z "<<endl;
#define deb(x) cout << '>' << #x << ':' << x << endl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
/*
    find_by_order() - Returns an iterator to the k-th largest element (counting from zero)
    order_of_key()  - The number of items in a set that are strictly smaller than our item
*/

ll mpow(ll a, ll b){
    if(!b)
        return 1;
    ll temp=mpow(a,b/2);
    temp=(temp*temp)%MOD;
    if(b%2)
        return (a*temp)%MOD;
    return temp;
}
ll _pow(ll a, ll b){
    if(!b)
        return 1;
    ll temp=_pow(a,b/2);
    temp=(temp*temp);
    if(b%2)
        return (a*temp);
    return temp;
}
ll mod_in(ll n){
    return mpow(n,MOD-2);
}
ll cl(ll a, ll b){
    if(a%b)
        return 1+a/b;
    return a/b;
}
ll dig(ll n){
    if(!n)
        return 0;
    return 1 + dig(n/10);
}
vector<vector<ll> > multiply_matrix(vector<vector<ll> > V1, vector<vector<ll> > V2){
    vector<vector<ll> > V;
    for(ll i=0 ; i<V1.size() ; i++){
        vector<ll> V0;
        for(ll j=0 ; j<V2[0].size() ; j++){
            ll tmp = 0;
            for(ll k=0 ; k<V1.size() ; k++){
                tmp += V1[i][k] * V2[k][j];
                tmp %= MOD;
            }
            V0.pb(tmp);
        }
        V.pb(V0);
    }
    return V;
}
vector<vector<ll> > matrix_exp(vector<vector<ll> > V1, ll n){
    if(n==1)
        return V1;
    vector<vector<ll> > V2 = matrix_exp(V1,n/2);
    V2 = multiply_matrix(V2,V2);
    if(n%2==0)
        return V2;
    return multiply_matrix(V2,V1);
}
int main(){
    fastio;
    ll w,l;
    cin>>w>>l;
    ll a[w+5];
    set<pair<ll,ll> > S;
    priority_queue<pair<ll,ll> > q;
    for(ll i=1 ; i<w ; i++){
        cin>>a[i];
        if(a[i]){
            // if(i<=l)
            //     q.push(mp(-i,a[i]));
            // else
                S.insert(mp(-i,a[i]));
                // deb(i)
        }
    }
    q.push(mp(0,INF));
    S.insert(mp(0,0));
    ll ans = 0;
    while(!q.empty()){
        pair<ll,ll> y = q.top();
        q.pop();
        if(y.first-l<=-w){
            ans += y.second;
            continue;
        }
        pair<ll,ll> x = *S.lower_bound(mp(y.first-l,-INF));
        ll num = min(y.second,x.second);
        // cout<<-y.first<<" "<<y.second<<"   "<<-x.first<<" "<<x.second<<" "<<num<<endl;
        if(x.first<y.first){
            S.erase(x);
            ll num = min(y.second,x.second);
            q.push(mp(x.first,num));
            if(num<y.second)
                q.push(mp(y.first, y.second-num));
            if(num<x.second)
                S.insert(mp(x.first, x.second-num));
        }
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
#define MOD 1000000007
// #define MOD 998244353
#define ll int
#define db double
#define endl "\n"
#define INF 1000000000000000000
#define INF1 1000000000
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define RAND mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pb push_back
#define mp make_pair
#define debx cout<<" X "<<endl;
#define deby cout<<" Y "<<endl;
#define debz cout<<" Z "<<endl;
#define deb(x) cout << '>' << #x << ':' << x << endl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> pbds;
/*
    find_by_order() - Returns an iterator to the k-th largest element (counting from zero)
    order_of_key()  - The number of items in a set that are strictly smaller than our item
*/

ll mpow(ll a, ll b){
    if(!b)
        return 1;
    ll temp=mpow(a,b/2);
    temp=(temp*temp)%MOD;
    if(b%2)
        return (a*temp)%MOD;
    return temp;
}
ll _pow(ll a, ll b){
    if(!b)
        return 1;
    ll temp=_pow(a,b/2);
    temp=(temp*temp);
    if(b%2)
        return (a*temp);
    return temp;
}
ll mod_in(ll n){
    return mpow(n,MOD-2);
}
ll cl(ll a, ll b){
    if(a%b)
        return 1+a/b;
    return a/b;
}
ll dig(ll n){
    if(!n)
        return 0;
    return 1 + dig(n/10);
}
vector<vector<ll> > multiply_matrix(vector<vector<ll> > V1, vector<vector<ll> > V2){
    vector<vector<ll> > V;
    for(ll i=0 ; i<V1.size() ; i++){
        vector<ll> V0;
        for(ll j=0 ; j<V2[0].size() ; j++){
            ll tmp = 0;
            for(ll k=0 ; k<V1.size() ; k++){
                tmp += V1[i][k] * V2[k][j];
                tmp %= MOD;
            }
            V0.pb(tmp);
        }
        V.pb(V0);
    }
    return V;
}
vector<vector<ll> > matrix_exp(vector<vector<ll> > V1, ll n){
    if(n==1)
        return V1;
    vector<vector<ll> > V2 = matrix_exp(V1,n/2);
    V2 = multiply_matrix(V2,V2);
    if(n%2==0)
        return V2;
    return multiply_matrix(V2,V1);
}
int main(){
    fastio;
    ll w,l;
    cin>>w>>l;
    ll a[w+5];
    set<pair<ll,ll> > S;
    priority_queue<pair<ll,ll> > q;
    for(ll i=1 ; i<w ; i++){
        cin>>a[i];
        if(a[i]){
            if(i<=l)
                q.push(mp(-i,a[i]));
            else
                S.insert(mp(-i,a[i]));
                // deb(i)
        }
    }
    // q.push(mp(0,INF));
    S.insert(mp(0,0));
    ll ans = 0;
    while(!q.empty()){
        pair<ll,ll> y = q.top();
        q.pop();
        if(y.first-l<=-w){
            ans += y.second;
            continue;
        }
        pair<ll,ll> x = *S.lower_bound(mp(y.first-l,-1));
        ll num = min(y.second,x.second);
        // cout<<-y.first<<" "<<y.second<<"   "<<-x.first<<" "<<x.second<<" "<<num<<endl;
        if(x.first<y.first){
            S.erase(x);
            ll num = min(y.second,x.second);
            q.push(mp(x.first,num));
            if(num<y.second)
                q.push(mp(y.first, y.second-num));
            if(num<x.second)
                S.insert(mp(x.first, x.second-num));
        }
    }
    cout<<ans;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-1)
                break;
            l=path.size();
            for(j=min(i+k,n-2);j>=max(i,0);j--){
                if(arr[j]) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=INT_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
    }
    l1:;
    cout<<ans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-1)
                break;
            l=path.size();
            for(j=min(i+k,n-2);j>=max(i,0);j--){
                if(arr[j]>0) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=INT_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
    }
    l1:;
    cout<<ans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define all(x) x.begin(),x.end()
#define prec(x) fixed<<setprecision(x)
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    val=0;
    for(i=0;i<k;i++)
        val+=arr[i];
    for(;i<n;i++){
        val+=arr[i];
        val-=arr[i-l];
        minans=min(minans,val);
    }
    cout<<minans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-1)
                break;
            l=path.size();
            for(j=i+k;j>=i&&j>=0;j--){
                if(arr[j]>0) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=INT_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
        path.clear();
    }
    l1:;
    cout<<ans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define all(x) x.begin(),x.end()
#define prec(x) fixed<<setprecision(x)
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    val=0;
    for(i=0;i<k;i++)
        val+=arr[i];
    minans=val;
    for(;i<n;i++){
        val+=arr[i];
        val-=arr[i-k];
        minans=min(minans,val);
    }
    cout<<minans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define all(x) x.begin(),x.end()
#define prec(x) fixed<<setprecision(x)
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    val=0;
    for(i=0;i<k;i++)
        val+=arr[i];
    minans=val;
    for(;i<n-1;i++){
        val+=arr[i];
        val-=arr[i-k];
        minans=min(minans,val);
    }
    cout<<minans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define all(x) x.begin(),x.end()
#define prec(x) fixed<<setprecision(x)
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    val=0;
    for(i=0;i<=k;i++)
        val+=arr[i];
    for(;i<n;i++){
        val+=arr[i];
        val-=arr[i-k];
        minans=min(minans,val);
    }
    cout<<minans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-1)
                break;
            l=path.size();
            for(j=min(i+k,n-1);j>=max(i,(ll)0);j--){
                if(arr[j]) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=LLONG_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
    }
    l1:;
    cout<<ans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define all(x) x.begin(),x.end()
#define prec(x) fixed<<setprecision(x)
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    val=0;
    for(i=0;i<k;i++)
        val+=arr[i];
    minans=val;
    for(;i<n;i++){
        val+=arr[i];
        val-=arr[i-l];
        minans=min(minans,val);
    }
    cout<<minans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define all(x) x.begin(),x.end()
#define prec(x) fixed<<setprecision(x)
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    val=0;
    for(i=0;i<l;i++)
        val+=arr[i];
    minans=val;
    for(;i<n;i++){
        val+=arr[i];
        val-=arr[i-l];
        minans=min(minans,val);
    }
    cout<<minans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-1)
                break;
            l=path.size();
            for(j=i+k;j>=max(i,0);j--){
                if(arr[j]>0) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=INT_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
        path.clear();
    }
    l1:;
    cout<<ans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-1)
                break;
            l=path.size();
            for(j=i+k;j>=i;j--){
                if(arr[j]>0) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=INT_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
        path.clear();
    }
    l1:;
    cout<<ans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-1)
                break;
            l=path.size();
            for(j=i+k;j>=max(i,(ll)0);j--){
                if(arr[j]) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=LLONG_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
    }
    l1:;
    cout<<ans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-2)
                break;
            l=path.size();
            for(j=min(i+k,n-2);j>=max(i,0);j--){
                if(arr[j]>0) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=INT_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
    }
    l1:;
    cout<<ans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-1)
                break;
            l=path.size();
            for(j=min(i+k,n-1);j>=max(i,(ll)0);j--){
                if(arr[j]) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=LLONG_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
    }
    l1:;
    cout<<ans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-1)
                break;
            l=path.size();
            for(j=min(i+k,n-1);j>=max(i,0);j--){
                if(arr[j]) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=INT_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
    }
    l1:;
    cout<<ans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-1)
                break;
            l=path.size();
            for(j=i+k;j>=i;j--){
                if(arr[j]) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=LLONG_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
    }
    l1:;
    cout<<ans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define all(x) x.begin(),x.end()
#define prec(x) fixed<<setprecision(x)
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    val=0;
    for(i=0;i<=k;i++)
        val+=arr[i];
    minans=val;
    for(;i<n;i++){
        val+=arr[i];
        val-=arr[i-k];
        minans=min(minans,val);
    }
    cout<<minans;
    return 0;
}


//Sinhov : "In Elon, we Musk."
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define mp make_pair
int main() {
    ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
    ll q,i, l,n,m, t,r, k, j, x,b1,b2,last=0, y, z, p, v, val = 0, minans, a, b, c, d,u,flag,ans=0;
    cin>>n>>k;
    vector<ll>arr(n-1);
    for(i=0;i<n-1;i++)
        cin>>arr[i];
    while(true){
        vector<ll>path;
        i=-1;
        while(true){
            if(i+k>=n-1)
                break;
            l=path.size();
            for(j=i+k;j>i&&j>=0;j--){
                if(arr[j]>0) {
                    path.pb(j);
                    break;
                }
            }
            if(path.size()==l){
                goto l1;
            }
            i=j;
        }
        val=INT_MAX;
        for(auto e:path)
            val=min(val,arr[e]);
        for(auto e:path)
            arr[e]-=val;
        ans+=val;
        path.clear();
    }
    l1:;
    cout<<ans;
    return 0;
}


#include <bits/stdc++.h>
#include <cmath>

using namespace std;

#define rep(i, n) for(ll i = 0; i < (ll)n; i++)
#define repi(i, a, n) for(ll i = a; i < (ll)n; i++)
#define repii(i, a, n, b) for(ll i = a; i < (ll)n; i+=b)
#define pb push_back
#define vi vector <int>
#define vll vector <long long>
#define vpll vector <pair <long long, long long> >
#define all(v) v.begin(), v.end()
#define M_PI 3.14159265358979323846
#define in cin
#define bs binary_search

typedef long long ll;
const long long max_sz = 5e5 + 5;
const long long mod = 1e9 + 7;
const long long MAXN = 1e18 + 1;

ll gcd(ll a, ll b){
	if(b == 0) return a;
	return gcd(b, a%b);
}

void print(vll v){
	rep(i, v.size()) cout << v[i] << " ";
	cout << endl;
}

vll div(ll n){
	vll v;
	repi(i, 1, sqrt(n)+1){
		if((n%i) == 0){
			if(i == n/i) v.pb(i);
			else v.pb(i), v.pb(n/i);}
	}
	return v;
}

ll powmod(ll a, ll b, ll c){
	if(b == 0) return 1%c;
	else if(b % 2 == 0) return powmod((a*a)%c, b/2, c);
	return (a*powmod((a*a)%c, (b-1)/2, c))%c;
}

void printp(vpll v){
	rep(i, v.size()) cout << v[i].first << " " << v[i].second << endl;
}

ll min(ll a, ll b){
	return a < b ? a : b;
}

ll max(ll a, ll b){
	return a > b ? a : b;
}

ll min3(ll a, ll b, ll c){
	return min(min(a, b), c);
}

void print_map(map <ll, ll> m){
	for(auto it : m) cout << it.first << " " << it.second << "\n";
}

void print_set(set <ll> s){
	for(auto it : s) cout << it << " ";
	cout << endl;
}

ll inv(ll a){
	return powmod(a, mod-2, mod);
}

bool prime[max_sz];
// Sieve of erathosthenes
void sieve(){
	memset(prime, true, sizeof(prime));
	for(ll i = 2; i * i < max_sz; i++){
		if(prime[i] == true){
			for(ll j = i*i ; j < max_sz; j+=i){
				prime[j] = false;
			}
		}
	}
}

vll pow(ll a){
	vll res;
	ll temp = 1;
	while(temp <= MAXN){
		res.pb(temp);
		temp *= a;
	}
	return res;
}

vll v(max_sz), powers(max_sz), inverse(max_sz);
void fact(){
	v[0] = 1;
	repi(i, 1, max_sz) v[i] = (v[i-1]*i)%mod;
	rep(i, max_sz) inverse[i] = powmod(v[i], mod - 2, mod);
}

// Obtain the binomial coefficients modulo mod
ll binom(ll n, ll p){
	if(n < p || p < 0) return 0;
	// ll tmp1 = powmod(v[p], mod-2, mod);
	// ll tmp2 = powmod(v[n - p], mod-2, mod);
	ll d = ((inverse[p] * inverse[n - p])%mod * v[n]) % mod;
	return d;
}

//Obtain the smallest x such that ax = 1 mod m and (a, m) = 1
ll modinv(ll a, ll m){
    ll m0 = m, y = 0, x = 1;
    if (m == 1) return 0;
    while (a > 1){
        ll q = a / m, t = m;
        m = a % m, a = t;
        t = y, y = x - q * y, x = t;
    }
    if (x < 0) x += m0;
    return x;
}

void swap(ll &a, ll &b){
	if(a > b){
		ll temp = a;
		a = b;
		b = temp;
	}
}

ll spf[max_sz];
//Compute the smallest prime factor of all numbers
void smallest_prime(){
    spf[1] = 1;
    for(ll i = 2; i <= max_sz; i++) spf[i] = i;
    for (ll i = 4; i <= max_sz; i += 2) spf[i] = 2;
    for (ll i = 3; i * i <= max_sz; i++){
        if (spf[i] == i){
            for (ll j= i * i; j <= max_sz; j+=i)
                if (spf[j] == j) spf[j] = i;
        }
    }
}

//Prime factorise a number in O(log n) time
vll factorise(ll x){
    vector<ll> res;
    while (x != 1){
        res.push_back(spf[x]);
        x = x / spf[x];
    }
		return res;
}

//Obtain the digits in the decimal 10 representation of number n
vll digits(ll n){
	vll res;
	while(n != 0){
		res.pb(n % 10);
		n /= 10;
	}
	// sort(all(res));
	return res;
}

//Check if a is a suffix of b
ll suffix(string a, string b){
	if(a.size() > b.size()) return 0;
	for(ll i = a.size() - 1; i >= 0; --i){
		if(b[b.size() - 1 - (a.size() - 1 - i)]) return 0;
	}
	return 1;
}

ll inc(vll a){
	rep(i, a.size() - 1) if(a[i + 1] < a[i]) return 0;
	return 1;
}

ll dec(vll a){
	rep(i, a.size() - 1) if(a[i + 1] > a[i]) return 0;
	return 1;
}

ll check(ll n){
	repi(i, 2, sqrt(n) + 1) if(n % i == 0) return 0;
	return 1;
}

ll n;

int main(){
		ll t = 1;
		// cin >> t;
		while(t--){
				ll w, l;
				cin >> w >> l;
				vll a(w);
				repi(i, 1, w) cin >> a[i];
				vll pref(w + 1, 0);
				repi(i, 1, w) pref[i] = pref[i - 1] + a[i];
				// repi(i, 1, w) cout << pref[i] << " ";
				// cout << endl;
				ll ans = MAXN;
				ll mx = (w - 1) / l;
				repi(i, 1, mx + 1){
					ll start = 1 + (i - 1) * l, end = start + l - 1;
					ans = min(ans, pref[end] - pref[start - 1]);
				}
				repi(i, 1, mx + 1){
					ll end = w - 1 - (i - 1) * l, start = end - l + 1;
					ans = min(ans, pref[end] - pref[start - 1]);
				}
				cout << ans << endl;
		}
		return 0;
}


#include <bits/stdc++.h>
#include <cmath>

using namespace std;

#define rep(i, n) for(ll i = 0; i < (ll)n; i++)
#define repi(i, a, n) for(ll i = a; i < (ll)n; i++)
#define repii(i, a, n, b) for(ll i = a; i < (ll)n; i+=b)
#define pb push_back
#define vi vector <int>
#define vll vector <long long>
#define vpll vector <pair <long long, long long> >
#define all(v) v.begin(), v.end()
#define M_PI 3.14159265358979323846
#define in cin
#define bs binary_search

typedef long long ll;
const long long max_sz = 5e5 + 5;
const long long mod = 1e9 + 7;
const long long MAXN = 1e18 + 1;

ll gcd(ll a, ll b){
	if(b == 0) return a;
	return gcd(b, a%b);
}

void print(vll v){
	rep(i, v.size()) cout << v[i] << " ";
	cout << endl;
}

vll div(ll n){
	vll v;
	repi(i, 1, sqrt(n)+1){
		if((n%i) == 0){
			if(i == n/i) v.pb(i);
			else v.pb(i), v.pb(n/i);}
	}
	return v;
}

ll powmod(ll a, ll b, ll c){
	if(b == 0) return 1%c;
	else if(b % 2 == 0) return powmod((a*a)%c, b/2, c);
	return (a*powmod((a*a)%c, (b-1)/2, c))%c;
}

void printp(vpll v){
	rep(i, v.size()) cout << v[i].first << " " << v[i].second << endl;
}

ll min(ll a, ll b){
	return a < b ? a : b;
}

ll max(ll a, ll b){
	return a > b ? a : b;
}

ll min3(ll a, ll b, ll c){
	return min(min(a, b), c);
}

void print_map(map <ll, ll> m){
	for(auto it : m) cout << it.first << " " << it.second << "\n";
}

void print_set(set <ll> s){
	for(auto it : s) cout << it << " ";
	cout << endl;
}

ll inv(ll a){
	return powmod(a, mod-2, mod);
}

bool prime[max_sz];
// Sieve of erathosthenes
void sieve(){
	memset(prime, true, sizeof(prime));
	for(ll i = 2; i * i < max_sz; i++){
		if(prime[i] == true){
			for(ll j = i*i ; j < max_sz; j+=i){
				prime[j] = false;
			}
		}
	}
}

vll pow(ll a){
	vll res;
	ll temp = 1;
	while(temp <= MAXN){
		res.pb(temp);
		temp *= a;
	}
	return res;
}

vll v(max_sz), powers(max_sz), inverse(max_sz);
void fact(){
	v[0] = 1;
	repi(i, 1, max_sz) v[i] = (v[i-1]*i)%mod;
	rep(i, max_sz) inverse[i] = powmod(v[i], mod - 2, mod);
}

// Obtain the binomial coefficients modulo mod
ll binom(ll n, ll p){
	if(n < p || p < 0) return 0;
	// ll tmp1 = powmod(v[p], mod-2, mod);
	// ll tmp2 = powmod(v[n - p], mod-2, mod);
	ll d = ((inverse[p] * inverse[n - p])%mod * v[n]) % mod;
	return d;
}

//Obtain the smallest x such that ax = 1 mod m and (a, m) = 1
ll modinv(ll a, ll m){
    ll m0 = m, y = 0, x = 1;
    if (m == 1) return 0;
    while (a > 1){
        ll q = a / m, t = m;
        m = a % m, a = t;
        t = y, y = x - q * y, x = t;
    }
    if (x < 0) x += m0;
    return x;
}

void swap(ll &a, ll &b){
	if(a > b){
		ll temp = a;
		a = b;
		b = temp;
	}
}

ll spf[max_sz];
//Compute the smallest prime factor of all numbers
void smallest_prime(){
    spf[1] = 1;
    for(ll i = 2; i <= max_sz; i++) spf[i] = i;
    for (ll i = 4; i <= max_sz; i += 2) spf[i] = 2;
    for (ll i = 3; i * i <= max_sz; i++){
        if (spf[i] == i){
            for (ll j= i * i; j <= max_sz; j+=i)
                if (spf[j] == j) spf[j] = i;
        }
    }
}

//Prime factorise a number in O(log n) time
vll factorise(ll x){
    vector<ll> res;
    while (x != 1){
        res.push_back(spf[x]);
        x = x / spf[x];
    }
		return res;
}

//Obtain the digits in the decimal 10 representation of number n
vll digits(ll n){
	vll res;
	while(n != 0){
		res.pb(n % 10);
		n /= 10;
	}
	// sort(all(res));
	return res;
}

//Check if a is a suffix of b
ll suffix(string a, string b){
	if(a.size() > b.size()) return 0;
	for(ll i = a.size() - 1; i >= 0; --i){
		if(b[b.size() - 1 - (a.size() - 1 - i)]) return 0;
	}
	return 1;
}

ll inc(vll a){
	rep(i, a.size() - 1) if(a[i + 1] < a[i]) return 0;
	return 1;
}

ll dec(vll a){
	rep(i, a.size() - 1) if(a[i + 1] > a[i]) return 0;
	return 1;
}

ll check(string a,string b){
	ll n = a.size();
	ll counter = 0;
	rep(i, n) if(a[i] != b[i]) counter++;
	if(counter > 1) return 0;
	return 1;
}

const ll nax = 1e6 + 1;

int main(){
		ll t = 1;
		// cin >> t;
		while(t--){
			ll w, l;
			cin >> w >> l;
			vll a(w);
			repi(i, 1, w) cin >> a[i];
			a[w] = MAXN;
			vll dp(w + 1, 0), sum(w + 1, 0);
			repi(i, 1, l + 1) {dp[i] = a[i];}
			repi(i, 1, w + 1) sum[i] = sum[i - 1] + dp[i];
			if(sum[w] == 0) {cout << 0; return 0;}
			repi(i, l + 1, w + 1){
					if(a[i] == 0) {
						sum[i] = sum[i - 1];
						continue;
					}
					ll start = max(0, i - l - 1);
					ll sm = sum[i - 1] - sum[start];
					if(sm < a[i]) dp[i] = sm, sum[i] = sum[i - 1] + dp[i];
					else dp[i] = a[i], sum[i] = sum[i - 1] + dp[i];
			}
			// repi(i, 1, w + 1) cout << sum[i] << " ";
			// cout << endl;
			// repi(i, 1, w + 1) cout << dp[i] << " ";
			// cout << endl;
			cout << dp[w] << endl;
}
		return 0;
	}


#include <bits/stdc++.h>
#include <cmath>

using namespace std;

#define rep(i, n) for(ll i = 0; i < (ll)n; i++)
#define repi(i, a, n) for(ll i = a; i < (ll)n; i++)
#define repii(i, a, n, b) for(ll i = a; i < (ll)n; i+=b)
#define pb push_back
#define vi vector <int>
#define vll vector <long long>
#define vpll vector <pair <long long, long long> >
#define all(v) v.begin(), v.end()
#define M_PI 3.14159265358979323846
#define in cin
#define bs binary_search

typedef long long ll;
const long long max_sz = 5e5 + 5;
const long long mod = 1e9 + 7;
const long long MAXN = 1e18 + 1;

ll gcd(ll a, ll b){
	if(b == 0) return a;
	return gcd(b, a%b);
}

void print(vll v){
	rep(i, v.size()) cout << v[i] << " ";
	cout << endl;
}

vll div(ll n){
	vll v;
	repi(i, 1, sqrt(n)+1){
		if((n%i) == 0){
			if(i == n/i) v.pb(i);
			else v.pb(i), v.pb(n/i);}
	}
	return v;
}

ll powmod(ll a, ll b, ll c){
	if(b == 0) return 1%c;
	else if(b % 2 == 0) return powmod((a*a)%c, b/2, c);
	return (a*powmod((a*a)%c, (b-1)/2, c))%c;
}

void printp(vpll v){
	rep(i, v.size()) cout << v[i].first << " " << v[i].second << endl;
}

ll min(ll a, ll b){
	return a < b ? a : b;
}

ll max(ll a, ll b){
	return a > b ? a : b;
}

ll min3(ll a, ll b, ll c){
	return min(min(a, b), c);
}

void print_map(map <ll, ll> m){
	for(auto it : m) cout << it.first << " " << it.second << "\n";
}

void print_set(set <ll> s){
	for(auto it : s) cout << it << " ";
	cout << endl;
}

ll inv(ll a){
	return powmod(a, mod-2, mod);
}

bool prime[max_sz];
// Sieve of erathosthenes
void sieve(){
	memset(prime, true, sizeof(prime));
	for(ll i = 2; i * i < max_sz; i++){
		if(prime[i] == true){
			for(ll j = i*i ; j < max_sz; j+=i){
				prime[j] = false;
			}
		}
	}
}

vll pow(ll a){
	vll res;
	ll temp = 1;
	while(temp <= MAXN){
		res.pb(temp);
		temp *= a;
	}
	return res;
}

vll v(max_sz), powers(max_sz), inverse(max_sz);
void fact(){
	v[0] = 1;
	repi(i, 1, max_sz) v[i] = (v[i-1]*i)%mod;
	rep(i, max_sz) inverse[i] = powmod(v[i], mod - 2, mod);
}

// Obtain the binomial coefficients modulo mod
ll binom(ll n, ll p){
	if(n < p || p < 0) return 0;
	// ll tmp1 = powmod(v[p], mod-2, mod);
	// ll tmp2 = powmod(v[n - p], mod-2, mod);
	ll d = ((inverse[p] * inverse[n - p])%mod * v[n]) % mod;
	return d;
}

//Obtain the smallest x such that ax = 1 mod m and (a, m) = 1
ll modinv(ll a, ll m){
    ll m0 = m, y = 0, x = 1;
    if (m == 1) return 0;
    while (a > 1){
        ll q = a / m, t = m;
        m = a % m, a = t;
        t = y, y = x - q * y, x = t;
    }
    if (x < 0) x += m0;
    return x;
}

void swap(ll &a, ll &b){
	if(a > b){
		ll temp = a;
		a = b;
		b = temp;
	}
}

ll spf[max_sz];
//Compute the smallest prime factor of all numbers
void smallest_prime(){
    spf[1] = 1;
    for(ll i = 2; i <= max_sz; i++) spf[i] = i;
    for (ll i = 4; i <= max_sz; i += 2) spf[i] = 2;
    for (ll i = 3; i * i <= max_sz; i++){
        if (spf[i] == i){
            for (ll j= i * i; j <= max_sz; j+=i)
                if (spf[j] == j) spf[j] = i;
        }
    }
}

//Prime factorise a number in O(log n) time
vll factorise(ll x){
    vector<ll> res;
    while (x != 1){
        res.push_back(spf[x]);
        x = x / spf[x];
    }
		return res;
}

//Obtain the digits in the decimal 10 representation of number n
vll digits(ll n){
	vll res;
	while(n != 0){
		res.pb(n % 10);
		n /= 10;
	}
	// sort(all(res));
	return res;
}

//Check if a is a suffix of b
ll suffix(string a, string b){
	if(a.size() > b.size()) return 0;
	for(ll i = a.size() - 1; i >= 0; --i){
		if(b[b.size() - 1 - (a.size() - 1 - i)]) return 0;
	}
	return 1;
}

ll inc(vll a){
	rep(i, a.size() - 1) if(a[i + 1] < a[i]) return 0;
	return 1;
}

ll dec(vll a){
	rep(i, a.size() - 1) if(a[i + 1] > a[i]) return 0;
	return 1;
}

ll check(ll n){
	repi(i, 2, sqrt(n) + 1) if(n % i == 0) return 0;
	return 1;
}

ll n;

int main(){
		ll t = 1;
		// cin >> t;
		while(t--){
				ll w, l;
				cin >> w >> l;
				vll a(w);
				repi(i, 1, w) cin >> a[i];
				vll pref(w + 1, 0);
				repi(i, 1, w) pref[i] = pref[i - 1] + a[i];
				// repi(i, 1, w) cout << pref[i] << " ";
				// cout << endl;
				ll ans = MAXN;
				ll mx = (w - 1);
				repi(i, 1, w){
					ll start = 1 + (i - 1), end = start + l - 1;
					if(end < w) ans = min(ans, pref[end] - pref[start - 1]);
				}
				repi(i, 1, w){
					ll end = w - 1 - (i - 1), start = end - l + 1;
					if(start > 0) ans = min(ans, pref[end] - pref[start - 1]);
				}
				cout << ans << endl;
		}
		return 0;
}


#include <bits/stdc++.h>
#include <cmath>

using namespace std;

#define rep(i, n) for(ll i = 0; i < (ll)n; i++)
#define repi(i, a, n) for(ll i = a; i < (ll)n; i++)
#define repii(i, a, n, b) for(ll i = a; i < (ll)n; i+=b)
#define pb push_back
#define vi vector <int>
#define vll vector <long long>
#define vpll vector <pair <long long, long long> >
#define all(v) v.begin(), v.end()
#define M_PI 3.14159265358979323846
#define in cin
#define bs binary_search

typedef long long ll;
const long long max_sz = 5e5 + 5;
const long long mod = 1e9 + 7;
const long long MAXN = 1e18 + 1;

ll gcd(ll a, ll b){
	if(b == 0) return a;
	return gcd(b, a%b);
}

void print(vll v){
	rep(i, v.size()) cout << v[i] << " ";
	cout << endl;
}

vll div(ll n){
	vll v;
	repi(i, 1, sqrt(n)+1){
		if((n%i) == 0){
			if(i == n/i) v.pb(i);
			else v.pb(i), v.pb(n/i);}
	}
	return v;
}

ll powmod(ll a, ll b, ll c){
	if(b == 0) return 1%c;
	else if(b % 2 == 0) return powmod((a*a)%c, b/2, c);
	return (a*powmod((a*a)%c, (b-1)/2, c))%c;
}

void printp(vpll v){
	rep(i, v.size()) cout << v[i].first << " " << v[i].second << endl;
}

ll min(ll a, ll b){
	return a < b ? a : b;
}

ll max(ll a, ll b){
	return a > b ? a : b;
}

ll min3(ll a, ll b, ll c){
	return min(min(a, b), c);
}

void print_map(map <ll, ll> m){
	for(auto it : m) cout << it.first << " " << it.second << "\n";
}

void print_set(set <ll> s){
	for(auto it : s) cout << it << " ";
	cout << endl;
}

ll inv(ll a){
	return powmod(a, mod-2, mod);
}

bool prime[max_sz];
// Sieve of erathosthenes
void sieve(){
	memset(prime, true, sizeof(prime));
	for(ll i = 2; i * i < max_sz; i++){
		if(prime[i] == true){
			for(ll j = i*i ; j < max_sz; j+=i){
				prime[j] = false;
			}
		}
	}
}

vll pow(ll a){
	vll res;
	ll temp = 1;
	while(temp <= MAXN){
		res.pb(temp);
		temp *= a;
	}
	return res;
}

vll v(max_sz), powers(max_sz), inverse(max_sz);
void fact(){
	v[0] = 1;
	repi(i, 1, max_sz) v[i] = (v[i-1]*i)%mod;
	rep(i, max_sz) inverse[i] = powmod(v[i], mod - 2, mod);
}

// Obtain the binomial coefficients modulo mod
ll binom(ll n, ll p){
	if(n < p || p < 0) return 0;
	// ll tmp1 = powmod(v[p], mod-2, mod);
	// ll tmp2 = powmod(v[n - p], mod-2, mod);
	ll d = ((inverse[p] * inverse[n - p])%mod * v[n]) % mod;
	return d;
}

//Obtain the smallest x such that ax = 1 mod m and (a, m) = 1
ll modinv(ll a, ll m){
    ll m0 = m, y = 0, x = 1;
    if (m == 1) return 0;
    while (a > 1){
        ll q = a / m, t = m;
        m = a % m, a = t;
        t = y, y = x - q * y, x = t;
    }
    if (x < 0) x += m0;
    return x;
}

void swap(ll &a, ll &b){
	if(a > b){
		ll temp = a;
		a = b;
		b = temp;
	}
}

ll spf[max_sz];
//Compute the smallest prime factor of all numbers
void smallest_prime(){
    spf[1] = 1;
    for(ll i = 2; i <= max_sz; i++) spf[i] = i;
    for (ll i = 4; i <= max_sz; i += 2) spf[i] = 2;
    for (ll i = 3; i * i <= max_sz; i++){
        if (spf[i] == i){
            for (ll j= i * i; j <= max_sz; j+=i)
                if (spf[j] == j) spf[j] = i;
        }
    }
}

//Prime factorise a number in O(log n) time
vll factorise(ll x){
    vector<ll> res;
    while (x != 1){
        res.push_back(spf[x]);
        x = x / spf[x];
    }
		return res;
}

//Obtain the digits in the decimal 10 representation of number n
vll digits(ll n){
	vll res;
	while(n != 0){
		res.pb(n % 10);
		n /= 10;
	}
	// sort(all(res));
	return res;
}

//Check if a is a suffix of b
ll suffix(string a, string b){
	if(a.size() > b.size()) return 0;
	for(ll i = a.size() - 1; i >= 0; --i){
		if(b[b.size() - 1 - (a.size() - 1 - i)]) return 0;
	}
	return 1;
}

ll inc(vll a){
	rep(i, a.size() - 1) if(a[i + 1] < a[i]) return 0;
	return 1;
}

ll dec(vll a){
	rep(i, a.size() - 1) if(a[i + 1] > a[i]) return 0;
	return 1;
}

ll check(string a,string b){
	ll n = a.size();
	ll counter = 0;
	rep(i, n) if(a[i] != b[i]) counter++;
	if(counter > 1) return 0;
	return 1;
}

const ll nax = 1e6 + 1;

int main(){
		ll t = 1;
		// cin >> t;
		while(t--){
			ll w, l;
			cin >> w >> l;
			vll a(w + 1);
			repi(i, 1, w) cin >> a[i];
			a[w] = MAXN;
			vll dp(w + 1, 0), sum(w + 1, 0);
			repi(i, 1, l + 1) {dp[i] = a[i];}
			repi(i, 1, w + 1) sum[i] = sum[i - 1] + dp[i];
			if(sum[w] == 0) {cout << 0; return 0;}
			repi(i, l + 1, w + 1){
					if(a[i] == 0) {
						sum[i] = sum[i - 1];
						continue;
					}
					ll start = max(0, i - l - 1);
					ll sm = sum[i - 1] - sum[start];
					if(sm < a[i]) dp[i] = sm, sum[i] = sum[i - 1] + dp[i];
					else dp[i] = a[i], sum[i] = sum[i - 1] + dp[i];
			}
			// repi(i, 1, w + 1) cout << sum[i] << " ";
			// cout << endl;
			// repi(i, 1, w + 1) cout << dp[i] << " ";
			// cout << endl;
			cout << dp[w] << endl;
}
		return 0;
	}