using System.Collections.Generic; using System; public class Hello { static void Main() { var n = int.Parse(Console.ReadLine().Trim()); string[] line = Console.ReadLine().Trim().Split(' '); var a = Array.ConvertAll(line, int.Parse); getAns(n,a); } static void getAns(int n,int[]a) { var ans = new List(); var now = a[0]; var flag = now== 1? true: false; var tc = flag ? 1 : 0; for (int i = 1; i < n; i++) { if (flag) { if (now == 1) { if (a[i] == 0) { tc++; now = 0; } else { ans.Add(tc); tc = 1; flag = true; now = 1; } } else { if (a[i] == 1) { tc++; now = 1; } else { ans.Add(tc); tc = 0; flag = false; now = 0; } } } else { if (a[i] == 1) { flag = true; tc++; now = 1; } else now = 0; } } if (flag && now == 1) ans.Add(tc - 1); if (flag && now == 0) ans.Add(tc ); var res = 0L; foreach(var x in ans) { if (x % 2 == 0) res += (x / 2) * (x / 2 + 1); else res += (x + 1) * (x + 1) / 4L; } Console.WriteLine(res); } }