Submission #632632

#TimeUsernameProblemLanguageResultExecution timeMemory
632632aryan12Art Exhibition (JOI18_art)C++17
100 / 100
212 ms16348 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
 
bool cmp(pair<int, int> a, pair<int, int> b) {
	if(a.first == b.first)
		return a.second < b.second;
	return a.first < b.first;
}
 
void Solve() {
	int n;
	cin >> n;
	vector<pair<int, int> > a(n + 1); //size, value;
	for(int i = 1; i <= n; i++) {
		cin >> a[i].first >> a[i].second;
	}
	if(n == 1)
	{
	    cout << a[1].second << "\n";
	    return;
	}
	sort(a.begin(), a.end(), cmp);
	int values[n + 1]; //values with respect to position 1
	values[1] = a[1].second;
	for(int i = 2; i <= n; i++) {
		values[i] = values[i - 1] + a[i].second - (a[i].first - a[i - 1].first);
	}
	for(int i = n - 1; i > 0; i--) {
		values[i] = max(values[i], values[i + 1]);
	}
	int diff = 0, ans = 0;
	for(int i = 1; i < n; i++) {
		ans = max(ans, values[i] - diff);
		diff += a[i].second;
		diff -= (a[i + 1].first - a[i].first);
	}
	cout << ans << "\n";
}
 
int32_t main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	Solve();
	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...