This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
using namespace std;
void solve(){
int n; cin >> n;
pair<ll,ll> painting[n];
for( int i = 0; i < n; i++ ){
ll s, v; cin >> s >> v;
painting[i] = mp( s, v );
}
sort( painting, painting+n );
ll prefixSumsValues[n];
memset( prefixSumsValues, 0, sizeof prefixSumsValues );
for( int i = 0; i < n; i++ ){
if ( i > 0 ){ prefixSumsValues[i] += prefixSumsValues[i-1]; }
prefixSumsValues[i] += painting[i].second;
}
// cout << "Prefix Sums of Values: ";
// for( ll i : prefixSumsValues ){ cout << i << " "; }
// cout << endl;
ll bestChoiceOfK[n];
memset( bestChoiceOfK, 0, sizeof bestChoiceOfK );
for( int i = n-1; i >= 0; i-- ){
bestChoiceOfK[i] = prefixSumsValues[i] - painting[i].first; // sum of 0..i (S) - Amax
if( i < n-1 ){ bestChoiceOfK[i] = max( bestChoiceOfK[i], bestChoiceOfK[i+1] ); } // check if greater is better
}
ll maxx = 0;
for( int start_index = 0; start_index < n; start_index++ ){
ll value = bestChoiceOfK[ start_index ] + painting[ start_index ].first; // add start index
if ( start_index > 0 ){ value -= prefixSumsValues[ start_index-1]; } // cut back on unnecessary values
maxx = max( maxx, value );
}
cout << maxx << endl;
return;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
//cin >> t;
while (t--) { solve(); }
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... |