이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define SZ(v) ((int)(v).size())
using ll = long long;
int main(void)
{
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int nb_pieces;
cin >> nb_pieces;
vector<pair<ll, ll> > pieces(nb_pieces);
for (auto &[taille, valeur] : pieces)
cin >> taille >> valeur;
sort(pieces.begin(), pieces.end());
vector<ll> prefix_sum(nb_pieces+1);
for (int i(0); i < nb_pieces; ++i)
prefix_sum[i+1] = prefix_sum[i] + pieces[i].second;
vector<ll> suffix_max(nb_pieces+1);
suffix_max[nb_pieces] = -1e18;
for (int i(nb_pieces-1); i >= 0; --i)
suffix_max[i] = max(suffix_max[i+1], prefix_sum[i+1] - pieces[i].first);
ll ans(-1e18);
for (int i(0); i < nb_pieces; ++i)
ans = max(ans, pieces[i].first + suffix_max[i] - prefix_sum[i]);
cout << ans << endl;
}
# | 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... |