From 7d6d4f74ac0b9b3c927992eca9ebf518bee64346 Mon Sep 17 00:00:00 2001 From: oncsr Date: Thu, 20 Feb 2025 17:31:53 +0900 Subject: [PATCH] =?UTF-8?q?[20250220]=20BOJ=20/=20G4=20/=20=EA=B0=99?= =?UTF-8?q?=EC=9D=80=20=EC=88=98=EB=A1=9C=20=EB=A7=8C=EB=93=A4=EA=B8=B0=20?= =?UTF-8?q?/=20=EA=B6=8C=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4 \353\247\214\353\223\244\352\270\260.md" | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 "khj20006/202502/20 BOJ G4 \352\260\231\354\235\200 \354\210\230\353\241\234 \353\247\214\353\223\244\352\270\260.md" diff --git "a/khj20006/202502/20 BOJ G4 \352\260\231\354\235\200 \354\210\230\353\241\234 \353\247\214\353\223\244\352\270\260.md" "b/khj20006/202502/20 BOJ G4 \352\260\231\354\235\200 \354\210\230\353\241\234 \353\247\214\353\223\244\352\270\260.md" new file mode 100644 index 00000000..525e324b --- /dev/null +++ "b/khj20006/202502/20 BOJ G4 \352\260\231\354\235\200 \354\210\230\353\241\234 \353\247\214\353\223\244\352\270\260.md" @@ -0,0 +1,36 @@ +```cpp + +#include +#include +#include +using namespace std; +using ll = long long; + +int main() +{ + cin.tie(0)->sync_with_stdio(0); + + ll N, ans = 0; + stack S; + cin >> N; + for (int i = 1; i <= N; i++) { + ll a; + cin >> a; + if (!S.empty() && S.top() < a) { + ll t = S.top(); + while (!S.empty() && S.top() < a) S.pop(); + ans += a - t; + } + S.push(a); + } + + ans -= S.top(); + ll mx = 0; + while (!S.empty()) mx = S.top(), S.pop(); + ans += mx; + + cout << ans; + +} + +```