Submission #1023818

#TimeUsernameProblemLanguageResultExecution timeMemory
1023818vjudge1Art Exhibition (JOI18_art)C++17
100 / 100
142 ms21080 KiB
// Stove (JOI 2017/18 Final Round) https://vjudge.net/contest/640014#problem/B 
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define nl "\n"
#define inf LONG_LONG_MAX
#define pi pair<int, int>
#define mp make_pair
#define ff first
#define ss second

signed main(){
	fastio
    int n;
    cin >> n;
    pi art[n];
    for (int i=0; i<n; i++){
		int a, b;
		cin >> a >> b;
		art[i] = mp(a, b); // a is size, b is value
	}
	sort(art, art+n); // sort by increasing size
	// iterate from back
	int prev = art[n-1].ss - art[n-1].ff, ans = art[n-1].ss;
	//int end = n-1;
	for (int i=n-2; i>=0; i--){
		int take = prev + art[i].ss;
		int restart = art[i].ss - art[i].ff;
		if(take>restart){
			prev = take;
			ans = max(ans, take + art[i].ff);
		}
		else{
			prev = restart;
			ans = max(ans, restart + art[i].ff);
		}
	}
	cout << ans << nl;
	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...