#include using namespace std; typedef long long ll; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b>N>>Q; string S;cin>>S; vector A(N+1),B(N+1); for(int i=0;i>H>>W>>P; H+=A[P]; W+=B[P]; ll L=0,R=15LL<<58; while(R-L>1){ ll M=(L+R)/2; ll to=M; ll h=(to/N)*A[N]+(A[to%N]); ll w=(to/N)*B[N]+(B[to%N]); if(h