#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define endl '\n'
#define pb(x) push_back(x)
const int MOD = 1e9+7;
const int inf =1e9;
const ll INF = 1e15;
const ll INV2 = 500000004;
class DSU {
vector<int> parent, size;
public:
DSU(int n) {
parent.resize(n+1);
size.resize(n+1);
for (int i=0; i<=n; i++) {
parent[i]=i;
size[i]=1;
}
}
int find (int node) {
if (node==parent[node]) return node;
return parent[node]=find(parent[node]);
}
void unite (int u, int v) {
int pv=find(v);
int pu=find(u);
if (pv==pu) return;
if (size[pu]<size[pv]) swap(pu, pv);
parent[pv]=pu;
size[pu]+=size[pv];
}
int getsize (int node) {
return size[find(node)];
}
};
ll getsqrt(ll x) {
ll val = sqrtl(x) + 2;
while (val * val > x) val--;
return val;
}
bool safe (int i, int j, int n, int m) {
if (i<0 || j<0 || i>=n || j>=m) return false;
return true;
}
vector<int> dx={-1, 0, 1, 0};
vector<int> dy={0, 1, 0, -1};
// SOLUTION STARTS FROM HERE //
void solve() {
int n;
cin>>n;
vector<pair<int, int>> a(n);
for (int i=0; i<n; i++) {
cin>>a[i].first>>a[i].second;
}
int s=0;
int maxi=a[0].first;
int mini=a[0].first;
for (int i=0; i<n; i++) {
int ai=a[i].first;
int v=a[i].second;
if (ai<=maxi && ai>=mini) {
s+=v;
}
if (ai>maxi) {
if (ai-maxi<v) {
s+=v;
maxi=ai;
}
}
if (ai<mini) {
if (mini-ai<v) {
s+=v;
mini=ai;
}
}
}
cout<<s-maxi+mini<<endl;
}
bool multi=false;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
//freopen("snakes.in", "r", stdin);
//freopen("snakes.out", "w", stdout);
int t=1;
if (multi) cin>>t;
while (t--) solve();
return 0;
}
# | 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... |