提交时间:2024-08-21 14:13:33

运行 ID: 180478

#pragma GCC optimize(2) //洛谷做题时去掉上面那一行!!! #include<bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const int INF = 0x3f3f3f3f; #define LL long long #define quick_cin_cout ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); #define endl '\n' #define I return #define AK 0 #define IOI ; int h[N]; int n; int solve() { if (n <= 2) return 0; int l = 0, r = n - 1; int lMax = 0, rMax = 0; int ans = 0; while (l <= r) { if (h[l] < h[r]) { if (h[l] >= lMax) { lMax = h[l]; } else { ans += lMax - h[l]; } ++l; } else { if (h[r] >= rMax) { rMax = h[r]; } else { ans += rMax - h[r]; } --r; } } return ans; } int main() { cin >> n; for (int i = 0; i < n;i++) { cin >> h[i]; } cout << solve(); return 0; } /*Note:*********************** *****************************/ //coder:Mino_XIE1212