stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#define N 305

using namespace std;

inline int rd() {

    int x=0,f=1;char ch=getchar();

    while (ch>'9'||ch<'0') {if(ch=='-')f=-1;ch=getchar();}

    while (ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}

    return x*f;

}

int n,m,a[N][N],vis[N],cnt[N];

int main() {

    n = rd(), m = rd();

    for (int i=1;i<=n;i++)

        for (int j=1;j<=m;j++) a[i][j] = rd();

    for (int i=1;i<=m;i++) vis[i] = 1;

    int ans = n;

    for (int _=1;_<=m;_++) {

        memset(cnt,0,sizeof(cnt));

        for (int i=1;i<=n;i++)

            for (int j=1;j<=m;j++)

                if (vis[ a[i][j] ]) {

                    cnt[ a[i][j] ]++;

                    break;

                }

        int mx = 0, id = 0;

        for (int i=1;i<=m;i++)

            if (cnt[i] >= mx)

                mx = cnt[i], id = i;

        ans = min(ans, mx);

        vis[id] = 0;

    }

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

    return 0;

}

// Optimized solution to the same problem (faster):
#include <stdint.h>

#include <stdio.h>



/* At most three digits. */

unsigned readUnsigned() {

    int c;

    do { c = ::getchar_unlocked(); } while (c < '0');

    unsigned result = c - '0';

    if ((c = ::getchar_unlocked()) >= '0') {

        result *= 10;

        result += c - '0';

        if ((c = ::getchar_unlocked()) >= '0') {

            result *= 10;

            result += c - '0';

        }

    }

    return result;

}



unsigned N, M;

unsigned A[304][304];



bool valid(unsigned x) {

    unsigned I[304] = {0};

    bool bad[304] = {0};

    for (unsigned sports = M;;) {

        unsigned counts[304] = {0};

        for (unsigned i = 0; i < N; ++i) {

            if (++counts[A[i][I[i]]] > x) {

                unsigned badsport = A[i][I[i]];

                bad[badsport] = true;

                if (--sports * x < N) return false;

                for (unsigned j = 0; j < N; ++j) {

                    while (bad[A[j][I[j]]]) {

                        ++I[j];

                    }

                }

                goto next;

            }

        }

        return true;

next:;

    }

    return false;

}



int main() {

    N = readUnsigned();

    M = readUnsigned();

    for (unsigned i = 0; i < N; ++i) {

        for (unsigned j = 0; j < M; ++j) {

            A[i][j] = readUnsigned() - 1;

        }

    }



    unsigned x = 0;

    for (unsigned step = M; step; step /= 2) {

        while (!valid(x + step)) x += step;

    }

    printf("%u\n", x + 1);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



int n,m,ans,mark[305],cnt[305],g[305][305];



int main(){

    scanf("%d%d",&n,&m);

    for(int i=1;i<=n;i++){

        for(int j=1;j<=m;j++)

          scanf("%d",&g[i][j]);

    }

    ans=n+1;

    for(int i=1;i<=m;i++){

        memset(cnt,0,sizeof(cnt));

        for(int j=1;j<=n;j++){

            int k=1;

            while(mark[g[j][k]]==1) k++;

            cnt[g[j][k]]++;

        }

        int mx=0;

        for(int j=1;j<=m;j++)

          if(cnt[j]>cnt[mx]) mx=j;

        ans=min(ans,cnt[mx]);

        mark[mx]=1;

    }

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

    return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;



const int MAXN = 305;

int g[MAXN][MAXN], pro[MAXN];

bool be_deleted[MAXN];



int main() {

	int n, m;

	scanf("%d%d", &n, &m);

	for (register int i = 1; i <= n; i++)

		for (register int j = 1; j <= m; j++)

			scanf("%d", &g[i][j]);

	int ans = n;

	for (register int i = 1; i <= m; i++) {

		memset(pro, 0, sizeof(pro));

		for (register int j = 1; j <= n; j++)

			for (register int k = 1; k <= m; k++) {

				int temp = g[j][k];

				if (!be_deleted[temp]) {

					pro[temp]++;

					break;

				}

			}

		int pos = int(max_element(pro+1, pro+1+m) - pro);

		ans = min(ans, pro[pos]);

		be_deleted[pos] = true;

	}

	printf("%d", ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define maxn 500

#define rt register int

using namespace std;

int n,m;

int a[maxn][maxn],vis[maxn],cnt[maxn];

inline int min(int a,int b){

	return a<b?a:b;

}

inline int read()

{

    int x=0,f=0;char s=getchar();

    while(!isdigit(s))f|=s=='-',s=getchar();

    while( isdigit(s))x=(x<<1)+(x<<3)+s-48,s=getchar();

    return !f?x:-x;

}

inline void print(int x)

{

    if(x/10>0)print(x/10);

    putchar(x%10+'0');

}

int main(){

    n=read();m=read();

    for (rt i=1;i<=n;i++)

        for (rt j=1;j<=m;j++)

            a[i][j]=read();

    int ans=n;

    for (rt i=1;i<=m;i++){

        memset(cnt,0,sizeof(cnt));

        for (rt j=1;j<=n;j++)

            for (rt k=1;k<=m;k++)

                if (!vis[a[j][k]]){

                    cnt[a[j][k]]++;

                    break;

                }

        int s,sum=0;

        for (rt j=1;j<=m;j++)

            if (sum<cnt[j])

                s=j,sum=cnt[j];

        vis[s]=1;

        ans=min(ans,sum);

    }

    print(ans);

    return 0;

}

// Optimized solution to the same problem (faster):
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;



const int MAXN = 305;

int g[MAXN][MAXN], pro[MAXN];

bool be_deleted[MAXN];



int main() {

	int n, m;

	scanf("%d%d", &n, &m);

	for (register int i = 1; i <= n; i++)

		for (register int j = 1; j <= m; j++)

			scanf("%d", &g[i][j]);

	int ans = n;

	for (register int i = 1; i <= m; i++) {

		memset(pro, 0, sizeof(pro));

		for (register int j = 1; j <= n; j++)

			for (register int k = 1; k <= m; k++) {

				int temp = g[j][k];

				if (!be_deleted[temp]) {

					pro[temp]++;

					break;

				}

			}

		int pos = int(max_element(pro+1, pro+1+m) - pro);

		ans = min(ans, pro[pos]);

		be_deleted[pos] = true;

	}

	printf("%d", ans);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int n,m,ans;

int a[305][305],sum[305];

bool vis[305];

int main(){

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			scanf("%d",&a[i][j]);

	ans=n;

	for(int i=1;i<=m;i++)

	{

		for(int j=1;j<=m;j++) sum[j]=0;

		for(int j=1;j<=n;j++)

			for(int k=1;k<=m;k++)

				if( !vis[a[j][k]] ) 

				{

					++sum[a[j][k]]; 

					break;

				}

		int k=1;

		for(int j=1;j<=m;j++) 

			if(sum[j]>sum[k]) k=j;

		vis[k]=true;

		if(sum[k]<ans) ans=sum[k];

	}

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

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int inf=1e8,maxn=305;

inline int read(){int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}

int n,m,ans,cnt[maxn],a[maxn][maxn];

bool cho[maxn];

inline void init(){

	n=read(); m=read();

	for (int i=1;i<=n;i++){

		for (int j=1;j<=m;j++){

			a[i][j]=read();

		}

	}

	ans=inf;

	memset(cho,1,sizeof(cho));

	for (int k=1;k<=m;k++){

		memset(cnt,0,sizeof(cnt));

		for (int i=1;i<=n;i++){

			for (int j=1;j<=m;j++){

				if (cho[a[i][j]]){

					cnt[a[i][j]]++;

					break;

				}

			}

		}

		int mx=0;

		for (int i=1;i<=m;i++){

			if ((!mx)||(mx&&cnt[mx]<cnt[i])){

				mx=i;

			}

		}

		cho[mx]=0;

		ans=min(cnt[mx],ans);

	}

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

}

int main(){

	init();

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define ll long long

#define For(i,a,b) for (int i=a;i<=b;i++)

#define Rep(i,a,b) for (int i=b;i>=a;i--)

using namespace std;

inline ll read()

{

    ll x=0,f=1;char ch=getchar();

    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();

    return x*f;

} 

int n,m,k,ans;  

int a[305][305],b[305];  

bool ban[305];  

int main()

{  

    n=read();m=read();

    For(i,1,n)For(j,1,m)a[i][j]=read();

    ans=n;  

    For(i,1,m)

    {

    	For(j,1,m)b[j]=0;

    	For(j,1,n)

    		For(k,1,m)

    			if(!ban[a[j][k]])

    			{

    				b[a[j][k]]++;

    				break;

				}

		int k=1;

		For(j,1,m)

			if(b[j]>b[k])k=j;

		ban[k]=true;

		if(b[k]<ans)ans=b[k];

	}

	cout<<ans<<endl;

    return 0;  

}  


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int inf=1e8,maxn=305;

inline int read(){int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}

int n,m,ans,cnt[maxn],a[maxn][maxn];

bool cho[maxn];

inline void init(){

	n=read(); m=read();

	for (int i=1;i<=n;i++){

		for (int j=1;j<=m;j++){

			a[i][j]=read();

		}

	}

	ans=inf;

	memset(cho,1,sizeof(cho));

	for (int k=1;k<=m;k++){

		memset(cnt,0,sizeof(cnt));

		for (int i=1;i<=n;i++){

			for (int j=1;j<=m;j++){

				if (cho[a[i][j]]){

					cnt[a[i][j]]++;

					break;

				}

			}

		}

		int mx=0;

		for (int i=1;i<=m;i++){

			if ((!mx)||(mx&&cnt[mx]<cnt[i])){

				mx=i;

			}

		}

		cho[mx]=0;

		ans=min(cnt[mx],ans);

	}

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

}

int main(){

	init();

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

const int N=305;

int a[N][N];

bool b[N];

queue<int>f[N];

int n,m;

int main()

{

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			scanf("%d",&a[i][j]);

	for(int i=1;i<=n;i++)

		f[a[i][1]].push(i),a[i][0]=1;

	memset(b,true,sizeof(b));

	int ret=0;

	for(int i=1;i<=m;i++)

		if(f[i].size()>ret)ret=f[i].size();

	int ans=ret;

	while(1){

		int k=-1;

		for(int i=1;i<=m;i++)

			if(k==-1||f[i].size()>f[k].size())k=i;

		if(k==-1)break;

		b[k]=false;

		while(!f[k].empty()){

			int i=f[k].front();

			f[k].pop();

			while(!b[a[i][a[i][0]]])a[i][0]++;

			f[a[i][a[i][0]]].push(i);

		}

		int ret=0;

		for(int i=1;i<=m;i++)

			if(f[i].size()>ret)ret=f[i].size();

		if(ret==0)break;

		ans=min(ans,ret);

	}

	printf("%d",ans);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int inf=1e8,maxn=305;

inline int read(){int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}

int n,m,ans,cnt[maxn],a[maxn][maxn];

bool cho[maxn];

inline void init(){

	n=read(); m=read();

	for (int i=1;i<=n;i++){

		for (int j=1;j<=m;j++){

			a[i][j]=read();

		}

	}

	ans=inf;

	memset(cho,1,sizeof(cho));

	for (int k=1;k<=m;k++){

		memset(cnt,0,sizeof(cnt));

		for (int i=1;i<=n;i++){

			for (int j=1;j<=m;j++){

				if (cho[a[i][j]]){

					cnt[a[i][j]]++;

					break;

				}

			}

		}

		int mx=0;

		for (int i=1;i<=m;i++){

			if ((!mx)||(mx&&cnt[mx]<cnt[i])){

				mx=i;

			}

		}

		cho[mx]=0;

		ans=min(cnt[mx],ans);

	}

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

}

int main(){

	init();

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <algorithm>

#include <cmath>

#include <cstring>

#include <iostream>

#include <ctime>

#include <map>

#include <queue>

#include <cstdlib>

#include <string>

#include <climits>

#include <set>

#include <vector>

using namespace std;

inline int read(){

	int k=0,f=1;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}

	return k*f;

}

int a[310][310],n,m,b[310],c[310];

bool flag;

inline void clear(int p,int x){

	if(!flag)return;b[p]=-1;

	for(int i=1;i<=n;i++){

		int cnt=0;

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++,cnt++;

		if(cnt==0)continue;

		if(c[i]>m){flag=0;return;}

		b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

}

inline bool check(int x){

	memset(b,0,sizeof b);

	flag=1;

	for(int i=1;i<=n;i++)c[i]=1;

	for(int i=1;i<=n;i++){

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++;

		if(c[i]>m)return 0;

		if(c[i]==1)b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

	return flag;

}

int main()

{

	n=read();m=read();

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)a[i][j]=read();

	int l=0,r=n,ans;

	while(l<=r){

		int mid=l+r>>1;

		if(check(mid))ans=mid,r=mid-1;

		else l=mid+1;

	}

	printf("%d",ans);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int inf=1e8,maxn=305;

inline int read(){int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}

int n,m,ans,cnt[maxn],a[maxn][maxn];

bool cho[maxn];

inline void init(){

	n=read(); m=read();

	for (int i=1;i<=n;i++){

		for (int j=1;j<=m;j++){

			a[i][j]=read();

		}

	}

	ans=inf;

	memset(cho,1,sizeof(cho));

	for (int k=1;k<=m;k++){

		memset(cnt,0,sizeof(cnt));

		for (int i=1;i<=n;i++){

			for (int j=1;j<=m;j++){

				if (cho[a[i][j]]){

					cnt[a[i][j]]++;

					break;

				}

			}

		}

		int mx=0;

		for (int i=1;i<=m;i++){

			if ((!mx)||(mx&&cnt[mx]<cnt[i])){

				mx=i;

			}

		}

		cho[mx]=0;

		ans=min(cnt[mx],ans);

	}

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

}

int main(){

	init();

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>

#include <ext/pb_ds/tree_policy.hpp>



using namespace std;

using namespace __gnu_pbds;



#define fi first

#define se second

#define mp make_pair

#define pb push_back

#define fbo find_by_order

#define ook order_of_key

#define INF 1e9



typedef long long ll;

typedef pair<ll,ll> ii;

typedef vector<int> vi;

typedef vector < pair<int, int> > vii;

typedef long double ld;

typedef tree<pair<int,int>, null_type, less<pair<int,int> >, rb_tree_tag, tree_order_statistics_node_update> pbds;

typedef set<int>::iterator sit;

typedef map<int,int>::iterator mit;

typedef vector<int>::iterator vit;



queue <int> a[301];

ll n, m, x, ans, mx, cur, cnt[301];

bool disabled[301];



int main()

{

	cin >> n >> m;

	ans=n;

	for(int i = 0; i < n; i++){

		for(int j = 0; j < m; j++){

			cin >> x;

			a[i].push(x);

		}

	}

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

		for(int j = 0; j < n; j++){

			while(disabled[a[j].front()]){

				a[j].pop();

			}

			cnt[a[j].front()]++;

		}

		mx=-1;

		for(int j = 1; j <= m; j++){

			if(cnt[j]>mx){

				mx=cnt[j];

				cur=j;

			}

			cnt[j]=0;

		}

		ans=min(ans,mx);

		disabled[cur]=1;

	}

	cout << ans;

	return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



int n, m, k;

int a[305][305], vis[305], been[305];



bool check(int x) {

    memset(been, 0, sizeof(been));

    for (int i = 1; i <= n; i++) a[i][0] = 1;

    while (1) {

        memset(vis, 0, sizeof(vis));

        bool flag = true;

        for (int i = 1; i <= n; i++) {

            while (a[i][0] <= m && been[a[i][a[i][0]]]) a[i][0]++;

            if (++vis[a[i][a[i][0]]] > x) {

                been[a[i][a[i][0]]] = 1;

                flag = false;

            }

            if (a[i][0] > m) return false;

        }

        if (flag) return true;

    }

    return false;

}



int main() {

    scanf("%d%d", &n, &m);

    for (int i = 1; i <= n; i++) {

        a[i][0] = 1;

        for (int j = 1; j <= m; j++)

            scanf("%d", &a[i][j]);

    }

    int l = 1, r = n;

    while (l < r) {

        int mid = (l + r) >> 1;

        if (check(mid)) r = mid;

        else l = mid + 1;

    }

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

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<iostream>

#include<algorithm>

#include<cmath>

long long pos[100010],sum[100010];

using namespace std;

int main()

{

	int n,k,x;long long ans=- int(1e9);

	cin>>n>>k;

	for(int i=1;i<=n;i++)

	{

		cin>>x;

		sum[i]=sum[i-1]+x;

		pos[i]=x<0?pos[i-1]:pos[i-1]+x;

	//	cout<<sum[i]<<" "<<pos[i]<<endl;

	}

	for(int i=k;i<=n;i++)

	{

		long long s=pos[n]-(pos[i]-pos[i-k]);//Çø¼äÍâË¢Õý 

		if(sum[i]-sum[i-k]>0) s+=sum[i]-sum[i-k];//Èç¹ûÇø¼äºÍ´óÓÚÁã¾ÍË¢ºÚ 

		ans=max(s,ans);

	}

	cout<<ans;

	return 0; 

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

const int maxn = 100000;

long long b[maxn + 10];

long long w[maxn + 10];

int main()

{

	int n,k;

	long long x;

	scanf("%d%d",&n,&k);

	for(int i = 1;i <= n;++i)

	{

		w[i] += w[i - 1];

		b[i] += b[i - 1];

		scanf("%lld",&x);

		if(x > 0)

			w[i] += x;

		if(x < 0)

			b[i] -= x;

	}

	long long mina = b[k] - b[0];

	if(w[k] - w[0] < mina)

		mina = w[k] - w[0];

	for(int i = 1;i <= n - k;++i)

	{

		long long temp = b[k + i] - b[i];

		if(temp < mina)

			mina = temp;

		temp = w[k + i] - w[i];

		if(temp < mina)

			mina = temp;

	}

	printf("%lld",w[n] - mina);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <set>

#include <map>

#include <stack>

#include <queue>

#include <cmath>

#include <cstdio>

#include <vector>

#include <bitset>

#include <cstring>

#include <utility>

#include <iostream>

#include <algorithm>

#define LL long long

#define Rg register

#define RI register int

#define RC register char

#define ed puts("") 

#define pair pair<LL,LL>

#define space putchar(' ')

using namespace std;

    

template <typename tp> inline void read(tp & x)

{	

    x=0;

    RI f=1;

    RC c=getchar();

    while(c<'0' || c>'9'){if(c=='-') f=-1;c=getchar(); }

    while(c>='0' && c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();} 

 	x*=f;

} 	

    

template <typename Tp> inline void print(Tp x)

{		

    if(x<0) x=-x,putchar('-');

    if(x>9) print(x/10);

    putchar(x%10+'0');

}	



int n,k;

LL a[1000005];

LL s[1000005];

LL b[1000005];//b[i]表示正数的前缀和 正序

LL c[1000005];//c[i]表示正数的前缀和 逆序 

LL ans;



int main()

{

	read(n);

	read(k);

	for(RI i=1;i<=n;++i)

	{

		read(a[i]);

		if(a[i]>0) b[i]=b[i-1]+a[i];

		else b[i]=b[i-1];

		s[i]=s[i-1]+a[i];

	}

	for(RI i=n;i>=1;--i)//逆序求正数的前缀和 

	{

		if(a[i]>0) c[i]=c[i+1]+a[i];

		else c[i]=c[i+1];

	}

	for(RI i=0;i<=n-k;++i)

	{

		if(s[i+k]-s[i]<0) ans=max(ans,b[i]+c[i+k+1]);//中间那段连续的区间和为负 此时可以把它们涂成白色 使总的和变大 

		else ans=max(ans,b[i]+c[i+k+1]+s[i+k]-s[i]);

	}

	print(ans);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

const int maxn = 100000;

long long b[maxn + 10];

long long w[maxn + 10];

int main()

{

	int n,k;

	long long x;

	scanf("%d%d",&n,&k);

	for(int i = 1;i <= n;++i)

	{

		w[i] += w[i - 1];

		b[i] += b[i - 1];

		scanf("%lld",&x);

		if(x > 0)

			w[i] += x;

		if(x < 0)

			b[i] -= x;

	}

	long long mina = b[k] - b[0];

	if(w[k] - w[0] < mina)

		mina = w[k] - w[0];

	for(int i = 1;i <= n - k;++i)

	{

		long long temp = b[k + i] - b[i];

		if(temp < mina)

			mina = temp;

		temp = w[k + i] - w[i];

		if(temp < mina)

			mina = temp;

	}

	printf("%lld",w[n] - mina);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

using namespace std;

long long n,k;

long long a[200000];

long long sum[200000];

long long sumz[200000];

long long mx = -(long long)1e20;

long long mymax(long long a,long long b){

	return a>b?a:b;

}

int main(){

	scanf("%lld%lld",&n,&k);

	for(long long i = 1; i <= n;i ++){

		scanf("%lld",&a[i]);

		sum[i] = sum[i-1] + a[i];

		if(a[i] > 0){

			sumz[i] = sumz[i-1] + a[i];

		}

		else{

			sumz[i] = sumz[i-1];

		}

	}

	if(n == k){

		printf("%lld",mymax(sum[n],0));

		return 0;

	}

	for(long long i = 1;i <= n-k+1 ;i ++){

		long long t = sumz[i-1] + sumz[n] - sumz[i+k-1] + sum[i+k-1] - sum[i-1];

		mx = mymax(mx,t);

	}

	for(long long i = 1;i <= n-k+1 ;i ++){

		long long t = sumz[i-1] + sumz[n] - sumz[i+k-1];

		mx = mymax(mx,t);

	}

	printf("%lld",mx);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

using namespace std;

const int maxn = 100000;

long long b[maxn + 10];

long long w[maxn + 10];

int main()

{

	int n,k;

	long long x;

	scanf("%d%d",&n,&k);

	for(int i = 1;i <= n;++i)

	{

		w[i] += w[i - 1];

		b[i] += b[i - 1];

		scanf("%lld",&x);

		if(x > 0)

			w[i] += x;

		if(x < 0)

			b[i] -= x;

	}

	long long mina = b[k] - b[0];

	if(w[k] - w[0] < mina)

		mina = w[k] - w[0];

	for(int i = 1;i <= n - k;++i)

	{

		long long temp = b[k + i] - b[i];

		if(temp < mina)

			mina = temp;

		temp = w[k + i] - w[i];

		if(temp < mina)

			mina = temp;

	}

	printf("%lld",w[n] - mina);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long int lli;

lli qsp[100005],qsn[100005];

int main(){

	int n,k;

	scanf("%d%d",&n,&k);

	lli a;

	qsp[0]=qsn[0]=0;

	for(int i=1;i<=n;++i){

		scanf("%lld",&a);

		qsp[i]=qsp[i-1];

		qsn[i]=qsn[i-1];

		if(a>0)

			qsp[i]+=a;

		else

			qsn[i]+=a;

	}

	lli min=qsp[k],max=qsp[k]+qsn[k],maxidx=k,ans=0;

	for(int i=k;i<=n;++i){

		lli sum=qsp[n];

		if(qsp[i]-qsp[i-k]+qsn[i]-qsn[i-k]>0)

			sum+=qsn[i]-qsn[i-k];

		else

			sum-=qsp[i]-qsp[i-k];

		if(ans<sum)

			ans=sum;

	}

	printf("%lld",ans);

}

/*



10 5

5 -4 -5 -8 -4 7 2 -4 0 7



*/

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<cstring>

#define MN 100005

using namespace std;

int n,k,a[MN];

long long ans,s[MN],ss[MN];

int main(){

	scanf("%d%d",&n,&k);

	for(int i=1;i<=n;i++)scanf("%d",&a[i]),s[i]=s[i-1]+a[i],ss[i]=ss[i-1]+max(a[i],0);

	for(int i=0;i<=n-k;i++)ans=max(ans,ss[n]-ss[i+k]+ss[i]+max(0LL,s[i+k]-s[i]));

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

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;

typedef long long lli;

lli n,k;

vector<lli> a;

lli ans = 0;

int main(){

    cin >> n >> k;

    a = vector<lli> (n);

    for(lli i = 0;i < n;i++) cin >> a[i];

    lli t = 0,s;

    for(lli i = 0;i < n;i++){

        if(a[i] > 0) t += a[i];

    }

    s = t;

    for(lli i = 0;i < k;i++){

        if(a[i] > 0) t -= a[i];

        else s += a[i];

    }

    ans = max(ans,s);

    ans = max(ans,t);

    for(lli i = k,j = 0;i < n;i++,j++){

        if(a[j] > 0) t += a[j];

        else s -= a[j];

        if(a[i] > 0) t -= a[i];

        else s += a[i];

        ans = max(ans,s);

        ans = max(ans,t);        

    }

    cout << ans << endl;

    return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<cstring>

#define MN 100005

using namespace std;

int n,k,a[MN];

long long ans,s[MN],ss[MN];

int main(){

	scanf("%d%d",&n,&k);

	for(int i=1;i<=n;i++)scanf("%d",&a[i]),s[i]=s[i-1]+a[i],ss[i]=ss[i-1]+max(a[i],0);

	for(int i=0;i<=n-k;i++)ans=max(ans,ss[n]-ss[i+k]+ss[i]+max(0LL,s[i+k]-s[i]));

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

}




// Fastest known solution for this problem: