/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 23ms 572.0 KiB
#3 Wrong Answer 39ms 612.0 KiB

Code

// SEC_Infra_Red
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define nl "\n"
#define pb push_back
#define fi first
#define F first
#define pll pair<ll,ll>
#define sc second
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define sortv(v) sort(v.begin(), v.end())
#define revv(v) reverse(v.begin(), v.end())
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define vinput ll n; cin >> n; vector<ll>v; for(ll i = 0; i <n; i++){ll in; cin >> in; v.push_back(in);}
#define FIO \
    ios_base::sync_with_stdio(false);\
    cin.tie(NULL);\
    cout.tie(NULL);


void solve()
{
    ll n,k;   cin>>n>>k;
    vector<ll>v(n);
    for(ll i=0;i<n;i++) cin>>v[i];
    vector<ll>age(n),pore(n);
    ll mn=LLONG_MAX;
    for(ll i=0;i<n;i++)
    {
        age[i]=min(mn,v[i]);
        mn=min(mn,v[i]);
    }
    mn=LLONG_MAX;
    for(ll i=n-1;i>=0;i--)
    {
        pore[i]=min(mn,v[i]);
        mn=min(mn,v[i]);
    }

    ll sum=0,l=0,ans=LLONG_MAX;
    multiset<ll>mst;
    for(ll i=0;i<k;i++)
    {
        mst.insert(v[i]);
        sum+=v[i];
    }
    for(ll i=k;i<n;i++)
    {
        if(l==0 && i==n-1)
        {
            ans=min(ans,sum);
        }
        else if(l==0)
        {
            if(*(--mst.end())>pore[i])    ans=min(ans,(sum-*(--mst.end())+pore[i]));
            else ans=min(ans,sum);
        }
        else if(i==n-1)
        {
            if(*(--mst.end())>age[l-1])    ans=min(ans,(sum-*(--mst.end())+age[l-1]));
            else ans=min(ans,sum);
        }
        else
        {
            if(*(--mst.end())>min(age[l-1],pore[i+1]))  ans=min(ans,(sum-*(--mst.end())+min(age[l-1],pore[i])));
            else ans=min(ans,sum);
        }

        sum-=v[l];
        mst.erase(mst.find(v[l]));
        sum+=v[i];
        mst.insert(v[i]);
        l++;
    }
    if(*(--mst.end())>age[l-1])    ans=min(ans,(sum-*(--mst.end())+age[l-1]));
    else ans=min(ans,sum);
    cout<<ans<<nl;
    

}

int32_t main()
{
    FIO

    ll t=1;
    cin>>t;
    for(ll i=1;i<=t;i++)
    {
        // cout<<"Case "<<i<<": ";
        solve();
    }
}

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 08:01:06
Judged At
2024-12-09 08:01:06
Judged By
Score
1
Total Time
39ms
Peak Memory
612.0 KiB