#include using namespace std; #define all(x) begin(x), end(x) #define sz(x) (ll)(x).size() using ll = long long; using ld = long double; constexpr ll inf = 0x3FFFFFFFFFFF; struct ds { vector widths; deque> lengths; set> active; ds() { widths.clear(); lengths.clear(); active.clear(); } void appendMaxLength(ll length) { ll l = sz(widths); while (!lengths.empty() && lengths.back().first <= length) { auto tmp = lengths.back(); lengths.pop_back(); active.erase(active.find({widths[tmp.second] + tmp.first, tmp.second})); l = tmp.second; } if (l < sz(widths)) { lengths.push_back({length, l}); active.insert({length + widths[l], l}); } } void appendWidth(ll width) { lengths.push_back({0, sz(widths)}); active.insert({width, sz(widths)}); widths.push_back(width); } pair query(ll l) { while (sz(lengths) > 1 && l >= lengths[1].second) { auto tmp = lengths.front(); lengths.pop_front(); active.erase(active.find({widths[tmp.second] + tmp.first, tmp.second})); } pair res = {widths[l] + lengths.front().first, l}; for (auto it = active.begin(); it != active.end(); it++) { if (it->second >= l) { res = min(*it, res); break; } } return res; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); ll n, w; cin >> n >> w; w++; vector in(n); for (ll i = 0; i < n; i++) cin >> in[i]; ll cols = 0; vector pref(n), width(n); auto solve = [&](ll m) { pref[0] = -1; width[0] = sz(in[0]) + 1; ds state; state.appendWidth(width[0]); for (ll i = 1; i < n; i++) { state.appendMaxLength(sz(in[i]) + 1); if (i < m) { pref[i] = -1; width[i] = max(width[i - 1], sz(in[i]) + 1); } else { auto e = state.query(i-m); pref[i] = e.second; width[i] = e.first; } state.appendWidth(width[i]); } }; ll r = 0; do { r++; solve(r); } while (width.back() > w); vector last = {n-1}, col = {width.back()}; while (pref[last.back()] >= 0) { last.push_back(pref[last.back()]); ll tmp = width[last.back()]; col.back() -= tmp; col.push_back(tmp); } reverse(all(last)); reverse(all(col)); vector output(r); for (ll i = 0, j = 0; i < sz(last); i++) { for (ll k = 0; j <= last[i]; j++, k++) { output[k] += in[j]; } ll ma = 0; for (ll k = 0; k < r; k++) ma = max(ma, sz(output[k])); for (ll k = 0; k < r; k++) { while (sz(output[k]) <= ma) output[k].push_back(' '); } } cout << r << " " << sz(last) << endl; for (ll i = 0; i < sz(col); i++) { cout << col[i]-1; if (i + 1 < sz(col)) cout << " "; } cout << endl; for (auto e : output) { e.pop_back(); cout << e << endl; } }