using System;
using System.Collections.Generic;
using System.Linq;
public class Test
{
public static void Main()
{
var f = new int[] {2, 0, 2};
Console.WriteLine("{0}", GetWater(f.ToList()));
}
public static int GetWater(List<int> h)
{
if (h == null || h.Count < 2) return 0;
int n = h.Count;
var left = new int[n];
var right = new int[n];
int held = 0;
left[0] = h[0];
for (int i = 1; i < n; i++)
left [i] = Math.Max(left[i-1], h[i]);
right[n-1] = h[n-1];
for (int i = n-2; i >= 0; i--)
right[i] = Math.Max(right[i+1], h[i]);
for (int i =0; i < n; i++)
held += Math.Min(left[i], right[i]) - h[i];
return held;
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uQ29sbGVjdGlvbnMuR2VuZXJpYzsKdXNpbmcgU3lzdGVtLkxpbnE7CgpwdWJsaWMgY2xhc3MgVGVzdAp7CglwdWJsaWMgc3RhdGljIHZvaWQgTWFpbigpCgl7CgkJdmFyIGYgPSBuZXcgaW50W10gezIsIDAsIDJ9OwoJCUNvbnNvbGUuV3JpdGVMaW5lKCJ7MH0iLCBHZXRXYXRlcihmLlRvTGlzdCgpKSk7Cgl9CgpwdWJsaWMgc3RhdGljIGludCBHZXRXYXRlcihMaXN0PGludD4gaCkKewogICAgaWYgKGggPT0gbnVsbCB8fCBoLkNvdW50IDwgMikgcmV0dXJuIDA7CiAgICBpbnQgbiA9IGguQ291bnQ7CiAgICB2YXIgbGVmdCA9IG5ldyBpbnRbbl07CiAgICB2YXIgcmlnaHQgPSBuZXcgaW50W25dOwogICAgCglpbnQgaGVsZCA9IDA7CgkKCWxlZnRbMF0gPSBoWzBdOwoJZm9yIChpbnQgaSA9IDE7IGkgPCBuOyBpKyspCgkgICBsZWZ0IFtpXSA9IE1hdGguTWF4KGxlZnRbaS0xXSwgaFtpXSk7CgkKCSAgIHJpZ2h0W24tMV0gPSBoW24tMV07CgkgICAgZm9yIChpbnQgaSA9IG4tMjsgaSA+PSAwOyBpLS0pCgkgICAgICAgcmlnaHRbaV0gPSBNYXRoLk1heChyaWdodFtpKzFdLCBoW2ldKTsKCQoJZm9yIChpbnQgaSA9MDsgaSA8IG47IGkrKykKCSAgICBoZWxkICs9IE1hdGguTWluKGxlZnRbaV0sIHJpZ2h0W2ldKSAtIGhbaV07CgkKCXJldHVybiBoZWxkOwp9Cn0=