#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define FI( type) vector<type>::iterator
#define isBetween( A,B,C) (((A <= B) and (B <= C)))

using namespace std;

typedef unsigned uint;
typedef vector<int> i_vec ;
typedef vector< vector<int> > iv_vec ;

void Begin();

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(){}
   
   inline bool operator() (const frend uno, const frend duo)
   {
      return ( uno.food < duo.food ); 
   }
};

typedef vector <frend> f_vec;

struct Dorm
{
   uint foodNeed, numDays;
   iv_vec action;
   i_vec food_per_day;
   FI( frend) it;
   f_vec Berland;
   
public:   
   Dorm (int n, int f)
   :foodNeed( f), numDays( n)
   {
      food_per_day.resize( n+1 );
      
      for ( uint i=0; i < (uint) n ; ++i )
     scanf( "%d", &food_per_day[i] ), food_per_day[i] -= foodNeed ;//food for Vasya is stored
   }
   
   void Print( int );
   void Solve( );
   void HandleFood( uint&, uint, int&, int & );
};


int main()
{
   int n, v, m, t(1), k(0);
   
   //Begin();
   
   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.Solve();
   
   return 0;
}

void Dorm::Solve()
{
   stable_sort( Berland.begin(), Berland.end(), Berland.back() );//sort in order of lowest need for food to highest
   uint Pop(0);
   
   for ( uint p = 0, l(0), prev(0); p < numDays; ++p, l = 0)
   {
      i_vec row;
      l = food_per_day[p], row.push_back(0);
      for ( it = Berland.begin(); it != Berland.end() and (( food_per_day[p] - it->food ) >= 0); ++it )
      {
	 if ( isBetween(it->start, int(p+1), it->end) )//Are you staying with me this day?
	 {
	    food_per_day[p] -= it->food, ++Pop;//increase popularity
	    ++( row.front() );//number Friends fed
	    row.push_back( it->num ) ;//index of friend
	 }
      }
      
      HandleFood(prev, l, food_per_day[p], food_per_day[p+1]);
      action.push_back(row);
   }
   
   Print(Pop);
}

void Dorm::Print(int P)
{
   printf( "%d\n", P );
   
   for ( iv_vec::iterator it = action.begin(); it !=action.end(); ++it )
   {
      for ( i_vec::iterator pi = it->begin(); pi != it->end(); ++pi )
	 printf( "%d ", *pi );
      
      puts( "" );
   }
}

void Dorm::HandleFood(uint &left2, uint tot, int &left1 , int &next)
{
   if ( left2 > ( tot -= left1 ) )//did not finish leftover from yesterday?
      left1 -= ( left2 - tot );//Remove the amount not finished
   next += left1;//add whatever is left from today to tomorrow's food
   left2 = left1;//Amount left for today is stored
}

void Begin()
{
   freopen("input.txt","r", stdin);
   freopen("output.txt","w", stdout);
}