A. Buying Sweets … Simple print modulus (=changes) result

1 2 3 4 5 |
x = int(input()) a = int(input()) b = int(input()) print((x-a)%b) |

B. Coins … This takes N^3, and it’s much costly. And obviously there is a room for improvement.

1 2 3 4 5 6 7 8 9 10 11 12 13 |
a = int(input()) b = int(input()) c = int(input()) x = int(input()) count = 0 for i in range(a+1): for j in range(b+1): for k in range(c+1): if 500 * i + 100 * j + 50 * k == x: count += 1 print(count) |

C. Candies … Iterate index one by one, so the cost is N.

1 2 3 4 5 6 7 8 9 10 11 |
n = int(input()) rows = [] for i in range(2): rows.append(list(map(int, input().split()))) max_candies = 0 for j in range(n): if sum(rows[0][:j+1]) + sum(rows[1][j:]) > max_candies: max_candies = sum(rows[0][:j+1]) + sum(rows[1][j:]) print(max_candies) |