Source Code
n = int(input())
a = [*map(int,input().split())]
ans = []
if(n==1):
    print(a[0])
    exit(0)
if(any(i<0 for i in a[:-1])):
    b = [sum(i for i in a[:-1] if i<0),a[-1]+sum(i for i in a[:-1] if(i>0))]
    if(a[0]>b[0]+b[1]):
        print(b[0]+b[1])
    else:
        print(*b)
elif(a[-1]<0):
    if(n==2):
        print(a[-1]+a[0])
    else:
        b = [a[0]+a[-1],sum(a[i] for i in range(1,n-1))]
        if (a[0] > b[0] + b[1]):
            print(b[0] + b[1])
        else:
            print(*b)
else:
    s = sorted(a)
    d = dict()
    ds = dict()
    ele = -1
    for i in range(n):
        d[a[i]] = i
    for i in range(n):
        ds[s[i]] = i
    for i in range(n):
        if(d[s[i]]!=i):
            ele = s[i]
            break
    b = [s[i] for i in range(ds[ele])]
    b += [ele]
    b += [sum(s[i] for i in range(ds[ele]+1,n))]
    print(*b)
Copy
a tamahom
Python 3
20 ms
4.1 MB
Wrong Answer