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;

char stage[2][5000][5];

int main()
{
    int n;
    
    while (scanf("%d", &n) && n){
        int height[5] = {0};
        int turn = 0;
        int ct = 0;
        memset(stage, 0, sizeof(stage));
        for (int rep = 0; rep < n; rep++){
            int d, p, q;
            scanf("%d %d %d", &d, &p, &q); --q;
            if (d == 1){
                int mh = *max_element(height + q, height + p + q);
                for (int j = q; j < p + q; j++){
                    stage[turn][mh][j] = 1;
                    height[j] = mh + 1;
                }
            }
            else {
                for (int j = 0; j < p; j++){
                    stage[turn][height[q]++][q] = 1;
                }
            }
            
            int look = *max_element(height, height + 5);
            //memset(stage[1 - turn], 0, sizeof(stage[1 - turn]));
            memset(height, 0, sizeof(height));
            ct = 0;
            int nh = 0;
            for (int i = 0; i < look; i++){
                bool ok = true;
                for (int j = 0; j < 5; j++){
                    if (stage[turn][i][j] == 0) ok = false;
                }
                if (!ok){
                    for (int j = 0; j < 5; j++){
                        stage[1 - turn][nh][j] = stage[turn][i][j];
                        if (stage[1 - turn][nh][j]){
                            height[j] = nh + 1;
                            ct++;
                        }
                    }
                    nh++;
                }
            }
            turn = 1 - turn;
        }
        
        printf("%d\n", ct);
    }
    
    return (0);
}

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

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const int INF=1<<24;

void mainmain(){
	int n;
	while(cin>>n,n){
		vector<string> v(5);
		rep(i,n){
			int a,b,c;
			cin>>a>>b>>c;
			if(a==1){
				int t=0;
				reep(j,c-1,b+c-1){
					t=max(t,(int)v[j].size());
				}
				reep(j,c-1,b+c-1){
					while(v[j].size()<t){
						v[j]=v[j]+" ";
					}
					v[j]=v[j]+"#";
				}
			}
			else{
				string tmp="";
				rep(j,b){
					tmp=tmp+"#";
				}
				v[c-1]=v[c-1]+tmp;
			}
			int M=0;
			rep(j,5){
				M=max(M,(int)v[j].size());
			}
			if(M==0) continue;
			for(int j=M-1;j;j--){
				bool f=true;
				rep(k,5){
					if(v[k][j]!='#'){
						f=false;
					}
				}
				if(f){
					rep(k,5){
						v[k]=v[k].substr(0,j)+v[k].substr(j+1);
					}
				}
			}
		}
		int ans=0;
		rep(i,5){
			rep(j,v[i].size()){
				if(v[i][j]=='#') ans++;
			}
		}
		cout<<ans<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

C++: #include <bits/stdc++.h>
 
using namespace std;
 
#define INF 1e9
 
int field[5000][5],tmp[5000][5];
 
void down()
{
    memset(tmp, 0, sizeof(tmp));
    for (int j = 0; j < 5; j++) {
	int p = 4999;
	for (int i = 4999; i >= 0; i--) {
	    if (field[i][j] == -1) continue;
	    tmp[p--][j] = field[i][j];
	}
    }
    for (int i = 0; i < 5000; i++) {
	for (int j = 0; j < 5; j++) {
	    field[i][j] = tmp[i][j];
	}
    }
}
 
void remove()
{
    bool flg = false;
    for (int i = 0; i < 5000; i++) {
	int cnt = 0;
	for (int j = 0; j < 5; j++, cnt++) {
	    if (!field[i][j]) break;
	}
	if (cnt == 5) {
	    flg = true;
	    for (int j = 0; j < 5; j++) {
		field[i][j] = -1;
	    }
	}
    }
   
    if (flg) {
	down();
    }
}
 
int main()
{
    int N, d, p, q;
 
    while (cin >> N, N) {
	memset(field, 0, sizeof(field));
	for (int i = 0; i < N; i++) {
	    cin >> d >> p >> q; q--;
	    if (d == 1) {
		int min = INF;
		for (int j = q; j < p+q; j++) {
		    for (int i = 0; i < 5000; i++) {
			if (i == 4999 || field[i+1][j] == 1) {
			    min = std::min(min, i);
			    break;
			}
		    }
		}
		for (int j = q; j < p+q; j++) {
		    field[min][j] = 1;
		}
	    } else {
		for (int i = 0; i < 5000; i++) {
		    if (i == 4999 || field[i+1][q] == 1) {
			for (int j = 0; j < p; j++) {
			    field[i-j][q] = 1;
			}
			break;
		    }
		}
	    }
	    remove();
	}
	int ans = 0;
	for (int i = 0; i < 5000; i++) {
	    for (int j = 0; j < 5; j++) {
		if (field[i][j] == 1) ans++;
	    }
	}
	cout << ans << endl;
    }
    return 0;
}