typedef long long ll; typedef long double ld; #include using namespace std; int main() { ll n,m,k; std::cin >> n>>m>>k; vector> a(n,vector(m)); ll minv = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { std::cin >> a[i][j]; } minv += a[i][0]; } if(minv>k){ std::cout << -1 << std::endl; return 0; } if(n==1){ ll now = 0; for (int i = 0; i < m; i++) { if(a[0][i]<=k)now = a[0][i]; } std::cout << k-now << std::endl; return 0; } vector v1,v2; function rec = [&] (int now, int val) { if(now==n/2){ v1.push_back(val); return; } for (int i = 0; i < m; i++) { rec(now+1, val + a[now][i]); } }; function rec2 = [&] (int now, int val) { if(now==n){ v2.push_back(val); return; } for (int i = 0; i < m; i++) { rec2(now+1, val + a[now][i]); } }; rec(0,0); rec2(n/2,0); sort(v1.begin(),v1.end()); sort(v2.begin(),v2.end()); ll ans = k; for (auto e : v1) { if(e>=k)continue; auto itr = upper_bound(v2.begin(),v2.end(),k-e); if(itr == v2.begin()){ continue; }else{ --itr; ans = min(ans,k-e-*itr); } } std::cout << ans << std::endl; }