From 9675b0746076a5a12ec4b5afc8ae132560984882 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Mon, 28 Mar 2022 16:50:52 +0800 Subject: [PATCH] =?UTF-8?q?#399.=20=E5=8F=A0=E8=99=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://sjzezoj.com/submission/51873 --- S2OJ/399/399.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 S2OJ/399/399.cpp diff --git a/S2OJ/399/399.cpp b/S2OJ/399/399.cpp new file mode 100644 index 00000000..11e63cb7 --- /dev/null +++ b/S2OJ/399/399.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 1e5 + 5; + +int n; +long long sum[N], ans = std::numeric_limits::min(); +std::pair a[N]; + +int main() { + std::ios::sync_with_stdio(false); + + cin >> n; + for (int i = 1; i <= n; i++) { + cin >> a[i].first >> a[i].second; + } + + std::sort(a + 1, a + 1 + n, [](std::pair a, std::pair b) -> bool { + return a.first + a.second < b.first + b.second; + }); + + for (int i = 1; i <= n; i++) { + sum[i] = sum[i - 1] + a[i].first; + ans = std::max(ans, sum[i - 1] - a[i].second); + } + + cout << ans << endl; + + return 0; +}