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 <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main(){

	int n;
	char field[10][5];
	while(cin>>n&&n!=0){
		for(int i = 0; i < 10; i++)
			for(int j = 0; j < 5; j++)
				field[i][j]='0';
		int d,p,q;
		for(int i = 0; i < n; i++){
			cin>>d>>p>>q;
			if(d==1){
				int init=q-1;
				int at=10000000;
				for(int j = init; j < p+init; j++){
					for(int k = 0; k < 10; k++){
						if(field[k][j]=='1'){
							at=min(at,k-1);
							break;
						}
						else if(k==9){
							at=min(at,k);
							break;
						}
					}
				}
				// atÌÀWÌ³ÉubNð¨­
				for(int j = init; j < p+init; j++){
					field[at][j]='1';
				}
			}
			else{
				int at=10000000;
				for(int k = 0; k < 10; k++){
					if(field[k][q-1]=='1'){
						at=k-1;
						break;
					}
					else if(k==9)
						at=k;
				}
				//ubNð¨­
				for(int j = at; j!=at-p; j--){
					field[j][q-1]='1';
				}
			}

			// ubNÌÁ
			for(int j = 0; j < 10; j++){
				bool f=false;
				for(int k = 0; k < 5; k++){
					if(field[j][k]!='1'){
						f=true;
						break;
					}
				}
				if(!f){
					for(int k = 0; k < 5; k++)
						field[j][k]='0';
					// Á¦½sæèãÉ éubNðº
					for(int k = j-1; k >= 0; k--){
						for(int l = 0; l < 5; l++){
							field[k+1][l]=field[k][l];
						}
					}
				}
			}
			//for(int j = 0; j < 10; j++){
			//	for(int k = 0; k < 5; k++){
			//		cout<<field[j][k]<<flush;
			//	}
			//	cout<<endl;
			//}
			//cout<<endl;
		}
		int cnt=0;
		for(int i = 0; i < 10; i++){
			for(int j = 0; j < 5; j++){
				if(field[i][j]=='1')
					cnt++;
			}
		}
		cout<<cnt<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	for(int n;cin>>n,n;){
		vvi field(5000,vi(5));
		vi top(5);
		rep(k,n){
			int d,p,q; cin>>d>>p>>q;
			if(d==1){
				int t=*max_element(top.begin()+q-1,top.begin()+q-1+p);
				rep(i,p){
					field[t][q-1+i]=1;
					top[q-1+i]=t+1;
				}
			}
			else{
				rep(i,p)
					field[top[q-1]+i][q-1]=1;
				top[q-1]+=p;
			}
			
			int cnt=0;
			rep(i,field.size()){
				if(count(allof(field[i]),1)<5)
					field[i-cnt]=field[i];
				else
					cnt++;
			}
			
			repi(i,*max_element(allof(top))-cnt,field.size())
				field[i]=vi(5);
			fill(allof(top),0);
			rep(i,field.size())
				rep(j,5)
					if(field[i][j])
						top[j]=i+1;
		}
		int res=0;
		rep(i,field.size())
			res+=count(allof(field[i]),1)%5;
		cout<<res<<endl;
	}
	
	return 0;
}

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,};
    short M[5][N]={{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;
	  M[j][maxh]=1;
	}
	row[maxh]+=p;
      }else{
	for(int j = h[q]; j < p + h[q]; ++j){
	  ++row[ j ];
	  M[q][j]=1;
	}
	h[q]+=p;
      }

      for(int j = 0; j < 5; ++j){
	mh = max( mh , h[j] );
      }

      /*
      for(int j = mh; j >= 0; --j){
	for(int k = 0; k < 5; ++k){
	  cout << M[k][j];
	}
	cout << endl;
      }
      cout << endl;
      */
      for(int j = 0; j < mh+1; ++j){
	if( row[j] == 5 ){
	  for(int k = j; k < mh+2; ++k){
	    row[k] = row[k+1];
	    for(int l = 0; l < 5; ++l){
	      M[l][k] = M[l][k+1];
	    }
	  }
	  j = -1;
	}
      }

      for(int j = 0; j < 5; ++j){
	for(int k = 0; k < mh+3; ++k){
	  if( M[j][k] ){
	    h[j] = k+1;
	  }
	}
      }
      
    }
    for(int i = 0; i < 5; ++i){
      for(int j = 0; j < N; ++j){
	if( M[i][j] )
	  ans++;
      }
    }
    printf("%d\n",ans);
  }
  return 0;
}