Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Job Scheduling
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<pair<i64, i64>> item(n); for (int i = 0; i < n; i++) { cin >> item[i].second >> item[i].first; } sort(item.begin(), item.end()); i64 res = item[n - 1].second; i64 cur = 0; i64 ans = 0; for (int i = 0, j = n - 1; i <= j; i++) { while (i <= j && cur < item[i].first) { i64 t = min(res, item[i].first - cur); cur += t; ans += 2 * t; res -= t; if (res == 0) { j--; res = item[j].second; } } if (i > j) { break; } i64 t = i == j ? res : item[i].second; ans += t; cur += t; } cout << ans << "\n"; return 0; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
fork mode
|
history
|
discussion