Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
var (
	dat = [5000][5]int{}
	m = [5]int{}
)

func main() {
	for scanner.Scan() {
		for i := range dat {
			for j := range dat[i] {
				dat[i][j] = 0
			}
		}
		for i := range m {
			m[i] = 0
		}
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		count := 0
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			d, _ := strconv.Atoi(buf[0])
			p, _ := strconv.Atoi(buf[1])
			q, _ := strconv.Atoi(buf[2])
			q--
			count += p
			if d == 2 {
				for j := 0; j < p; j++ {
					dat[m[q]][q] = 1
					ok := true
					for k := 0; k < 5; k++ {
						if dat[m[q]][k] == 0 {
							ok = false
						}
					}
					if ok {
						for k := 0; k < 5; k++ {
							dat[m[q]][k] = 0
							for l := m[q]; l < 4999; l++ {
								dat[l][k] = dat[l+1][k]
							}
							for l := 4999; l >= 0; l-- {
								if dat[l][k] != 0 {
									m[k] = l+1
									break
								}
								if l == 0 {
									m[k] = 0
								}
							}
						}
						count -= 5
					} else {
						m[q]++
					}
				}
			} else {
				h := 0
				for i := 0; i < p; i++ {
					h = max(h, m[i+q])
				}
				for i := 0; i < p; i++ {
					dat[h][i+q] = 1
				}
				for i := 0; i < p; i++ {
					m[i+q] = h+1
				}
				ok := true
				for j := 0; j < 5; j++ {
					if dat[h][j] == 0 {
						ok = false
					}
				}
				if ok {
					for j := 0; j < 5; j++ {
						dat[h][j] = 0
						for k := h; k < 4999; k++ {
							dat[k][j]=dat[k+1][j]
						}
						for k := 4999; k>= 0; k-- {
							if dat[k][j] != 0 {
								m[j]=k+1
								break
							}
							if k == 0 {
								m[j] = 0
							}
						}
					}
					count -= 5
				}
			}
		}
		fmt.Println(count)
	}
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

C++: #include <bits/stdc++.h>
using namespace std;
const double eps=1e-9;
typedef vector<int> vi;
typedef long long ll;
typedef pair<int,int> pii;
int N;
int board[6005][6];//たて:1~5000,よこ:1~5;
int mxcol[6];

int setblock(int d,int p,int q)
{
  int setheight=0;
  if(d==1)
    {
      for(int i=q;i<p+q;i++)
	{
	  setheight=max(setheight,mxcol[i]);
	}
      //cout << setheight << endl;
      for(int row=q;row<p+q;row++)
	{
	  board[setheight+1][row]=1;
	}
      for(int i=q;i<p+q;i++) mxcol[i]=setheight+1;
    }
  else if(d==2)
    {
      setheight=mxcol[q];
      for(int col=setheight;col<setheight+p;col++)
	{
	  board[col+1][q]=1;
	}
      mxcol[q]=setheight+p;
    }
  return setheight+1;
}

void vanishcheck(int minheight,int p,vector<int> &vanishcol)
{
  bool vanish;
  for(int col=minheight;col<minheight+p;col++)
    {
      vanish=true;
      for(int row=1;row<=5;row++)
	{
	  if(board[col][row]==0) vanish=false;
	}
      if(vanish)
	{
	  for(int row=1;row<=5;row++) board[col][row]=0;
	  vanishcol.push_back(col);
	}
    }
  return;
}

void aftervanish(vector<int> vanishcol)
{
  int mxrow=0;
  for(int i=1;i<=5;i++)
    {
      mxrow=max(mxrow,mxcol[i]);
    }
  for(int i=0;i<int(vanishcol.size());i++)
    {
      for(int j=1;j<=5;j++) mxcol[j]--;
      for(int row=vanishcol[i]-i;row<=mxrow;row++)
	{
	  for(int col=1;col<=5;col++)
	    {
	      swap(board[row][col],board[row+1][col]);
	    }
	}
    }
  for(int row=1;row<=5;row++)
    {
      for(int col=mxcol[row];col>=0;col--) 
	{
	  if(board[col][row]==1 || col==0)
	    {
	      mxcol[row]=col;
	      break;
	    }
	}
    }
}

int main(void)
{
  int minheight;
  while(cin >> N && N)
    {
      int ans=0;
      memset(board,0,sizeof(board));
      memset(mxcol,0,sizeof(mxcol));
      for(int i=0;i<N;i++)
	{
	  int d,p,q;
	  cin >> d >> p >> q;
	  minheight=setblock(d,p,q);
	  vector<int> vanishcol;
	  if(d==1)
	    {
	      vanishcheck(minheight,1,vanishcol);
	    }
	  else if(d==2)
	    {
	      vanishcheck(minheight,p,vanishcol);
	    }
	  if(int(vanishcol.size()!=0))
	    {
	      sort(vanishcol.begin(),vanishcol.end());
	      aftervanish(vanishcol);
	    }
	  /*
	  cout << "NOWcol" << endl;
	  for(int row=1;row<=5;row++)
	    {
	      cout << mxcol[row] << ' ';
	    }
	  cout << endl;
	  for(int col=10;col>=1;col--)
	    {
	      for(int row=1;row<=5;row++)
		{
		  cout << board[col][row];
		}
	      cout << endl;
	      }*/
	}
      for(int i=1;i<=6000;i++)
	{
	  for(int j=1;j<=5;j++)
	    {
	      if(board[i][j]==1) ans++;
	    }
	}
      cout << ans << endl;
    }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
bool t[5001][5];
int u[5];
void init();
void make_table();
void move(int);
int n;
int d,p,q,cnt;
int main(){

  while(cin>>n&&n!=0){
    init();
    for(int i=0;i<n;i++){
      cin>>d>>p>>q;
      q--;
      make_table();
    }
    cnt=0;
    for(int i=0;i<5000;i++)for(int j=0;j<5;j++)if(t[i][j])cnt++;
    cout<<cnt<<endl;
  }  
  return 0;
}

void init(){
  for(int i=0;i<=5000;i++){
    for(int j=0;j<5;j++){
      t[i][j]=false;
    }
  }
  for(int i=0;i<5;i++)u[i]=0;
}

void make_table(){
  int h;
  if(d==1){
    h=0;
    for(int j=q;j<q+p;j++)h=max(h,u[j]);
    for(int j=q;j<q+p;j++){t[h][j]=true;u[j]=h+1;}
  }else{
    h=u[q];
    for(int i=h;i<h+p;i++)t[i][q]=true;
    u[q]+=p;
  }





  /*
  h=0;
  for(int j=0;j<5;j++)h=max(h,u[j]);

  for(int i=0;i<h+1;i++){
    if(t[i][0]&&t[i][1]&&t[i][2]&&t[i][3]&&t[i][4]){
      for(int j=0;j<5;j++){
	t[i][j]=false;
      }
    }
  }
 
  


      
  int H=0;
  for(int i=0;i<=5000;i++){
    if(t[i][0]||t[i][1]||t[i][2]||t[i][3]||t[i][4]){
      for(int j=0;j<5;j++){
	t[H][j]=t[i][j];
	if(H!=i)t[i][j]=false;
      }
      H++;
    }
  }
  */
  /*
  for(int I=7;I>=0;I--){
    for(int j=0;j<5;j++){
      cout<<(int)t[I][j];
    }
    cout<<endl;
  }
  cout<<endl;
  */
  
  for(int i=0;i<5000;i++){
    while(1){
      if(t[i][0]&&t[i][1]&&t[i][1]&&t[i][2]&&t[i][3]&&t[i][4]){
	move(i);
	//cout<<i<<endl;
	/*
	for(int I=7;I>=0;I--){
	  for(int j=0;j<5;j++){
	    cout<<(int)t[I][j];
	  }
	  cout<<endl;
	}
	cout<<endl;
	*/


	
      }else{
	break;
      }
    }
  }



  

  for(int j=0;j<5;j++){
    u[j]=0;
    for(int i=4999;i>=0;i--){
      if(t[i][j]){
	u[j]=i+1;
	break;
      }
    }
  }

  //for(int i=0;i<5;i++)cout<<u[i]<<' ';cout<<endl;

}

void move(int x){
  for(int j=0;j<5;j++){
    for(int i=x;i<5000;i++){
      t[i][j]=t[i+1][j];
    }
  }
  for(int j=0;j<5;j++)t[5000][j]=false;
}

C++: #include<iostream>
#define N 10020
using namespace std;
int main(){
  while(true){
    int n;
    int ans=0;
    scanf("%d",&n);

    if(n==0)break;
    int row[N]={0,};
    int h[5]={0,};
    for(int i = 0; i < n; ++i){
      int mh = 0;
      int d,p,q;
      scanf("%d%d%d", &d, &p, &q);
      --q;
      
      if(d==1){
	int maxh = 0;
	for(int j = q; j < q + p; ++j){
	  maxh = max( maxh, h[j] );
	}
	for(int j = q; j < q + p; ++j){
	  h[j] = maxh + 1;
	}
	row[maxh]+=p;
      }else{
	for(int j = h[q]; j < p + h[q]; ++j){
	  row[ j ]++;
	}
	h[q]+=p;
      }

      // cout << " DROPPED : " << i << endl;
      for(int j = 0; j < 5; ++j){
	mh = max( mh , h[j] );
      }

      for(int j = 0; j < mh; ++j){
	// cout << " row["<<j<<"] = " << row[j] << endl;
	if( row[j] == 5 ){
	  for(int k = 0; k < 5; ++k){
	    --h[k];
	  }
	  for(int k = j; k < mh; ++k){
	    row[k] = row[k + 1];
	  }
	  j = -1;
	  continue;
	}
      }
    }
    for(int i = 0; i < N; ++i){
      ans += row[i];
    }
    printf("%d\n",ans);
  }
  return 0;
}