Source Code
n = int(input())
a = [*map(int,input().split())]
ans = []
if(any(i<0 for i in a)):
    b = [sum(i for i in a if i<0),sum(i for i in a if(i>0))]
    if(a[0]>b[0]+b[1]):
        print(b[0]+b[1])
    else:
        print(*b)
else:
    o = min(a)
    ind = 0
    while ind < n-1:
        if a.index(o) != 0:
            for i in a[0:a.index(o)]:
                a[-1] += i
                ind += 1
        ind += 1
        ans.append(o)
        o = min(a[a.index(o)+1:])
    ans.append(a[-1])
    print(*ans)
Copy
a AbdelH2O
Python 3
15 ms
3.8 MB
Wrong Answer