We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 0929b93 + 7d6d4f7 commit 5d42c2eCopy full SHA for 5d42c2e
khj20006/202502/20 BOJ G4 같은 수로 만들기.md
@@ -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