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 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; } }