Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/matritsa-qatorlarni-saralash

Sort rows by sum

Medium 1000 ms 128 MB 49%
Share

An integer matrix of size $n \times m$ is given. Sort its rows in increasing order of the sums of their elements and print the matrix. Rows with equal sums keep their original order.

Rearranging the rows of the matrix in increasing order of their sums

Input

The first line contains two positive integers $n$ and $m$. Each of the next $n$ lines contains $m$ integers.

Output

$n$ lines with $m$ space-separated integers each.

Note

$1 \le n, m \le 200$
$-1000 \le a_{i,j} \le 1000$

Examples

Input #1
3 2
5 5
1 2
0 3
Output #1
1 2
0 3
5 5
Input #2
2 2
1 1
2 0
Output #2
1 1
2 0

Sign in to submit a solution

Signing up is free and takes a minute.