Problem:
Score : 600 points

Problem Statement

Given is an integer sequence A_1, ..., A_N of length N.

We will choose exactly \left\lfloor \frac{N}{2} \right\rfloor elements from this sequence so that no two adjacent elements are chosen.

Find the maximum possible sum of the chosen elements.

Here \lfloor x \rfloor denotes the greatest integer not greater than x.

Constraints

2 \leq N \leq 2\times 10^5

|A_i|\leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 ... A_N

Output

Print the maximum possible sum of the chosen elements.

Sample Input 1

6
1 2 3 4 5 6

Sample Output 1

12

Choosing 2, 4, and 6 makes the sum 12, which is the maximum possible value.

Sample Input 2

5
-1000 -100 -10 0 10

Sample Output 2

0

Choosing -10 and 10 makes the sum 0, which is the maximum possible value.

Sample Input 3

10
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 3

5000000000

Watch out for overflow.

Sample Input 4

27
18 -28 18 28 -45 90 -45 23 -53 60 28 -74 -71 35 -26 -62 49 -77 57 24 -70 -93 69 -99 59 57 -49

Sample Output 4

295

Solution in C# (.NET Core 3.1.201) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace AtCorder
{
	public class Program
	{
		static void Main(string[] args)
		{
			var cin = new Scanner2();
			int n = cin.Int();
			var a = cin.ArrayLong(n);

			int m = n / 2;
			long ans = 0;
			if (n % 2 == 0) {
				long odd = 0;
				long even = 0;
				for (int i = 0; i < m; i++) {
					odd += a[i * 2];
					even += a[i * 2 + 1];
				}

				ans = Math.Max(odd, even);

				int jc = 2;
				var dp = new long[n + 1, jc, 2];
				for (int i = 0; i < n + 1; i++) {
					for (int j = 0; j < jc; j++) {
						for (int k = 0; k < 2; k++) {
							if (i == 0 && j == 0 && k == 0) {
								dp[i, j, k] = 0;
							} else {
								dp[i, j, k] = long.MinValue;
							}
						}
					}
				}

				for (int i = 0; i < n; i++) {
					for (int j = 0; j < jc; j++) {
						//選ぶ
						if (dp[i, j, 0] != long.MinValue) {
							dp[i + 1, j, 1] = Math.Max(
								dp[i + 1, j, 1],
								dp[i, j, 0] + a[i]);
						}

						//選べない
						if (dp[i, j, 1] != long.MinValue) {
							dp[i + 1, j, 0] = Math.Max(
								dp[i + 1, j, 0],
								dp[i, j, 1]);
						}

						//選ばない
						if (j != jc - 1) {
							if (dp[i, j, 0] != long.MinValue) {
								dp[i + 1, j + 1, 0] = Math.Max(
									dp[i + 1, j + 1, 0],
									dp[i, j, 0]);
							}
						}
					}
				}

				for (int j = 1; j < jc; j++) {
					ans = Math.Max(
						ans,
						Math.Max(dp[n, j, 0], dp[n, j, 1]));
				}
			} else {
				int jc = 3;
				var dp = new long[n + 1, jc, 2];
				for (int i = 0; i < n + 1; i++) {
					for (int j = 0; j < jc; j++) {
						for (int k = 0; k < 2; k++) {
							if (i == 0 && j == 0 && k == 0) {
								dp[i, j, k] = 0;
							} else {
								dp[i, j, k] = long.MinValue;
							}
						}
					}
				}

				for (int i = 0; i < n; i++) {
					for (int j = 0; j < jc; j++) {
						//選ぶ
						if (dp[i, j, 0] != long.MinValue) {
							dp[i + 1, j, 1] = Math.Max(
								dp[i + 1, j, 1],
								dp[i, j, 0] + a[i]);
						}

						//選べない
						if (dp[i, j, 1] != long.MinValue) {
							dp[i + 1, j, 0] = Math.Max(
								dp[i + 1, j, 0],
								dp[i, j, 1]);
						}

						//選ばない
						if (j != jc - 1) {
							if (dp[i, j, 0] != long.MinValue) {
								dp[i + 1, j + 1, 0] = Math.Max(
									dp[i + 1, j + 1, 0],
									dp[i, j, 0]);
							}
						}
					}
				}

				for (int j = 1; j < jc; j++) {
					ans = Math.Max(
						ans,
						Math.Max(dp[n, j, 0], dp[n, j, 1]));
				}

				long odd = 0;
				for (int i = 0; i < m; i++) {
					odd += a[i * 2];
				}

				ans = Math.Max(ans, odd);
			}

			Console.WriteLine(ans);
		}
	}

	public static class Helper
	{
		public static void UpdateMin32<T>(T array, int index, int value) where T : IList<int>
			=> array[index] = Math.Min(array[index], value);
		public static void UpdateMax32<T>(T array, int index, int value) where T : IList<int>
			=> array[index] = Math.Max(array[index], value);
		public static void UpdateMin64<T>(T array, int index, long value) where T : IList<long>
			=> array[index] = Math.Min(array[index], value);
		public static void UpdateMax64<T>(T array, int index, long value) where T : IList<long>
			=> array[index] = Math.Max(array[index], value);

		public static T[] Array<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToArray();
		public static List<T> List<T>(int n, Func<int, T> init)
			=> Enumerable.Range(0, n).Select(x => init(x)).ToList();

		private static readonly int[] delta4_ = { 1, 0, -1, 0, 1 };
		public static void DoAt4(int i, int j, int imax, int jmax, Action<int, int> action)
		{
			for (int n = 0; n < 4; n++) {
				int ii = i + delta4_[n];
				int jj = j + delta4_[n + 1];
				if ((uint)ii < (uint)imax && (uint)jj < (uint)jmax) {
					action(ii, jj);
				}
			}
		}
	}

	public class Scanner2
	{
		private readonly char[] delimiter_ = new char[] { ' ' };
		private readonly string filePath_;
		private readonly Func<string> reader_;
		private string[] buf_;
		private int index_;

		public Scanner2(string file = "")
		{
			if (string.IsNullOrWhiteSpace(file)) {
				reader_ = Console.ReadLine;
			} else {
				filePath_ = file;
				var fs = new StreamReader(file);
				reader_ = fs.ReadLine;
			}
			buf_ = new string[0];
			index_ = 0;
		}

		public string Next()
		{
			if (index_ < buf_.Length) {
				return buf_[index_++];
			}

			string st = reader_();
			while (st == "") {
				st = reader_();
			}

			buf_ = st.Split(delimiter_, StringSplitOptions.RemoveEmptyEntries);
			if (buf_.Length == 0) {
				return Next();
			}

			index_ = 0;
			return buf_[index_++];
		}

		public int Int() => int.Parse(Next());
		public long Long() => long.Parse(Next());
		public double Double() => double.Parse(Next());

		public int[] ArrayInt(int N, int add = 0)
		{
			int[] Array = new int[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Int() + add;
			}
			return Array;
		}

		public long[] ArrayLong(int N, long add = 0)
		{
			long[] Array = new long[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Long() + add;
			}
			return Array;
		}

		public double[] ArrayDouble(int N, double add = 0)
		{
			double[] Array = new double[N];
			for (int i = 0; i < N; i++) {
				Array[i] = Double() + add;
			}
			return Array;
		}

		public void Save(string text)
		{
			if (string.IsNullOrWhiteSpace(filePath_)) {
				return;
			}

			File.WriteAllText(filePath_ + "_output.txt", text);
		}
	}
}