/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 36ms 576.0 KiB
#3 Accepted 67ms 592.0 KiB
#4 Accepted 85ms 788.0 KiB
#5 Accepted 104ms 588.0 KiB
#6 Accepted 150ms 1.609 MiB
#7 Accepted 19ms 1.227 MiB
#8 Accepted 124ms 5.957 MiB
#9 Accepted 267ms 6.074 MiB
#10 Accepted 240ms 5.953 MiB
#11 Accepted 260ms 5.902 MiB
#12 Accepted 259ms 5.816 MiB

Code

#include<bits/stdc++.h>
using namespace std;

#define nl '\n'
#define ll long long
#define int long long
#define pii pair<int, int> 



void solve(){
    int n, k; cin >> n >> k;
    vector<int> a(n);
    for(int i=0;i<n;++i) {
        cin >> a[i];
    }
    if(k==n){
        cout << accumulate(a.begin(), a.end(), 0ll) << nl;
        return;
    }
    multiset<int> st, all(a.begin(), a.end());
    int s = 0, ans = 2e18;
    for(int i = 0, j = 0;i<n;++i) {
        st.insert(a[i]);
        all.erase(all.find(a[i]));
        s+=a[i];
        if(i<k-1) continue;
        assert(st.size() == k);
        int tmp = s;
        int boro = *st.rbegin();
        int suto = *all.begin();
        if(boro > suto) {
            tmp -= boro;
            tmp += suto;
        }
        ans = min(ans, tmp);
        all.insert(a[j]);
        st.erase(st.find(a[j]));
        s -= a[j];
        j++;
    }
    cout << ans << nl;
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t = 1, tc = 0;
    cin >> t ;
    while(t--){
        // cout << "Case " << ++tc  << ": ";
        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 05:18:10
Judged At
2024-12-09 05:18:10
Judged By
Score
100
Total Time
267ms
Peak Memory
6.074 MiB