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

using namespace std;

int n;
int h[7];
bool m[6010][7];
int b[1010][5];

int main()
{
    while (true)
    {
        cin >> n;
        if (n == 0)
            break;
        for (int i = 0; i < 5500; i++)
        {
            for (int j = 0; j < 7; j++)
                m[i][j] = false;
        }
        for (int i = 0; i <= 6; i++)
        {
            h[i] = 0;
        }
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                cin >> b[i][j];
                if (j == 1)
                {
                    ans += b[i][j];
                }
            }
        }
        for(int i = 0;i < n;i++){
            if(b[i][0] == 1){
                int hmax = h[b[i][2]];
                for(int j = 0;j < b[i][1];j++){
                    hmax = max(hmax,h[b[i][2]+j]);
                }
                hmax++;
                for(int j = 0;j < b[i][1];j++){
                    h[b[i][2]+j] = hmax;
                    m[hmax][b[i][2]+j] = true;
                }
            }else{
                int hmax = h[b[i][2]];
                for(int j = 1;j <= b[i][1];j++){
                    m[hmax+j][b[i][2]] = true;
                }
                h[b[i][2]] = hmax + b[i][1];
            }
            int hmax = h[1];
            for(int j = 1;j <= 5;j++) hmax = max(hmax,h[j]);
            set<int> s;
            for(int j = 1;j <= hmax;j++){
                int count = 0;
                for(int k = 1;k <= 5;k++)if(m[j][k] == true)count++;
                if(count == 5){
                    ans -= 5;
                    s.insert(j);
                }
            }
            int nj = 1;
            for(int j = 1;j <= hmax;j++){
                if( s.find(j) != s.end() )continue;
                for(int k = 1;k <= 5;k++){
                    m[nj][k] = m[j][k];
                }
                nj++;
            }
            for(int j = nj;j < 5050;j++){
                for(int k = 1;k <= 5;k++)m[j][k] = false;
            }
            for(int j = 1;j <= 5;j++){
                h[j] = 0;
                for(int k = hmax;k >= 0;k--){
                    if(m[k][j] == true){
                        h[j] = k;
                        break;
                    }
                }
            }
        }
        int hmax = h[1];
        for(int j = 1;j <= 5;j++) hmax = max(hmax,h[j]);
        for(int j = hmax;j >= 1;j--){
        }
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define F first
#define S second
#define INF 1 << 30
#define UNDER 10000

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int t[UNDER+10][5], n;

int count(){
  int res = 0;
  rep(i, UNDER) rep(j, 5) if(t[i][j] == 1) res++;
  return res;
}

void deleteblock(){
  for(int y = UNDER-1; y >= 0; y--){
    int f = 0;
    rep(i, 5) if(t[y][i] == 0){ f = 1; break;}
    if(f) continue;
    rep(i, 5){
      for(int c = y; c > 0; c--){
	t[c][i] = t[c-1][i];
      }
    }
    t[0][0] = t[0][1] = t[0][2] = t[0][3] = t[0][4] = 0;
    y++;
  }
  return;
}

int main(){
  while(scanf("%d", &n) && n){
    int d, p, q;
    memset(t, 0, sizeof(t));
    rep(i, 5) t[UNDER][i] = 1;

    rep(i, n){
      scanf("%d%d%d", &d, &p, &q); q--;
      if(d == 1){
	REP(y, 1, UNDER+1){
	  int f = 0;
	  REP(x, q, q+p) if(t[y][x] == 1) f = 1;
	  if(!f) continue;
	  REP(x, q, q+p) t[y-1][x] = 1;
	  break;
	}
      }else{
	rep(y, UNDER+1){
	  if(!t[y][q]) continue;
	  for(int c = y-1; c > y-p-1; c--) t[c][q] = 1;
	  break;
	}
      }
      deleteblock();
      /*      REP(i, 990, UNDER+1){
	rep(j, 5) printf("%d ", t[i][j]); puts("");
	}*/
    }

    printf("%d\n", count());
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;

int o[5011][5];

int yoko(int L,int R){
	for(int i = 5010 ; i > 0 ; i--){
		int f = 0;
		for(int j = L ; j <= R ; j++){
			if( o[i-1][j] ){
				f = 1;
			}
		}
		if( f ){
			for(int k = L ; k <= R ; k++)
				o[i][k] = 1;
			return 0;
		}
	}
	for(int i = L ; i <= R ; i++)
		o[0][i] = 1;
}
int tate(int x,int l){
	for(int i = 5010 ; i > 0 ; i--){
		int f = o[i-1][x];
		if( f ){
			for(int j = 0 ; j < l ; j++)
				o[i+j][x] = 1;
			return 0;
		}
	}
	
	for(int i = 0 ; i < l ; i++) o[i][x] = 1;
}
int eraser(){
	vector<int> v[5];
	for(int i = 0 ; i <= 5010 ; i++){
		if( o[i][0] && o[i][1] && o[i][2] && o[i][3] && o[i][4] ){
			o[i][0] = o[i][1] = o[i][2] = o[i][3] = o[i][4] = 0;
		}
		if( o[i][0] || o[i][1] || o[i][2] || o[i][3] || o[i][4] ){
			for(int j = 0 ; j < 5 ; j++)
				v[j].push_back(o[i][j]);
		}
	}
	for(int i = 0 ; i < 5011 ; i++)
		for(int j = 0 ; j < 5 ; j++)
			o[i][j] = 0;
	for(int i = 0 ; i < v[0].size() ; i++)
		for(int j = 0 ; j < 5 ; j++){
			o[i][j] = v[j][i];
		}
	
}
int main(){
	int n;
	while(cin >> n && n){
		for(int i = 0 ; i < 5011 ; i++)
			for(int j = 0 ; j < 5 ; j++)
				o[i][j] = 0;
		
		for(int i = 0 ; i < n ; i++){
			int d,l,x;
			cin >> d >> l >> x;
			x--;
			if( d == 1 ){
				yoko(x,x+l-1);
			}else{
				tate(x,l);
			}
			eraser();
		}
		int ans = 0;
		for(int i = 0 ; i < 5011 ; i++)
			for(int j = 0 ; j < 5 ; j++)
				ans += o[i][j];
		cout << ans << endl;
	}
}