#include<bits/stdc++.h>
#define resp1 resp
#define int long long
using namespace std;
const int N = 1000010;
int res[N];
const int mod = 1e6+3;
int main(){
int n;
cin >> n;
vector <int> v;
int idxmax = 0;
int mxval = 0;
for(int i = 0;i < n;i++){
int x;
cin >> x;
mxval = max(mxval, x);
v.push_back(x);
if(x > v[idxmax]) idxmax = i;
}
int pref = 0;
for(int i = 0;i < idxmax;i++){
pref = max(pref, v[i]);
res[i] = pref;
}
pref = 0;
for(int i = n-1;i > idxmax;i--){
pref = max(pref, v[i]);
res[i] = pref;
}
res[idxmax] = v[idxmax];
int resposta = 0;
for(int val = 1;val <= mxval;val++){
int st = n, ed = -1;
for(int i = 0;i < n;i++){
if(v[i] == val and v[i] != res[i]){
st = min(st, i);
ed = max(ed, i);
}
}
if(st == n) continue;
//cout << val << ' ';
int resp = 101;
for(int i = 0;i < st;i++){
if(v[i] <= val) continue;
resp = min(resp, v[i]);
}
int resp2 = 101;
for(int i = n-1;i > st;i--){
if(v[i] <= val) continue;
resp2 = min(resp2, v[i]);
}
int resp3 = 101, resp4 = 101;
for(int i = 0;i < ed;i++){
if(v[i] <= val) continue;
resp3 = min(resp3, v[i]);
}
for(int i = n-1;i > ed;i--){
if(v[i] <= val) continue;
resp4 = min(resp4, v[i]);
}
if(st == ed){
v[st] = val+1;
resposta += resp+resp2+val;
continue;
}
if(resp2 < resp3){
resposta += resp1+resp2+val;
v[st] = val+1;
resposta += val+1+val+resp4;
v[ed] = val+1;
}
else{
resposta += resp3+resp4+val+val+val+1+resp1;
v[st] = v[ed] = val+1;
}
for(int i = 0;i < n;i++){
if(v[i] == val and v[i] != res[i]){
resposta += val+1+val+1+val;
v[i]++;
}
}
}
cout << (resposta % mod) << '\n';
}
Compilation message
cc1plus: error: '::main' must return 'int'