Problem:


Score : 700 points

Problem Statement

You have an integer sequence of length N: a_1, a_2, ..., a_N.

You repeatedly perform the following operation until the length of the sequence becomes 1:

First, choose an element of the sequence.

If that element is at either end of the sequence, delete the element.

If that element is not at either end of the sequence, replace the element with the sum of the two elements that are adjacent to it. Then, delete those two elements.

You would like to maximize the final element that remains in the sequence.

Find the maximum possible value of the final element, and the way to achieve it.

Constraints

All input values are integers.

2 \leq N \leq 1000

|a_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

In the first line, print the maximum possible value of the final element in the sequence.

In the second line, print the number of operations that you perform.

In the (2+i)-th line, if the element chosen in the i-th operation is the x-th element from the left in the sequence at that moment, print x.

If there are multiple ways to achieve the maximum value of the final element, any of them may be printed.

Sample Input 1

5
1 4 3 7 5

Sample Output 1

11
3
1
4
2

The sequence would change as follows:

After the first operation: 4, 3, 7, 5

After the second operation: 4, 3, 7

After the third operation: 11(4+7)

Sample Input 2

4
100 100 -1 100

Sample Output 2

200
2
3
1

After the first operation: 100, 200(100+100)

After the second operation: 200

Sample Input 3

6
-1 -2 -3 1 2 3

Sample Output 3

4
3
2
1
2

After the first operation: -4, 1, 2, 3

After the second operation: 1, 2, 3

After the third operation: 4

Sample Input 4

9
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000

Sample Output 4

5000000000
4
2
2
2
2



Sample input:
5
1 4 3 7 5


Sample output:
11
3
1
4
2

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
 
    int n;
    cin>>n;

    vector<ll> v;
    for(int i=0;i<n;i++){
        int a;
        cin>>a;
        v.push_back(a);
    }
    ll ans=-1e18;
    vector<ll> out;
    while(v.size()!=1){
        vector<ll> em;
        ll mx=max(v[0],v[v.size()-1]);
        ll sum=-1e18;
        ll p=-1;
        for(int i=1;i<v.size()-1;i++){
            ll t=v[i-1]+v[i+1];
            mx=max(mx,v[i]);
            if(t>sum && t>v[i] && t>v[i+1] && t>v[i-1]){
                sum=t;
                p=i;
            }
        }
        if(p==-1){
            if(mx==v[0]){
                out.push_back(v.size());
                for(int i=0;i<v.size()-1;i++){
                    em.push_back(v[i]);
                }
            }
            else {
                out.push_back(1);
                for(int i=1;i<v.size();i++){
                    em.push_back(v[i]);
                }
            }
        }
        else {
            for(int i=0;i<v.size();i++){
                if(i==p-1) continue;
                if(i==p){
                    out.push_back(i+1);
                    em.push_back(v[i-1]+v[i+1]);
                    continue;
                }
                if(i==p+1) continue;
                em.push_back(v[i]);
            }

        }
        v=em;
        //cerr<<v.size()<<endl;
        if(v.size()==1){
            ans=v[0];
            cout<<ans<<"\n";
            cout<<out.size()<<"\n";
            for(auto i:out){
                cout<<i<<"\n";
            }
            return 0;
        }
    }

     
}

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
g1 = a[1::2]
g2 = a[::2]
ans1,ans2 = 0,0
l1,l2 = [],[]
for i in range(len(g1)):
    if g1[i]>0:
        ans1 += g1[i]
        l1.append(i*2+2)
for i in range(len(g2)):
    if g2[i]>0:
        ans2 += g2[i]
        l2.append(i*2+1)
length = n
if ans1>=ans2:
    print(ans1)
    list = []
    for i in range(n,l1[-1],-1):
        list.append(i)
    length -= n-l1[-1]
    for i in range(l1[-1],l1[0],-2):
        if not i in l1:
            list.append(i)
            length -= 2
    for i in range(l1[0]-1):
        list.append(1)
    length -= l1[0]
    for _ in range(length,0,-2):
        list.append(2)
    print(len(list))
    for i in list:
        print(i)
else:
    print(ans2)
    list = []
    for i in range(n,l2[-1],-1):
        list.append(i)
    length -= n-l2[-1]
    for i in range(l2[-1],l2[0],-2):
        if not i in l2:
            list.append(i)
            length -= 2
    for i in range(l2[0]-1):
        list.append(1)
    length -= l2[0]
    for _ in range(length,0,-2):
        list.append(2)
    print(len(list))
    for i in list:
        print(i)

Submission status: Runtime Error

Solution code:
N = int(input())
a = [(int(v),i) for i, v in enumerate(input().split())]
ao = sum([v for v, i in a if i%2 and v>0])
ae = sum([v for v, i in a if not i%2 and v>0])
if max(ao, ae)== 0:
     ai = [a.index(max(a))]
     Ans = [1]*(ai)+list(range(N-ai,1,-1))
     for i in Ans:
         print(i)
if ao >= ae:
    print(ao)
    yn = [i for v, i in a if i%2 and v>0]
else:
    print(ae)
    yn = [i for v, i in a if not i%2 and v>0]
listyn = [i in yn for i in range(N)]
Ans = []
while not listyn[0]:
    Ans.append(1)
    listyn = listyn[1:]
while not listyn[-1]:
    Ans.append(len(listyn))
    listyn = listyn[:-1]
while True:
    if len(listyn) == 1:
        break
    if len(listyn) in [2,3]:
        Ans.append(2)
        break
    if listyn[2]:
        Ans.append(2)
        listyn = [True] + listyn[3:]
    else:
        Ans.append(3)
        listyn = [True] + listyn[4:]
print(len(Ans))
for i in Ans:
    print(i)

Submission status: Runtime Error

Solution code:
def inpl(): return [int(i) for i in input().split()]
from collections import defaultdict
import sys
def edge(S):
    e = defaultdict(lambda: set())
    for i in range(N+1):
        for j in S:
            e[i].add(i%j)
        e[i] = e[i] | {i}
    return e
def path(edge):
    p = defaultdict(lambda: set())
    for st in range(N+1):
        Q, p[st], new = [edge[st]]*3
        while Q:
            for j in Q:
                new = new | edge[j]
            Q = new - p[st]
            p[st] = p[st] | new
    return p

K = int(input())
A = inpl()
B = inpl()
for i in range(K):
    if A[i] < B[i]:
        print(-1)
        sys.exit()
if A == B:
    print(0)
    sys.exit()
N = max(A) 
T = []
for l in range(N,-1,-1):
    S = T + list(range(1,1+l))
    Pa = path(edge(S))
    for i in range(K):
        if B[i] not in Pa[A[i]]:
            break
    else:
        continue
    T.append(l+1)
if not T:
    print(2)
    sys.exit()
if N+1 in T:
    print(-1)
    sys.exit()
print(sum([2**i for i in T]))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))
odd = 0
even = 0
num = []
res = []

for i in range(N):
    if i%2 == 0 and a[i] > 0:even += a[i]
    if i%2 == 1 and a[i] > 0:odd += a[i]
if even >= odd:
    for i in range(N):
        if i % 2 == 0 and a[i] > 0: num.append(i)
else:
    for i in range(N):
        if i % 2 == 1 and a[i] > 0: num.append(i)

count = len(num)

ed = N-1
while ed > num[-1]:
    res.append(ed)
    ed -= 1
for i in range(count)[::-1]:
    if i > 0:
        if num[i] - num[i-1] > 2:
            for j in range(1,(num[i]-num[i-1])/2):
                res.append(num[i]-2*j)
for i in range(num[0]):
    res.append(0)
for i in range(count-1):
    res.append(1)

print(max(even,odd))
print(len(res))
print('\n'.join(map(str, res)))

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))
odd = 0
even = 0
num = []
res = []

for i in range(N):
    if i%2 == 0 and a[i] > 0:even += a[i]
    if i%2 == 1 and a[i] > 0:odd += a[i]
if even >= odd:
    for i in range(N):
        if i % 2 == 0 and a[i] > 0: num.append(i)
else:
    for i in range(N):
        if i % 2 == 1 and a[i] > 0: num.append(i)

count = len(num)

ed = N-1
while ed > num[-1]:
    res.append(ed)
    ed -= 1
for i in range(count)[::-1]:
    if i > 0:
        if num[i] - num[i-1] > 2:
            for j in range(1,(num[i]-num[i-1])/2):
                res.append(num[i]-2*j)
for i in range(num[0]):
    res.append(0)
for i in range(count-1):
    res.append(1)

print(max(even,odd),sep='\n')
print(len(res),sep='\n')
for x in res:
    print(x,sep='\n')

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))
odd = 0
even = 0
num = []
res = []

for i in range(N):
    if i%2 == 0 and a[i] > 0:even += a[i]
    if i%2 == 1 and a[i] > 0:odd += a[i]
if even >= odd:
    for i in range(N):
        if i % 2 == 0 and a[i] > 0: num.append(i)
else:
    for i in range(N):
        if i % 2 == 1 and a[i] > 0: num.append(i)

count = len(num)

ed = N-1
while ed > num[-1]:
    res.append(ed)
    ed -= 1
for i in range(count)[::-1]:
    if i > 0:
        if num[i] - num[i-1] > 2:
            for j in range(1,(num[i]-num[i-1])/2):
                res.append(num[i]-2*j)
for i in range(num[0]):
    res.append(0)
for i in range(count-1):
    res.append(1)

print(max(even,odd))
print(len(res))
for x in res:
    print(x)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))
odd = 0
even = 0
num = []
res = []

for i in range(N):
    if i%2 == 0 and a[i] > 0:even += a[i]
    if i%2 == 1 and a[i] > 0:odd += a[i]
if even >= odd:
    for i in range(N):
        if i % 2 == 0 and a[i] > 0: num.append(i)
else:
    for i in range(N):
        if i % 2 == 1 and a[i] > 0: num.append(i)

count = len(num)

ed = N
while ed > num[-1]:
    res.append(ed)
    ed -= 1
for i in range(count)[::-1]:
    if i > 0:
        if num[i] - num[i-1] > 2:
            for j in range(1,(num[i]-num[i-1])/2):
                res.append(num[i]-2*j)
for i in range(num[0]):
    res.append(0)
for i in range(count-1):
    res.append(1)

print(max(even,odd))
print(len(res))
for x in res:
    print(x)

Submission status: Runtime Error

Solution code:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,collections,itertools,re,math,fractions,decimal,random,array,bisect,heapq

# decimal.getcontext().prec = 50
# sys.setrecursionlimit(10000)
# MOD = 10**9 + 7


def solve(f):
    n = f.read_int()
    a = f.read_int_list()

    memo = {}
    def calc(l, r):
        if l > r: return (0, [])
        if (l,r) in memo: return memo[(l,r)]
        if r-l == 0:
            res = (a[r], [])
        elif r-l == 1:
            if a[l] > a[r]:
                res = (a[l], [r])
            else:
                res = (a[r], [l])
        else:
            ma = -float('inf')
            for i in xrange(l, r+1):
                c11, c12 = calc(l, i-1)
                c21, c22 = calc(i+1, r)
                if c11+c21 > ma:
                    res = (c11+c21, c12+c22+[i])
                    ma = c11+c21

        memo[(l,r)] = res
        return res

    ans = []
    num, pro = calc(0, n-1)
    ans.append(num)
    ans.append(len(pro))

    while len(pro) > 0:
        p = pro.pop()
        ans.append(p+1)
        if p == 0 or p == n-1:
            diff = 1
        else:
            diff = 2
        for i in xrange(len(pro)):
            if pro[i] > p: pro[i] -= diff
        n -= diff

    return "\n".join(map(str, ans))


class Reader(object):
    def __init__(self, filename=None):
        self.file = open(filename) if filename is not None else None
        self.case = 1

    def __readline(self):
        return self.file.next().strip() if self.file else raw_input()

    def next_case(self):
        self.file.next()
        self.case += 1

    def read_int(self): return int(self.__readline())
    def read_float(self): return float(self.__readline())
    def read_long(self): return long(self.__readline())
    def read_decimal(self): return decimal.Decimal(self.__readline())
    def read_str(self): return self.__readline()

    def read_int_list(self): return map(int, self.__readline().split())
    def read_float_list(self): return map(float, self.__readline().split())
    def read_long_list(self): return map(long, self.__readline().split())
    def read_decimal_list(self): return map(decimal.Decimal, self.__readline().split())
    def read_str_list(self): return self.__readline().split()

if __name__ == '__main__':
    filename = sys.argv[1] if len(sys.argv) > 1 else None
    f = Reader(filename)
    if f.file:
        while True:
            print "Case #%d\n"%f.case, solve(f)
            try:
                f.next_case()
            except StopIteration:
                break
    else:
        print solve(f)

Submission status: Runtime Error

Solution code:
# coding: utf-8
n=int(input())
a=list(map(int,input().split()))
i=0
x,y=0,0
def dl(x):
    if x==1:
        print(x)
        del a[0]
        return
    elif x==len(a):
        print(x)
        del a[x-1]
        return
    x-=1
    print(x)
    a[x]=a[x+1]+a[x-1]
    del a[x+1]
    del a[x-1]

while i<len(a):
    if a[i]>0:
        x+=a[i]
    i+=2
i=1
while i<len(a):
    if a[i]>0:
        y+=a[i]
    i+=2
if x>y:
    print(x)
    while len(a)!=1:
        if a[0]<0:
            dl(1)
            dl(1)
        elif len(a)>=3 and a[2]<0:
            if len(a)==3:
                dl(3)
                dl(2)
            else:
                dl(3)
        else:
            dl(2)
else:
    print(y)
    while len(a)!=1:
        if len(a)==2:
            dl(1)
        elif a[1]<0:
            dl(2)
            dl(1)
        elif len(a)>=4 and a[3]<0:
            dl(1)
            dl(2)
        else:
            if a[1]<0:
                dl(1)
                dl(1)
                dl(1)
            else:
                dl(3)
                dl(1)

Submission status: Runtime Error

Solution code:
N = int(input())
src = list(map(int,input().split()))

use = [a >= 0 for a in src]
if not any(use):
    i = src.index(max(src))
    use[i] = True

opes = []
end = N-1
while not use[end]:
    opes.append(end + 1)
    end -= 1
start = 0
while not use[start]:
    opes.append(1)
    start += 1

arr = [[a,u] for a, u in zip(src[start:end+1], use[start:end+1])]

def out_exit():
    print(arr[0][0])
    print(len(opes))
    print(*opes, sep='\n')
    exit()

if len(arr) == 1:
    out_exit()

sum1 = sum(a for a,u in arr[::2] if u)
sum2 = sum(a for a,u in arr[1::2] if u)
for i in range(int(sum1>sum2),len(arr),2):
    arr[i][1] = False

if not arr[-1][1]:
    opes.append(len(arr))
    arr.pop()
if not arr[0][1]:
    opes.append(1)
    arr = arr[1:]

def merge_at(i):
    a1,u1 = arr[i-1]
    a2,u2 = arr[i+1]
    assert u1 == u2
    arr[i] = [a1+a2, u1]
    del arr[i+1]
    del arr[i-1]
    opes.append(i+1)

while True:
    for i in range(2,len(arr)-1,2):
        if not arr[i][1]:
            merge_at(i)
            break
    else:
        break

while len(arr) >= 3:
    merge_at(1)

out_exit()

Submission status: Runtime Error

Solution code:
N = int(input())
src = list(map(int,input().split()))

use = [a >= 0 for a in src]
all_negative = False
if not any(use):
    all_negative = True
    i = src.index(max(src))
    use[i] = True

opes = []
end = N-1
while not use[end]:
    opes.append(end + 1)
    end -= 1

start = 0
while not use[start]:
    opes.append(1)
    start += 1

arr = [[a,u] for a, u in zip(src[start:end+1], use[start:end+1])]

if not all_negative:
    sum1 = sum(a for a,u in arr[::2] if u)
    sum2 = sum(a for a,u in arr[1::2] if u)
    for i in range(int(sum1>sum2),len(arr),2):
        arr[i][1] = False

if not arr[-1][1]:
    opes.append(len(arr))
    arr.pop()

if not arr[0][1]:
    opes.append(1)
    arr = arr[1:]

def merge_at(i):
    a1,u1 = arr[i-1]
    a2,u2 = arr[i+1]
    assert u1 == u2
    arr[i] = [a1+a2, u1]
    del arr[i+1]
    del arr[i-1]
    opes.append(i+1)

while True:
    for i in range(2,len(arr)-1,2):
        if not arr[i][1]:
            merge_at(i)
            break
    else:
        break

while len(arr) >= 3:
    merge_at(1)

print(arr[0][0])
print(len(opes))
print(*opes, sep='\n')

Submission status: Runtime Error

Solution code:
N = int(input())
src = list(map(int,input().split()))

use = [a >= 0 for a in src]
all_negative = False
if not any(use):
    all_negative = True
    i = src.index(max(src))
    use[i] = True

opes = []
end = N-1
while not use[end]:
    opes.append(end + 1)
    end -= 1

start = 0
while not use[start]:
    opes.append(1)
    start += 1

arr = [[a,u] for a, u in zip(src[start:end+1], use[start:end+1])]

if not all_negative:
    sum1 = sum(a for a,u in arr[::2] if u)
    sum2 = sum(a for a,u in arr[1::2] if u)
    for i in range(int(sum1>sum2),len(arr),2):
        arr[i][1] = False

if not arr[-1][1]:
    opes.append(len(arr))
    arr.pop()

if not arr[0][1]:
    opes.append(1)
    arr = arr[1:]
#
# assert len(arr)%2 == 1
# assert arr[0][1]
# assert arr[-1][1]

def merge_at(i):
    a1,u1 = arr[i-1]
    a2,u2 = arr[i+1]
    assert u1 == u2
    arr[i] = [a1+a2, u1]
    del arr[i+1]
    del arr[i-1]
    opes.append(i+1)

while True:
    for i in range(2,len(arr),2):
        if not arr[i][1]:
            merge_at(i)
            break
    else:
        break

while len(arr) >= 3:
    merge_at(1)

print(arr[0][0])
print(len(opes))
print(*opes, sep='\n')

Submission status: Runtime Error

Solution code:
N = int(input())
src = list(map(int,input().split()))

use = [a >= 0 for a in src]
all_negative = False
if not any(use):
    all_negative = True
    i = src.index(max(src))
    use[i] = True

opes = []
end = N-1
while not use[end]:
    opes.append(end + 1)
    end -= 1

start = 0
while not use[start]:
    opes.append(1)
    start += 1

arr = [[a,u] for a, u in zip(src[start:end+1], use[start:end+1])]

if not all_negative:
    sum1 = sum(a for a,u in arr[::2] if u)
    sum2 = sum(a for a,u in arr[1::2] if u)
    for i in range(int(sum1>sum2),len(arr),2):
        arr[i][1] = False

if not arr[-1][1]:
    opes.append(len(arr))
    arr.pop()

if not arr[0][1]:
    opes.append(1)
    arr = arr[1:]

assert len(arr)%2 == 1
assert arr[0][1]
assert arr[-1][1]

def merge_at(i):
    a1,u1 = arr[i-1]
    a2,u2 = arr[i+1]
    assert u1 == u2
    arr[i] = [a1+a2, u1]
    del arr[i+1]
    del arr[i-1]
    opes.append(i+1)

while True:
    for i in range(2,len(arr),2):
        if not arr[i][1]:
            merge_at(i)
            break
    else:
        break

while len(arr) >= 3:
    merge_at(1)

print(arr[0][0])
print(len(opes))
print(*opes, sep='\n')

Submission status: Runtime Error

Solution code:
N = int(input())
src = list(map(int,input().split()))

use = [a >= 0 for a in src]
if not any(use):
    i = src.index(max(src))
    use[i] = True

opes = []
end = N-1
while not use[end]:
    opes.append(end + 1)
    end -= 1

start = 0
while not use[start]:
    opes.append(1)
    start += 1

arr = [[a,u] for a, u in zip(src[start:end+1], use[start:end+1])]

sum1 = sum(a for a,u in arr[::2] if u)
sum2 = sum(a for a,u in arr[1::2] if u)
for i in range(int(sum1>sum2),len(arr),2):
    arr[i][1] = False

if not arr[-1][1]:
    opes.append(len(arr))
    arr.pop()

if not arr[0][1]:
    opes.append(1)
    arr = arr[1:]

assert len(arr)%2 == 1
assert arr[0][1]
assert arr[-1][1]

def merge_at(i):
    a1,u1 = arr[i-1]
    a2,u2 = arr[i+1]
    assert u1 == u2
    arr[i] = [a1+a2, u1]
    del arr[i+1]
    del arr[i-1]
    opes.append(i+1)

while True:
    for i in range(2,len(arr),2):
        if not arr[i][1]:
            merge_at(i)
            break
    else:
        break

while len(arr) >= 3:
    merge_at(1)

print(arr[0][0])
print(len(opes))
print(*opes, sep='\n')