Submission #723875

# Submission time Handle Problem Language Result Execution time Memory
723875 2023-04-14T11:54:41 Z OttincaM Hacker (BOI15_hac) C++17
100 / 100
227 ms 117840 KB
#include <iostream>
#include <vector>
#include "stdio.h"

using namespace std;
#define int long long






//
// Created by Otabek Jurabekov on 4/12/22.
//

template <typename Type> struct SegTree{
    int Sz; 
    vector <Type> t, arr; 
    SegTree(int _n, Type _m){
        t.assign((_n + 1) * 4 + 7, _m); 
        Sz = _n;
    }
    Type com(Type F, Type S){
        return max(F, S);
    }
    
    void BuildTree(int v, int l, int r, vector <Type> &Array){
        arr = Array; build(v, l, r);
    }

    void build(int v, int l, int r){
        if(l == r){
            t[v] = arr[l]; return;
        }
        int m = (l + r) >> 1;
        build(v << 1, l, m); build(v << 1 | 1, m + 1, r);
        t[v] = com(t[v << 1], t[v << 1 | 1]);
    }

    void upd_put_one(int v, int tl, int tr, int idx, Type del){
        if(tl == tr){
            t[v] = del; return;
        }
        int tm = (tl + tr) >> 1;
        if(idx > tm) upd_put_one(v << 1 | 1, tm + 1, tr, idx, del);
        else upd_put_one(v << 1, tl, tm, idx, del);
        t[v] = com(t[v << 1], t[v << 1 | 1]);
    }

    void upd_add_one(int v, int tl, int tr, int idx, Type del){
        if(tl == tr){
            t[v] += del; return;
        }
        int tm = (tl + tr) >> 1;
        if(idx > tm) upd_add_one(v << 1 | 1, tm + 1, tr, idx, del);
        else upd_add_one(v << 1, tl, tm, idx, del);
        t[v] = com(t[v << 1], t[v << 1 | 1]);
    }

    Type get_one(int v, int tl, int tr, int idx){
        if(tl == tr){
            return t[v];
        }
        int tm = (tl + tr) >> 1;
        if(idx > tm) return t[v] + get_one(v << 1 | 1, tm + 1, tr, idx);
        return t[v] + get_one(v << 1, tl, tm, idx);
    }

    Type get_lr(int v, int tl, int tr, int l, int r){
        if(tl >= l && tr <= r) return t[v];
        int tm = (tl + tr) >> 1;
        if(tm < l) return get_lr(v << 1 | 1, tm + 1, tr, l, r);
        if(tm >= r) return get_lr(v << 1, tl, tm, l, r);
        return com(get_lr(v << 1, tl, tm, l, r), get_lr(v << 1 | 1, tm + 1, tr, l, r));
    }
    
    
    
    void upd_put_one(int idx, Type del){upd_put_one(1, 1, Sz, idx, del);}
    void upd_add_one(int idx, Type del){upd_add_one(1, 1, Sz, idx, del);}
    Type get_lr(int l, int r){return get_lr(1, 1, Sz, l, r);}
    Type get_one(int idx){return get_one(1, 1, Sz, idx);}
};








signed main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
    #ifdef WTF
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
        freopen("error.txt", "w", stderr);
    #endif
 
    int n; cin >> n;
    vector <int> a(2 * n + 2);
    for(int i = 1; i <= n; i ++){
        cin >> a[i];
        a[i + n] = a[i];
    }

    vector <int> pr(2 * n + 2, 0), sf(2 * n + 2, 0), P(2 * n + 2, 0), S(2 * n + 2, 0);
    for(int i = 1; i <= n * 2; i ++){
        pr[i] = pr[i - 1] + a[i];
        P[i] = pr[i];
        if(i - n / 2 >= 1) P[i] -= pr[i - n / 2];
    }  
    for(int i = n * 2; i >= 1; i --){
        sf[i] = sf[i + 1] + a[i];
        S[i] = sf[i];
        if(i + n / 2 <= 2 * n) S[i] -= sf[i + n / 2];
    }
    int K = n + n;
    SegTree <int> SF(K, 0), PR(K, 0);
    SF.BuildTree(1, 1, K, S);
    PR.BuildTree(1, 1, K, P);


    int ans = 0;
    for(int i = 1; i <= n; i ++){
        int mxs = n - (n / 2 - (i - 1)) + 1;
        if(n / 2 - (i - 1) <= 0) mxs = n;
        int mnp = (n / 2 - (n - i));
        mnp = max(mnp, 1LL);
        int cnt = 0;
        if(mnp <= i - 1) cnt = PR.get_lr(1, 1, K, mnp, i - 1);
        if(i + 1 <= mxs) cnt = max(cnt, SF.get_lr(1, 1, K, i + 1, mxs));
        ans = max(ans, pr[n] - cnt);
    }
    cout << ans << "\n";
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 368 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 368 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 2 ms 980 KB Output is correct
23 Correct 2 ms 1492 KB Output is correct
24 Correct 1 ms 980 KB Output is correct
25 Correct 2 ms 1492 KB Output is correct
26 Correct 2 ms 1492 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 2 ms 1492 KB Output is correct
31 Correct 2 ms 1492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 3 ms 1492 KB Output is correct
4 Correct 32 ms 19028 KB Output is correct
5 Correct 81 ms 47236 KB Output is correct
6 Correct 106 ms 59084 KB Output is correct
7 Correct 127 ms 70652 KB Output is correct
8 Correct 227 ms 117840 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 368 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 2 ms 980 KB Output is correct
23 Correct 2 ms 1492 KB Output is correct
24 Correct 1 ms 980 KB Output is correct
25 Correct 2 ms 1492 KB Output is correct
26 Correct 2 ms 1492 KB Output is correct
27 Correct 1 ms 212 KB Output is correct
28 Correct 0 ms 212 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 2 ms 1492 KB Output is correct
31 Correct 2 ms 1492 KB Output is correct
32 Correct 0 ms 212 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 3 ms 1492 KB Output is correct
35 Correct 32 ms 19028 KB Output is correct
36 Correct 81 ms 47236 KB Output is correct
37 Correct 106 ms 59084 KB Output is correct
38 Correct 127 ms 70652 KB Output is correct
39 Correct 227 ms 117840 KB Output is correct
40 Correct 4 ms 2644 KB Output is correct
41 Correct 8 ms 4948 KB Output is correct
42 Correct 12 ms 7304 KB Output is correct
43 Correct 86 ms 47224 KB Output is correct
44 Correct 226 ms 117736 KB Output is correct
45 Correct 42 ms 23764 KB Output is correct
46 Correct 135 ms 70768 KB Output is correct
47 Correct 206 ms 117720 KB Output is correct
48 Correct 210 ms 117736 KB Output is correct
49 Correct 210 ms 117708 KB Output is correct
50 Correct 202 ms 117756 KB Output is correct