-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
41 lines (35 loc) · 1.14 KB
/
main.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
#include <iostream>
#include <vector>
using namespace std;
int main() {
int testCases;
cin >> testCases;
for (int i = 0; i < testCases; ++i) {
int count;
cin >> count;
vector<int> numbers;
for (int j = 0; j < count; ++j) {
int number;
cin >> number;
numbers.push_back(number);
}
//init values
std::pair<int, int> high, low; //in high, we consider next number as highest
high = make_pair(numbers.at(0), 0);
low = make_pair(1, 0);
//dp
for (unsigned long k = 1; k < count; ++k) {
//calculate for high
//new high
auto newHigh = make_pair(numbers.at(k), max(low.second + abs(1 - numbers.at(k)),
high.second + abs(high.first - numbers.at(k))));
//new low
auto newLow = make_pair(1, max(low.second + abs(low.first - 1), high.second + abs(high.first - 1)));
high = newHigh;
low = newLow;
}
//print result
cout << max(high.second, low.second) << "\n";
}
return 0;
}