Submission #1296265

#TimeUsernameProblemLanguageResultExecution timeMemory
1296265nikaa123Art Exhibition (JOI18_art)C++20
100 / 100
207 ms24040 KiB
#include <bits/stdc++.h>
using namespace std;
 
#define int long long 
#define eb emplace_back
#define mp make_pair
#define pb push_back
#define pp pop_back
#define endl '\n'
#define ff first
#define ss second
#define stop exit(0)
#define sz(x) (int)x.size()
#define pause system("pause")
#define all(x) (x).begin(), (x).end()
#define deb(x) cout << #x << "-" << x << endl
mt19937 mt(time(nullptr)); 
 
typedef char chr;
typedef string str;
typedef long long ll;
typedef vector<int> vii;
typedef pair<int, int> pii;
 
const long long INF = LLONG_MAX;
const int inf = INT_MAX;
const int mod = 1000003;
const int MOD = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,1,0,-1};
const double PI = 2 * acos(0.0);
 
const int N = 1e6+5;

int n;
pii c[N];
int a[N],b[N];
int pref[N],suf[N];
int sum;

inline void test_case() {

    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> c[i].ff >> c[i].ss;
        sum += c[i].ss;
    }
    sort(c+1,c+1+n);
    int cur = 0;
    for (int i = 1; i <= n-1; i++) {
        cur += c[i].ss;
        a[i] = c[i+1].ff-c[1].ff-cur;
    }
    // deb(a[2]);
    cur = 0;
    for (int i = n; i >= 2; i--) {
        cur += c[i].ss;
        b[i] = c[n].ff-c[i-1].ff-cur;
    }
    for (int i = 1; i <= n; i++) {
        pref[i] = max(pref[i-1],a[i]);
    }
    for (int i = n; i >= 1; i--) {
        suf[i] = max(suf[i+1],b[i]);
    }
    int ans = sum-c[n].ff+c[1].ff;
    // deb(ans);
    int mx = 0;
    for (int i = 1; i <= n; i++) {
        int cur = pref[i-1]+suf[i+1];
        mx = max(mx,cur);
        // cout << i << " --> " << pref[i-1] << " " << suf[i+1] << endl;
    }
    ans += mx;
    cout << ans << endl;

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

    int T = 1;
    // cin >> T;
    while (T--) test_case();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...