/ SeriousOJ /

Record Detail

Runtime Error


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 488.0 KiB
#2 Runtime Error 8ms 564.0 KiB
#3 Runtime Error 86ms 620.0 KiB

Code

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

#define ll long long
#define endl '\n'
#define pi 2*acos(0.0)




void solve() 
{

    ll n, k; cin >> n >> k;
    ll a[n + 1]; a[0] = 0;
    multiset<ll>out, in;

    for (ll i = 1; i <= n; i++) {
        cin >> a[i];
        out.insert(a[i]);
    }

    ll ans = LONG_LONG_MAX;
    ll sum = 0;
    for (ll i = 1; i <= n; i++) {
        in.insert(a[i]);
        out.erase(a[i]);
        sum += a[i];

        if (i >= k) {
            if (i > k) {
                in.erase(a[i - k]);
                sum -= a[i - k];
                out.insert(a[i - k]);
            }
            ll mx = *in.rbegin();
            ll mn = *out.begin();
            //cerr << sum << " " << mx << " " << mn << endl;
            //cerr << mx << " " << mn << endl;
            if (out.size()) {
                ll sum2 = min(sum, sum - mx + mn);
                ans = min(sum2, ans);
            }
            else {
                ans = min(sum, ans);
            }

        }
 

    }

    cout << ans << endl;


}

int main () 
{

    ios::sync_with_stdio(false); cin.tie(nullptr);
    

    ll t = 1; 
    cin >> t;
    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 08:41:30
Judged At
2024-12-09 08:41:30
Judged By
Score
1
Total Time
86ms
Peak Memory
620.0 KiB