#include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); long long N,M; cin >> N >> M; vector T(N); for(auto &t : T) cin >> t; vector> A(N,vector(N)); for(auto &h : A) for(auto &w : h) cin >> w; int n = 1201; vector> day(n+1,vector(N)),OK = day; day.at(0) = T; priority_queue,vector>,greater<>> Q; for(int i=0; i d){ OK.at(let).at(pos)--; Q.push({day.at(let-1).at(pos),let,pos}); continue; } day.at(let).at(pos) = d; for(int to=0; to n){ vector diff(200); for(int d=1000; d<1200; d++) diff.at(d-1000) = day.at(d+1).at(i)-day.at(d).at(i); for(int len=100; ; len--){ bool ok = true; for(int p=0; p+len<200; p++) if(diff.at(p) != diff.at(p+len)){ok = false; break;} if(!ok) continue; long long sum = 0,answer = day.at(1000).at(i); for(int p=0; p