import std.stdio, std.string, std.conv; import std.array, std.algorithm; void main(){ auto l = readln.strip.to!int; auto arr = readln.strip.split.map!(to!int).array; uint ans; if(l == 1){ans = arr[0];} while(arr.length > 1){ int max, pos; foreach(i, x; arr){ if(max < x){ max = x; pos = i.to!int; }else if(max == x){ int sum = (pos-1 > 0 ? arr[pos-1]:0) + arr[pos] + (pos+1 < arr.length ? arr[pos+1]:0); int currSum = (i-1 > 0 ? arr[i-1]:0) + arr[i] + (i+1 < arr.length ? arr[i+1]:0); if(sum < currSum){ max = x; pos = i.to!int; } } } ans += max; if(pos+1 < arr.length){ arr = arr.remove(pos+1); } arr = arr.remove(pos); if(pos-1 >= 0){ arr = arr.remove(pos-1); } } ans.writeln; }