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>

using namespace std;

int main()
{
	int n, d, p, q, s, t;
	
	while(cin>>n&&n){
		vector<bool> vec[5];
		
		while(n--){
			cin >> d >> p >> q;
			q--;
			if(d == 1){
				t = -1;
				for(int i = q; i < p; i++){
					for(int j = vec[i].size()-1; j >= 0; j--){
						if(vec[i][j]){
							t = max(t, j);
						}
					}
				}
				
				for(int i = q; i < p+q; i++){
					while(vec[i].size() < (t+1)){
						vec[i].push_back(false);
					}
					vec[i].push_back(true);
				}
			} else {
				t = -1;
				for(int i = vec[q].size()-1; i >= 0; i--){
					if(vec[q][i]){
						t = i;
						break;
					}
				}
				s = 0;
				for(int i = t+1; i < vec[q].size() && s < p; i++, s++){
					vec[q][i] = true;
				}
				for(;s < p; s++){
					vec[q].push_back(true);
				}
			}
			t = vec[0].size();
			for(int i = 1; i < 5; i++){
				s = vec[i].size();
				t = min(s, t);
			}

			for(int i = t-1; i >= 0; i--){
				bool f = true;
				for(int j = 0; j < 5; j++){
					if(!vec[j][i]){
						f = false;
						break;
					}
				}
				
				if(f){
					for(int j = 0; j < 5; j++){
						vec[j].erase((vec[j].begin()+i));
					}
				}
			}
		}
		
		t = 0;
		for(int i = 0; i < 5; i++){
			for(int j = 0; j < vec[i].size(); j++){
				if(vec[i][j]){
					t++;
				}
			}
		}
		
		cout << t << endl;
	}
	
	return 0;
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
int main(){
  int h,i,j,k,l,m;
  int a[5002][5],n,d,p,q,c;
  a[5001][0]=a[5001][1]=a[5001][2]=a[5001][3]=a[5001][4]=1;
  for(;;){
    cin>>n;
    if(n==0)
      break;
    memset(a,0,5*5001*sizeof(int));
    for(i=0;i<n;i++){
      cin>>d>>p>>q;
      for(j=0;j<5002;j++){
	if(d==1){
	  for(k=q-1;k<p+q-1;k++){
	    if(a[j][k])
	      break;
	  }
	  if(k!=p+q-1){
	    for(k=q-1;k<p+q-1;k++)
	      a[j-1][k]=1;
	    break;
	  }
	}else{
	  if(a[j][q-1]){
	    for(k=j-p;k<j;k++)
	      a[k][q-1]=1;
	    break;
	  }
	}
      }
      if(d==1){
	for(k=0;k<5;k++){
	  if(a[j-1][k]==0)
	    break;
	}
	if(k==5){
	  for(k=2;;k++){
	    for(l=0;l<5;l++){
	      if(a[j-k][l])
		break;
	    }
	    memcpy(&a[j-k+1][0],&a[j-k][0],5*sizeof(int));
	    if(l==5)
	      break;
	  }
	}
      }else{
	h=0;
	for(k=0;k<p;k++){
	  for(l=0;l<5;l++){
	    if(a[j-h-1][l]==0)
	      break;
	  }
	  if(l==5){
	    for(l=2;;l++){
	      for(m=0;m<5;m++){
		if(a[j-h-l][m])
		  break;
	      }
	      memcpy(&a[j-h-l+1][0],&a[j-h-l][0],5*sizeof(int));
	      if(m==5)
		break;
	    }
	  }else
	    h++;
	}
      }
    }
    c=0;
    for(i=1;i<5001;i++){
      for(j=0;j<5;j++){
	if(a[i][j])
	  c++;
      }
    }
    cout<<c<<endl;
  }
  return 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++){
							dat[m[d]+i][j]=0;
							for(int k=m[d]+i;k<4999;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++){
						dat[m[d]+i][j]=0;
						for(int k=h;k<4999;k++)dat[k][j]=dat[k+1][j];
						m[j]--;
					}
					count-=5;
				}
			}
		}
		printf("%d\n",count);
	}
}