#include #ifdef LOCAL #include #else #define debug(...) void(0) #endif template std::istream& operator>>(std::istream& is, std::vector& v) { for (auto& e : v) { is >> e; } return is; } template std::ostream& operator<<(std::ostream& os, const std::vector& v) { for (std::string_view sep = ""; const auto& e : v) { os << std::exchange(sep, " ") << e; } return os; } template bool chmin(T& x, U&& y) { return y < x and (x = std::forward(y), true); } template bool chmax(T& x, U&& y) { return x < y and (x = std::forward(y), true); } template void mkuni(std::vector& v) { std::ranges::sort(v); auto result = std::ranges::unique(v); v.erase(result.begin(), result.end()); } template int lwb(const std::vector& v, const T& x) { return std::distance(v.begin(), std::ranges::lower_bound(v, x)); } using ll = long long; using namespace std; const ll INF = 1e18; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(15); 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 V(1 << M, 0), W(1 << M, 0); for (int S = 0; S < 1 << M; S++) { for (int i = 0; i < M; i++) { if (S >> i & 1) { V[S] += v[i]; W[S] += w[i]; } } } vector dp(N + 1, vector(1 << M, -INF)), pre(N + 1, vector(1 << M, -1)); dp[0][0] = 0; for (int i = 0; i < N; i++) { for (int S = 0; S < 1 << M; S++) { ll& val = dp[i][S]; if (val == -INF) continue; int rest = (1 << M) - 1 - S; for (int T = rest;; T = (T - 1) & rest) { if (W[T] > e[i]) continue; if (chmax(dp[i + 1][S | T], val + V[T])) pre[i + 1][S | T] = T; if (T == 0) break; } } } ll maxi = -INF, argmax = -1; for (int S = 0; S < 1 << M; S++) { if (chmax(maxi, dp[N][S])) { argmax = S; } } vector> res(N); for (int i = N - 1; i >= 0; i--) { int T = pre[i + 1][argmax]; for (int j = 0; j < M; j++) { if (T >> j & 1) { res[i].emplace_back(j); } } argmax ^= T; } cout << maxi << "\n"; for (int i = 0; i < N; i++) { cout << res[i].size(); for (int& x : res[i]) cout << " " << x + 1; cout << "\n"; } return 0; }