#include #define int long long using namespace std; const int INF = 1LL << 60; const int MOD = (int) 1e9 + 7; int n, m, k; int dfs(vector>& v, int depth, int sum) { if (depth == n - 1) return sum; int max_v = 0; for (int i = 0; i < m; i++) { int tmp = sum + v[depth + 1][i]; if (tmp > k) continue; int x = dfs(v, depth + 1, tmp); max_v = max(max_v, x); } return max_v; } signed main(){ cin >> n >> m >> k; vector> v(n, vector()); int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { int x; cin >> x; if (j == 0) ans += x; v[i].push_back(x); } } if (ans > k) { cout << -1 << "\n"; return 0; } ans = dfs(v, -1, 0); cout << k - ans << "\n"; }