@@ -0,0 +1,1758 @@
+%22%22%22%0Ahttps://code.google.com/codejam/contest/635101/dashboard#s=p1%0A%22%22%22%0A%0A%0Adef merge_sort_count_inversions(A):%0A    if len(A) %3C= 1:%0A        return 0, A%0A%0A    b_inversions, B = merge_sort_count_inversions(A%5B:len(A) / 2%5D)%0A    c_inversions, C = merge_sort_count_inversions(A%5Blen(A) / 2:%5D)%0A    inversions = b_inversions + c_inversions%0A    b, c = 0, 0%0A    A = %5B%5D%0A%0A    while b %3C len(B) and c %3C len(C):%0A        if B%5Bb%5D %3C= C%5Bc%5D:%0A            A.append(B%5Bb%5D)%0A            b += 1%0A        else:%0A            A.append(C%5Bc%5D)%0A            c += 1%0A            # Starting positions of B are less than those of C, so count%0A            # inversions here.%0A            inversions += (len(B) - b)%0A%0A    d, D = (b, B) if b %3C len(B) else (c, C)%0A    A += D%5Bd:%5D%0A    return inversions, A%0A%0A%0A%0Adef solve(N, K, B, T, initial_locations, natural_speeds):%0A    end_locations = %5B(x + v * T) for x,v in zip(initial_locations, natural_speeds)%5D%0A    # end_locations: sorted by starting locations but values are ending locations.%0A    k, swaps, cannot_reach_barn = 0, 0, 0%0A%0A    for loc in reversed(end_locations):%0A        if k == K:%0A            break%0A        elif loc %3C B:%0A            cannot_reach_barn += 1%0A        else:%0A            swaps += cannot_reach_barn%0A            k += 1%0A%0A    return swaps if k %3E= K else -1%0A%0A%0Adef main():%0A    C = int(raw_input().strip())%0A%0A    for c in xrange(1, C+1):%0A        N, K, B, T = map(int, raw_input().strip().split())%0A        initial_locations = map(int, raw_input().strip().split())  # Distinct and in increasing order.%0A        natural_speeds = map(int, raw_input().strip().split())%0A        n_swaps = solve(N, K, B, T, initial_locations, natural_speeds)%0A        print 'Case #%7B%7D: %7B%7D'.format(c, n_swaps if n_swaps %3E= 0 else 'IMPOSSIBLE')%0A%0A%0Aif __name__ == '__main__':%0A    main()%0A

Add google-code-jam/picking_up_chicks.py.