1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <vector>
#include <list>

std::vector<std::vector<long long>> st;

int de(int i, int dep) {

	if (st[i].size() >= dep) {
		return st[i].size() - dep;
	}
	
	return st[i][st.size() - 1];
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie();
	int n, m, k, dep;
	long long x, w = 0, max, limit = 0;
	bool rowne;

	std::cin >> n >> m >> k;

	st.resize(n);

	for (int i = 0; i < n; i++) {

		for (int j = 0; j < m; j++) {
			std::cin >> x;
			st[i].push_back(x);
		}

	}

	dep = 1;

	while (limit <= k) {
		rowne = false;
		max = -1;

		for (int i = 0; i < n; i++) {

			if (!st[i].empty()) {

				if (st[i][de(i, dep)] == max) {
					rowne = true;
				}
				else {
					rowne = false;

					if (st[i][de(i, dep)] > max) {
						max = st[i][de(i, dep)];
					}

				}

			}

		}

		limit++;
		
		if (rowne) {
			w += st[0][de(0, dep)];
			dep++;
		}
		else {
			w += st[max][de(max, dep)];
			st[max].erase(st[max].end() - dep, st[max].end());
		}

	}

	std::cout << w;
	return 0;
}