#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define FI(type) vector<type>::iterator
#define isBetween(A,B,C) (((A <= B) && (B <= C)))
using namespace std;
struct frend
{
int food, num, start, end;
frend(int beg, int last, int f, int n)
:food(f), num(n), start(beg), end(last) {}
frend(){}
};
struct Dorm
{
int foodNeed, numDays, *food_per_day, Pop, p, i, **action;
FI(frend) it;
public:
bool operator() (frend, frend);
Dorm (int n, int f)
:foodNeed(f), numDays(n), food_per_day(new int[n]), Pop(0)
{
action = new (nothrow)int*[n];
for (int c = 0; c < numDays; ++c)
{
action[c] = new (nothrow)int[n+1];
memset(action[c], 0, sizeof action[c]);
scanf("%d", &food_per_day[c]);
food_per_day[c] -= foodNeed;//subtract food for Vasya
}
}
vector <frend> Berland;
void Print();
void Get();
};
void Dorm::Print()
{
printf("%d\n", Pop);
for (p = 0; p < numDays; ++p)
{
for (int i = 0; action[p][i] >= 0; ++i)
printf("%d ", action[p][i]);
puts("");
}
}
int main()
{
int n, v, m, t(1), k(1);
scanf("%d%d", &n, &v);
Dorm Vasya(n,v);
for (scanf("%d", &m); m--;)
{
scanf("%d%d%d", &n, &v, &t);
Vasya.Berland.push_back(frend(n,v,t,k++));
}
Vasya.Get();
return 0;
}
void Dorm::Get()
{
sort(Berland.begin(), Berland.end(), *this);//sort in order of lowest need for food to highest
for (p = 0; p < numDays; ++p)
{
for (it = Berland.begin(), i = 1; it != Berland.end() && (food_per_day[p] - (*it).food >= 0); ++it)
{
if (isBetween((*it).start, p+1, (*it).end))
{
food_per_day[p] -= (*it).food, ++Pop;
action[p][0] += 1;
action[p][i++] = (*it).num;
}
}
action[p][i] = -1;
p+1 < numDays ? food_per_day[p+1] += food_per_day[p] : 0;//left over food
}
Print();
}
bool Dorm::operator() (frend uno, frend duo)
{
return (uno.food < duo.food);
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVk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