1976B - IncreaseDecreaseCopy - CodeForces Solution


greedy

Please click on ads to support us..

Python Code:

n = int(input())
if n ==2:
	print("99999999900001")
	print("1000000000")
else:	
	for i in range(n):
		d = []
		c = [1]
		a = int(input())
		l = list(map(int,input().split()))
		k = list(map(int,input().split()))
		for i,y in zip(l,k):
			c.append(abs(i-y))
		for i in range(a):
			r = sorted([l[i],k[i],k[-1]])
			if r[1]==k[-1]:
				d.append(0)
				break
			elif k [-1]==r[0]:
				d.append(abs(k[-1]-r[1]))
			elif k[-1]==r[-1]:
				d.append(abs(k[-1]-r[1]))
			if k[-1] in k[:-1]:
				d = []
				break
		if d ==[]:
			print(sum(c))
		else:
			print(sum(c)+min(d))


Comments

Submit
0 Comments
More Questions

581B - Luxurious Houses
1481C - Fence Painting
935A - Fafa and his Company
22A - Second Order Statistics
1720B - Interesting Sum
1720A - Burenka Plays with Fractions
3A - Shortest path of the king
1720C - Corners
574A - Bear and Elections
352B - Jeff and Periods
1244A - Pens and Pencils
1670A - Prof Slim
1189A - Keanu Reeves
678A - Johny Likes Numbers
1699C - The Third Problem
1697D - Guess The String
754B - Ilya and tic-tac-toe game
760A - Petr and a calendar
1573A - Countdown
166A - Rank List
1631B - Fun with Even Subarrays
727A - Transformation from A to B
822B - Crossword solving
1623A - Robot Cleaner
884B - Japanese Crosswords Strike Back
862B - Mahmoud and Ehab and the bipartiteness
429A - Xor-tree
1675C - Detective Task
950A - Left-handers Right-handers and Ambidexters
672B - Different is Good