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: Time Limit Exceeded)

using System;
using System.Collections.Generic;

namespace ForATCODER
{
    public class Program
    {
        public static void Main(string[] args)
        {
            int N = s2i(Console.ReadLine());
            int[] an = S2Iarray(Console.ReadLine().Split(' '));
            int[] an2 = new int[N];
            int[] flag = new int[N];
            Array.Copy(an, an2, N);
            int n2 = (int)Math.Floor(N / 2.0f);
            int[] indices = new int[an.Length];
            for (int i = 0; i < indices.Length; i++) indices[i] = i;
            Array.Sort(an, indices);
            long sum = 0;
            int c = 0;
            for (int i = an.Length-1;i >=0;i--)
            {
                if (c >= n2) break;
                int ii = Array.IndexOf(an2, an[i]);
                if (flag[ii] == 1)
                {
                    List<int> except = new List<int>();
                    except.Add(ii);
                    while (true)
                    {
                        int res = arser(an2, an[i], except.ToArray());
                        if (res == -1) break;
                        if (Math.Abs(res - ii) <= 1) { except.Add(res); continue; }
                        if ((res - 1) >= 0 && flag[res - 1] == 1) { except.Add(res); continue; }
                        else if ((res + 1) < N && flag[res + 1] == 1) { except.Add(res); continue; }

                        if (flag[res] == 1) except.Add(res);
                        else { ii = res;break; };
                    }
                }
                if ((ii - 1) >= 0 && flag[ii - 1] == 1) continue;
               else  if ((ii + 1) < N && flag[ii + 1] == 1) continue;
                if (flag[ii] == 1) continue;
                sum += (long)an[i];
                flag[ii] = 1;
                c++;
            }
            Console.WriteLine(sum);

        }
    static int arser(int[] ar ,int search, int[] except)
        {
            for(int i  = 0;i < ar.Length;i++)
            {
                if(ar[i] == search)
                {
                    if (Array.IndexOf(except, i) >= 0) continue;
                    return i;
                }
            }
            return -1;
        }

        static int s2i(String s)
        {
            return Convert.ToInt32(s);
        }
        static void RESULT(int res)
        {
            Console.WriteLine(res);
        }
        static void RESULT(String res)
        {
            Console.WriteLine(res);
        }
        static String[] ReadLineAll(int count)
        {
            String[] res = new String[count];
            for (int i = 0; i < count; i++)
            {
                res[i] = Console.ReadLine();
            }
            return res;
        }

        static int[] S2Iarray(String[] src)
        {
            int len = src.Length;
            int[] res = new int[len];
            for (int i = 0; i < len; i++)
            {
                res[i] = Convert.ToInt32(src[i]);
            }
            return res;
        }
    }
}