Thứ Năm, 19 tháng 5, 2022

ZDIST C++


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9;
const int N = 1e5 + 5;
int n, a[N], last = INF;
ll owe, now;
void nhap(){
    cin >> n;
    for(int i=1;i<=n;i++) cin >> a[i];
}
int res;
void giai(){
    res = n;
    for(int i=1;i<=n;i++){
       if (a[i] >= 0) now += a[i];
       else{
            owe += 1LL*abs(a[i]);
            last = min(last, i);
       }
       if (now >= owe && owe > 0){
            now -= owe;
            owe = 0;
            res += 2*(i - last);
            last = INF;
       }
    }
    cout << res;
}

int main(){
    freopen("b.inp","r",stdin);
    freopen("b.out","w",stdout);
    nhap();
    giai();
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.