1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <bits/stdc++.h>
using namespace std;


int main(){
    int n, l0, l1, l2; cin>>n;
    int tab[n], d[n][2];
    for(auto&x:tab)cin>>x;
    d[0][0]=d[0][1]=0;
    l0=l1=tab[0];
    for(int i=1;i<n;i++){
        l2=l0;
        d[i][0] = d[i-1][1] + ( tab[i]<l1 ? l0=tab[i],0 : (l0=-1e9,1) );
        d[i][1] = d[i-1][0] + ( tab[i]>l2 ? l1=tab[i],0 : (l1=1e9,1) );
    }

    cout<<min(d[n-1][0], d[n-1][1])<<endl;

}