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; + +} + +```