Project Euler #222: Sphere Packing

  • + 0 comments

    I have no idea why i didn't pass all test case

    import math
    
    R, n = input().split()
    R, n = int(R), int(n)
    rOld = sorted([int(i) for i in input().split(" ")])
    if n == 1:
        res = 2*rOld[0]
    else:
        r = []
        for i in range(len(rOld)):
            if i % 2 == 0:
                r.append(rOld[i])
            else:
                r = [rOld[i]] + r
        res = r[0] + r[-1]
        for i in range(n-1):
            res += math.sqrt((r[i]+r[i+1])**2 - (2*R-(r[i]+r[i+1]))**2)
    print(round(res*1000))