-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1534B Histogram Ugliness.cpp
74 lines (73 loc) · 1.7 KB
/
1534B Histogram Ugliness.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
author : MishkatIT
created : Friday 2023-06-16-21.07.10
*/
#include<bits/stdc++.h>
#define fio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define debug(_) cout << #_ << " is " << _ << '\n';
using namespace std;
using ll = long long;
const ll mod = 1e9 + 7;
const ll N = 1e5 + 10;
const ll inf = 1e9;
const ll linf = 1e18;
#define int long long
signed main()
{
fio;
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
vector<int>v(n);
for (auto& i : v)
{
cin >> i;
}
if(n == 1)
{
if(v[0] == 0)cout << 0 << '\n';
else cout << v[0] << '\n';
continue;
}
int ans = 0;
for (int i = 0; i < n; i++)
{
if(i == 0)
{
if(v[1] < v[0])
{
ans += (v[0] - v[1]);
v[0] = v[1];
}
}
else if(i == n - 1)
{
if(v[n - 2] < v[n - 1])
{
ans += (v[n - 1] - v[n - 2]);
v[n - 1] = v[n - 2];
}
}
else
{
if(v[i] > v[i - 1] && v[i] > v[i + 1])
{
int mx = max(v[i - 1], v[i + 1]);
// debug(mx)
ans += (v[i] - mx);
v[i] = mx;
}
}
}
// ans += *max_element(v.begin(), v.end()) * 2;
ans += v.front();
for (int i = 0; i + 1 < n; i++)
ans += abs(v[i] - v[i + 1]);
ans += v.back();
cout << ans << '\n';
}
return 0;
}