#include using namespace std; #include #include using namespace __gnu_pbds; template using ordered_set = tree, rb_tree_tag, tree_order_statistics_node_update>; template using ordered_map = tree, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; typedef pair pii; typedef pair pll; typedef vector vi; typedef vector vll; #define FOR(i,j,k,in) for(int i=(j); i < (k);i+=in) #define FORD(i,j,k,in) for(int i=(j); i >=(k);i-=in) #define REP(i,b) FOR(i,0,b,1) #define REPD(i,b) FORD(i,b,0,1) #define pb push_back #define mp make_pair #define ff first #define ss second #define all(x) begin(x), end(x) #define MANY_TESTS int tcase; cin >> tcase; while(tcase--) const double EPS = 1e-9; const int MOD = 1e9+7; const ll INFF = 1e18; const int INF = 1e9; const ld PI = acos((ld)-1); const vi dy = {1, 0, -1, 0, -1, 1, 1, -1}; const vi dx = {0, 1, 0, -1, -1, 1, -1, 1}; void DBG(){cout << "]" << endl;} template void DBG(const T& head, const U... args){ cout << head << "; "; DBG(args...); } #define dbg(...) cout << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__); #define chk() cout << "Check at line(" << __LINE__ << ") hit." << endl; template ostream& operator<<(ostream& out, const array &v){out << "["; REP(i, U) out << v[i] << ", "; out << "]"; return out;} template ostream& operator<<(ostream& out, const pair &par) {out << "[" << par.first << ";" << par.second << "]"; return out;} template ostream& operator<<(ostream& out, const set &cont) { out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; } template ostream& operator<<(ostream& out, const map &cont) {out << "{"; for( const auto &x:cont) out << x << ", "; out << "}"; return out; } template ostream& operator<<(ostream& out, const vector &v){ out << "["; REP(i, v.size()) out << v[i] << ", "; out << "]"; return out;} template istream& operator>>(istream& in, vector &v){ for(auto &x : v) in >> x; return in; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int h, t; cin >> h >> t; vector c(h); cin >> c; vector d(t), in(t); REP(i, t){ cin >> d[i] >> in[i]; } vector su((1 << t), 0); REP(mask, (1 << t)){ REP(j, t){ if(mask & (1 << j)) su[mask] += d[j]; } } const int mx = 17; vector> dp(mx + 1, vector((1 << t), 0)); dp[0][0] = 1; REP(i, mx){ vector intervals; int curr = 0; REP(j, h){ if(c[j] > i){ curr++; } else{ if(curr > 0){ intervals.pb(curr); } curr = 0; } } if(curr > 0) intervals.pb(curr); sort(all(intervals), greater()); if(intervals.size() == 0){ REP(mask, (1 << t)){ if(dp[i][mask]) dp[i + 1][mask] = 1; } continue; } if(intervals.size() > mx){ cout << 0 << "\n"; return 0; } vector> ndp(intervals.size() + 1, vector((1 << t), 0)); ndp[0][0] = 1; FOR(j, 1, intervals.size() + 1, 1){ REP(mask, (1 << t)){ for(int s = mask; s > 0; s = (s - 1) & mask){ if(su[s] == intervals[j - 1] && ndp[j - 1][mask&(~s)]){ ndp[j][mask] = 1; break; } } } } bool fn = false; REP(mask, (1 << t)){ for(int s = mask; s > 0; s = (s - 1) & mask){ if(ndp[intervals.size()][s] && dp[i][mask&(~s)]) { dp[i + 1][mask] = 1; fn = true; break; } } } if(!fn){ cout << 0 << "\n"; return 0; } } int ans = 0; REP(mask, (1 << t)){ if(dp[mx][mask]){ int tm = 0; REP(j, t){ if(mask & (1 << j)){ tm += in[j]; } } ans = max(ans, tm); } } cout << ans << "\n"; return 0; }