#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define int long long
#define f first
#define s second
#define pii pair<int,int>
template<class T>bool umax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T>bool umin(T &a,T b){if(b<a){a=b;return true;}return false;}
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
tree_order_statistics_node_update> ordered_set;
const int mod= 1e17 +7;
const int N=1e5*4;
int binpow (int a, int n) {
if (n == 0)
return 1;
if (n % 2 == 1)
return binpow (a, n-1) * a;
else {
int b = binpow (a, n/2);
return b * b;
}
}
void solve(){
int n,m,k;
cin>>n;
vector<pii>v;
map<int,int>mp;
for(int i = 1;i<=n;i++){
cin>>m>>k;
mp[m] += k;
}
for(auto to:mp)
v.pb(to);
//~ for(auto to:v)cout<<to.s<<" ";
int ans = -1 * mod;
int sum = 0;
int l = 0,r = 0;
while(r<v.size()){
while(r<v.size()&&v[l].s - (v[r].f-v[l].f)>=0){
sum += v[r].s;
umax(ans,sum - (v[r].f-v[l].f));
r++;
}
if(r<v.size()){
sum += v[r].s;
umax(ans,sum - (v[r].f-v[l].f));
}
while(l<r&&v[l].s - (v[r].f-v[l].f)<0){
sum -= v[l].s;
l++;
umax(ans,sum - (v[r].f-v[l].f));
}
}
cout<<ans<<"\n";
}
signed main()
{
// freopen("seq.in", "r", stdin);
// freopen("seq.out", "w", stdout);
ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
int tt=1;//cin>>tt;
while(tt--)solve();
}
Compilation message
art.cpp: In function 'void solve()':
art.cpp:51:9: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | while(r<v.size()){
| ~^~~~~~~~~
art.cpp:53:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | while(r<v.size()&&v[l].s - (v[r].f-v[l].f)>=0){
| ~^~~~~~~~~
art.cpp:58:7: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | if(r<v.size()){
| ~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |