All Tracks>Basic Programming> Input/Output> Basics of Input/Output> ProblemTry Out The Problem Here
You are given two arrays
and
. In each step, you can set
if
. Determine the minimum number of steps that are required to make all
's equal.
Input format
First line:
Second line:
Third line:
Output format
Print the minimum number of steps that are required to make all
's equal. If it is not possible, then print -1.
Constraints
Sample input
25 64 3
Sample output
-1
Sample Input
5
5 7 10 5 15
2 2 1 3 5
Sample Output
8
My Solution
def min_steps(n, a, b):
min_steps = float("inf")
for j in range(n): # try each a[j] as candidate final value
F = a[j]
possible = True
total = 0
for i in range(n):
if a[i] < F: # can't reduce below F
possible = False
break
if b[i] != 0 and (a[i] - F) % b[i] != 0: # must be reachable
possible = False
break
if b[i] != 0:
total += (a[i] - F) // b[i]
else:
total += 0
if possible:
min_steps = min(min_steps, total)
return -1 if min_steps == float("inf") else min_steps
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
# Example Usage:
print(min_steps(n, A, B))
No comments:
Post a Comment