#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<random>
#include<cmath>
#include<stack>
#include<map>
#include <iomanip>
#include <queue>
#include <set>
using namespace std;
using ll = long long;
using ull = unsigned long long;
ll mod = 1e9 + 7;
ll pv(ll a, ll b) {
if (b == 0)return 1;
ll res = (pv(a, b / 2));
if (b % 2) {
return (((res * res) % mod) * (a % mod)) % mod;
}
else {
return (res * res) % mod;
}
}
ll gcd(ll a, ll b) {
while (b) {
a %= b;
swap(a, b);
}
return a;
}
void solve() {
ll n,s1; cin >> n >> s1;
vector<ll>s(n),p(n);
for(ll i=0;i<n;i++){
cin >> s[i] >> p[i];
}
vector<ll>v;
ll ans=0;
for(ll i=0;i<n;i++){
if(p[i]!=0){
v.push_back(s[i]);
}
else{
ll sum=0,mx=0,mn=1e18;
for(ll j=0;j<v.size();j++){
sum+=v[j];
mn=min(mn,sum);
if(s1+mn>=0){
mx=max(mx,sum);
}
}
ans+=mx;
v.clear();
v.push_back(s[i]);
}
}
if(v.size()){
ll sum=0,mx=0,mn=1e18;
for(ll j=0;j<v.size();j++){
sum+=v[j];
mn=min(mn,sum);
if(s1+mn>=0){
mx=max(mx,sum);
}
}
ans+=mx;
}
cout<<ans<<endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll _ = 1;
// cin >> _;
while (_--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |