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 <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <iostream>
#include <sstream>
#include <climits>
#include <cfloat>
#include <complex>

using namespace std;

bool field[5000][5];
int top[5];

int main()
{
	int n;
	while (scanf("%d", &n), n)
	{
		memset(field, 0, sizeof(field));
		memset(top, 0, sizeof(top));

		while (n--)
		{
			int d, p, q;
			scanf("%d %d %d", &d, &p, &q);
			--q;

			int width = d == 1 ? p : 1;
			int pos = -1;
			for (int i = 5000-1; pos == -1; --i)
				for (int j = 0; j < width; ++j)
					if (field[i][q + j] || i == -1)
					{
						pos = i + 1;
						break;
					}

			if (d == 1)
				for (int i = 0; i < p; ++i)
					field[pos][q + i] = true;
			else
				for (int i = 0; i < p; ++i)
					field[pos + i][q] = true;

			while (true)
			{
				bool vanished = false;
				for (int i = 0; i < 5000; ++i)
				{
					int j;
					for (j = 0; j < 5 && field[i][j]; ++j)
						;
					if (j == 5)
					{
						for (j = 0; j < 5; ++j)
							field[i][j] = false;
						vanished |= true;
					}
				}
				if (!vanished)
					break;

				int diff = 0;
				for (int i = 0; i < 5000; ++i)
				{
					int j;
					for (j = 0; j < 5 && !field[i][j]; ++j)
						;
					if (j == 5)
						++diff;
					else if (diff)
						for (int j = 0; j < 5; ++j)
						{
							field[i - diff][j] = field[i][j];
							field[i][j] = false;
						}
				}
			}
		}
		int ans = 0;
		for (int i = 0; i < 5000; ++i)
			for (int j = 0; j < 5; ++j)
				if (field[i][j])
					++ans;
		printf("%d\n", ans);
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <cmath>
#include <string>
#include <sstream>
#include <iomanip>
#include <complex>
using namespace std;

#define ll long long
#define vvi vector< vector<int> >
#define All(X) X.begin(),X.end()
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define pb push_back 
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}

int grid[5][6000];
int n,d,p,q;
ll int ans = 0;

int GetBottom(int x){
	REP(i,6000){
		if(grid[x][i]==-1) return i;
	}
	return 0;
}

bool isallclear(int x){
	REP(i,5){
		if(grid[i][x]!=-1) return false;
	}
	return true;
}

bool isallblock(int x){
	REP(i,5){
		if(grid[i][x]!=1) return false;
	}
	return true;
}

int main(){
	while(cin>>n,n){
		REP(i,5) REP(j,6000) grid[i][j] = -1;
		REP(i,n){
			cin >> d >> p >> q;
			if(d==2){
				int tmp = GetBottom(q-1);
				FOR(j,tmp,tmp+p) grid[q-1][j] = 1;
			}else{
				int tmp = -1;
				FOR(j,q-1,q-1+p){
					tmp = max(tmp,GetBottom(j));
				}
				FOR(j,q-1,q-1+p) {
					grid[j][tmp] = 1;
					REP(k,tmp) if(grid[j][k]==-1) grid[j][k] = 0;
				}
			}
			int nowline = 0;
			while(1){
				if(isallclear(nowline)) break;
				if(isallblock(nowline)){
					REP(j,5) {
						int nline = nowline+1;
						while(1){
							if(grid[j][nline]==-1) {
								grid[j][nline-1] = grid[j][nline];
								break;
							}
							grid[j][nline-1] = grid[j][nline];
							nline++;
						}
					}
				}else{
					nowline++;
				}
			}
		}
		ans = 0;
		int line = 0;
		while(1){
			if(isallclear(line)) break;
			REP(i,5){
				if(grid[i][line]==1) ans++;
			}
			line++;
		}
		cout << ans << endl;

	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int main() {
	int n;
	while (scanf("%d", &n), n) {
		int E[15]{};
		rep(i, n) {
			int d, p, q; scanf("%d%d%d", &d, &p, &q); q--;
			if (d == 1) {
				rep(i, p)E[q + i]++;
			}
			else E[q] += p;
			int Min = *min_element(E, E + 5);
			rep(i, 5)E[i] -= Min;
		}
		printf("%d\n", accumulate(E, E + 5, 0));
	}
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int dat[5000][5];
int m[5];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<5000;i++)
			for(int j=0;j<5;j++)dat[i][j]=0;
		for(int i=0;i<5;i++)m[i]=0;
		int count=0;
		for(int i=0;i<a;i++){
			int b,c,d;
			scanf("%d%d%d",&b,&c,&d);d--;
			count+=c;
			if(b==2){
				for(int i=0;i<c;i++){
					dat[m[d]+i][d]=1;
					bool ok=true;
					for(int j=0;j<5;j++)if(!dat[m[d]+i][j])ok=false;
					if(ok){
						for(int j=0;j<5;j++){
							for(int k=m[d]+i;k<m[j];k++)dat[k][j]=dat[k+1][j];
							m[j]--;
						}
						count-=5;
					}
				}
				m[d]+=c;
			}else{
				int h=0;
				for(int i=0;i<c;i++)h=max(h,m[i+d]);
				for(int i=0;i<c;i++)dat[h][i+d]=1;
				for(int i=0;i<c;i++)m[i+d]=h+1;
				bool ok=true;
				for(int j=0;j<5;j++)if(!dat[h][j])ok=false;
				if(ok){
					for(int j=0;j<5;j++){
						for(int k=h;k<m[j];k++)dat[k][j]=dat[k+1][j];
						m[j]--;
					}
					count-=5;
				}
			}
		}
		for(int i=0;i<5000;i++){
			bool ok=true;
			for(int j=0;j<5;j++)if(!dat[i][j])ok=false;
			if(ok)count-=5;
		}
		printf("%d\n",count);
	}
}

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

int main() {
	int i,j,n,a,b,c,d,e,f,g,m[5050];
	while(cin >> n) {
		if (n==0) break;
		for (j=0;j<5050;j++) m[j]=0; d=1;
		for (i=0;i<n;i++) {
			cin >> a >> b >> c;
			f=a >1 ? 1 << (c-1) : ((1 << b)-1)  << (c-1);  
			for (j=d;j>=0;j--) if ( m[j]+f != (m[j] | f)) break;
			e=g=j+1; 
			if (a==1) { m[e]+=f; d++;}  else for (j=0;j<b;j++) { m[e+j]+=f; d++;}
			for (j=g; j<g+d+5; j++) if (m[j]<31) m[e++]=m[j]; 
		}
		f=0;
		for (i=0;i<5050;i++) for (j=0;j<5;j++) f+=(m[i] & ( 1 << j))>0;
		cout << f << endl;
	}
	return 0;
}

C++: #include<iostream>
#define rep(i,n) for(int i=0;i<n;i++)
#define MH 5000
using namespace std;
bool ck(int *f){
	rep(i,5)if(!f[i])return 0;
	return 1;
}
int main(){
	int n;
	while(cin>>n,n){
		int d,l,x,y,c=0;
		int field[MH][5]={0};
		rep(i,n){
			cin>>d>>l>>x,y=MH,x--,d--;
			for(;y>0;y--)rep(j,(d?1:l))if(field[y-1][x+j])goto END;
			END:
			rep(j,l)field[y+d*j][x+(1-d)*j]=1;
			rep(i,MH)while(ck(field[i])){
				rep(j,MH-i-1)rep(k,5)field[j+i][k]=field[j+i+1][k];
				rep(k,5)field[MH-1][k]=0;
			}
		}
		rep(i,MH)rep(j,5)c+=field[i][j];
		cout<<c<<endl;
	}
	return 0;
}