#include #include #include using namespace std; struct Node { int val, w, id; bool operator <(const Node &i) const { return w > i.w; } } b[20], a[20]; bool vis[20]; vector out[20]; int n, m, k, ans, p[20]; void Check(int s) { int c = 0; vector t[20]; for (int i = 1; i <= k; i++) { vis[i] = 0; } for (int i = 0; i < n; i++) { int weight = a[i].w; for (int j = 1; j <= k; j++) { if (!vis[j] && b[p[j]].w <= weight) { c++, vis[j] = 1, weight -= b[p[j]].w; t[a[i].id].push_back(b[p[j]].id); } } } if (c < k) return ; ans = s; for (int i = 0; i < n; i++) { out[i] = t[i]; } } void dfs(int x, int s) { if (x == m + 1) { if (s > ans) Check(s); return ; } dfs(x + 1, s), p[++k] = x; dfs(x + 1, s + b[x].val), k--; } int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n >> m; for (int i = 0; i < n; i++) { cin >> a[i].w, a[i].id = i; } for (int i = 1; i <= m; b[i].id = i, i++) { cin >> b[i].val >> b[i].w; } sort(a, a + n); sort(b + 1, b + m + 1); dfs(1, 0); cout << ans << '\n'; for (int i = 0; i < n; i++) { cout << out[i].size() << ' '; for (auto u : out[i]) cout << u << ' '; cout << '\n'; } return 0; }