이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// 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 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... |