Problem p03229 (python)

Fixed submission:


def main():
    num = int(input())
    data_list = [int(input()) for i in range(num)]
    data_list.sort()


    if num % 2 == 1:
        hikukazu = 0
        for i in range(num // 2 + 1):
            hikukazu += 2 * data_list[i]
        hikukazu -= data_list[num // 2] + data_list[num // 2 - 1]

        tasukazu = 0
        for i in range(num // 2 + 1, num):
            tasukazu += 2 * data_list[i]
    else:
        hikukazu = 0
        for i in range(num // 2):
            hikukazu += 2 * data_list[i]
        hikukazu -= data_list[num // 2 - 1]

        tasukazu = 0
        for i in range(num // 2, num):
            tasukazu += 2 * data_list[i]
        tasukazu -= data_list[num // 2]

    a = tasukazu - hikukazu

    if num % 2 == 1:
        hikukazu = 0
        for i in range(num // 2):
            hikukazu += 2 * data_list[i]

        tasukazu = 0
        for i in range(num // 2, num):
            tasukazu += 2 * data_list[i]
        tasukazu -= data_list[num // 2] + data_list[num // 2 + 1]
    else:
        hikukazu = 0
        for i in range(num // 2):
            hikukazu += 2 * data_list[i]
        hikukazu -= data_list[num // 2 - 1]

        tasukazu = 0
        for i in range(num // 2, num):
            tasukazu += 2 * data_list[i]
        tasukazu -= data_list[num // 2]

    b = tasukazu - hikukazu

    # print(a, b)
    print(max(a, b))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):

import sys

def calc(data_ans, num):
    ans = 0
    for i in range(num - 1):
        ans += abs(data_ans[i] - data_ans[i + 1])
    print(ans)
    sys.exit()


def data_next(data_list, data_ans, flg, data_ind_righ, data_ind_left, ans_ind_left, ans_ind_righ, num):
    # print(data_ans, data_ind_left, data_ind_righ, ans_ind_left, ans_ind_righ)
    if ans_ind_left < 0:
        calc(data_ans, num)
    else:
        if flg:
            data_ans[ans_ind_left] = data_list[data_ind_left]
            if ans_ind_righ > num - 1:
                calc(data_ans, num)
            else:
                data_ans[ans_ind_righ] = data_list[data_ind_left + 1]
                data_ind_left += 2

        else:
            data_ans[ans_ind_left] = data_list[data_ind_righ]
            if ans_ind_righ > num - 1:
                calc(data_ans, num)
            else:
                data_ans[ans_ind_righ] = data_list[data_ind_righ - 1]
                data_ind_righ -= 2
        ans_ind_left -= 1
        ans_ind_righ += 1
        if flg:
            flg = 0
        else:
            flg = 1
        data_next(data_list, data_ans, flg, data_ind_righ, data_ind_left, ans_ind_left, ans_ind_righ, num)




def main():
    num = int(input())
    data_list = [int(input()) for i in range(num)]
    if num % 2 == 0:
        data_list.sort()
    else:
        data_list.sort(reverse=True)
    data_ans = [0 for i in range(num)]

    # print(data_list)

    data_ind_righ = num - 1
    data_ind_left = 0

    data_ans[num // 2] = data_list[data_ind_righ]
    data_ind_righ -= 1

    ans_ind_righ = num // 2 + 1
    ans_ind_left = num // 2 - 1

    # print(ans_ind_left, ans_ind_righ)

    data_next(data_list, data_ans, 1, data_ind_righ, data_ind_left, ans_ind_left, ans_ind_righ, num)




if __name__ == '__main__':
    main()