Submission #3611009


Source Code Expand

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

template<typename T>
struct Segtree {
    int n;
    T e;
    vector<T> dat;
    typedef function<T(T a, T b)> Func;
    Func f;

    Segtree(int n_input, Func f, T e_input):f(f), e(e_input){
        n = 1;
        while(n < n_input) n <<= 1;
        dat.resize(2*n-1, e);
    }

    void update(int k, T a){
        k += n - 1;
        dat[k] = a;
        while(k > 0){
            k = (k - 1)/2;
            dat[k] = f(dat[2*k+1], dat[2*k+2]);
        }
    }

    T get(int k){
        return dat[k+n-1];
    }

    T between(int a, int b){
        return query(a, b+1, 0, 0, n);
    }

    T query(int a, int b, int k, int l, int r){
        if(r<=a || b<=l) return e;
        if(a<=l && r<=b) return dat[k];
        T vl = query(a, b, 2*k+1, l, (l+r)/2);
        T vr = query(a, b, 2*k+2, (l+r)/2, r);
        return f(vl, vr);
    }
};

int main(){
    int N, K;
    cin >> N >> K;
    int A[100000];
    for(int i=0; i<N; i++) cin >> A[i];

    vector<int> order(N);
    for(int i=0; i<N; i++) order[i] = i;
    sort(order.begin(), order.end(), [&](int a, int b){return A[a] < A[b]; });

    Segtree<int> st(N, [](int a, int b){return min(a, b);}, N);
    int nxt[100000];
    for(auto i : order){
        nxt[i] = st.between(i, N-1);
        st.update(i, i);
    }

    int64_t k = 0;
    int64_t ans = 0;
    for(int i=0; i<N; i++){
        int64_t num = min(K, nxt[i] - i);
        if(num > k){
            ans += (num-k) * A[i];
            k = num;
        }
        k--;
    }
    cout << ans << endl;
    return 0;
}

Submission Info

Submission Time
Task E - Tough Journey
User betrue12
Language C++14 (GCC 5.4.1)
Score 600
Code Size 1649 Byte
Status
Exec Time 76 ms
Memory 2432 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 sample_01.txt, sample_02.txt
All 600 / 600 big_01.txt, big_02.txt, big_03.txt, big_04.txt, big_05.txt, sample_01.txt, sample_02.txt, small_01.txt, small_02.txt, small_03.txt, small_04.txt, small_05.txt, small_06.txt, small_07.txt
Case Name Status Exec Time Memory
big_01.txt 76 ms 2432 KB
big_02.txt 75 ms 2432 KB
big_03.txt 73 ms 2432 KB
big_04.txt 74 ms 2432 KB
big_05.txt 74 ms 2432 KB
sample_01.txt 1 ms 256 KB
sample_02.txt 1 ms 256 KB
small_01.txt 1 ms 256 KB
small_02.txt 1 ms 256 KB
small_03.txt 1 ms 256 KB
small_04.txt 1 ms 256 KB
small_05.txt 1 ms 256 KB
small_06.txt 1 ms 256 KB
small_07.txt 1 ms 256 KB