Runtime Error
Code
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t;cin>>t;
while(t--)
{
int n,k;
cin>>n>>k;
vector<int> a(n);
int prefix[n+1]={0};
// prefix[0]=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
prefix[i+1]+=prefix[i]+a[i];
}
int mini=INT_MAX,st=0;
for(int i=0;i+k<n ;i++)
{
int mini2=min(mini,(prefix[i+k]-prefix[i]));
if(mini2<mini)
{
mini=mini2;
st=i;
}
}
int x,y,z;
if(st==0)
{
sort(a.begin(),a.begin()+k);
z=a[st+k];
x=z;
}
else
{
sort(a.begin(),a.begin()+st);
x=a[0];
// cout<<"x"<<x<<endl;
}
if((st+k)==(n-1))
{
sort(a.begin(),a.end());
z=a[n-1];
y=z;
}
else
{
sort(a.begin()+st+k+1,a.end());
y=a[st+k+1];
}
if(st!=0 && st+k!=n-1)
{
sort(a.begin()+st,a.begin()+st+k);
z=a[st+k];
}
int mini3=min(x,y);
if(mini3<z)
{
mini-=z;
mini+=mini3;
}
cout<<mini<<endl;
}
return 0;
}
Information
- Submit By
- Type
- Submission
- Problem
- P1149 Swap and Minimize
- Contest
- LU IUJPC : Sylhet Division 2024
- Language
- C++17 (G++ 13.2.0)
- Submit At
- 2024-12-09 06:09:51
- Judged At
- 2024-12-09 06:09:51
- Judged By
- Score
- 1
- Total Time
- 3ms
- Peak Memory
- 856.0 KiB