Skip to content

Commit 7d6d4f7

Browse files
authored
[20250220] BOJ / G4 / 같은 수로 만들기 / 권혁준
1 parent 4779fc7 commit 7d6d4f7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
```cpp
2+
3+
#include <iostream>
4+
#include <algorithm>
5+
#include <stack>
6+
using namespace std;
7+
using ll = long long;
8+
9+
int main()
10+
{
11+
cin.tie(0)->sync_with_stdio(0);
12+
13+
ll N, ans = 0;
14+
stack<ll> S;
15+
cin >> N;
16+
for (int i = 1; i <= N; i++) {
17+
ll a;
18+
cin >> a;
19+
if (!S.empty() && S.top() < a) {
20+
ll t = S.top();
21+
while (!S.empty() && S.top() < a) S.pop();
22+
ans += a - t;
23+
}
24+
S.push(a);
25+
}
26+
27+
ans -= S.top();
28+
ll mx = 0;
29+
while (!S.empty()) mx = S.top(), S.pop();
30+
ans += mx;
31+
32+
cout << ans;
33+
34+
}
35+
36+
```

0 commit comments

Comments
 (0)