P9810题解

· · 题解

显然可以简化题意为模拟一个栈。

数据不大,用一个数组模拟即可。 ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5+5; int T,x; int k,a[N]; signed main(){ ios::sync_with_stdio(false); cin.tie(NULL); cin >> T; while(T--){ cin >> x; if(x) a[++k] = x; else k--; } int cnt = 0; for(int i = 1; i <= k; i++) cnt += a[i]; cout << cnt << endl; return 0; } ```