#include "fstream" using namespace std; ifstream cin("input.txt"); ofstream cout("output.txt"); int a[31][31]; int main() { int m, n, k, max, t, result; cin >> k >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> a[i][j]; } } int h = 3; max = a[n][1]; for (int j = 1; j < m; j++) { if (max < a[h][j]) { max = a[h][j]; t = j; } else t = 1; } result = max; k--; for (int i = n; i >= 0; i++) { if (a[i][t + 1] >= a[i - 1][t] && a[i][t + 1] >= a[i][t - 1] && k>0) { result = result + a[i][t+1]; t++; k--; i--; } else if (a[i - 1][t] >= a[i][t + 1] && k>0) { result = result + a[i - 1][t]; k--; } else break; } cout << result << endl; system("pause"); return 0; }