Problem:


Score : 1200 points

Problem Statement

Takahashi is a magician. He can cast a spell on an integer sequence (a_1,a_2,...,a_M) with M terms, to turn it into another sequence (s_1,s_2,...,s_M), where s_i is the sum of the first i terms in the original sequence.

One day, he received N integer sequences, each with M terms, and named those sequences A_1,A_2,...,A_N. He will try to cast the spell on those sequences so that A_1 < A_2 < ... < A_N will hold, where sequences are compared lexicographically.
Let the action of casting the spell on a selected sequence be one cast of the spell. Find the minimum number of casts of the spell he needs to perform in order to achieve his objective.

Here, for two sequences a = (a_1,a_2,...,a_M), b = (b_1,b_2,...,b_M) with M terms each, a < b holds lexicographically if and only if there exists i (1 ≦ i ≦ M) such that a_j = b_j (1 ≦ j < i) and a_i < b_i.

Constraints

1 ≦ N ≦ 10^3

1 ≦ M ≦ 10^3

Let the j-th term in A_i be A_{(i,j)}, then 1 ≦ A_{(i,j)} ≦ 10^9.

Partial Scores

In the test set worth 200 points, Takahashi can achieve his objective by at most 10^4 casts of the spell, while keeping the values of all terms at most 10^9.

In the test set worth another 800 points, A_{(i,j)} ≦ 80.

Input

The input is given from Standard Input in the following format:

N M
A_{(1,1)} A_{(1,2)} … A_{(1,M)}
A_{(2,1)} A_{(2,2)} … A_{(2,M)}
:
A_{(N,1)} A_{(N,2)} … A_{(N,M)}

Output

Print the minimum number of casts of the spell Takahashi needs to perform. If he cannot achieve his objective, print -1 instead.

Sample Input 1

3 3
2 3 1
2 1 2
2 6 3

Sample Output 1

1

Takahashi can achieve his objective by casting the spell on A_2 once to turn it into (2 , 3 , 5).

Sample Input 2

3 3
3 2 10
10 5 4
9 1 9

Sample Output 2

-1

In this case, Takahashi cannot achieve his objective by casting the spell any number of times.

Sample Input 3

5 5
2 6 5 6 9
2 6 4 9 10
2 6 8 6 7
2 1 7 3 8
2 1 4 8 3

Sample Output 3

11



Sample input:
3 3
2 3 1
2 1 2
2 6 3


Sample output:
1

Submission status: Wrong Answer

Solution code:
from math import log2
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
B = max(max(Ai) for Ai in A)
if M == 1:
    if all(A[i][0] < A[i+1][0] for i in range(N-1)):
        print("0")
    else:
        print("-1")
    exit(0)
logB = log2(B)
logBi = int(logB)
INF = 10**18
def gen(P, t):
    for k in range(t):
        for i in range(M-1):
            P[i+1] += P[i]
T = [0]*N
ans = 0
P = [0]*M
for i in range(N-1):
    a0, a1 = A[i][:2]
    b0, b1 = A[i+1][:2]
    if a0 < b0:
        continue
    if a0 > b0:
        ans = -1
        break
    t0 = T[i]
    v = max(t0*a0 + a1 - b1, 0)
    if v % b0 > 0:
        T[i+1] = t1 = (v + b0-1) // b0
        ans += t1
        continue

    t1 = v // b0
    if t0 < t1:
        P[:] = A[i+1]
        gen(P, t1 - t0)
        if P < A[i]:
            t1 += 1
    else:
        P[:] = A[i]
        gen(P, t0 - t1)
        if A[i+1] < P:
            t1 += 1
    T[i+1] = t1
    ans += t1
print(ans)

Submission status: Wrong Answer

Solution code:
from math import log2
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
B = max(max(Ai) for Ai in A)
if M == 1:
    if all(A[i][0] <= A[i+1][0] for i in range(N-1)):
        print("0")
    else:
        print("-1")
    exit(0)
logB = log2(B)
logBi = int(logB)
INF = 10**18
def gen(P, t):
    for k in range(t):
        for i in range(M-1):
            P[i+1] += P[i]
T = [0]*N
ans = 0
P = [0]*M
for i in range(N-1):
    a0, a1 = A[i][:2]
    b0, b1 = A[i+1][:2]
    if a0 < b0:
        continue
    if a0 > b0:
        ans = -1
        break
    t0 = T[i]
    v = max(t0*a0 + a1 - b1, 0)
    if v % b0 > 0:
        T[i+1] = t1 = (v + b0-1) // b0
        ans += t1
        continue

    t1 = v // b0
    if t0 < t1:
        P[:] = A[i+1]
        gen(P, t1 - t0)
        if P < A[i]:
            t1 += 1
    else:
        P[:] = A[i]
        gen(P, t0 - t1)
        if A[i+1] < P:
            t1 += 1
    T[i+1] = t1
    ans += t1
print(ans)

Submission status: Wrong Answer

Solution code:
from math import log2
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
B = max(max(Ai) for Ai in A)
if M == 1:
    if all(A[i][0] <= A[i+1][0] for i in range(N-1)):
        print("0")
    else:
        print("-1")
    exit(0)
logB = log2(B)
logBi = int(logB)
INF = 10**18
def gen(P, t):
    if t <= B:
        for k in range(t):
            for i in range(M-1):
                P[i+1] += P[i]
    else:
        for k in range(t):
            for i in range(logBi+1):
                P[i+1] += P[i]
        for i in range(logBi+1, M):
            P[i] = INF
T = [0]*N
ans = 0
P = [0]*M
for i in range(N-1):
    a0, a1 = A[i][:2]
    b0, b1 = A[i+1][:2]
    if a0 < b0:
        continue
    if a0 > b0:
        ans = -1
        break
    t0 = T[i]
    v = max(t0*a0 + a1 - b1, 0)
    if v % b0 > 0:
        T[i+1] = t1 = (v + b0-1) // b0
        ans += t1
        continue

    t1 = v // b0
    if t0 < t1:
        P[:] = A[i+1]
        gen(P, t1-t0)
        if P < A[i]:
            t1 += 1
    else:
        P[:] = A[i]
        gen(P, t0 - t1)
        if A[i+1] < P:
            t1 += 1
    T[i+1] = t1
    ans += t1
print(ans)

Submission status: Wrong Answer

Solution code:
from math import log2
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
B = max(max(Ai) for Ai in A)
if M == 1:
    if all(A[i][0] <= A[i+1][0] for i in range(N-1)):
        print("0")
    else:
        print("-1")
    exit(0)
logB = log2(B)
logBi = int(logB)
INF = 10**18
def gen(P, t):
    if t <= B:
        for k in range(t):
            for i in range(M-1):
                P[i+1] += P[i]
    else:
        for k in range(t):
            for i in range(logBi+1):
                P[i+1] += P[i]
        for i in range(logBi+1, M):
            P[i] = INF
T = [0]*N
ans = 0
P = [0]*M
for i in range(N-1):
    a0, a1 = A[i][:2]
    b0, b1 = A[i+1][:2]
    if a0 < b0:
        continue
    if a0 > b0:
        ans = -1
        break
    t0 = T[i]
    v = t0*a0 + a1 - b1
    if v % b0 > 0:
        T[i+1] = t1 = (v + b0-1) // b0
        ans += t1
        continue

    t1 = v // b0
    if t0 < t1:
        P[:] = A[i+1]
        gen(P, t1-t0)
        if P < A[i]:
            t1 += 1
    else:
        P[:] = A[i]
        gen(P, t0 - t1)
        if A[i+1] < P:
            t1 += 1
    T[i+1] = t1
    ans += t1
print(ans)

Submission status: Wrong Answer

Solution code:
def read2():
	return map(int, raw_input().split())
n, m = read2()
a = []
for i in xrange(n):
	row = raw_input().split()
	new = [ int(row[j]) for j in xrange(m)]
	a.append(new)

for i in range(1,n):
	if a[i][0]!=a[0][0]:
		print -1
		exit()

for i in range(1,n):
	diff = False
	for j in xrange(m):
		if a[i][j-1]!=a[i-1][j-1]:
			diff = True
			break
	if not diff:
		print -1
		exit()

c = 0
for i in range(1,n):
	print str(i) + ': '
	j=1
	while(j<m):
		if a[i][j]==a[i-1][j]:
			j+=1
			continue
		if a[i][j]<a[i-1][j]:
			c+=1
			for k in range(1,m):
				a[i][k] +=a[i][k-1]
		else:
			break
print c

Submission status: Wrong Answer

Solution code:
def read2():
	return map(int, raw_input().split())
n, m = read2()
a = []
for i in xrange(n):
	row = raw_input().split()
	new = [ int(row[j]) for j in xrange(m)]
	a.append(new)

for i in range(1,n):
	if a[i][0]!=a[0][0]:
		print -1
		exit()

for i in range(1,n):
	diff = False
	for j in xrange(m):
		if a[i][j-1]!=a[i-1][j-1]:
			diff = True
			break
	if not diff:
		print -1
		exit()

c = 0
for i in range(1,n):		
	for j in range(1,m):
		if a[i][j]<=a[i-1][j]:
			c+=1
			for k in range(1,m):
				a[i][k] +=a[i][k-1]
		else:
			break


print c

Submission status: Wrong Answer

Solution code:
def read2():
	return map(int, raw_input().split())
n, m = read2()
a = []
for i in xrange(n):
	row = raw_input().split()
	new = [ int(row[j]) for j in xrange(m)]
	a.append(new)

for i in range(1,n):
	if a[i][0]!=a[0][0]:
		print -1
		exit()

for i in range(1,n):
	diff = False
	for j in xrange(m):
		if a[i][j-1]!=a[i-1][j-1]:
			diff = True
			break
	if not diff:
		print -1
		exit()

c = 0
for i in range(1,n):		
	for j in range(1,m):
		if a[i][j]<a[i-1][j]:
			c+=1
			for k in range(1,m):
				a[i][k] +=a[i][k-1]
		else:
			break

print c

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline
from math import log2
from itertools import accumulate
N, M = map(int, readline().split())
A = [list(map(int, readline().split())) for i in range(N)]
B = max(max(Ai) for Ai in A)
if M == 1:
    if N == 1 or all(A[i][0] < A[i+1][0] for i in range(N-1)):
        print("0")
    else:
        print("-1")
    exit(0)
logB = log2(B)
logBi = int(logB)
INF = 10**18
def gen(P, t, L = min(logBi+2, M-1)):
    if t <= logB:
        for k in range(t):
            P[:] = accumulate(P)
    else:
        for k in range(t):
            P[:L] = accumulate(P[:L])
        for i in range(logBi+2, M):
            P[i] = INF
T = [0]*N
ans = 0
P = [0]*M
for i in range(N-1):
    a0, a1 = A[i][:2]
    b0, b1 = A[i+1][:2]
    if a0 < b0:
        continue
    if a0 > b0:
        ans = -1
        break
    t0 = T[i]
    v = max(t0*a0 + a1 - b1, 0)
    if v % b0 > 0:
        T[i+1] = t1 = (v + b0-1) // b0
        ans += t1
        continue

    t1 = v // b0
    if t0 <= t1:
        P[:] = A[i+1]
        gen(P, t1 - t0)
        if P <= A[i]:
            t1 += 1
    else:
        P[:] = A[i]
        gen(P, t0 - t1)
        if A[i+1] <= P:
            t1 += 1
    T[i+1] = t1
    ans += t1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
readline = sys.stdin.readline
from math import log2
N, M = map(int, readline().split())
A = [list(map(int, readline().split())) for i in range(N)]
B = max(max(Ai) for Ai in A)
if M == 1:
    if N == 1 or all(A[i][0] < A[i+1][0] for i in range(N-1)):
        print("0")
    else:
        print("-1")
    exit(0)
logB = log2(B)
logBi = int(logB)
INF = 10**18
def gen(P, t):
    if t <= logB:
        for k in range(t):
            for i in range(M-1):
                P[i+1] += P[i]
    else:
        for k in range(t):
            for i in range(min(logBi+2, M-1)):
                P[i+1] += P[i]
        for i in range(logBi+2, M):
            P[i] = INF
T = [0]*N
ans = 0
P = [0]*M
for i in range(N-1):
    a0, a1 = A[i][:2]
    b0, b1 = A[i+1][:2]
    if a0 < b0:
        continue
    if a0 > b0:
        ans = -1
        break
    t0 = T[i]
    v = max(t0*a0 + a1 - b1, 0)
    if v % b0 > 0:
        T[i+1] = t1 = (v + b0-1) // b0
        ans += t1
        continue

    t1 = v // b0
    if t0 <= t1:
        P[:] = A[i+1]
        gen(P, t1 - t0)
        if P <= A[i]:
            t1 += 1
    else:
        P[:] = A[i]
        gen(P, t0 - t1)
        if A[i+1] <= P:
            t1 += 1
    T[i+1] = t1
    ans += t1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from math import log2
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
B = max(max(Ai) for Ai in A)
if M == 1:
    if N == 1 or all(A[i][0] < A[i+1][0] for i in range(N-1)):
        print("0")
    else:
        print("-1")
    exit(0)
logB = log2(B)
logBi = int(logB)
INF = 10**18
def gen(P, t):
    for k in range(t):
        for i in range(M-1):
            P[i+1] += P[i]
T = [0]*N
ans = 0
P = [0]*M
for i in range(N-1):
    a0, a1 = A[i][:2]
    b0, b1 = A[i+1][:2]
    if a0 < b0:
        continue
    if a0 > b0:
        ans = -1
        break
    t0 = T[i]
    v = max(t0*a0 + a1 - b1, 0)
    if v % b0 > 0:
        T[i+1] = t1 = (v + b0-1) // b0
        ans += t1
        continue

    t1 = v // b0
    if t0 <= t1:
        P[:] = A[i+1]
        gen(P, t1 - t0)
        if P <= A[i]:
            t1 += 1
    else:
        P[:] = A[i]
        gen(P, t0 - t1)
        if A[i+1] <= P:
            t1 += 1
    T[i+1] = t1
    ans += t1
print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
from math import log2
from itertools import accumulate
N, M = map(int, readline().split())
A = [list(map(int, readline().split())) for i in range(N)]
B = max(max(Ai) for Ai in A)
if M == 1:
    if N == 1 or all(A[i][0] < A[i+1][0] for i in range(N-1)):
        print("0")
    else:
        print("-1")
    exit(0)
logB = log2(B)
logBi = int(logB)
INF = 10**18
INFL = [INF]*(M - logBi-2)
def gen(P, t, L = min(logBi+2, M)):
    if t <= logB:
        # O(M log B)
        for k in range(t):
            P[:] = accumulate(P)
    else:
        # O(log^2 B)
        V = [1]*L # V[k] = C(i-j+t-1, i-j)
        for k in range(1, L):
            V[k] = V[k-1] * (t + k - 1)//k

        for i in range(L-1, 0, -1):
            P[i] += sum(P[j] * V[i-j] for j in range(i))
        if logBi+2 < M:
            P[logBi+2:] = INFL
T = [0]*N
ans = 0
P = [0]*M
for i in range(N-1):
    a0, a1 = A[i][:2]
    b0, b1 = A[i+1][:2]
    if a0 < b0:
        continue
    if a0 > b0:
        ans = -1
        break
    # a1 + a0*t0 = b1 + b0*t1 となる正の整数 t1 が存在するか
    t0 = T[i]
    v = max(t0*a0 + a1 - b1, 0)
    if v % b0 > 0:
        T[i+1] = t1 = (v + b0-1) // b0
        ans += t1
        continue

    # そのような t1 が存在するならばチェック

    t1 = v // b0
    if t0 <= t1:
        # A_i < f(A_{i+1}, t1-t0) となるか
        P[:] = A[i+1]
        gen(P, t1 - t0)
        if P <= A[i]:
            t1 += 1
    else:
        # f(A_i, t0-t1) < A_{i+1} となるか
        P[:] = A[i]
        gen(P, t0 - t1)
        if A[i+1] <= P:
            t1 += 1
    T[i+1] = t1
    ans += t1
print(ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
from math import log2
from itertools import accumulate
N, M = map(int, readline().split())
A = [list(map(int, readline().split())) for i in range(N)]
B = max(max(Ai) for Ai in A)
if M == 1:
    if N == 1 or all(A[i][0] < A[i+1][0] for i in range(N-1)):
        print("0")
    else:
        print("-1")
    exit(0)
logB = log2(B)
logBi = int(logB)
INF = 10**18
INFL = [INF]*(M - logBi-2)
def gen(P, t, L = min(logBi+2, M)):
    if t <= logB:
        for k in range(t):
            P[:] = accumulate(P)
    else:
        V = [1]*L
        for k in range(1, L):
            V[k] = V[k-1] * (t + k - 1)//k

        for i in range(L-1, 0, -1):
            P[i] += sum(P[j] * V[i-j] for j in range(i))
        if logBi+2 < M:
            P[logBi+2:] = INFL
T = [0]*N
ans = 0
P = [0]*M
for i in range(N-1):
    a0, a1 = A[i][:2]
    b0, b1 = A[i+1][:2]
    if a0 < b0:
        continue
    if a0 > b0:
        ans = -1
        break
    t0 = T[i]
    v = max(t0*a0 + a1 - b1, 0)
    if v % b0 > 0:
        T[i+1] = t1 = (v + b0-1) // b0
        ans += t1
        continue

    t1 = v // b0
    if t0 <= t1:
        P[:] = A[i+1]
        gen(P, t1 - t0)
        if P <= A[i]:
            t1 += 1
    else:
        P[:] = A[i]
        gen(P, t0 - t1)
        if A[i+1] <= P:
            t1 += 1
    T[i+1] = t1
    ans += t1
print(ans)

Submission status: Runtime Error

Solution code:
from math import log2
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
B = max(max(Ai) for Ai in A)
if M == 1:
    if N == 1 or all(A[i][0] < A[i+1][0] for i in range(N-1)):
        print("0")
    else:
        print("-1")
    exit(0)
logB = log2(B)
logBi = int(logB)
INF = 10**18
def gen(P, t):
    if t <= logB:
        for k in range(t):
            for i in range(M-1):
                P[i+1] += P[i]
    else:
        for k in range(t):
            for i in range(min(logBi+2, M)):
                P[i+1] += P[i]
        for i in range(logBi+2, M):
            P[i] = INF
T = [0]*N
ans = 0
P = [0]*M
for i in range(N-1):
    a0, a1 = A[i][:2]
    b0, b1 = A[i+1][:2]
    if a0 < b0:
        continue
    if a0 > b0:
        ans = -1
        break
    t0 = T[i]
    v = max(t0*a0 + a1 - b1, 0)
    if v % b0 > 0:
        T[i+1] = t1 = (v + b0-1) // b0
        ans += t1
        continue

    t1 = v // b0
    if t0 <= t1:
        P[:] = A[i+1]
        gen(P, t1 - t0)
        if P <= A[i]:
            t1 += 1
    else:
        P[:] = A[i]
        gen(P, t0 - t1)
        if A[i+1] <= P:
            t1 += 1
    T[i+1] = t1
    ans += t1
print(ans)

Submission status: Runtime Error

Solution code:
from math import log2
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
B = max(max(Ai) for Ai in A)
if M == 1:
    if all(A[i][0] <= A[i+1][0] for i in range(N-1)):
        print("0")
    else:
        print("-1")
    exit(0)
logB = log2(B)
logBi = int(logB)
INF = 10**18
def gen(P, t):
    if t <= logB:
        for k in range(t):
            for i in range(M-1):
                P[i+1] += P[i]
    else:
        for k in range(t):
            for i in range(logBi+2):
                P[i+1] += P[i]
        for i in range(logBi+2, M):
            P[i] = INF
T = [0]*N
ans = 0
P = [0]*M
for i in range(N-1):
    a0, a1 = A[i][:2]
    b0, b1 = A[i+1][:2]
    if a0 < b0:
        continue
    if a0 > b0:
        ans = -1
        break
    t0 = T[i]
    v = max(t0*a0 + a1 - b1, 0)
    if v % b0 > 0:
        T[i+1] = t1 = (v + b0-1) // b0
        ans += t1
        continue

    t1 = v // b0
    if t0 < t1:
        P[:] = A[i+1]
        gen(P, t1 - t0)
        if P < A[i]:
            t1 += 1
    else:
        P[:] = A[i]
        gen(P, t0 - t1)
        if A[i+1] < P:
            t1 += 1
    T[i+1] = t1
    ans += t1
print(ans)