#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); 
}