fork download
  1. import sys
  2. sys.stdin=open("CANDY.inp","r")
  3. sys.stdout=open("CANDY.out","w")
  4. n=int(input())
  5. a=list(map(int,input().split()))
  6. a.sort(reverse=True)
  7. if n<=20:
  8. A=10**9
  9. k=sum(a)
  10. def đm(i,S):
  11. global A,a
  12. if i==n:
  13. A=min(A,abs(S-(k-S)))
  14. else:
  15. đm(i+1,S)
  16. đm(i+1,S+a[i])
  17. đm(0,0)
  18. print(A)
  19. else:
  20. B=0
  21. C=0
  22. for i in range(n):
  23. if B>C:
  24. C+=a[0]
  25. a.pop(0)
  26. else:
  27. B+=a[0]
  28. a.pop(0)
  29. print(abs(B-C))
  30.  
Success #stdin #stdout 0.03s 9688KB
stdin
Standard input is empty
stdout
Standard output is empty