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 <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <queue>
#define MAX 5000
using namespace std;

int main(void){
	int i,j,k,n,ans;
	bool f[5][MAX];
	while(1){
		cin >> n;
		if(n == 0) break;
		for(i=0;i<5;i++){
			for(j=0;j<MAX;j++) f[i][j] = false;
		}
		ans = 0;
		for(i=0;i<n;i++){
			int d,p,q;
			cin >> d >> p >> q;
			q--;
			ans += p;
			if(d == 2){
				for(j=MAX-1;j>=0;j--){
					if(j == 0 || f[q][j-1] == true){
						for(k=j;k<j+p;k++) f[q][k] = true;
						break;
					}
				}
			}
			else{
				for(j=MAX-1;j>=0;j--){
					k=5;
					if(j != 0){
						for(k=q;k<q+p;k++) if(f[k][j-1] == true) break;
					}
					if(j == 0 || k != q+p){
						for(k=q;k<q+p;k++) f[k][j] = true;
						break;
					}
				}
			}
			for(j=0;j<MAX;j++){
				for(k=0;k<5;k++) if(f[k][j] == false) break;
				if(k == 5){
					ans -= 5;
					for(k=0;k<5;k++){
						for(int t=j;t<MAX-1;t++) f[k][t] = f[k][t+1];
						f[k][MAX-1] = false;
					}
					j--;
				}
			}
		//			cout << endl;
		//		for(int ii=7;ii>=0;ii--){
		//	for(j=0;j<5;j++) cout << f[j][ii] << " ";
		//	cout << endl;
		//}
		//cout << endl;
		}
		cout << ans << endl;


	}
	return 0;
}

C++: #include <bitset>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <iostream>

#define VARIABLE(x) cerr << #x << "=" << x << endl
#define BINARY(x) cerr << #x << "=" << static_cast<bitset<16> >(x) << endl;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

const int INF = 10000000;
int dx[4]={1, 0, -1, 0}, dy[4]={0, 1, 0, -1};
using namespace std;
typedef long long  ll;

/** Problem0178 : TETORIS **/
bool board[5][5000];

int main()
{
	int n, d, p, q;
	while (cin>>n, n) {
		rep(x, 5) rep(y, 5000) board[x][y]=false;
		rep(k, n) {
			cin >> d >> p >> q;
			q--;
			
			int dx=1, dy=0;
			if (d==2) {
				dx=0; dy=1;
			}
			
			vector<int> low;
			for (int ii=0; ii<p; ii++) {
				for (int i=4999; i>=0; i--) {
					if (board[q+ii][i]) {
						low.push_back(i+1);
						goto end;
					}
				}
				low.push_back(0);
			end:;
				if (d==2) break;
			}

			int m=0;
			rep(j, low.size()) {
				m = max(m, low[j]);
			}
			
			board[q][m]=true;
			rep(j, p-1) {
				q+=dx;m+=dy;
				board[q][m] = true;
			}
			
			rep(y, 5000) {
				bool flg = true;
				rep(x, 5) {
					flg &= board[x][y];
				}
				if (flg) {
					for (int yy=y; yy<5000; yy++) {
						for (int xx=0; xx<5; xx++) {
							board[xx][yy] = board[xx][yy+1];
						}
					}
					for (int xx=0; xx<5; xx++) {
						board[xx][4999] = false;
					}
					y--;
				}
			}
			/**
			rep(y, 10) {
				rep(x, 5) {
					cout << (board[x][9-y]?"#":"0");
				}
				cout << endl;
			}
			cout << endl; **/
		}
		int ans=0;
		rep(y, 5000) {
			rep(x, 5) {
				if (board[x][y])
					ans++;
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

char stage[2][5001][5];

void place(int *height, int col, int d, int len, int pos, int turn)
{
    if (d == 1){
        int mh = *max_element(height + pos, height + pos + len);
        for (int j = pos; j < pos + len; j++){
            stage[turn][mh][j] = col;
            height[j] = mh + 1;
        }
    }
    else {
        for (int j = 0; j < len; j++){
            stage[turn][height[pos]++][pos] = col;
        }
    }
    return;
}

int main()
{
    int n;
    
    while (scanf("%d", &n) && n){
        int height[5] = {0};
        int d[1001] = {0}, len[1001] = {0}, pos[1001] = {0};
        
        memset(stage, 0, sizeof(stage));
        
        for (int i = 1; i <= n; i++){
            scanf("%d %d %d", d + i, len + i, pos + i); --pos[i];
            place(height, i, d[i], len[i], pos[i], 0);
        }
        
        int turn = 0;
        for (int i = 0; i < n; i++){
            int ht = *max_element(height, height + 5);
            
            for (int j = 0; j < ht; j++){
                bool ok = true;
                for (int k = 0; k < 5; k++){
                    stage[1 - turn][j][k] = 0;
                    if (!stage[turn][j][k]) ok = false;
                }
                if (ok){
                    for (int k = 0; k < 5; k++){
                        len[stage[turn][j][k]]--;
                    }
                }
            }
            
            memset(height, 0, sizeof(height));
            for (int j = 1; j <= n; j++){
                place(height, j, d[j], len[j], pos[j], 1 - turn);
            }
            turn = 1 - turn;
        }
        
        int ret = 0;
        int h = *max_element(height, height + 5);
        for (int i = 0; i < h; i++){
            for (int j = 0; j < 5; j++){
                ret += !!stage[turn][i][j];
            }
        }
        
        printf("%d\n", ret);
    }
    
    return (0);
}