/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 340.0 KiB
#2 Wrong Answer 76ms 552.0 KiB
#3 Wrong Answer 125ms 596.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
#define int long long
void solve()
{
	int n,k; cin>>n>>k;
	vector<int>v(n);
	for(int i=0;i<n;i++) cin>>v[i];
	
    int ans = 0;
    
    
    for(int i=0;i<k;i++)
    {
    	ans+=v[i];
    }
    int curr = ans;
    
   
    int idx = 0;
    // cout<<curr<<' ';
    for(int i=1;i<=n-k;i++)
    {
    	curr+=v[i+k-1];
    	
    	curr-=v[i-1];
    	if(curr<=ans)
    	{
    		ans = curr;
    		idx = i;
    	}
    	//cout<<curr<<' ';
    	
    	

    }
    
    int mx = INT_MIN;
    for(int i=idx;i<idx+k;i++)
    {
    	mx = max(v[i],mx);
    }
   
    int mn = mx;
    for(int i=0;i<idx;i++)
    {
    	mn = min(mn,v[i]);
    }
     
    for(int i = idx+k;i<n;i++)
    {
    	mn = min(mn,v[i]);
    }
   
    curr = ans;
    cout<<min(ans - mx + mn,curr)<<endl;

}
int32_t main()
{
    int t; 
    cin>>t;
    // t=1;
    while(t--)
    {
    	solve();
    }
	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 09:30:35
Judged At
2024-12-09 09:30:35
Judged By
Score
1
Total Time
125ms
Peak Memory
596.0 KiB