Chef and his friend Miron were getting bored and decided to play a game. 
Miron thinks of a sequence of  N  integers (A1, A2, …., AN) and gives Chef a matrix B, where Bi,j = |Ai - Aj|. He further tells Chef that A1 = 0. The game is for Chef to guess the sequence that Miron thought of. 
But Miron is an adversarial player. Every time Chef tries to guess the sequence, he makes a change to the matrix. He makes such a change Q times. Each time, he replaces an entry in some row and the corresponding column with a new one leaving Chef to guess the sequence after each change. 
Chef needs a friend to help him against such an adversarial player. Can you be that friend and help Chef find a suitable sequence A for the initial matrix B and also after each change Miron makes? 
Note that if several answers exist, then print the lexicographically smallest answer. Further, the numbers in the sequence can be negative.

-----Input-----

The first line contains two space-separated integers N, Q. Each of the N subsequent lines contains N space-separated integers, denoting the matrix B.

Q queries follow. Each query has two lines. The first line of each query contains an integer p, denoting the number of row and column that is changed. The second line of each query contains N space-separated integers F1, F2, F3, ... FN, denoting the new values to the corresponding cells of the matrix (you should make the following assignments Bi,p = Bp,i = Fi for all valid i). 

-----Output-----
Print Q + 1 lines which contain N space-separated integers, Miron's initial array and Miron's array after each query.

-----Constraints-----
- 3 ≤ N ≤  1000 
- 1 ≤ Q ≤  1000 
- 0 ≤ Bi,j ≤  5000 
- 1 ≤ p ≤  n 
- 0 ≤ Fi ≤  5000 
- it's guaranteed there's always an answer

-----Example-----
Input:
3 2
0 1 2
1 0 1
2 1 0
1
0 4 3
2
4 0 7
Output:
0 -1 -2
0 -4 -3
0 -4 3

-----Explanation-----
Example case 1. Initially, sequence {0, 1, 2} is also suitable, but {0, -1, -2} is lexicografically smaller.

Source: codechef | Difficulty: VERY_HARD
Tags: Implementation

Constructive algorithms


Solutions:
def update_B(B, query):
	(p, R) = query
	for i in range(len(R)):
		B[p][i] = R[i]
		B[i][p] = R[i]

def get_A(B):
	N = len(B)
	A = [0] * N
	i = 0
	for j in range(N):
		if B[0][j] != 0:
			i = j
			A[i] = -B[0][i]
			break
	for j in range(i + 1, N):
		if abs(A[i] - B[0][j]) == B[i][j]:
			A[j] = B[0][j]
		else:
			A[j] = -B[0][j]
	return A

def print_list(A):
	print(' '.join([str(a) for a in get_A(B)]))
(N, Q) = [int(x) for x in input().rstrip().split()]
B = []
for i in range(N):
	B += [[int(x) for x in input().rstrip().split()]]
queries = []
for i in range(Q):
	p = int(input()) - 1
	arr = input().rstrip().split()
	queries += [(p, [int(x) for x in arr])]
print_list(get_A(B))
for q in queries:
	update_B(B, q)
	print_list(' '.join([str(a) for a in get_A(B)]))


(n, q) = tuple(map(int, input().strip().split()))
B = []
for i in range(n):
	B.append(list(map(int, input().strip().split())))
A = B[0][:]
idx = 0
for i in range(1, n):
	if B[0][i] != 0:
		A[i] *= -1
		idx = i
		break
for i in range(idx + 1, n):
	if B[idx][i] != abs(A[idx] - A[i]):
		A[i] *= -1
print(' '.join(map(str, A)).strip())
for i in range(q):
	p = int(input().strip()) - 1
	C = list(map(int, input().strip().split()))
	for i in range(n):
		B[p][i] = C[i]
		B[i][p] = C[i]
	idx = 0
	for i in range(1, n):
		A[i] = B[0][i]
		if B[0][i] != 0:
			idx = i
			A[i] *= -1
			break
	for i in range(idx + 1, n):
		A[i] = B[0][i]
		if B[idx][i] != abs(A[idx] - A[i]):
			A[i] *= -1
	print(' '.join(map(str, A)).strip())


def func(b):
	s = [0]
	for l in range(1, n):
		s.append(b[0][l])
	ide = 1
	for k in range(1, n):
		if s[k] != 0:
			s[k] = -s[k]
			ide = k
			break
	for k in range(ide + 1, n):
		if abs(s[k]) + abs(s[ide]) == b[ide][k]:
			continue
		else:
			s[k] = -s[k]
	for l in range(n):
		print(s[l], end=' ')
	print('\n')
(n, q) = list(map(int, input().split()))
b = []
a = []
for i in range(n):
	a = list(map(int, input().split()))
	b.append(a)
func(b)
while q:
	q -= 1
	p = int(input())
	w = list(map(int, input().split()))
	for o in range(n):
		b[o][p - 1] = b[p - 1][o] = w[o]
	func(b)