#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 100010;
int pai[N], val[N];
stack <pair <int, int>> s[N];
int32_t main(){
int n, c;
cin >> n >> c;
vector<int> aux;
int con = 0;
for(int i = 1;i <= n;i++){
cin >> val[i] >> pai[i];
if(pai[i] == 0){
if(!aux.empty()){
reverse(aux.begin(), aux.end());
vector <pair <int, int>> res;
int ac = 0;
int mnac = 0;
while(!aux.empty()){
// cout << aux.back() << ' ' << ac << '\n';
if(aux.back() + ac >= 0){
ac = 0;
res.push_back({aux.back()+ac, mnac});
mnac = 0;
}
else{
ac += aux.back();
mnac = min(mnac, ac);
}
aux.pop_back();
}
for(auto x : res){
//cout << x.first << ' ';
s[con].push(x);
}
con++;
}
}
//cout << val[i] << ' ';
aux.push_back(val[i]);
}
if(!aux.empty()){
reverse(aux.begin(), aux.end());
vector <pair <int, int>> res;
int ac = 0;
int mnac = 0;
while(!aux.empty()){
// cout << aux.back() << ' ' << ac << '\n';
if(aux.back() + ac >= 0){
res.push_back({aux.back()+ac, mnac});
ac = 0;
mnac = 0;
}
else{
ac += aux.back();
mnac = min(mnac, ac);
}
aux.pop_back();
}
for(auto x : res){
//cout << x.first << ' ';
s[con].push(x);
}
con++;
}
priority_queue <array <int, 3>> pq;
for(int i = 0;i < con;i++){
if(s[i].empty()) continue;
pq.push({s[i].top().second, s[i].top().first, i});
}
int res = 0;
while(!pq.empty()){
auto [mnac, valtot, i] = pq.top();
pq.pop();
// cout << mnac << ' ';
if(c + mnac < 0) break;
res += valtot;
c += valtot;
s[i].pop();
if(s[i].empty()) continue;
pq.push({s[i].top().second, s[i].top().first, i});
}
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
114 ms |
141896 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
68184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
68184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
68184 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
114 ms |
141896 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |