-
Notifications
You must be signed in to change notification settings - Fork 77
/
MakingCandies.py
49 lines (38 loc) · 1.04 KB
/
MakingCandies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the minimumPasses function below.
def minimumPasses(m, w, p, n):
candy = 0
invest = 0
spend = sys.maxsize
while candy < n:
passes = (p - candy) // (m * w)
if passes <= 0:
mw = (candy // p) + m + w
half = math.ceil(mw / 2)
if m > w:
m = max(m, half)
w = mw - m
else:
w = max(w, half)
m = mw - w
candy %= p
passes = 1
candy += passes * m * w
invest += passes
spend = min(spend, invest + math.ceil((n - candy) / (m * w)))
return min(invest, spend)
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
mwpn = input().split()
m = int(mwpn[0])
w = int(mwpn[1])
p = int(mwpn[2])
n = int(mwpn[3])
result = minimumPasses(m, w, p, n)
fptr.write(str(result) + '\n')
fptr.close()