#define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; #include using namespace std; int dp[5010][5010]; const int INF=1e9+7; int main(){ int n,c; cin>>n>>c; vector P(n); rep(i,n) cin>>P[i]; sort(ALL(P)); reverse(ALL(P)); vector X1,X2; rep(i,c){ int t,x; cin>>t>>x; if(t==1) X1.push_back(x); else X2.push_back(x); } sort(ALL(X1)); sort(ALL(X2)); reverse(ALL(X1)); reverse(ALL(X2)); int n1=X1.size(); rep(i,5010){ rep(j,5010) dp[i][j]=INF; } dp[0][0]=0; for(int i=0;i<=min(n,c)-1;i++){ for(int j=0;j<=c;j++){ if(j=0 && i-j