#include #include using namespace std; using ll = long long; #define rep(i, s, t) for (ll i = s; i < (ll)(t); i++) template bool chmin(T &x, T y) { return x > y ? (x = y, true) : false; } template bool chmax(T &x, T y) { return x < y ? (x = y, true) : false; } struct io_setup { io_setup() { ios::sync_with_stdio(false); std::cin.tie(nullptr); cout << fixed << setprecision(15); } } io_setup; int main(){ int n,m; cin>>n>>m; vector e(n); rep(i,0,n) cin>>e.at(i); vector> vw(m); for(auto&[v,w]:vw) cin>>v>>w; vector smv(1<>i)&1){ tpv+=vw.at(i).first; tpw+=vw.at(i).second; } smv.at(bit)=tpv; smw.at(bit)=tpw; } vector> dp(n+1,vector(1<>> mm(n+1,vector>(1<0;sub=(sub-1)&bit){ if(smw.at(sub)>e.at(i)) continue; if(chmax(dp.at(i+1).at(bit),dp.at(i).at(bit^sub)+smv.at(sub))){ mm.at(i+1).at(bit)=mm.at(i).at(bit^sub); mm.at(i+1).at(bit).push_back(sub); } } if(chmax(dp.at(i+1).at(bit),dp.at(i).at(bit))){ mm.at(i+1).at(bit)=mm.at(i).at(bit); mm.at(i+1).at(bit).push_back(0); } } } ll mx=-1; int ct=-1; rep(i,0,1<>j)&1) cout<