#include using namespace std; using ll = long long; struct Data { ll v, use; Data* prev; }; int main() { int N, M; cin >> N >> M; vector e(N), v(M), w(M); for (int i = 0; i < N; i++) cin >> e[i]; for (int i = 0; i < M; i++) cin >> v[i] >> w[i]; vector sum_v(1<>i & 1) { sum_v[S] += v[i]; sum_w[S] += w[i]; } } } vector> dp(N+1, vector(1< 0; T = (T-1)&U) { if (sum_w[T] > e[i]) continue; if (!dp[i+1][S|T] || dp[i][S]->v+sum_w[T] > dp[i+1][S|T]->v) { dp[i+1][S|T] = new Data{dp[i][S]->v+sum_v[T], T, dp[i][S]}; } } dp[i+1][S] = new Data{dp[i][S]->v, 0, dp[i][S]}; } } ll m = 0; for (int S = 0; S < (1<v > m) m = dp[N][S]->v; } int S = 0; for (int i = 0; i < (1<v == m) { S = i; break; } } vector> ans(N); Data* p = dp[N][S]; while (p) { for (int i = 0; i < M; i++) { if (p->use>>i & 1) ans[N-1].push_back(i+1); } p = p->prev; N--; } cout << m << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i].size(); for (int j = 0; j < ans[i].size(); j++) cout << " " << ans[i][j]; cout << endl; } }