Problem:
美術展(Art Exhibition)
JOI 国で美術展が行われることになった．美術展では，国中から集まった様々な美術品が展示される予定である．
展示される美術品の候補として，
N
点の美術品が集まった．これらの美術品には
1, 2, ..., N
の番号が付けられている．それぞれの美術品には大きさと価値と呼ばれる値が定まっている．美術品
i
(1 \leq i \leq N)
の大きさは
A_i
，価値は
B_i
である．
美術展では，これらの美術品のうちから1 点以上を選んで展示する．美術展の会場は十分に広く，
N
点の美術品すべてを展示することも可能である．しかし，JOI 国の人々の美的感覚のため，美術品の間で大きさの差があまり大きくならないように展示する美術品を選びたい．一方，できるだけ価値の大きい美術品を多く展示したい．そのため，次の条件を満たすように展示する美術品を選ぶことにした：
選んだ美術品の中で，大きさが最大のものの大きさを
A_{max}
，最小のものの大きさを
A_{min}
とする．また，選んだ美術品の価値の総和を
S
とする．
このとき，
S - (A_{max} - A_{min})
を最大化する．
課題
展示される美術品の候補の個数と，それぞれの美術品の大きさと価値が与えられたとき，
S - (A_{max} - A_{min})
の最大値を求めよ．
入力
標準入力から以下の入力を読み込め．
1 行目には，整数
N
が書かれている．これは，展示される美術品の候補の個数を表す．
続く
N
行のうちの
i
行目(
1 \leq i \leq N
) には，2 個の整数
A_i, B_i
が空白を区切りとして書かれている．これらは，美術品
i
の大きさが
A_i
，価値が
B_i
であることを表す．
出力
標準出力に，
S - (A_{max} - A_{min})
の最大値を1 行で出力せよ．
制限
すべての入力データは以下の条件を満たす．
2 \leq N \leq 500 000．
1 \leq A_i \leq 1 000 000 000 000 000 = 10^{15} (1 \leq i \leq N)．
1 \leq B_i \leq 1 000 000 000 (1 \leq i \leq N)．
入出力例
入力例1
3
2 3
11 2
4 5
出力例1
6
この入力例では，展示される美術品の候補が3 点ある．それぞれの美術品の大きさ，価値は次の通りである．
美術品1 の大きさは2，価値は3 である．
美術品2 の大きさは11，価値は2 である．
美術品3 の大きさは4，価値は5 である．
この場合，美術品1 と美術品3 を展示するために選ぶと，次のようにして
S - (A_{max} - A_{min}) = 6
となる．
選んだ中で大きさが最大の美術品は，美術品3 である．よって，
A_{max} = 4
である．
選んだ中で大きさが最小の美術品は，美術品1 である．よって，
A_{min} = 2
である．
選んだ美術品の価値の総和は
3 + 5 = 8
であるから，
S = 8
である．
S - (A_{max} - A_{min})
を7 以上にすることは不可能なので，6 を出力する．
入力例2
6
4 1
1 5
10 3
9 1
4 2
5 3
出力例2
7
入力例3
15
1543361732 260774320
2089759661 257198921
1555665663 389548466
4133306295 296394520
2596448427 301103944
1701413087 274491541
2347488426 912791996
2133012079 444074242
2659886224 656957044
1345396764 259870638
2671164286 233246973
2791812672 585862344
2996614635 91065315
971304780 488995617
1523452673 988137562
出力例3
4232545716
情報オリンピック日本委員会作 『第17 回日本情報オリンピック(JOI 2017/2018) 本選』

Submitted solutions:

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n; cin >> n;
    vector<P> v;
    rep(i, n) {
        ll a, b; cin >> a >> b;
        v.pb({a, b});
    }
    sort(all(v));
    vector<ll> A(n), B(n);
    rep(i, n) {
        A[i] = v[i].first;
        B[i] = v[i].second;
    }
    vector<ll> S;
    rep(i, n) {
        if (i) S.pb(A[i-1] - A[i]);
        S.pb(B[i]);
    }
    ll ans = -linf;
    ll sum = 0, mn = 0;
    rep(i, S.size()) {
        sum += S[i];
        chmax(ans, sum - mn);
        chmin(mn, sum);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 110
memory_kb: 32740
final_rank: 18

code_snippet: #include<bits/stdc++.h>
#define ll long long
const int N=1e6+5;
#define pii pair<ll,ll>
#define fi first
#define se second
#define F(i,a,b,c) for(int i=a;i<=b;i+=c)
#define fo(i,a,b,c) for(int i=a;i>=b;i-=c)
const int MOD=1e9+7;
#define el cout<<'\n'
using namespace std;
int n;
pii a[N];
ll s[N];
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    if(fopen(".inp","r"))
    {
        freopen(".inp","r",stdin);
        freopen(".out","w",stdout);
    }
    //input:
    cin>>n;
    F(i,1,n,1) cin>>a[i].fi>>a[i].se;
    sort(a+1,a+n+1);
    F(i,1,n,1) s[i]=s[i-1]+a[i].se;
    clock_t begin = clock();
    //code:
    ll ans=-1e18;
    ll ma=a[1].fi;
    F(i,1,n,1)
    {
        ll v=s[i]-a[i].fi+ma;
        ans=max(ans,v);
        if(i<n)
        {
            ma=max(ma,a[i+1].fi-s[i]);
        }
    }
    cout<<ans;el;
}
accuracy: 1
time_ms: 100
memory_kb: 17784
final_rank: 13

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define pb push_back
#define task ""
const int nmax = 5e5 + 5;
const ll mod = 1e9 + 7;
const int inf = 1e9;
const ll linf = 1e18;
int n;
pair<ll, ll> node[nmax];
ll pre[nmax], mn[nmax];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	// freopen(task".inp", "r", stdin);
	// freopen(task".out", "w", stdout);
	cin >> n;
	for (int i = 1; i <= n; ++i) {
		cin >> node[i].fi >> node[i].se;
	}
	ll ans = -linf;
	sort(node + 1, node + 1 + n);
	for (int i = 1; i <= n; ++i) {
		pre[i] = pre[i - 1] + node[i].se;
		// cout << pre[i] << ' ';
	}
	mn[0] = linf;
	// cout << '\n';''
	for (int i = 1; i <= n; ++i) {
		mn[i] = min(mn[i - 1], pre[i - 1] - node[i].fi);
		// cout << mn[i] << ' ';
	}
	for (int i = 1; i <= n; ++i) {
		ans = max(ans, pre[i] - node[i].fi - mn[i]);
	}
	// cout << ans;
	cout << ans << '\n';
	return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 19088
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std ; 


#define ll long long 
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fir first 
#define sec second 
#define piint pair < int , int >
#define FOR( i , a , b ) for (int i = (a) , _b = (b) ; i <= _b ; i ++ )  
#define pb push_back 
#define str string 
#define ALL(a) (a).begin() , (a).end() 
#define rep( i , a , b) for (int i = (a) ; i < (b) ; i ++ )
#define ld long double
const int maxn = 500000;
#define debug 0
#define oo (ll)(1e18)



pair < ll ,ll > a[maxn + 3 ] ;
ll save_val[maxn + 3 ]; 
ll max_of_save[maxn + 3 ]; 
// map < ll ,
void input(){
	int n ; cin >> n ;
	FOR ( i , 1 , n ) cin >> a[i].fir >> a[i].sec ;
	sort ( a + 1 , a + n + 1 , [&] ( pair < ll , ll > a , pair < ll , ll > b ) { if ( a.fir != b.fir ) return a.fir > b.fir ; else return b.sec >a.sec ; }) ; 
	for ( int i = 1 ; i <= n ; i ++  ) {
		save_val[i] = save_val[i-1] + a[i].sec ;
	}
	max_of_save[n+1] = -1e18 ; 
	for ( int i = n ; i >= 1 ; i --  ) {
		max_of_save[i] = max ( save_val[i] + a[i].fir , max_of_save[i+1] ) ;
	}
	ll res = -1e18 ;
	// ll d = 0 ;
	if(debug){
		FOR ( i , 1 , n ) {
			cout << a[i].fir << ' ' << a[i].sec << '\n' ;
		}
	}
	FOR ( i , 1 , n ) {
		ll val =  max_of_save[i] - save_val[i-1] - a[i].fir ;
		res = max ( res ,val) ;
	}
	cout << res << '\n' ;
}
#define name "TASK" 
int main(){
    fast 
    if(fopen(name".INP","r")) {
        freopen (name".INP","r",stdin);
        freopen (name".OUT","w",stdout);
    }
    input() ;
    //cerr << "\nTIME: = " << (1.0*clock())/CLOCKS_PER_SEC << '\n';
    return(0) ;
}
accuracy: 1
time_ms: 100
memory_kb: 19076
final_rank: 14

code_snippet: #include <iostream>
#include <fstream>
#define f first
#define se second
#include <algorithm>

using namespace std;
int n;
long long res, tmp, sum;
pair<long long,long long>a[500002];
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i].f>>a[i].se;
    sort(a+1,a+n+1,greater<>());
    tmp=a[1].f;
    res=sum=a[1].se;
    for(int i=2;i<=n;i++)
    {
        tmp=min(tmp,sum+a[i].f);
        res=max(res,sum+a[i].se+a[i].f-tmp);
        sum+=a[i].se;
    }
    cout<<res<<'\n';
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 11260
final_rank: 4

code_snippet: /**
 *   author:   phuonglinhn09
 *   created:  30.05.2025
 */

#include <bits/stdc++.h>
#define fi first
#define se second

using namespace std;

typedef long long ll;

const int nmax = 5e5;
const ll inf = 1e18;

int n;
pair<ll, ll> a[nmax + 5];
ll s[nmax + 5], dp[nmax + 5];

int main() {
    ios_base::sync_with_stdio(0); 
    cin.tie(0); cout.tie(0);
    
    if (fopen (".INP", "r")) {
        freopen (".INP", "r", stdin);
        freopen (".OUT", "w", stdout);
    }

    cin >> n;

    for (int i = 1; i <= n; i++) {
        cin >> a[i].fi >> a[i].se;
    }
    sort (a + 1, a + 1 + n);

    ll tmp = inf;
    for (int i = 1; i <= n; i++) {
        s[i] = s[i - 1] + a[i].se;
        dp[i] = max({dp[i - 1], a[i].se, s[i] - a[i].fi - tmp});
        tmp = min (tmp, s[i - 1] - a[i].fi);
    }

    cout << dp[n] << "\n";
    
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 19092
final_rank: 16

code_snippet: #include <bits/stdc++.h>


using namespace std;
//make -f ../makefile SRC=
/*
maximise: sum - (max_v - min_v) = (sum + min_v) - max_v (hint ?)
kadane ?

WA
6
4 1
1 5
10 3
9 1
4 2
5 3
output:
7
*/


//------------------------------------------------------------------------------
bool DEBUG = false;
const int INF = 2147483647;

const int MAX_N = 100;
static int vect[MAX_N];
//------------------------------------------------------------------------------
struct Pict
{
    int64_t a, b;

    Pict(): a(0), b(0) {}
    Pict(int64_t x1, int64_t x2): a(x1), b(x2) {}
    bool operator<(const Pict& p1) const
    {
        if (a == p1.a) return b < p1.b;
        return a < p1.a;
    }
    void debug() const { printf("a=%ld, b=%ld\n", a, b); }
};

typedef vector<Pict> Picts;


//------------------------------------------------------------------------------
int64_t kadane(int N, const Picts& PP)
{
    int64_t max_ending_here = PP[0].b;
    int64_t min_a = PP[0].a;
    int64_t max_a = PP[0].a;

    int64_t max_so_far = PP[0].b;

    for (int i=1; i<N; ++i)
    {
        max_ending_here += max_a + PP[i].b - PP[i].a;
        max_a = PP[i].a;

        if (PP[i].b > max_ending_here)
        {
            max_ending_here = PP[i].b;
            min_a = PP[i].a;
            max_a = PP[i].a;
        }

        max_so_far = max(max_so_far, max_ending_here);
        if (DEBUG)
            printf("i=%d: max_ending_here=%ld, max_a=%ld, min_a=%ld, max_so_far=%ld\n",i,max_ending_here,max_a,min_a,max_so_far);
    }
    return max_so_far;
}
//------------------------------------------------------------------------------
void test()
{

}

//------------------------------------------------------------------------------
int main()
{
    //test(); return 0;
    //DEBUG = true;
    //--------------------------------------------------------------------------
    int N, num;
    int64_t A, B;
    num = scanf("%d ", &N);
    Picts PP(N);
    for (int i=0; i<N; ++i)
    {
        num = scanf("%ld %ld ", &A, &B);
        PP[i] = Pict(A, B);
    }
    sort(PP.begin(), PP.end());
    if (DEBUG) {for (Pict p: PP) p.debug();}
    int64_t res = kadane(N, PP);
    printf("%ld\n", res);
    //--------------------------------------------------------------------------
    return 0;
}
//------------------------------------------------------------------------------
accuracy: 1
time_ms: 100
memory_kb: 10856
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define ii pair<ll, ll>
#define fi first
#define se second
const int N = 5e5+5;
int n;
ii a[N];
map<ll, ll> mp;

int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    if (fopen("A.inp", "r")) {
        freopen("A.inp", "r", stdin);
        freopen("A.out", "w", stdout);
    }
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i].fi >> a[i].se;
        mp[a[i].fi] += a[i].se;
    }
    n = 0;
    for (auto &it : mp) a[++n] = it;
    sort(a+1, a+n+1);
    long long ans = 0, mx = 0;
    for (int i = 1; i <= n; ++i) {
        ans = max(ans, mx - a[i].fi + a[i].se);
        ans = max(ans, a[i].se);
        mx = max(mx + a[i].se, a[i].fi + a[i].se);
    }
    cout << ans << '\n';
}
accuracy: 1
time_ms: 320
memory_kb: 42528
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N;
    cin>>N;
    vector<pair<ll,ll>> P(N);
    for(int i=0;i<N;i++)cin>>P[i].first>>P[i].second;
    sort(P.begin(),P.end());
    vector<ll> A(N),B(N+1,0);
    for(int i=0;i<N;i++){
        A[i]=P[i].first;
        B[i+1]=B[i]+P[i].second;
    }
    ll SM=-1e18;
    ll an=0;
    for(int i=N-1;i>=0;i--){
        SM=max(SM,B[i+1]-A[i]);
        an=max(an,SM+A[i]-B[i]);
    }
    cout<<an<<endl;;
}
accuracy: 1
time_ms: 110
memory_kb: 18844
final_rank: 17

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;

typedef long long llong;

struct Data {
    llong size;
    llong value;
};

#define N_MAX 500000
vector<Data> art(N_MAX);

bool cmp(const Data &a, const Data &b) {
    return a.size == b.size ? a.value < b.value : a.size < b.size;
}

llong Max(llong a, llong b) { return a > b ? a : b; }

int main() {
    int N;
    scanf("%d", &N);

    for (int i = 0; i < N; ++i) {
        scanf("%lld %lld", &art[i].size, &art[i].value);
    }

    sort(art.begin(), art.begin() + N, cmp);

    llong min_size = art[0].size;
    llong sum_value = art[0].value;
    llong ans = art[0].value;

    for (int i = 1; i < N; ++i) {
        llong value = art[i].value;
        llong size = art[i].size;
        llong tmp = sum_value + value - (size - min_size);

        if (tmp < value) {
            ans = Max(ans, value);
            sum_value = value;
            min_size = size;
        } else {
            ans = Max(ans, tmp);
            sum_value += value;
        }
    }

    printf("%lld\n", ans);
    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 10708
final_rank: 5

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) { return (ull)rng() % B; }

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<pair<ll, ll>> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i].first >> v[i].second;
    }
    sort(v.rbegin(), v.rend());
    ll res = 0, sum = 0, mx = -1e18;
    for (int i = 0; i < n; i++) {
        mx = max(mx, -sum - v[i].first);
        sum += v[i].second;
        res = max(res, sum + v[i].first + mx);
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 11020
final_rank: 3

code_snippet: #include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long llong;
#define N_MAX 500000
pair<llong, llong> art[N_MAX];

bool cmp(const pair<llong, llong> &a, const pair<llong, llong> &b) {
  return a.first == b.first ? a.second < b.second : a.first < b.first;
}
int main(void) {
  int N;
  scanf("%d", &N);
  for (int i = 0; i < N; ++i) {
    scanf("%lld %lld", &art[i].first, &art[i].second);
  }
  sort(art, art + N, cmp);
  llong min_size = art[0].first;
  llong sum_value = art[0].second;
  llong ans = art[0].second;
  for (int i = 1; i < N; ++i) {
    llong value = art[i].second;
    llong size = art[i].first;
    llong tmp = sum_value + value - (size - min_size);
    if (tmp < value) {
      ans = max(ans, value);
      sum_value = value;
      min_size = size;
    } else {
      ans = max(ans, tmp);
      sum_value += value;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 10576
final_rank: 9

code_snippet: #include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
typedef long long llong;
struct Data {
  llong size;
  llong value;
};

inline bool cmp(const Data &a, const Data &b) {
  return a.size == b.size ? a.value < b.value : a.size < b.size;
}

inline llong Max(llong a, llong b) { return a > b ? a : b; }

int main(void) {
  ios_base::sync_with_stdio(false); 
  cin.tie(NULL); 
  cout.tie(NULL);

  int N;
  cin >> N;
  vector<Data> art(N);
  for (int i = 0; i < N; ++i) {
    cin >> art[i].size >> art[i].value;
  }
  sort(art.begin(), art.end(), cmp);
  llong min_size = art[0].size;
  llong sum_value = art[0].value;
  llong ans = art[0].value;
  for (int i = 1; i < N; ++i) {
    llong value = art[i].value;
    llong size = art[i].size;
    llong tmp = sum_value + value - (size - min_size);
    if (tmp < value) {
      ans = Max(ans, value);
      sum_value = value;
      min_size = size;
    } else {
      ans = Max(ans, tmp);
      sum_value += value;
    }
  }
  cout << ans << "\n";
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 10744
final_rank: 6

code_snippet: #include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
typedef long long llong;
typedef pair<llong, llong> Data;
#define N_MAX 500000

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int N;
  cin >> N;
  vector<Data> art(N);
  for (int i = 0; i < N; ++i) {
    cin >> art[i].first >> art[i].second;
  }
  sort(art.begin(), art.end());
  llong min_size = art[0].first;
  llong sum_value = art[0].second;
  llong ans = art[0].second;
  for (int i = 1; i < N; ++i) {
    llong tmp = sum_value + art[i].second - (art[i].first - min_size);
    if (tmp < art[i].second) {
      ans = max(ans, art[i].second);
      sum_value = art[i].second;
      min_size = art[i].first;
    } else {
      ans = max(ans, tmp);
      sum_value += art[i].second;
    }
  }
  cout << ans << "\n";
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 10756
final_rank: 1

code_snippet: #include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long llong;
struct Data {
  llong size;
  llong value;
};
#define N_MAX 500000
Data art[N_MAX];
inline bool cmp(const Data &a, const Data &b) {
  return a.size == b.size ? a.value < b.value : a.size < b.size;
}
int main(void) {
  int N;
  scanf("%d", &N);
  int i;
  for (i = 0; i < N; ++i) {
    scanf("%lld %lld", &art[i].size, &art[i].value);
  }
  sort(art, art + N, cmp);
  llong min_size = art[0].size;
  llong sum_value = art[0].value;
  llong ans = art[0].value;
  for (i = 1; i < N; ++i) {
    llong value = art[i].value;
    llong size = art[i].size;
    llong tmp = sum_value + value - (size - min_size);
    if (tmp < value) {
      ans = (value > ans) ? value : ans; // Replaced the function call with direct comparison
      sum_value = value;
      min_size = size;
    } else {
      ans = (tmp > ans) ? tmp : ans; // Replaced the function call with direct comparison
      sum_value += value;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 10608
final_rank: 11

code_snippet: #include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long llong;

struct Data {
  llong size;
  llong value;
};

#define N_MAX 500000
Data art[N_MAX];

inline bool cmp(const Data &a, const Data &b) {
  return a.size == b.size ? a.value < b.value : a.size < b.size;
}

inline llong Max(llong a, llong b) { return a > b ? a : b; }

int main(void) {
  int N;
  scanf("%d", &N);

  for (int i = 0; i < N; ++i) {
    scanf("%lld %lld", &art[i].size, &art[i].value);
  }

  sort(art, art + N, cmp);

  llong min_size = art[0].size;
  llong sum_value = art[0].value;
  llong ans = art[0].value;

  for (int i = 1; i < N; ++i) {
    llong value = art[i].value;
    llong size = art[i].size;

    if (sum_value + value - (size - min_size) < value) {
      ans = Max(ans, value);
      sum_value = value;
      min_size = size;
    } else {
      llong tmp = sum_value + value - (size - min_size);
      ans = Max(ans, tmp);
      sum_value += value;
    }
  }

  printf("%lld\n", ans);
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 10608
final_rank: 11

code_snippet: #include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long llong;
struct Data {
  llong size;
  llong value;
};
#define N_MAX 500000
Data art[N_MAX];
inline bool cmp(const Data &a, const Data &b) {
  return a.size == b.size ? a.value < b.value : a.size < b.size;
}
inline llong Max(llong a, llong b) { return a > b ? a : b; }
int main(void) {
  int N;
  scanf("%d", &N);
  int i;
  for (i = 0; i < N; ++i) {
    scanf("%lld %lld", &art[i].size, &art[i].value);
  }
  sort(art, art + N, cmp);
  llong min_size = art[0].size;
  llong sum_value = art[0].value;
  llong ans = art[0].value;
  for (i = 1; i < N; ++i) {
    Data &current = art[i]; // store current element in a local variable to reduce array access
    llong value = current.value;
    llong size = current.size;
    llong tmp = sum_value + value - (size - min_size);
    if (tmp < value) {
      ans = Max(ans, value);
      sum_value = value;
      min_size = size;
    } else {
      ans = Max(ans, tmp);
      sum_value += value;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 10572
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
    #define int long long
    int n;cin>>n;
    vector<pair<int,int>>s(n);
    int sizmi=LONG_LONG_MAX;
    for(int i=0;i<n;i++){
        cin>>s[i].first>>s[i].second;
        sizmi=min(sizmi,s[i].first);
    }
    for(int i=0;i<n;i++){
        s[i].first-=sizmi;
    }
    sort(s.begin(),s.end());
    vector<int>t;
    for(int i=0;i<n;i++){
        t.push_back(s[i].second);
        if(i!=n-1){
            t.push_back(s[i].first-s[i+1].first);
        }
    }
    t.push_back(0);

    /*
    for(int i=0;i<n;i++){
        cout<<t[i]<<" ";
    }cout<<"\n";
    */
    int m=n*2-1;
    int ans=t[0];
    int k=ans;
    int r=0,l=0;
    while(r<=m){
        while(k>=0&&r<m){
            r++;
            k+=t[r];
            ans=max(ans,k);
            if(r==m){
                break;
            }
            //cout<<"@ "<<l<<" "<<r<<" "<<k<<" "<<ans<<"\n";
        }
        if(r==m){
            break;
        }
        if(k<0){
            l=r+1;
            r=r+1;
            if(m<l){
                break;
            }
            k=t[l];
            ans=max(ans,k);
        }
    }
    cout<<ans<<"\n";
    /*
    for(int i=0;i<t.size();i++){
        cout<<t[i]<<" ";
    }cout<<"\n";
    */
}
accuracy: 1
time_ms: 300
memory_kb: 20948
final_rank: 19

code_snippet: #include <algorithm>
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>

struct Data {
  int64_t size;
  int64_t value;
};

const int N_MAX = 500000;
Data art[N_MAX];

inline bool cmp(const Data &a, const Data &b) {
  return a.size == b.size ? a.value < b.value : a.size < b.size;
}

int main(void) {
  int N;
  scanf("%d", &N);
  
  for (int i = 0; i < N; ++i) {
    scanf("%lld %lld", &art[i].size, &art[i].value);
  }
  
  std::sort(art, art + N, cmp);
  
  int64_t min_size = art[0].size;
  int64_t sum_value = art[0].value;
  int64_t ans = art[0].value;
  
  for (int i = 1; i < N; ++i) {
    int64_t value = art[i].value;
    int64_t size = art[i].size;
    int64_t tmp = sum_value + value - (size - min_size);
    
    if (tmp < value) {
      ans = std::max(ans, value);
      sum_value = value;
      min_size = size;
    } else {
      ans = std::max(ans, tmp);
      sum_value += value;
    }
  }
  
  printf("%lld\n", ans);
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 10600
final_rank: 10

code_snippet: #include <algorithm>
#include <iostream>
using namespace std;
typedef long long llong;
struct Data {
  llong size;
  llong value;
};
#define N_MAX 500000
Data art[N_MAX];
inline bool cmp(const Data &a, const Data &b) {
  return a.size == b.size ? a.value < b.value : a.size < b.size;
}
int main(void) {
  std::ios_base::sync_with_stdio(false);
  int N;
  cin >> N;
  int i;
  for (i = 0; i < N; ++i) {
    cin >> art[i].size >> art[i].value;
  }
  sort(art, art + N, cmp);
  llong min_size = art[0].size;
  llong sum_value = art[0].value;
  llong ans = art[0].value;
  for (i = 1; i < N; ++i) {
    llong value = art[i].value;
    llong size = art[i].size;
    llong tmp = sum_value + value - (size - min_size);
    if (tmp < value) {
      ans = std::max(ans, value);
      sum_value = value;
      min_size = size;
    } else {
      ans = std::max(ans, tmp);
      sum_value += value;
    }
  }
  cout << ans << "\n";
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 11024
final_rank: 7