#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 200 + 10;
const int mod = 1000000007;
int vis[26];
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    //IOS
    ms(vis, 0);
    int n, k;
    string s;
    cin >> n >> k >> s;
    for(int i = 0; i<n; i++) {
        vis[s[i]-'a'] = 1;
    }
    int Min = inf, Max = -1;
    for(int i = 0; i<26; i++) {
        if(vis[i]) {
            Min = min(Min, i);
            Max = max(Max, i);
        }
    }
    if(k<=n) {
        int pos;
        for(int i = k-1; i>=0; i--) {
            if(s[i]-'a'<Max) {
                pos = i;
                break;
            }
        }
        for(int i = 0; i<pos; i++) {
            printf("%c", s[i]);
        }
        printf("%c", 'a'+Max);
        for(int i = pos+1; i<k; i++) {
            printf("%c", 'a'+Min);
        }
    } else {
        cout << s;
        for(int i = 0; i<k-n; i++) {
            printf("%c", 'a'+Min);
        }
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 200 + 10;
const int mod = 1000000007;
int vis[26];
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    //IOS
    ms(vis, 0);
    int n, k;
    string s;
    cin >> n >> k >> s;
    for(int i = 0;i<n;i++){
        vis[s[i]-'a'] = 1;
    }
    char ans;
    for(int i = 26;i>=0;i--){
        if(vis[i]){
            ans = 'a'+i;
            break;
        }
    }
    for(int i = 0;i<k;i++){
        printf("%c", ans);
    }
    printf("\n");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 200 + 10;
const int mod = 1000000007;
int vis[26];
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    //IOS
    ms(vis, 0);
    int n, k;
    string s;
    cin >> n >> k >> s;
    for(int i = 0; i<n; i++) {
        vis[s[i]-'a'] = 1;
    }
    int Min;
    for(int i = 0;i<26;i++){
        if(vis[i]){
            Min = i;
            break;
        }
    }
    int pos, ans;
    for(int i = min(k-1, n-1); i>=0; i--) {
        for(int j = s[i]-'a'+1;j<26;j++){
            if(vis[j]){
                pos = i;
                ans = j;
                goto hh;
            }
        }
    }
hh:
    for(int i = 0; i<pos; i++) {
        printf("%c", s[i]);
    }
    printf("%c", 'a'+ans);
//    printf("!");
    for(int i = pos+1; i<k; i++) {
        printf("%c", 'a'+Min);
    }
//    printf("!");
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int maxn = 200 + 10;
const int mod = 1000000007;
int vis[26];
int main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
#endif
    //IOS
    ms(vis, 0);
    int n, k;
    string s;
    cin >> n >> k >> s;
    for(int i = 0; i<n; i++) {
        vis[s[i]-'a'] = 1;
    }
    int Min = inf, Max = -1;
    for(int i = 0; i<26; i++) {
        if(vis[i]) {
            Min = min(Min, i);
            Max = max(Max, i);
        }
    }
        int pos;
        for(int i = min(k-1, n-1); i>=0; i--) {
            if(s[i]-'a'<Max) {
                pos = i;
                break;
            }
        }
        for(int i = 0; i<pos; i++) {
            printf("%c", s[i]);
        }
        printf("%c", 'a'+Max);
        for(int i = pos+1; i<k; i++) {
            printf("%c", 'a'+Min);
        }
    return 0;
}


#include<cstdio>

int n,ans,tot,x[205],y[205],p[205],edge[205][205];
bool v[205];
inline int sqr(int x){return x*x;}
void dfs(int k)
{
    if(v[k])return;
    v[k]=1;tot++;
    for(register int i=1;i<=edge[k][0];i++)dfs(edge[k][i]);
}
int main()
{
    scanf("%d",&n);
    for(register int i=1;i<=n;i++)scanf("%d%d%d",x+i,y+i,p+i);
    for(register int i=1;i<=n;i++)
        for(register int j=1;j<=n;j++)
            if(sqr(p[i])>=sqr(x[i]-x[j])+sqr(y[i]-y[j]))
                edge[i][++edge[i][0]]=j;
    for(register int i=1;i<=n;i++){
        for(register int j=1;j<=n;j++)v[j]=0;
        tot=0;
        dfs(i);
        if(tot>ans)ans=tot;
    }
    printf("%d",ans);
}


#include<bits/stdc++.h>
using namespace std;
int a[27];
int main()
{
	int n,k;
	string s1;
	while(cin>>n>>k){
		memset(a,0,sizeof(a));
		cin>>s1;
		for(int i=0;i<n;i++)
			a[s1[i]-'a']=1;
		char mmin,mmax;
		for(int i=0;i<26;i++){
			if(a[i]==1){
				mmin='a'+i;
				break;
			}
		}
		for(int i=26;i>=0;i--){
			if(a[i]==1){
				mmax='a'+i;
				break;
			}
		}
		if(n>=k){
			int count;
			for(int i=k-1;i>=0;i--){
				if(s1[i]<mmax){
					count=i;
					break;
				}
			}
			for(int i=0;i<count;i++){
				cout<<s1[i];
			}
			for(int i=s1[count]-'a'+1;i<26;i++){
				if(a[i]==1){
					printf("%c",'a'+i);
					break;
				}
			}
			for(int i=count+1;i<k;i++)
				cout<<mmin;
			cout<<endl;
		}
		if(n<k){
			for(int i=0;i<n;i++)
				cout<<s1[i];
			for(int i=n;i<k;i++)
				cout<<mmin;
			cout<<endl;
		}
	}
 	return 0;
}


#include<bits/stdc++.h>

int n,ans,tot,x[205],y[205],p[205],edge[205][205];
bool v[205];
inline int sqr(int x){return x*x;}
void dfs(int k)
{
    if(v[k])return;
    v[k]=1;tot++;
    for(register int i=1;i<=edge[k][0];i++)dfs(edge[k][i]);
}
int main(){
    scanf("%d",&n);
    for(register int i=1;i<=n;i++)scanf("%d%d%d",x+i,y+i,p+i);
    for(register int i=1;i<=n;i++)
        for(register int j=1;j<=n;j++)
            if(sqr(p[i])>=sqr(x[i]-x[j])+sqr(y[i]-y[j]))
                edge[i][++edge[i][0]]=j;
    for(register int i=1;i<=n;i++){
        for(register int j=1;j<=n;j++)v[j]=0;
        tot=1;
        dfs(i);
        if(tot>ans)ans=tot;
    }
    printf("%d",ans);
}


#include<bits/stdc++.h>

using namespace std;

int n,ans,tot,x[205],y[205],p[205],edge[205][205];
bool v[205];
inline int sqr(int x){return x*x;}
void dfs(int k)
{
    if(v[k])return;
    v[k]=1;
	tot++;
    for(register int i=1;i<=edge[k][0];i++)dfs(edge[k][i]);
}
int main(void)
{
	scanf("%d",&n);
	for(int i=1;i<
	scanf("%d",&n);
    for(register int i=1;i<=n;i++) scanf("%d%d%d",x+i,y+i,p+i);
    for(register int i=1;i<=n;i++)
        for(register int j=1;j<=n;j++)
            if(sqr(p[i])>=sqr(x[i]-x[j])+sqr(y[i]-y[j]))
                edge[i][++edge[i][0]]=j;
                    for(register int i=1;i<=n;i++){
        for(register int j=1;j<=n;j++)v[j]=0;
        tot=1;
        dfs(i);
        if(tot>ans)ans=tot;
    }
    printf("%d",ans);
}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#define N 110000
using namespace std;
int n, k, i, j;
char s[N], t[N];
bool cmp(char e, char f) {
	return e > f;
}
int main() {
	scanf("%d%d%s", &n, &k, s + 1);
	for (i = 1; i <= n; i++)
		t[i] = s[i];
	sort(t + 1, t + 1 + n, cmp);
	printf("%d%d",n, k);
	for (i = 1; i <= n; i++) cout << s[i];
	cout << endl;
	if (n < k) {
		for (i = 1; i <= n; i++)
			printf("%c", s[i]);
		for (i = n + 1; i <= k; i++)
			printf("%c", t[n]);
	}
	else {
		for (i = n; i >= 1; i--) {
			for (j = 1; j <= n; j++)
				if (t[j] <= s[i])
					break;
			if (j != 1) {
				s[i] = t[j - 1];
				break;
			}
		}
		for (j = 1; j <= i; j++)
			printf("%c", s[j]);
		for (j = i + 1; j <= k; j++)
			printf("%c", t[n]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int n,k,i,j;
string str;
set <char> s;
int main(){
    cin>>n>>k>>str;
    for(i=0;i<n;i++)s.insert(str[i]);
    if(k>n){
        cout<<str;
        for(i=0;i<k-n;i++)
		cout<<*s.begin();
    }
    else{
        for(i=k-1;i>=0;i--)
        if(str[i]!=*s.rbegin()){
            for(j=0;j<i;j++)cout<<str[j];
            cout<<*s.upper_bound(str[i]);
            for(j=i+1;j<k;j++)cout<<*s.begin();
            break;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main(){
 int n,k;
 cin >> n >> k;
 string s,ss;
 cin>>s;
 set<char>c;
 char x=s[0];
 map<char,int>m;
 vector<char>v;
 for(int i=0 ; i<s.size() ; i++){
   m[s[i]]++;
 if(m[s[i]]==1)v.push_back(s[i]);
 }
 bool b=0;
 sort(v.begin(),v.end());
 for(int i=k-1; i>=0 ; i--){
    for(int j=0 ; j!=v.size() ; j++){
       if(s[i] < v[j]){
          s[i]=v[j];b=1;
          for(int h=i+1 ; h<n ; h++){
               s[h]=v[0];
          }
          break;
       }
       if(b)break;
    }
 }
  if (k > n ) for(int i=0 ; i<n ; i++)cout<<s[i];
  else for(int i=0 ; i<k ; i++)cout<<s[i];
  for(int i = s.size(); i < k; i++) cout << v[0];
 cout<<endl;
 }


#include <bits/stdc++.h>
using namespace std;
int main(){
 int n,k;
 cin >> n >> k;
 string s,ss;
 cin>>s;
 set<char>c;
 char x=s[0];
 map<char,int>m;
 vector<char>v;
 for(int i=0 ; i<s.size() ; i++){
   m[s[i]]++;
 if(m[s[i]]==1)v.push_back(s[i]);
 }
 bool b=0;
 sort(v.begin(),v.end());
 for(int i=k-1; i>=0 ; i--){
    for(int j=0 ; j!=v.size() ; j++){
       if(s[i] < v[j]){
          s[i]=v[j];b=1;
          for(int h=i+1 ; h<n ; h++){
               s[h]=v[0];
          }
          break;
       }
       if(b)break;
    }
 }
  for(int i=0 ; i<n ; i++)cout<<s[i];
  for(int i = s.size(); i < k-1; i++) cout << v[0];
 cout<<endl;
 }


#include <bits/stdc++.h>
using namespace std;
int main(){
 int n,k;
 cin >> n >> k;
 string s,ss;
 cin>>s;
 set<char>c;
 char x=s[0];
 map<char,int>m;
 vector<char>v;
 for(int i=0 ; i<s.size() ; i++){
   m[s[i]]++;
 if(m[s[i]]==1)v.push_back(s[i]);
 }
 bool b=0;
 sort(v.begin(),v.end());
 for(int i=k-1; i>=0 ; i--){
    for(int j=0 ; j!=v.size() ; j++){
       if(s[i] < v[j]){
          s[i]=v[j];b=1;
          for(int h=i+1 ; h<n ; h++){
               s[h]=v[0];
          }
          break;
       }
       if(b)break;
    }
 }
  for(int i=0 ; i<k ; i++)cout<<s[i];
  for(int i = s.size(); i < k; i++) cout << v[0];
 cout<<endl;
 }


#include <bits/stdc++.h>
using namespace std;
int main(){
 int n,k;
 cin >> n >> k;
 string s,ss;
 cin>>s;
 set<char>c;
 char x=s[0];
 map<char,int>m;
 vector<char>v;
 for(int i=0 ; i<s.size() ; i++){
   m[s[i]]++;
 if(m[s[i]]==1)v.push_back(s[i]);
 }
 bool b=0;
 sort(v.begin(),v.end());
 for(int i=k-1; i>=0 ; i--){
    for(int j=0 ; j!=v.size() ; j++){
       if(s[i] < v[j]){
          s[i]=v[j];b=1;
          for(int h=i+1 ; h<n ; h++){
               s[h]=v[0];
          }
          break;
       }
       if(b)break;
    }
 }
  for(int i=0 ; i<k ; i++)cout<<s[i];
  for(int i = s.size(); i < k-1; i++) cout << v[0];
 cout<<endl;
 }


#include <bits/stdc++.h>
using namespace std;
int main(){
 int n,k;
 cin >> n >> k;
 string s,ss;
 cin>>s;
 set<char>c;
 char x=s[0];
 map<char,int>m;
 vector<char>v;
 for(int i=0 ; i<s.size() ; i++){
   m[s[i]]++;
 if(m[s[i]]==1)v.push_back(s[i]);
 }
 bool b=0;
 sort(v.begin(),v.end());
 if(k>n){
  cout<<s;
  for(int i = n ; i < k; i++){
    cout<<v[0];
  }
  return 0;
 }
 for(int i=k-1; i>=0 ; i--){
    for(int j=0 ; j!=v.size() ; j++){
       if(s[i] < v[j]){
          s[i]=v[j];b=1;
          for(int h=i+1 ; h<k ; h++){
               s[h]=v[0];
          }
          break;
       }
       if(b)break;
    }
 }
  for(int i=0 ; i<k ; i++)cout<<s[i];
 cout<<endl;
 }


#include <bits/stdc++.h>
using namespace std;
int main(){
 int n,k;
 cin >> n >> k;
 string s,ss;
 cin>>s;
 set<char>c;
 char x=s[0];
 map<char,int>m;
 vector<char>v;
 for(int i=0 ; i<s.size() ; i++){
   m[s[i]]++;
 if(m[s[i]]==1)v.push_back(s[i]);
 }
 bool b=0;
 sort(v.begin(),v.end());
 for(int i=k-1; i>=0 ; i--){
    for(int j=0 ; j!=v.size() ; j++){
       if(s[i] < v[j]){
          s[i]=v[j];b=1;
          for(int h=i+1 ; h<n ; h++){
               s[h]=v[0];
          }
          break;
       }
       if(b)break;
    }
 }
  if (k > n ) for(int i=0 ; i<n ; i++)cout<<s[i];
  else for(int i=0 ; i<k ; i++)cout<<s[i];
  for(int i = s.size(); i < k-1; i++) cout << v[0];
 cout<<endl;
 }


#include <bits/stdc++.h>
using namespace std;
int main(){
 int n,k;
 cin >> n >> k;
 string s,ss;
 cin>>s;
 set<char>c;
 char x=s[0];
 map<char,int>m;
 vector<char>v;
 for(int i=0 ; i<s.size() ; i++){
   m[s[i]]++;
 if(m[s[i]]==1)v.push_back(s[i]);
 }
 bool b=0;
 sort(v.begin(),v.end());
 if(k>n){
  cout<<s;
  for(int i=0 ; i<n; i++){
    cout<<v[0];
  }
  return 0;
 }
 for(int i=k-1; i>=0 ; i--){
    for(int j=0 ; j!=v.size() ; j++){
       if(s[i] < v[j]){
          s[i]=v[j];b=1;
          for(int h=i+1 ; h<k ; h++){
               s[h]=v[0];
          }
          break;
       }
       if(b)break;
    }
 }
  for(int i=0 ; i<k ; i++)cout<<s[i];
 cout<<endl;
 }


#include <bits/stdc++.h>
using namespace std;
int main(){
 int n,k;
 cin >> n >> k;
 string s,ss;
 cin>>s;
 set<char>c;
 char x=s[0];
 map<char,int>m;
 vector<char>v;
 for(int i=0 ; i<s.size() ; i++){
   m[s[i]]++;
 if(m[s[i]]==1)v.push_back(s[i]);
 }
 bool b=0;
 sort(v.begin(),v.end());
 for(int i=k-1; i>=0 ; i--){
    for(int j=0 ; j!=v.size() ; j++){
       if(s[i] < v[j]){
          s[i]=v[j];b=1;
          for(int h=i+1 ; h<k ; h++){
               s[h]=v[0];
          }
          break;
       }
       if(b)break;
    }
 }
  for(int i=0 ; i<k ; i++)cout<<s[i];
 cout<<endl;
 }


#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 2;
char in[N];
set<char> s;
int main()
{
#ifdef local
    freopen("test.in", "r", stdin);
#endif
    int n, k;
    scanf("%d%d\n%s", &n, &k, in);
    for(int i=0;i<n;++i)
        s.insert(in[i]);
    if(n < k)
    {
        printf("%s", in);
        for (int i=n+1;i<=k;++i)
            putchar(*s.begin());
        return 0*putchar(10);
    }
    bool f = 1;
    for (int i = k - 1; i >= 0; --i)
    {
        if (f)
        {
            auto it = s.upper_bound(in[i]);
            if (it == s.end())
                in[i] = *s.begin();
            else
            {
                f = 0;
                in[i] = *it;
            }
        }
    }
    for (int i=0;i<k;++i)
        putchar(in[i]);
    return 0*putchar(10);
}


#include <bits/stdc++.h>
#define ZhaoYu ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define rep(i, a, b) for(auto (i) = (a); (i) < (b); ++(i))
using namespace std;
typedef long long ll;
int x[1000];
char a[100005];
int last_one=-1;
int first_one=-1;
int n,k;
void go() {
    a[k]='\0';
    for (int i=k-1;i>=0;i--) {
        if (a[i]==last_one) {
            a[i]=first_one;
        }
        else {
            a[i]=x[(int)a[i]];
            break;
        }
    }
    cout << a << endl;
}
void init() {
    for (int i='a';i<='z';i++) {
        if (x[i]==1) {
            if (first_one==-1) {
                first_one=i;
            }
            if (last_one!=-1) {
                x[last_one]=i;
            }
            last_one=i;
        }
    }
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
#endif
    ZhaoYu
    cin >> n >> k;
    cin >> a;
    rep(i, 0, n) {
        x[(int)a[i]]=1;
    }
    init();
    x[last_one]=-1;
    if (k > n) {
        cout << a;
        while(1) {
            cout << first_one;
            k--;
            if (k == n) break;
        }
        return cout << endl, 0;
    }
    else {
        go();
    }
    return 0;
}


#include <bits/stdc++.h>
#define ZhaoYu ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define rep(i, a, b) for(auto (i) = (a); (i) < (b); ++(i))
using namespace std;
typedef long long ll;
int x[1000];
char a[100005];
int last_one=-1;
int first_one=-1;
int n,k;
void go() {
    a[k]='\0';
    for (int i=k-1;i>=0;i--) {
        if (a[i]==last_one) {
            a[i]=first_one;
        }
        else {
            a[i]=x[(int)a[i]];
            break;
        }
    }
    cout << a << endl;
}
void init() {
    for (int i='a';i<='z';i++) {
        if (x[i]==1) {
            if (first_one==-1) {
                first_one=i;
            }
            if (last_one!=-1) {
                x[last_one]=i;
            }
            last_one=i;
        }
    }
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
#endif
    ZhaoYu
    cin >> n >> k;
    cin >> a;
    rep(i, 0, n) {
        x[(int)a[i]]=1;
    }
    init();
    x[last_one]=-1;
    if (k > n) {
        cout << a;
        while(1) {
            cout << (char)first_one;
            k--;
            if (k == n) break;
        }
        return cout << endl, 0;
    }
    else {
        go();
    }
    return 0;
}


#include <bits/stdc++.h>
#define ZhaoYu ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define rep(i, a, b) for(auto (i) = (a); (i) < (b); ++(i))
using namespace std;
typedef long long ll;
int x[1000];
string a;
int last_one=-1;
int first_one=-1;
int n,k;
void go() {
    a[k]='\0';
    for (int i=k-1;i>=0;i--) {
        if (a[i]==last_one) {
            a[i]=first_one;
        }
        else {
            a[i]=x[(int)a[i]];
            break;
        }
    }
    cout << a << endl;
}
int main() {
#ifndef ONLINE_JUDGE
    freopen("test.in", "r", stdin);
#endif
    ZhaoYu

    cin >> n >> k;
    cin >> a;
    rep(i, 0, n) {
        x[(int)a[i]]=1;
    }

    rep(i, 'a', 'z' + 1) {
        if (x[i]==1) {
            if (first_one==-1) {
                first_one=i;
            }
            if (last_one!=-1) {
                x[last_one]=i;
            }
            last_one=i;
        }
    }
    x[last_one]=-1;
    if (k > n) {
        cout << a;
        while(1) {
            cout << first_one;
            k--;
            if (k == n) break;
        }
        return cout << endl, 0;
    }
    else {
        go();
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int n,m;
string A;
bool is[26];

int main(){

    cin >> n >> m;
    cin >> A;

    char mi = *min_element(A.begin(), A.end());
    char ma = *max_element(A.begin(), A.end());

    for (int i = 0 ; i < A.size() ; i++){
        is[A[i] - 'a'] = true;
    }

    if (m <= n){

        int i; int midle = 0;

        for (i = m - 1 ; i >= 0 ; i--){
            /*bool p = false;
            for (int j = A[i] - 'a' + 1 ; j < 26 ; j++){
                if (is[j] == true){
                    p = true;
                    midle = j;
                }
            }

        if (p == true)
            break;*/
            if (A[i] != ma)
                break;
        }

        for (int j = 0 ; j < i ;j++)
            cout << A[j];

        for (int j = 0 ; j < n ; j++){
            if (A[j] > A[i])ma = min(ma, A[j]);
        }
        cout << ma << " ";

        for (int j = i+1 ; j < m ; j++){
            cout << mi;
        }

    }

    else{
        cout << A;
        int midle1;
        for (int j = 0 ; j < 26 ; j++){
            if (is[j] == true){
                midle1 = j;
                break;
            }
        }
            for (int j = n ; j < m ; j++){
                char a = midle1 + 'a';
                cout << a;
            }
        }

    cout << endl;

return 0;

}


#include <bits/stdc++.h>

using namespace std;

int n,m;
string A;
bool is[26];

int main(){

    cin >> n >> m;
    cin >> A;

    char mi = *min_element(A.begin(), A.end());
    char ma = *max_element(A.begin(), A.end());

    for (int i = 0 ; i < A.size() ; i++){
        is[A[i] - 'a'] = true;
    }

    if (m <= n){

        int i; int midle = 0;

        for (i = m - 1 ; i >= 0 ; i--){
            /*bool p = false;
            for (int j = A[i] - 'a' + 1 ; j < 26 ; j++){
                if (is[j] == true){
                    p = true;
                    midle = j;
                }
            }

        if (p == true)
            break;*/
            if (A[i] != ma)
                break;
        }

        for (int j = 0 ; j < i ; j++){
            cout << A[j];
        }

        for (int j = 0 ; j < 26 ; j++){
            if (A[i] - 'a' < j and is[j] == true){
                char b = j + 'a';
                cout << b;
            }
        }

        for (int j = i+1 ; j < m ; j++){
            cout << mi;
        }

    }

    else{
        cout << A;
        int midle1;
        for (int j = 0 ; j < 26 ; j++){
            if (is[j] == true){
                midle1 = j;
                break;
            }
        }
            for (int j = n ; j < m ; j++){
                char a = midle1 + 'a';
                cout << a;
            }
        }

    cout << endl;

return 0;

}


#include <bits/stdc++.h>

using namespace std;

int n,m;
string A;
bool is[26];

int main(){

    cin >> n >> m;
    cin >> A;

    for (int i = 0 ; i < A.size() ; i++){
        is[A[i] - 'a'] = true;
    }

    if (m <= n){

        int i; int midle =0;

        for (i = m - 1 ; i >= 0 ; i--){
            bool p = false;
            for (int j = A[i] - 'a' + 1 ; j < 26 ; j++){
                if (is[j] == true){
                    p = true;
                    midle = j;
                }
            }

        if (p == true)
            break;
        }

        for (int j = 0 ; j < i ; j++){
            cout << A[j];
        }

        char a = midle + 'a';
        cout << a;

        for (int j = 0 ; j < 26 ; j++){
            if (is[j] == true){
                midle = j;
                break;
            }
        }

        for (int j = i+1 ; j < m ; j++){
            char a = midle + 'a';
            cout << a;
        }
    }

    else{
        cout << A;
        int midle;
        for (int j = 0 ; j < 26 ; j++){
            if (is[j] == true){
                midle = j;
                break;
            }
        }
            for (int i = A.size() ; i < m ; i++){
                char a = midle + 'a';
                cout << a;
            }
        }

    cout << endl;

return 0;

}


#include <bits/stdc++.h>

using namespace std;

int n,m;
string A;
bool is[26];

int main(){

    cin >> n >> m;
    cin >> A;

    for (int i = 0 ; i < A.size() ; i++){
        is[A[i] - 'a'] = true;
    }

    if (m < n){

    int i; int midle =0;

    for (i = min((int)A.size(), m) - 1 ; i >= 0 ; i--){
        bool p = false;
        for (int j = A[i] - 'a' + 1 ; j < 26 ; j++){
            if (is[j] == true){
                p = true;
                midle = j;
            }
        }
        if (p == true)
            break;
    }

    for (int j = 0 ; j < i ; j++){
        cout << A[j];
    }

    char a = midle + 'a';
    cout << a;

    for (int j = 0 ; j < 26 ; j++){
        if (is[j] == true){
            midle = j;
            break;
        }
    }

    for (int j = i+1 ; j < m ; j++){
        char a = midle + 'a';
        cout << a;
    }
    }
    else{
        cout << A;
     int midle;
    for (int j = 0 ; j < 26 ; j++){
        if (is[j] == true){
            midle = j;
            break;
        }
    }
        for (int i = A.size() ; i < m ; i++){
            char a = midle + 'a';
            cout << a;
        }
    }
    cout << endl;
return 0;

}


#include <bits/stdc++.h>

using namespace std;

int n,m;
string A;
bool is[26];

int main(){

    cin >> n >> m;
    cin >> A;

    char mi = *min_element(A.begin(), A.end());

    for (int i = 0 ; i < A.size() ; i++){
        is[A[i] - 'a'] = true;
    }

    if (m <= n){

        int i; int midle = 0;

        for (i = m - 1 ; i >= 0 ; i--){
            bool p = false;
            for (int j = A[i] - 'a' + 1 ; j < 26 ; j++){
                if (is[j] == true){
                    p = true;
                    midle = j;
                }
            }

        if (p == true)
            break;
        }

        for (int j = 0 ; j < i ; j++){
            cout << A[j];
        }

        char a = midle + 'a';
        cout << a;

        for (int j = i+1 ; j < m ; j++){
            cout << mi;
        }
    }

    else{
        cout << A;
        int midle1;
        for (int j = 0 ; j < 26 ; j++){
            if (is[j] == true){
                midle1 = j;
                break;
            }
        }
            for (int j = n ; j < m ; j++){
                char a = midle1 + 'a';
                cout << a;
            }
        }

    cout << endl;

return 0;

}


#include <bits/stdc++.h>

using namespace std;

int n,m;
string A;
bool is[26];

int main(){

    cin >> n >> m;
    cin >> A;

    char mi = *min_element(A.begin(), A.end());
    char ma = *max_element(A.begin(), A.end());

    for (int i = 0 ; i < A.size() ; i++){
        is[A[i] - 'a'] = true;
    }

    if (m <= n){

        int i; int midle = 0;

        for (i = m - 1 ; i >= 0 ; i--){
            bool p = false;
            for (int j = A[i] - 'a' + 1 ; j < 26 ; j++){
                if (is[j] == true){
                    p = true;
                    midle = j;
                }
            }

        if (p == true)
            break;
        }

        for (int j = 0 ; j < i ;j++)
            cout << A[j];

  /*      for (int j = 0 ; j < n ; j++){
            if (A[j] > A[i])ma = min(ma, A[j]);
        }
        cout << ma;
*/
        char niuber = midle + 'a';
        cout << niuber;

        for (int j = i+1 ; j < m ; j++){
            cout << mi;
        }

    }

    else{
        cout << A;
        int midle1;
        for (int j = 0 ; j < 26 ; j++){
            if (is[j] == true){
                midle1 = j;
                break;
            }
        }
            for (int j = n ; j < m ; j++){
                char a = midle1 + 'a';
                cout << a;
            }
        }

    cout << endl;

return 0;

}


#include <bits/stdc++.h>

using namespace std;

int n,m;
string A;
bool is[27];

int main(){

    cin >> n >> m;
    cin >> A;

    char mi = *min_element(A.begin(), A.end());
    char ma = *max_element(A.begin(), A.end());

    for (int i = 0 ; i < A.size() ; i++){
        is[A[i] - 'a'] = true;
    }

    if (m <= n){

        int i; int midle = 0;

        for (i = m - 1 ; i >= 0 ; i--){
            bool p = false;
            for (int j = A[i] - 'a' + 1 ; j < 27 ; j++){
                if (is[j] == true){
                    p = true;
                    midle = j;
                }
            }

        if (p == true)
            break;
        }

        for (int j = 0 ; j < i ;j++)
            cout << A[j];

  /*      for (int j = 0 ; j < n ; j++){
            if (A[j] > A[i])ma = min(ma, A[j]);
        }
        cout << ma;
*/
        char niuber = midle + 'a';
        cout << niuber;

        for (int j = i+1 ; j < m ; j++){
            cout << mi;
        }

    }

    else{
        cout << A;
        int midle1;
        for (int j = 0 ; j < 26 ; j++){
            if (is[j] == true){
                midle1 = j;
                break;
            }
        }
            for (int j = n ; j < m ; j++){
                char a = midle1 + 'a';
                cout << a;
            }
        }

    cout << endl;

return 0;

}


#include <bits/stdc++.h>

using namespace std;

int n,m;
string A;
bool is[26];

int main(){

    cin >> n >> m;
    cin >> A;

    char mi = *min_element(A.begin(), A.end());
    char ma = *max_element(A.begin(), A.end());

    for (int i = 0 ; i < A.size() ; i++){
        is[A[i] - 'a'] = true;
    }

    if (m <= n){

        int i; int midle = 0;

        for (i = m - 1 ; i >= 0 ; i--){
            /*bool p = false;
            for (int j = A[i] - 'a' + 1 ; j < 26 ; j++){
                if (is[j] == true){
                    p = true;
                    midle = j;
                }
            }

        if (p == true)
            break;*/
            if (A[i] != ma)
                break;
        }

        for (int j = 0 ; j < i ;j++)
            cout << A[j];

        for (int j = 0 ; j < n ; j++){
            if (A[j] > A[i])ma = min(ma, A[j]);
        }
        cout << ma;

        for (int j = i+1 ; j < m ; j++){
            cout << mi;
        }

    }

    else{
        cout << A;
        int midle1;
        for (int j = 0 ; j < 26 ; j++){
            if (is[j] == true){
                midle1 = j;
                break;
            }
        }
            for (int j = n ; j < m ; j++){
                char a = midle1 + 'a';
                cout << a;
            }
        }

    cout << endl;

return 0;

}


#include <bits/stdc++.h>

using namespace std;

int n,m;
string A;
bool is[26];

int main(){

    cin >> n >> m;
    cin >> A;

    for (int i = 0 ; i < A.size() ; i++){
        is[A[i] - 'a'] = true;
    }

    if (m <= n){

    int i; int midle =0;

    for (i = min((int)A.size(), m) - 1 ; i >= 0 ; i--){
        bool p = false;
        for (int j = A[i] - 'a' + 1 ; j < 26 ; j++){
            if (is[j] == true){
                p = true;
                midle = j;
            }
        }
        if (p == true)
            break;
    }

    for (int j = 0 ; j < i ; j++){
        cout << A[j];
    }

    char a = midle + 'a';
    cout << a;

    for (int j = 0 ; j < 26 ; j++){
        if (is[j] == true){
            midle = j;
            break;
        }
    }

    for (int j = i+1 ; j < m ; j++){
        char a = midle + 'a';
        cout << a;
    }
    }
    else{
        cout << A;
     int midle;
    for (int j = 0 ; j < 26 ; j++){
        if (is[j] == true){
            midle = j;
            break;
        }
    }
        for (int i = A.size() ; i < m ; i++){
            char a = midle + 'a';
            cout << a;
        }
    }
    cout << endl;
return 0;

}


#include <bits/stdc++.h>

using namespace std;

bool a[27];
int p[27];
int main(){
    int n,k; cin >> n >> k;
    string s; cin >> s;
    string st = "";
    string t = s;
    for (int i = 0; i < (int)s.size(); ++i)
        a[s[i] - 'a'] = 1;

    int pos = 0;
    for (int i = 0; i < 27; ++i) {
        if(a[i]) {
            st += (char)(i + 'a');
            p[i] = pos++;
        }
    }

    for (int i = k - 1; i >= 0; i--) {
        int cp = p[s[i] - 'a'];
        if(cp + 1 < (int)st.size()) {
            char l = st[cp + 1];
            t[i] = l;
            for (int j = i + 1; j < min(k,(int)t.size()); ++j)
                t[j] = st[0];
            break;
        }
    }
    while((int)t.size() < k) t += 'a';
    for (int i = 0; i < k; ++i)
        cout << t[i];
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool a[27];
int p[27];
int main(){
    int n,k; cin >> n >> k;
    string s; cin >> s;
    string st = "";
    string t = s;
    for (int i = 0; i < (int)s.size(); ++i)
        a[s[i] - 'a'] = 1;

    int pos = 0;
    for (int i = 0; i < 27; ++i) {
        if(a[i]) {
            st += (char)(i + 'a');
            p[i] = pos++;
        }
    }

    for (int i = k - 1; i >= 0; i--) {
        int cp = p[s[i] - 'a'];
        if(cp + 1 < (int)st.size()) {
            char l = st[cp + 1];
            t[i] = l;
            for (int j = i + 1; j < k; ++j)
                t[j] = st[0];
            break;
        }
    }
    while((int)t.size() < k) t += 'a';
    for (int i = 0; i < k; ++i)
        cout << t[i];
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool a[27];
int p[27];
int main(){
    int n,k; cin >> n >> k;
    string s; cin >> s;
    string st = "";
    string t = s;
    for (int i = 0; i < (int)s.size(); ++i)
        a[s[i] - 'a'] = 1;

    int pos = 0;
    for (int i = 0; i < 27; ++i) {
        if(a[i]) {
            st += (char)(i + 'a');
            p[i] = pos++;
        }
    }
    cout << st[0] << " " << t << "\n";
    for (int i = min((int)t.size() - 1,k - 1); i >= 0; i--) {
        int cp = p[s[i] - 'a'];
        if(cp + 1 < (int)st.size()) {
            char l = st[cp + 1];
            t[i] = l;
            for (int j = i + 1; j < min(k,(int)t.size()); ++j)
                t[j] = st[0];
            break;
        }
    }
    while((int)t.size() < k) t += st[0];
    for (int i = 0; i < k; ++i)
        cout << t[i];
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

bool a[27];
int p[27];
int main(){
    int n,k; cin >> n >> k;
    string s; cin >> s;
    string st = "";
    string t = s;
    for (int i = 0; i < (int)s.size(); ++i)
        a[s[i] - 'a'] = 1;

    int pos = 0;
    for (int i = 0; i < 27; ++i) {
        if(a[i]) {
            st += (char)(i + 'a');
            p[i] = pos++;
        }
    }
    if((int)t.size() < k) {
        while((int)t.size() < k) t += st[0];
        cout <<t << "\n";
        return 0;
    }
    for (int i = k - 1; i >= 0; i--) {
        int cp = p[s[i] - 'a'];
        if(cp + 1 < (int)st.size()) {
            char l = st[cp + 1];
            t[i] = l;
            for (int j = i + 1; j < min(k,(int)t.size()); ++j)
                t[j] = st[0];
            break;
        }
    }
    for (int i = 0; i < k; ++i)
        cout << t[i];
    cout << "\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const long long inf = 1e15;
const int mod = 1e6;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
}

const long long N = 1000 + 10;

//ll a[N], b[N];
ll a[N];

int main()
{
	faster();
	int n, k;
	string s;
	cin >> n >> k >> s;
	ll maxi = 0;
	for (int i = 0; i < n; i++)
	{
		ll t = s[i] - 'a';
		a[t]++;
		maxi = max(maxi, t);
	}
	for (int i = 0; i < k; i++)
	{
		char m = 'a' + maxi;
		cout << m;
	}
	cout << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const long long inf = 1e15;
const int mod = 1e6;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
}

const long long N = 1000 + 10;

//ll a[N], b[N];
ll a[N];

int main()
{
	faster();
	int n, k;
	string s;
	cin >> n >> k >> s;
	ll maxi = 0, mini = 26;
	for (int i = 0; i < n; i++)
	{
		ll t = s[i] - 'a';
		a[t] = 1;
		maxi = max(maxi, t);
		mini = min(mini, t);
	}
	if(k > n)
	{
		cout << s;
		for (int i = 0; i < k - n; i++)
		{
			char m = 'a' + mini;
			cout << m;
		}
		return 0;
	}
	string v;
	for (int i = 0; i < k; i++)
		v.push_back(s[i]);
	bool ans = false;
	for (int i = v.size() - 1; i >= 0; i--)
	{
		for (int j = 0; j < 26; j++)
		{
			char m = 'a' + j;
			if(m > v[i] && a[j] > 0)
			{
				v[i] = m;
				ans = true;
				break;
			}
		}
		if(ans)
		{
			for (int j = i + 1; j < v.size(); j++)
				v[j] = 'a' + mini;
			break;
		}
	}
	cout << v << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const long long inf = 1e15;
const int mod = 1e6;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
}

const long long N = 1000 + 10;

//ll a[N], b[N];
ll a[N];

int main()
{
	faster();
	int n, k;
	string s;
	cin >> n >> k >> s;
	if(k > n)
	{
		cout << s;
		for (int i = 0; i < k - n; i++)
			cout << "a";
		return 0;
	}
	ll maxi = 0;
	for (int i = 0; i < n; i++)
	{
		ll t = s[i] - 'a';
		a[t] = 1;
		maxi = max(maxi, t);
	}
	string v;
	bool ans = false;
	for (int i = k - 1; i >= 0; i--)
	{
		for (int j = s[i] - 'a' + 1; j < 26; j++)
		{
			if(a[j] > 0)
			{
				char m = 'a' + j;
				v.push_back(m);
				for (int t = i - 1; t >= 0; t--)
					v.push_back(s[t]);
				ans = true;
			}
			if(ans)
				break;
			if(j == 25)
				v.push_back('a');
		}
		if(ans)
			break;
	}
	reverse(v.begin(), v.end());
	if(v <= s)
	{
		v.pop_back();
		for (int i = 0; i < 26; i++)
		{
			char m = 'a' + i;
			if(m > s[k - 1])
			{
				v.push_back(m);
				break;
			}
		}
	}
	cout <<	v << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const long long inf = 1e15;
const int mod = 1e6;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
}

const long long N = 1000 + 10;

//ll a[N], b[N];
ll a[N];

int main()
{
	faster();
	int n, k;
	string s;
	cin >> n >> k >> s;
	if(k > n)
	{
		cout << s;
		for (int i = 0; i < k - n; i++)
			cout << "a";
		return 0;
	}
	ll maxi = 0;
	for (int i = 0; i < n; i++)
	{
		ll t = s[i] - 'a';
		a[t] = 1;
		maxi = max(maxi, t);
	}
	string v;
	for (int i = 0; i < k; i++)
		v.push_back(s[i]);
	bool ans = false;
	for (int i = v.size() - 1; i >= 0; i--)
	{
		for (int j = 0; j < 26; j++)
		{
			char m = 'a' + j;
			if(m > v[i] && a[j] > 0)
			{
				v[i] = m;
				ans = true;
				break;
			}
		}
		if(ans)
		{
			for (int j = i + 1; j < v.size(); j++)
				v[j] = 'a';
			break;
		}
	}
	if(s >= v)
	{
		v.pop_back();
		for (int i = 0; i < 26; i++)
		{
			char m = 'a' + i;
			if(m > s[k - 1] && a[i] > 0)
			{
				v.push_back(m);
				break;
			}
		}
	}
	cout << v << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const long long inf = 1e15;
const int mod = 1e6;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
}

const long long N = 1000 + 10;

//ll a[N], b[N];
ll a[N];

int main()
{
	faster();
	int n, k;
	string s;
	cin >> n >> k >> s;
	if(k > n)
	{
		cout << s;
		for (int i = 0; i < k - n; i++)
			cout << "a";
		return 0;
	}
	ll maxi = 0;
	for (int i = 0; i < n; i++)
	{
		ll t = s[i] - 'a';
		a[t] = 1;
		maxi = max(maxi, t);
	}
	string v;
	for (int i = 0; i < k; i++)
		v.push_back(s[i]);
	bool ans = false;
	for (int i = v.size() - 1; i >= 0; i--)
	{
		for (int j = 0; j < 26; j++)
		{
			char m = 'a' + j;
			if(m > v[i] && a[j] > 0)
			{
				v[i] = m;
				ans = true;
				break;
			}
		}
		if(ans)
		{
			for (int j = i + 1; j < v.size(); j++)
				v[j] = 'a';
			break;
		}
	}
	cout << v << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const long long inf = 1e15;
const int mod = 1e6;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
}

const long long N = 1000 + 10;

//ll a[N], b[N];
ll a[N];

int main()
{
	faster();
	int n, k;
	string s;
	cin >> n >> k >> s;
	if(k > n)
	{
		cout << s;
		for (int i = 0; i < k - n; i++)
			cout << "a";
		return 0;
	}
	ll maxi = 0;
	for (int i = 0; i < n; i++)
	{
		ll t = s[i] - 'a';
		a[t] = 1;
		maxi = max(maxi, t);
	}
	string v;
	bool ans = false;
	for (int i = k - 1; i >= 0; i--)
	{
		for (int j = s[i] - 'a' + 1; j < 26; j++)
		{
			if(a[j] > 0)
			{
				char m = 'a' + j;
				v.push_back(m);
				for (int t = i - 1; t >= 0; t--)
					v.push_back(s[t]);
				ans = true;
			}
			if(ans)
				break;
			if(j == 25)
				v.push_back('a');
		}
		if(ans)
			break;
	}
	reverse(v.begin(), v.end());
	if(v <= s)
	{
		v.pop_back();
		s.push_back(maxi + 'a');
	}
	cout <<	v << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const long long inf = 1e15;
const int mod = 1e6;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
}

const long long N = 1000 + 10;

//ll a[N], b[N];
ll a[N];

int main()
{
	faster();
	int n, k;
	string s;
	cin >> n >> k >> s;
	if(k > n)
	{
		cout << s;
		for (int i = 0; i < k - n; i++)
			cout << "a";
		return 0;
	}
	for (int i = 0; i < n; i++)
	{
		ll t = s[i] - 'a';
		a[t]++;
	}
	string v;
	bool ans = false;
	for (int i = k - 1; i >= 0; i--)
	{
		for (int j = s[i] - 'a' + 1; j < 26; j++)
		{
			if(a[j] > 0)
			{
				char m = 'a' + j;
				v.push_back(m);
				for (int t = i - 1; t >= 0; t--)
					v.push_back(s[t]);
				ans = true;
			}
			if(ans)
				break;
			if(j == 25)
				v.push_back('a');
		}
		if(ans)
			break;
	}
	reverse(v.begin(), v.end());
	cout <<	v << endl;
}


#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
const long long inf = 1e15;
const int mod = 1e6;
typedef long long ll;
typedef long double ld;
typedef pair <ll, ll> pii;

void faster()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
}

const long long N = 1000 + 10;

//ll a[N], b[N];
ll a[N];

int main()
{
	faster();
	int n, k;
	string s;
	cin >> n >> k >> s;
	ll maxi = 0;
	for (int i = 0; i < n; i++)
	{
		ll t = s[i] - 'a';
		a[t]++;
		maxi = max(maxi, t);
	}
	string v;
	bool ans = false;
	for (int i = k - 1; i >= 0; i--)
	{
		for (int j = s[i] - 'a' + 1; j < 26; j++)
		{
			if(a[j] > 0)
			{
				char m = 'a' + j;
				v.push_back(m);
				for (int t = i - 1; t >= 0; t--)
					v.push_back(s[t]);
				ans = true;
			}
			if(ans)
				break;
			if(j == 25)
				v.push_back('a');
		}
		if(ans)
			break;
	}
	reverse(v.begin(), v.end());
	cout <<	v << endl;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s,t="";
    int n,k;
    cin>>n>>k>>s;
    int alphabet[26]={0};
    char let='a';
    char mnlet='a';
    for(int i=0; i<s.size(); i++)
        alphabet[s[i]-'a']++;
    for(int i=0; i<26; i++)
    {
        if(alphabet[i]>0)
        {
            mnlet+=i;
            break;
        }
    }
    for(int i=0; i<k; i++)
        t+=mnlet;
    for(int i=0; i<n; i++)
        t[i]=s[i];
    if(k>n)
    {
        cout<<t<<endl;
        return 0;
    }
    else
    {
        bool breaked=0;
//        cout<<t<<endl;
        for(int i=min(n,k)-1; i>-1; i--)
        {
//            cout<<i<<endl;
            for(int j=s[i]-'a'+1; j<26; j++)
            {
                if(alphabet[j]>0)
                {
                    t[i]=let+j;
//                    cout<<t[i]<<endl;
                    breaked=1;
                    break;
                }
                else
                    t[i]=mnlet;
            }
            if(breaked)
                break;
        }
    }
    cout<<t<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s,t="";
    int n,k;
    cin>>n>>k>>s;
    int alphabet[26]={0};
    char let='a';
    for(int i=0; i<s.size(); i++)
        alphabet[s[i]-'a']++;
    for(int i=0; i<k; i++)
        t+=let;
    for(int i=0; i<n; i++)
        t[i]=s[i];
    if(k>n)
    {
        cout<<t<<endl;
        return 0;
    }
    else
    {
        bool breaked=0;
//        cout<<t<<endl;
        for(int i=min(n,k)-1; i>-1; i--)
        {
//            cout<<i<<endl;
            for(int j=s[i]-'a'+1; j<26; j++)
            {
                if(alphabet[j]>0)
                {
                    t[i]=let+j;
//                    cout<<t[i]<<endl;
                    breaked=1;
                    break;
                }
                else
                    t[i]=s[i];
            }
            if(breaked)
                break;
        }
    }
    cout<<t<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s,t="";
    int n,k;
    cin>>n>>k>>s;
    int alphabet[26]={0};
    char let='a';
    char mnlet='a';
    for(int i=0; i<s.size(); i++)
        alphabet[s[i]-'a']++;
    for(int i=0; i<26; i++)
    {
        if(alphabet[i]>0)
        {
            mnlet+=i;
            break;
        }
    }
    for(int i=0; i<k; i++)
        t+=mnlet;
    for(int i=0; i<min(n,k); i++)
        t[i]=s[i];
    if(k>n)
    {
        cout<<t<<endl;
        return 0;
    }
    else
    {
        bool breaked=0;
//        cout<<t<<endl;
        for(int i=min(n,k)-1; i>-1; i--)
        {
//            cout<<i<<endl;
            for(int j=s[i]-'a'+1; j<26; j++)
            {
                if(alphabet[j]>0)
                {
                    t[i]=let+j;
//                    cout<<t[i]<<endl;
                    breaked=1;
                    break;
                }
            }
            if(breaked)
                break;
            t[i]=mnlet;
        }
    }
    cout<<t<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s,t="";
    int n,k;
    cin>>n>>k>>s;
    int alphabet[26]={0};
    char let='a';
    for(int i=0; i<s.size(); i++)
        alphabet[s[i]-'a']++;
    for(int i=0; i<k; i++)
        t+=let;
    for(int i=0; i<n; i++)
        t[i]=s[i];
    if(k>n)
    {
        cout<<t<<endl;
        return 0;
    }
    else
    {
        bool breaked=0;
//        cout<<t<<endl;
        for(int i=min(n,k)-1; i>-1; i--)
        {
//            cout<<i<<endl;
            for(int j=s[i]-'a'+1; j<26; j++)
            {
                if(alphabet[j]>0)
                {
                    t[i]=let+j;
//                    cout<<t[i]<<endl;
                    breaked=1;
                    break;
                }
                else
                    t[i]='a';
            }
            if(breaked)
                break;
        }
    }
    cout<<t<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    string s,t="";
    int n,k;
    cin>>n>>k>>s;
    int alphabet[26]={0};
    char let='a';
    char mnlet='a';
    for(int i=0; i<s.size(); i++)
        alphabet[s[i]-'a']++;
    for(int i=0; i<26; i++)
    {
        if(alphabet[i]>0)
        {
            mnlet+=i;
            break;
        }
    }
    for(int i=0; i<k; i++)
        t+=mnlet;
    for(int i=0; i<n; i++)
        t[i]=s[i];
    if(k>n)
    {
        cout<<t<<endl;
        return 0;
    }
    else
    {
        bool breaked=0;
//        cout<<t<<endl;
        for(int i=min(n,k)-1; i>-1; i--)
        {
//            cout<<i<<endl;
            for(int j=s[i]-'a'+1; j<26; j++)
            {
                if(alphabet[j]>0)
                {
                    t[i]=let+j;
//                    cout<<t[i]<<endl;
                    breaked=1;
                    break;
                }
            }
            if(breaked)
                break;
            t[i]=mnlet;
        }
    }
    cout<<t<<endl;
    return 0;
}


    import java.io.*;
    import java.util.*;

    public class Main {
        public static void main(String[] args) throws IOException {
            FastReader scan = new FastReader();
            //PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("taming.out")));
            PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
            Task solver = new Task();
            //int t = scan.nextInt();
            int t = 1;
            for(int i = 1; i <= t; i++) solver.solve(i, scan, out);
            out.close();
        }
        static class Task {
            public void solve(int testNumber, FastReader sc, PrintWriter pw) {
                int n = sc.nextInt();
                int m = sc.nextInt();
                int[] arr= new int[n];
                long[] prefs = new long[n+1];
                for(int i=0;i<n;i++){
                    arr[i]=sc.nextInt();
                    prefs[i+1]=prefs[i]+arr[i];
                }
                spt rmq = new spt(n,arr);
                long[] dp = new long[n+1];
                for(int i=1;i<=Math.min(n,m);i++){
                    dp[i]=prefs[i];
                }
                for(int i=m;i<=n;i++){
                    dp[i]=Math.min(dp[i-1]+arr[i-1],dp[i-m]+prefs[i]-prefs[i-m]-rmq.query(i-m,i-1));
                }
                pw.println(dp[n]);
            }
        }
        static class spt{
            int n;
            int[][] st;
            spt(int n,int[] x){
                this.n=n;
                st =new int[n][20];
                for(int i=0;i<n;i++) {
                    st[i][0] = x[i];
                }
                for (int j = 1; (1 << j) <= n; j++) {
                    for (int i = 0; (i + (1 << j) - 1) < n; i++) {
                        if (st[i][j - 1] < st[i + (1 << (j - 1))][j - 1]) //change for max query
                            st[i][j] = st[i][j - 1];
                        else
                            st[i][j] = st[i + (1 << (j - 1))][j - 1];
                    }
                }
            }
            int query(int L, int R)
            {
                int j = (int)(Math.log(R - L + 1)/Math.log(2));
                if (st[L][j] <= st[R - (1 << j) + 1][j]) //change for maxquery
                    return st[L][j];

                else
                    return st[R - (1 << j) + 1][j];
            }

        }
        static class tup implements Comparable<tup> {
            int a, b;

            tup() {
            }
            ;

            tup(int a, int b) {
                this.a=a;
                this.b=b;
            }

            @Override
            public int compareTo(tup o2) {
                return Double.compare((double)o2.a/o2.b,(double)a/b);
            }

        }
        static void shuffle(long[] a) {
            Random get = new Random();
            for (int i = 0; i < a.length; i++) {
                int r = get.nextInt(a.length);
                long temp = a[i];
                a[i] = a[r];
                a[r] = temp;
            }
        }
        static void shuffle(int[] a) {
            Random get = new Random();
            for (int i = 0; i < a.length; i++) {
                int r = get.nextInt(a.length);
                int temp = a[i];
                a[i] = a[r];
                a[r] = temp;
            }
        }
        static void shuffle(int[] a,int l, int rb) {
            Random get = new Random();
            for (int i = l; i < rb; i++) {
                int r = get.nextInt(a.length);
                int temp = a[i];
                a[i] = a[r];
                a[r] = temp;
            }
        }
        static class FastReader {
            BufferedReader br;
            StringTokenizer st;

            public FastReader() {
                br = new BufferedReader(new InputStreamReader(System.in));
            }

            public FastReader(String s) throws FileNotFoundException {
                br = new BufferedReader(new FileReader(new File(s)));
            }

            String next() {
                while (st == null || !st.hasMoreElements()) {
                    try {
                        st = new StringTokenizer(br.readLine());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }

            int nextInt() {
                return Integer.parseInt(next());
            }

            long nextLong() {
                return Long.parseLong(next());
            }

            double nextDouble() {
                return Double.parseDouble(next());
            }

            String nextLine() {
                String str = "";
                try {
                    str = br.readLine();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return str;
            }
        }

    }


import java.io.*;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.*;

public class cf940e {

    public static void main(String[] args) throws IOException {
        int n = rni(), c = ni(), a[] = ria(n);
        long dp[] = new long[n + 1], pre[] = new long[n + 1];
        for (int i = 0; i < n; ++i) {
            pre[i + 1] = pre[i] + a[i];
        }
        fill(dp, LMAX >> 2);
        sptb sptb = new sptb(a, Integer::min);
        dp[0] = 0;
        for (int i = 1, end = min(c, n + 1); i < end; ++i) {
            dp[i] = dp[i - 1] + a[i - 1];
        }
        for (int i = c; i <= n; ++i) {
            dp[i] = min(dp[i - 1] + a[i - 1], dp[i - c] + pre[i] - pre[i - c] - sptb.qry(i - c, i));
        }
        prln(dp[n]);
        close();
    }

    @FunctionalInterface
    interface IntOperator {
        int merge(int a, int b);
    }

    // cannot use with summation
    static class sptb {
        IntOperator op;
        int n, lg[], lookup[][];

        sptb(int[] a, IntOperator operator) {
            n = a.length;
            lg = new int[n + 1];
            int pow2 = 4;
            for (int i = 2, ind = 1; i <= n; ++i) {
                if (pow2 == i) {
                    ++ind;
                    pow2 <<= 1;
                }
                lg[i] = ind;
            }
            op = operator;
            lookup = new int[n][lg[n] + 1];
            for (int i = 0; i < n; ++i) {
                lookup[i][0] = a[i];
            }
            for (int j = 1; (1 << j) <= n; ++j) {
                for (int i = 0; (i + (1 << j)) <= n; ++i) {
                    lookup[i][j] = op.merge(lookup[i][j - 1], lookup[i + (1 << (j - 1))][j - 1]);
                }
            }
        }

        int qry(int l, int r) {
            int j = lg[r - l];
            return op.merge(lookup[l][j], lookup[r - (1 << j)][j]);
        }
    }

    static BufferedReader __in = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter __out = new PrintWriter(new OutputStreamWriter(System.out));
    static StringTokenizer input;
    static Random __rand = new Random();

    // references
    // IBIG = 1e9 + 7
    // IMAX ~= 2e9
    // LMAX ~= 9e18

    // constants
    static final int IBIG = 1000000007;
    static final int IMAX = 2147483647;
    static final int IMIN = -2147483648;
    static final long LMAX = 9223372036854775807L;
    static final long LMIN = -9223372036854775808L;
    // math util
    static int minof(int a, int b, int c) {return min(a, min(b, c));}
    static int minof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
    static long minof(long a, long b, long c) {return min(a, min(b, c));}
    static long minof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min;}
    static int maxof(int a, int b, int c) {return max(a, max(b, c));}
    static int maxof(int... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
    static long maxof(long a, long b, long c) {return max(a, max(b, c));}
    static long maxof(long... x) {if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max;}
    static int powi(int a, int b) {if (a == 0) return 0; int ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static long powl(long a, int b) {if (a == 0) return 0; long ans = 1; while (b > 0) {if ((b & 1) > 0) ans *= a; a *= a; b >>= 1;} return ans;}
    static int fli(double d) {return (int) d;}
    static int cei(double d) {return (int) ceil(d);}
    static long fll(double d) {return (long) d;}
    static long cel(double d) {return (long) ceil(d);}
    static int gcf(int a, int b) {return b == 0 ? a : gcf(b, a % b);}
    static long gcf(long a, long b) {return b == 0 ? a : gcf(b, a % b);}
    static int lcm(int a, int b) {return a * b / gcf(a, b);}
    static long lcm(long a, long b) {return a * b / gcf(a, b);}
    static int randInt(int min, int max) {return __rand.nextInt(max - min + 1) + min;}
    static long mix(long x) {x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31);}
    // array util
    static void reverse(int[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(long[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(double[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void reverse(char[] a) {for (int i = 0, n = a.length, half = n / 2; i < half; ++i) {char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap;}}
    static void shuffle(int[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(long[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void shuffle(double[] a) {int n = a.length - 1; for (int i = 0; i < n; ++i) {int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap;}}
    static void rsort(int[] a) {shuffle(a); sort(a);}
    static void rsort(long[] a) {shuffle(a); sort(a);}
    static void rsort(double[] a) {shuffle(a); sort(a);}
    static int[] copy(int[] a) {int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static long[] copy(long[] a) {long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static double[] copy(double[] a) {double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    static char[] copy(char[] a) {char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans;}
    // input
    static void r() throws IOException {input = new StringTokenizer(rline());}
    static int ri() throws IOException {return Integer.parseInt(rline());}
    static long rl() throws IOException {return Long.parseLong(rline());}
    static double rd() throws IOException {return Double.parseDouble(rline());}
    static int[] ria(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni(); return a;}
    static int[] riam1(int n) throws IOException {int[] a = new int[n]; r(); for (int i = 0; i < n; ++i) a[i] = ni() - 1; return a;}
    static long[] rla(int n) throws IOException {long[] a = new long[n]; r(); for (int i = 0; i < n; ++i) a[i] = nl(); return a;}
    static double[] rda(int n) throws IOException {double[] a = new double[n]; r(); for (int i = 0; i < n; ++i) a[i] = nd(); return a;}
    static char[] rcha() throws IOException {return rline().toCharArray();}
    static String rline() throws IOException {return __in.readLine();}
    static String n() {return input.nextToken();}
    static int rni() throws IOException {r(); return ni();}
    static int ni() {return Integer.parseInt(n());}
    static long rnl() throws IOException {r(); return nl();}
    static long nl() {return Long.parseLong(n());}
    static double rnd() throws IOException {r(); return nd();}
    static double nd() {return Double.parseDouble(n());}
    // output
    static void pr(int i) {__out.print(i);}
    static void prln(int i) {__out.println(i);}
    static void pr(long l) {__out.print(l);}
    static void prln(long l) {__out.println(l);}
    static void pr(double d) {__out.print(d);}
    static void prln(double d) {__out.println(d);}
    static void pr(char c) {__out.print(c);}
    static void prln(char c) {__out.println(c);}
    static void pr(char[] s) {__out.print(new String(s));}
    static void prln(char[] s) {__out.println(new String(s));}
    static void pr(String s) {__out.print(s);}
    static void prln(String s) {__out.println(s);}
    static void pr(Object o) {__out.print(o);}
    static void prln(Object o) {__out.println(o);}
    static void prln() {__out.println();}
    static void pryes() {prln("yes");}
    static void pry() {prln("Yes");}
    static void prY() {prln("YES");}
    static void prno() {prln("no");}
    static void prn() {prln("No");}
    static void prN() {prln("NO");}
    static boolean pryesno(boolean b) {prln(b ? "yes" : "no"); return b;};
    static boolean pryn(boolean b) {prln(b ? "Yes" : "No"); return b;}
    static boolean prYN(boolean b) {prln(b ? "YES" : "NO"); return b;}
    static void prln(int... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static void prln(long... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static void prln(double... a) {for (int i = 0, len = a.length - 1; i < len; pr(a[i]), pr(' '), ++i); if (a.length > 0) prln(a[a.length - 1]); else prln();}
    static <T> void prln(Collection<T> c) {int n = c.size() - 1; Iterator<T> iter = c.iterator(); for (int i = 0; i < n; pr(iter.next()), pr(' '), ++i); if (n >= 0) prln(iter.next()); else prln();}
    static void h() {prln("hlfd"); flush();}
    static void flush() {__out.flush();}
    static void close() {__out.close();}
}


import java.io.*;
import java.util.*;
public class Main
{

	private void solve()throws Exception
	{
		int n=nextInt();
		int c=nextInt();
		long sum=0;
		int a[]=new int[n+1];
		for(int i=1;i<=n;i++)
		{
			a[i]=nextInt();
			sum+=a[i];
		}
		//min[i] is the min element in [i,i+c-1], 0 if invalid
		int min[]=new int[n+1];

		Deque<Integer> deque=new ArrayDeque<>();
		for(int i=1;i<=n;i++)
		{
			while(!deque.isEmpty() && a[deque.peekLast()]>=a[i])
				deque.removeLast();
			deque.addLast(i);
			while(!deque.isEmpty() && deque.peekFirst()<=i-c)
				deque.removeFirst();
			if(i>=c)
				min[i-c+1]=a[deque.peekFirst()];
		}
		//dp[i] is the max removal from elements [i..n]
		long dp[]=new long[n+2];
		for(int i=n;i>=1;i--)
			dp[i]=Math.max(dp[i+1],min[i]+(i+c<=n?dp[i+c]:0));
		out.println(sum-dp[1]);
	}

	///////////////////////////////////////////////////////////

	public void run()throws Exception
	{
		br=new BufferedReader(new InputStreamReader(System.in));
		st=null;
		out=new PrintWriter(System.out);

		solve();

		br.close();
		out.close();
	}
	public static void main(String args[])throws Exception{
		new Main().run();
	}
	BufferedReader br;
	StringTokenizer st;
	PrintWriter out;
	String nextToken()throws Exception{
		while(st==null || !st.hasMoreTokens())
		st=new StringTokenizer(br.readLine());
		return st.nextToken();
	}
	String nextLine()throws Exception{
		return br.readLine();
	}
	int nextInt()throws Exception{
		return Integer.parseInt(nextToken());
	}
	long nextLong()throws Exception{
		return Long.parseLong(nextToken());
	}
	double nextDouble()throws Exception{
		return Double.parseDouble(nextToken());
	}
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;

public class d1 extends PrintWriter {

    static BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
    // static Scanner s=new Scanner(System.in);
    d1 () { super(System.out); }
    public static void main(String[] args) throws IOException{
        d1 d1=new d1();d1.main();d1.flush();
    }
    void main() throws IOException {
        BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer();
        StringBuffer sb1 = new StringBuffer();
        PrintWriter out = new PrintWriter(System.out);
        Deque<Integer> dq=new LinkedList<>();
        int min=Integer.MAX_VALUE;
        String[] s1=s();
        int n=i(s1[0]);int c=i(s1[1]);
        int[] a=new int[n];arr(a,n);long sum=0;
            for(int i:a) sum+=i;
        if(c>n){
            System.out.println(sum);
        }else{long[] mm=new long[n];
        for(int i=0;i<c;i++){
            min=Math.min(min,a[i]);
            while(!dq.isEmpty()&&dq.peekLast()>a[i]){
                dq.pollLast();
            }dq.add(a[i]);

        }mm[c-1]=min;
        long[] dp=new long[n];
        dp[c-1]=min;
        for(int i=c;i<n;i++){
            if(a[i-c]==dq.peekFirst()) dq.pollFirst();
            while(!dq.isEmpty()&&dq.peekLast()>a[i]){
                dq.pollLast();
            }dq.add(a[i]);
           mm[i]=dq.peekFirst();
            dp[i]=Math.max(dp[i-1],dp[i-c]+mm[i]);
          //  System.out.println(i+" "+dp[i]);
        }
            System.out.println(sum-dp[n-1]);
        }
    }int n;int[]z;
    public void zarr(char[] a){
        z=new int[n];
        z[0]=1;int l=0;int r=0;
        for(int i=1;i<a.length;i++){
            if(i<=r){
                if(i+z[i-l]-1>=r){
                    l=i;int j;
                    while(r < a.length && a[r - l] == a[r])
                        r++;
                    z[i]=r-l;
                    r--;
                }else{
                    z[i]=z[i-l];
                }
            }else{
                l=i;
                r=l;int j;
                while(r < a.length && a[r - l] == a[r])
                    r++;

                z[i]=r-l;
                r--;
            }}
    }
    static long powerwithmod(long  x, long y, long p) {
        long  res = 1;
        x = x % p;
        while (y > 0) {
            if ((y & 1) == 1)
                res = (res * x) % p;
            y = y >> 1;
            x = (x * x) % p;
        }
        return res;
    }
    public void arr(int[] a,int n) throws IOException{
        //        BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
        String[] s2=s();
        for(int i=0;i<n;i++){
            a[i]=i(s2[i]);
        }
    }
    public void sort(int[] a,int l,int h){
        if(l==h) return;
        int mid=(l+h)/2;

        sort(a,l,mid);
        sort(a,mid+1,h);
        merge(a,l,(l+h)/2,h);
    }
    void merge(int arr[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int[n1];
        int R[] = new int[n2];
        for (int i = 0; i < n1; ++i)
            L[i] = arr[l + i];
        for (int j = 0; j < n2; ++j)
            R[j] = arr[m + 1 + j];
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            }
            else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }
        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

    static long gcd(long a, long b) {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
    static String[] s() throws IOException {
        return s.readLine().trim().split("\\s+");
    }

    static int i(String ss) {
        return Integer.parseInt(ss);
    }
    static long l(String ss) {
        return Long.parseLong(ss);
    }
}
class Student {
    long  a;int b;int c;
    public Student(int a,int b) {
        this.a=a;this.c=c;this.b=b;
    }
}
class Pair {
    int  a,b,c;
    public Pair(int  a,int b){
        this.a=a;this.b=b;this.c=c;}
}
class Sortbyroll implements Comparator<Student> {

    public int compare(Student a, Student b){
        if(a.b==b.b) return (int)b.a-(int)a.a;
        return   a.b-b.b;}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ECashback solver = new ECashback();
        solver.solve(1, in, out);
        out.close();
    }

    static class ECashback {
        long INF = (long) 1e18 + 1;
        PrintWriter out;
        InputReader in;
        int n;
        long[] segtree;

        long merge(long x, long y) {
            return Math.min(x, y);
        }

        void build_segtree() {
            int i = 0;
            for (i = n - 1; i > 0; i--)
                segtree[i] = merge(segtree[i << 1], segtree[i << 1 | 1]);
        }

        long segtree_query(int l, int r) {
            long res = INF;
            for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
                if ((l & 1) == 1)
                    res = merge(res, segtree[l++]);
                if ((r & 1) == 1)
                    res = merge(res, segtree[--r]);
            }
            return res;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int c = ni();
            long[] arr = new long[n];
            int i = 0;
            long sum = 0;
            long[] pf = new long[n + 1];
            for (i = 0; i < n; i++) {
                sum += arr[i] = nl();
                pf[i + 1] = sum;
            }
            segtree = new long[2 * n];
            for (i = 0; i < n; i++)
                segtree[n + i] = arr[i];
            build_segtree();
            long[] dp = new long[n + 1];
            for (i = 1; i <= n; i++) {
                dp[i] = dp[i - 1] + arr[i - 1];
                if (i >= c)
                    dp[i] = Math.min(dp[i], dp[i - c] + pf[i] - pf[i - c] - segtree_query(i - c, i));
            }
            pn(dp[n]);
        }

        int ni() {
            return in.nextInt();
        }

        long nl() {
            return in.nextLong();
        }

        void pn(long zx) {
            out.println(zx);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new UnknownError();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ECashback solver = new ECashback();
        solver.solve(1, in, out);
        out.close();
    }

    static class ECashback {
        long INF = (long) 1e18 + 1;
        PrintWriter out;
        InputReader in;
        int n;
        long[] segtree;

        long merge(long x, long y) {
            return Math.min(x, y);
        }

        void build_segtree() {
            int i = 0;
            for (i = n - 1; i > 0; i--)
                segtree[i] = merge(segtree[i << 1], segtree[i << 1 | 1]);
        }

        long segtree_query(int l, int r) {
            long res = INF;
            for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
                if ((l & 1) == 1)
                    res = merge(res, segtree[l++]);
                if ((r & 1) == 1)
                    res = merge(res, segtree[--r]);
            }
            return res;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int c = ni();
            long[] arr = new long[n];
            int i = 0;
            long sum = 0;
            for (i = 0; i < n; i++)
                sum += arr[i] = nl();
            segtree = new long[2 * n];
            for (i = 0; i < n; i++)
                segtree[n + i] = arr[i];
            build_segtree();
            long[] dp = new long[n + 1];
            for (i = 1; i <= n; i++) {
                if (i >= c)
                    dp[i] = dp[i - c] + segtree_query(i - c, i);
            }
            long ans = INF;
            for (i = 0; i <= n; i++)
                ans = Math.min(ans, sum - dp[i]);
            pn(ans);
        }

        int ni() {
            return in.nextInt();
        }

        long nl() {
            return in.nextLong();
        }

        void pn(long zx) {
            out.println(zx);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new UnknownError();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        ECashback solver = new ECashback();
        solver.solve(1, in, out);
        out.close();
    }

    static class ECashback {
        long INF = (long) 1e18 + 1;
        PrintWriter out;
        InputReader in;
        int n;
        long[] segtree;

        long merge(long x, long y) {
            return Math.min(x, y);
        }

        void build_segtree() {
            int i = 0;
            for (i = n - 1; i > 0; i--)
                segtree[i] = merge(segtree[i << 1], segtree[i << 1 | 1]);
        }

        long segtree_query(int l, int r) {
            long res = INF;
            for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
                if ((l & 1) == 1)
                    res = merge(res, segtree[l++]);
                if ((r & 1) == 1)
                    res = merge(res, segtree[--r]);
            }
            return res;
        }

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            this.out = out;
            this.in = in;
            n = ni();
            int c = ni();
            long[] arr = new long[n];
            int i = 0;
            long sum = 0;
            for (i = 0; i < n; i++)
                sum += arr[i] = nl();
            segtree = new long[2 * n];
            for (i = 0; i < n; i++)
                segtree[n + i] = arr[i];
            build_segtree();
            long[] dp = new long[n + 1];
            for (i = 1; i <= n; i++) {
                if (i >= c)
                    dp[i] = dp[i - c] + segtree_query(i - c, i);
            }
            long ans = INF;
            for (i = 1; i <= n; i++)
                ans = Math.min(ans, sum - dp[i]);
            pn(ans);
        }

        int ni() {
            return in.nextInt();
        }

        long nl() {
            return in.nextLong();
        }

        void pn(long zx) {
            out.println(zx);
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new UnknownError();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new UnknownError();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuffer res = new StringBuffer();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));

            return res.toString();
        }

        private boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

    }
}


#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define f first
#define s second
#define N 100005
using namespace std;
long long dp[N],pref[N],a[N],minn[N];
multiset <int> st;
int main (){
    int n,c;
    cin>>n>>c;

    for (int i=1;i<=n;i++)
        cin>>a[i],pref[i] = pref[i - 1] + a[i];

    int l = 0;
    for (int i=1;i<=c;i++)
        st.insert(a[i]);

    minn[c] = *st.begin();
    for (int i = c + 1;i<=n;i++){
        st.erase(st.find(a[i - c]));
        st.insert(a[i]);
        minn[i] = *st.begin();
    }

    for (int i=1;i<=n;i++)
        dp[i] = min(dp[i - 1] + a[i],dp[i - c] + pref[i] - pref[i - c] - minn[i]);
    cout<<dp[n];
}


#include <bits/stdc++.h>
#define fast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define pb push_back
#define f first
#define s second
#define N 100005
using namespace std;
long long dp[N],pref[N];
int a[N],minn[N];
multiset <int> st;
int main (){
    int n,c;
    cin>>n>>c;

    for (int i=1;i<=n;i++)
        cin>>a[i],pref[i] = pref[i - 1] + a[i];

    int l = 0;
    for (int i=1;i<=c;i++)
        st.insert(a[i]);

    minn[c] = *st.begin();
    for (int i = c + 1;i<=n;i++){
        st.erase(st.find(a[i - c]));
        st.insert(a[i]);
        minn[i] = *st.begin();
    }

    for (int i=1;i<=n;i++)
        dp[i] = min(dp[i - 1] + a[i],dp[i - c] + pref[i] - pref[i - c] - minn[i]);
    cout<<dp[n];
}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ld long double
#define ll long long
#define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define F first
#define S second
using namespace std;
using namespace __gnu_pbds;
//typedef tree<int, nuint_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
vector <int> pref_function (string s)
{
    vector <int> ans(s.size());
    ans[0] = 0;
    for (int i = 1;i < s.size();i++)
    {
        int h = ans[i-1];
        while (h && s[i]!=s[h])
            h = ans[h-1];
        if (s[i]==s[h]) h++;
        ans[i] = h;
    }
    return ans;
}
vector <int> zed_function (string s)
{
    vector <int> z(s.size());
    z[0] = 0;
    int l = 0;
    int r = 0;
    for (int i = 1;i < s.size();i++)
    {
        if (i<=r) z[i] = min(z[i-l],r-i+1);
        while ((i+z[i])<s.size() && (s[z[i]]==s[i+z[i]])) z[i]++;
        if ((i+z[i]-1)>r) l = i,r = i + z[i] - 1;
    }
    return z;
}
int ans;
void output() {
    cout<<ans;
    exit(0);
}
struct zapr {
ll l;
ll r;
char t;
};
void ansuk() {

}
void delite() {

}
void vstavka() {

}
vector <ll> t(1e6);
ll get (ll v,ll tl,ll tr,ll l,ll r)
{
    if (l>r) return 0;
    if (tl>r || tr<l) return 0;
    if (tl>=l && tr<=r) return t[v];
    ll mid = (tl+tr)/2;
    ll a1 = get(v*2,tl,mid,l,r);
    ll a2 = get(v*2+1,mid+1,tr,l,r);
    if ((a1) && !a2) return a1;
    else if (!a1 && a2) return a2;
    else return min(a1,a2);
}
vector <zapr> q;
void sulve(ll l,ll r) {
}
ll st = 1;
ll n,k;
vector <int> a;
void init() {
    cin>>n>>k;
    a.resize(n);
    for (int i = 0;i < n;i++)
    {
        cin>>a[i];
    }
    while (st<n) st*=2;
    for (int i = 0;i < n;i++)
    t[i+st] = a[i];
    for (int i = st-1;i>=0;i--)
    {
        ll a1 = t[i*2];
        ll a2 = t[i*2+1];
        if (a1 && !a2) t[i] = a1;
        else if (!a1 && a2) t[i] = a2;
        else t[i] = min(a1,a2);
    }
    ll uk = n - k;
    ll sum = 0;
    for (int i = 0;i < n;i++)
        sum+=a[i];
}
void solve() {
    multiset <ll> st;
    vector <ll> prefs(n);
    for (int i = 0;i < n;i++)
    prefs[i] = (i ? prefs[i-1] : 0) + a[i];
    vector <ll> dp(n,1e18);
    dp[0] = a[0];
    if (n==1) {cout<<0;exit(0);}
    st.insert(a[0]);
    for (int i = 1;i < n;i++)
    {
        ll ss = prefs[i];
        if ((i-k)<0) ss-=0;
        else ss-=prefs[i-k];
        st.insert(a[i]);
        if (st.size()>k) {
            st.erase(st.find(a[i-k]));
        }
        ll git = *st.begin();
        if ((i>(k-1))) dp[i] = min(dp[i-1]+a[i],dp[i-k] + ss - git);
        else if (i==(k-1)) dp[i] = min(dp[i-1]+a[i],ss-git);
        else dp[i] = dp[i-1] + a[i];
    }
    cout<<dp[n-1];exit(0);
}
int main() {
    IOS;
    init();
    solve();
return 0;}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ld long double
#define ll long long
#define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define F first
#define S second
using namespace std;
using namespace __gnu_pbds;
//typedef tree<int, nuint_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
vector <int> pref_function (string s)
{
    vector <int> ans(s.size());
    ans[0] = 0;
    for (int i = 1;i < s.size();i++)
    {
        int h = ans[i-1];
        while (h && s[i]!=s[h])
            h = ans[h-1];
        if (s[i]==s[h]) h++;
        ans[i] = h;
    }
    return ans;
}
vector <int> zed_function (string s)
{
    vector <int> z(s.size());
    z[0] = 0;
    int l = 0;
    int r = 0;
    for (int i = 1;i < s.size();i++)
    {
        if (i<=r) z[i] = min(z[i-l],r-i+1);
        while ((i+z[i])<s.size() && (s[z[i]]==s[i+z[i]])) z[i]++;
        if ((i+z[i]-1)>r) l = i,r = i + z[i] - 1;
    }
    return z;
}
int ans;
void output() {
    cout<<ans;
    exit(0);
}
struct zapr {
ll l;
ll r;
char t;
};
void ansuk() {

}
void delite() {

}
void vstavka() {

}
vector <ll> t(1e6);
ll get (ll v,ll tl,ll tr,ll l,ll r)
{
    if (l>r) return 0;
    if (tl>r || tr<l) return 0;
    if (tl>=l && tr<=r) return t[v];
    ll mid = (tl+tr)/2;
    ll a1 = get(v*2,tl,mid,l,r);
    ll a2 = get(v*2+1,mid+1,tr,l,r);
    if ((a1) && !a2) return a1;
    else if (!a1 && a2) return a2;
    else return min(a1,a2);
}
vector <zapr> q;
void sulve(ll l,ll r) {
}
ll st = 1;
ll n,k;
vector <int> a;
void init() {
    cin>>n>>k;
    a.resize(n);
    for (int i = 0;i < n;i++)
    {
        cin>>a[i];
    }
    while (st<n) st*=2;
    for (int i = 0;i < n;i++)
    t[i+st] = a[i];
    for (int i = st-1;i>=0;i--)
    {
        ll a1 = t[i*2];
        ll a2 = t[i*2+1];
        if (a1 && !a2) t[i] = a1;
        else if (!a1 && a2) t[i] = a2;
        else t[i] = min(a1,a2);
    }
    ll uk = n - k;
    ll sum = 0;
    for (int i = 0;i < n;i++)
        sum+=a[i];
}
void solve() {
    multiset <ll> st;
    vector <ll> prefs(n);
    for (int i = 0;i < n;i++)
    prefs[i] = (i ? prefs[i-1] : 0) + a[i];
    vector <ll> dp(n,1e18);
    dp[0] = a[0];
    if (k==1) {cout<<0;exit(0);}
    st.insert(a[0]);
    for (int i = 1;i < n;i++)
    {
        ll ss = prefs[i];
        if ((i-k)<0) ss-=0;
        else ss-=prefs[i-k];
        st.insert(a[i]);
        if (st.size()>k) {
            st.erase(st.find(a[i-k]));
        }
        ll git = *st.begin();
        if ((i>(k-1))) dp[i] = min(dp[i-1]+a[i],dp[i-k] + ss - git);
        else if (i==(k-1)) dp[i] = min(dp[i-1]+a[i],ss-git);
        else dp[i] = dp[i-1] + a[i];
    }
    cout<<dp[n-1];exit(0);
}
int main() {
    IOS;
    init();
    solve();
return 0;}


#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ld long double
#define ll long long
#define IOS ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define F first
#define S second
using namespace std;
using namespace __gnu_pbds;
//typedef tree<int, nuint_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
vector <int> pref_function (string s)
{
    vector <int> ans(s.size());
    ans[0] = 0;
    for (int i = 1;i < s.size();i++)
    {
        int h = ans[i-1];
        while (h && s[i]!=s[h])
            h = ans[h-1];
        if (s[i]==s[h]) h++;
        ans[i] = h;
    }
    return ans;
}
vector <int> zed_function (string s)
{
    vector <int> z(s.size());
    z[0] = 0;
    int l = 0;
    int r = 0;
    for (int i = 1;i < s.size();i++)
    {
        if (i<=r) z[i] = min(z[i-l],r-i+1);
        while ((i+z[i])<s.size() && (s[z[i]]==s[i+z[i]])) z[i]++;
        if ((i+z[i]-1)>r) l = i,r = i + z[i] - 1;
    }
    return z;
}
int ans;
void output() {
    cout<<ans;
    exit(0);
}
struct zapr {
ll l;
ll r;
char t;
};
void ansuk() {

}
void delite() {

}
void vstavka() {

}
vector <ll> t(1e6);
ll get (ll v,ll tl,ll tr,ll l,ll r)
{
    if (l>r) return 0;
    if (tl>r || tr<l) return 0;
    if (tl>=l && tr<=r) return t[v];
    ll mid = (tl+tr)/2;
    ll a1 = get(v*2,tl,mid,l,r);
    ll a2 = get(v*2+1,mid+1,tr,l,r);
    if ((a1) && !a2) return a1;
    else if (!a1 && a2) return a2;
    else return min(a1,a2);
}
vector <zapr> q;
void sulve(ll l,ll r) {
}
ll st = 1;
ll n,k;
vector <int> a;
void init() {
    cin>>n>>k;
    a.resize(n);
    for (int i = 0;i < n;i++)
    {
        cin>>a[i];
    }
    while (st<n) st*=2;
    for (int i = 0;i < n;i++)
    t[i+st] = a[i];
    for (int i = st-1;i>=0;i--)
    {
        ll a1 = t[i*2];
        ll a2 = t[i*2+1];
        if (a1 && !a2) t[i] = a1;
        else if (!a1 && a2) t[i] = a2;
        else t[i] = min(a1,a2);
    }
    ll uk = n - k;
    ll sum = 0;
    for (int i = 0;i < n;i++)
        sum+=a[i];
}
void solve() {
    multiset <ll> st;
    vector <ll> prefs(n);
    for (int i = 0;i < n;i++)
    prefs[i] = (i ? prefs[i-1] : 0) + a[i];
    vector <ll> dp(n,1e18);
    dp[0] = a[0];
    st.insert(a[0]);
    for (int i = 1;i < n;i++)
    {
        ll ss = prefs[i];
        if ((i-k)<0) ss-=0;
        else ss-=prefs[i-k];
        st.insert(a[i]);
        if (st.size()>k) {
            st.erase(st.find(a[i-k]));
        }
        ll git = *st.begin();
        if ((i>(k-1))) dp[i] = min(dp[i-1]+a[i],dp[i-k] + ss - git);
        else if (i==(k-1)) dp[i] = min(dp[i-1]+a[i],ss-git);
        else dp[i] = dp[i-1] + a[i];
    }
    cout<<dp[n-1];exit(0);
}
int main() {
    IOS;
    init();
    solve();
return 0;}


#include <algorithm>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;

typedef long long ll;

constexpr ll INF = 1e16;

int n, c;
ll board[100001];
ll sum[100001];
ll memo[100001];

ll dp(int pos)
{
    if (pos < 0) return INF;
    if (pos == 0) return 0;

    if (memo[pos] == -1)
    {
        memo[pos] = min(dp(pos - 1) + board[pos], dp(pos - c) + sum[pos]);
    }
    return memo[pos];
}

int main(void)
{
    //freopen("input.txt", "r", stdin);

    scanf("%d %d", &n, &c);
    for (int i = 0; i < n; i++)
        scanf("%lld", &board[i + 1]);

    ll curr = 0;
    multiset<ll> s;
    multiset<ll>::iterator it;
    for (int i = 1; i <= n; i++)
    {
        curr += board[i];
        s.insert(board[i]);
        if (i >= c)
        {
            curr -= *(s.begin());
            sum[i] = curr;
            curr += *(s.begin());
            curr -= board[i - c + 1];
            it = s.find(board[i - c + 1]);
            s.erase(it);
        }
        else
            sum[i] = INF;
    }

    memset(memo, -1, sizeof(memo));
    printf("%lld\n", dp(n));

    return 0;
}


#include <cstdio>
#include <cstring>
#include <set>
using namespace std;

typedef long long ll;

constexpr ll INF = 1e16;

int n, c;
ll board[100001];
ll sum[100001];
ll memo[100001];

ll dp(int pos)
{
    if (pos < 0) return INF;
    if (pos == 0) return 0;

    if (memo[pos] == -1)
    {
        memo[pos] = min(dp(pos - 1) + board[pos], dp(pos - c) + sum[pos]);
    }
    return memo[pos];
}

int main(void)
{
    //freopen("input.txt", "r", stdin);

    scanf("%d %d", &n, &c);
    for (int i = 0; i < n; i++)
        scanf("%lld", &board[i + 1]);

    ll curr = 0;
    multiset<ll> s;
    for (int i = 1; i <= n; i++)
    {
        curr += board[i];
        s.insert(board[i]);
        if (i >= c)
        {
            curr -= *(s.begin());
            sum[i] = curr;
            curr += *(s.begin());
            curr -= board[i - c + 1];
            s.erase(board[i - c + 1]);
        }
        else
            sum[i] = INF;
    }

    memset(memo, -1, sizeof(memo));
    printf("%lld\n", dp(n));

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define ld double
#define vec vector<ll>
#define ms set<ll>
#define int long long
#define f(i,x,n) for(int i=x;i<n;i++)
#define modd 1000000007
#define endl "\n"
void fastio()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
}
set<pair<double,double>>se;
set<double>slp;
map<double,int>mpp;
int32_t main()
{
	fastio();
	int n;
	cin>>n;
	int x[n],y[n];
	f(i,0,n)
	{
		cin>>x[i]>>y[i];
	}
	int ans=0;
	int pp=0;
	f(i,0,n)
	{
		f(j,i+1,n)
		{
			int a1=x[j]-x[i],a2=y[j]-y[i];
			int gg=__gcd(abs(a1),abs(a2));
			a1=a1/gg;
			a2=a2/gg;
			double slope=(double)a2/(double)a1;
			if(se.count(mp(i,slope))==0)
			{
				ans++;
				if(slp.count(slope)!=0)
				{
					mpp[slope]++;
				}
			}
			slp.insert(slope);
			se.insert(mp(i,slope));
			se.insert(mp(j,slope));
		}
	}
	int fa=(ans*(ans-1))/2;
	for(auto i=slp.begin();i!=slp.end();++i)
	{
		int kk=mpp[*i];
		kk++;
		fa=fa-((kk*(kk-1))/2);
	}
	cout<<fa;
}


#include<bits/stdc++.h>
using namespace std;
#define  ld long double
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    pair<ld, ld> arr[N];
    map<ld, int64_t> g;
    int k1[1000000] = {0}, k2[1000000] = {0};
    map<pair<ld, ld>, ld> jrr;
    for (int i = 0; i < N; i++) {
        ld x, y;
        cin >> x >> y;
        arr[i] = {x, y};
    }
    for (int i = 0; i < N - 1; i++) {
        for (int j = i + 1; j < N; j++) {
            ld x1 = arr[i].first, x2 = arr[j].first, y1 = arr[i].second, y2 = arr[j].second;
            if (y1 == y2) {
                ld slope = 0;
                if (!k1[(int)y1]) g[slope]++, k1[(int)y1] = 1;//cout << i << ' ' << j << '\n';
            }
            else {
                if (x1 == x2) {
                    ld slope = 100000000;
                    if (!k2[(int)x1]) g[slope]++, k2[(int)x1] = 1;//cout << i << ' ' << j << '\n';
                }
                else {
                    ld slope = (y1 - y2) / (x1 - x2);
                    ld q = y1 - slope * x1;
                    if (jrr.find({q, slope}) == jrr.end()) g[slope]++, jrr[{q,slope}] = 1;
                    //cout << i << ' ' << j << '\n';
                }
            }
        }
    }
    int64_t ans = 0;
    vector<ld> v;
    for (auto to : g) v.push_back(to.second);
    //cout << v.size() << '\n';
    for (int i = 0; i < v.size() - 1; i++) {
        for (int j = i + 1; j < v.size(); j++) ans += v[i] * v[j];
    }
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
map<pair<int, int>, set<int>> sl;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N, total = 0, ans = 0;
    cin >> N;
    int X[N], Y[N];
    for (int i = 0; i < N; i++) cin >> X[i] >> Y[i];

    for (int i = 0; i < N - 1; i++) {
        for (int j = i + 1; j < N; j++) {

            int x1 = X[i], y1 = Y[i], x2 = X[j], y2 = Y[j];
            int a = y1 - y2, b = x1 - x2;
            int d = __gcd(a, b);
            a /= d, b /= d;

            if (a < 0 || (a == 0 && b < 0)) a *= -1, b *= -1;
            int c = x1 * a - y1 * b;

            if (sl[{a, b}].find(c) == sl[{a, b}].end()) {
                total++;
                sl[{a, b}].insert(c);
                ans += total - sl[{a, b}].size();
            }

        }
    }
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
#define  ld long double
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    pair<ld, ld> arr[N];
    map<ld, int64_t> g, k1, k2;
    //int k1[1000000] = {0}, k2[1000000] = {0};
    map<pair<ld, ld>, ld> jrr;
    for (int i = 0; i < N; i++) {
        ld x, y;
        cin >> x >> y;
        arr[i] = {x, y};
    }
    for (int i = 0; i < N - 1; i++) {
        for (int j = i + 1; j < N; j++) {
            ld x1 = arr[i].first, x2 = arr[j].first, y1 = arr[i].second, y2 = arr[j].second;
            if (y1 == y2) {
                ld slope = 0;
                if (k1.find(y1) == k1.end()) g[slope]++, k1[y1] = 1;//cout << i << ' ' << j << '\n';
            }
            else {
                if (x1 == x2) {
                    ld slope = 100000000;
                    if (k2.find(x1) == k2.end()) g[slope]++, k2[x1] = 1;//cout << i << ' ' << j << '\n';
                }
                else {
                    ld slope = (y1 - y2) / (x1 - x2);
                    ld q = y1 - slope * x1;
                    if (jrr.find({q, slope}) == jrr.end()) g[slope]++, jrr[{q,slope}] = 1;
                    //cout << i << ' ' << j << '\n';
                }
            }
        }
    }
    int64_t ans = 0;
    vector<ld> v;
    for (auto to : g) v.push_back(to.second);
    //cout << v.size() << '\n';
    for (int i = 0; i < v.size() - 1; i++) {
        for (int j = i + 1; j < v.size(); j++) ans += v[i] * v[j];
    }
    cout << ans;
}


#include<bits/stdc++.h>
using namespace std;
#define  ld long double
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    pair<ld, ld> arr[N];
    map<ld, int64_t> g;
    int k1[100000] = {0}, k2[100000] = {0};
    map<pair<ld, ld>, int64_t> jrr;
    for (int i = 0; i < N; i++) {
        ld x, y;
        cin >> x >> y;
        arr[i] = {x, y};
    }
    for (int i = 0; i < N - 1; i++) {
        for (int j = i + 1; j < N; j++) {
            ld x1 = arr[i].first, x2 = arr[j].first, y1 = arr[i].second, y2 = arr[j].second;
            if (y1 == y2) {
                ld slope = 0;
                if (!k1[(int)y1]) g[slope]++, k1[(int)y1] = 1;//cout << i << ' ' << j << '\n';
            }
            else {
                if (x1 == x2) {
                    ld slope = 100000000;
                    if (!k2[(int)x1]) g[slope]++, k2[(int)x1] = 1;//cout << i << ' ' << j << '\n';
                }
                else {
                    ld slope = (y1 - y2) / (x1 - x2);
                    ld q = y1 - slope * x1;
                    if (jrr.find({q, slope}) == jrr.end()) g[slope]++, jrr[{q,slope}] = 1;
                    //cout << i << ' ' << j << '\n';
                }
            }
        }
    }
    int64_t ans = 0;
    vector<ld> v;
    for (auto to : g) v.push_back(to.second);
    //cout << v.size() << '\n';
    for (int i = 0; i < v.size() - 1; i++) {
        for (int j = i + 1; j < v.size(); j++) ans += v[i] * v[j];
    }
    cout << ans;
}