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<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;
				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;
			}
		}
		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++: //14
#include<iostream>
#include<algorithm>

using namespace std;

int main(){
  for(int n;cin>>n,n;){
    const int h=5000;
    bool g[h][5]={{}};
    fill(g[h-1],g[h],true);
    while(n--){
      int d,p,q;
      cin>>d>>p>>q;
      int y;
      for(y=0;;y++){
	if(d==2){
	  if(g[y+1][q-1])break;
	}else{
	  int i;
	  for(i=0;i<p;i++){
	    if(g[y+1][q-1+i])break;
	  }
	  if(i<p)break;
	}
      }
      for(int i=0;i<p;i++){
	int dy=0,dx=0;
	((d==2)?dy:dx)=i;
	g[y-dy][q-1+dx]=true;
      }
      for(int i=0;i<h-1;i++){
	if(count(g[i],g[i+1],true)==5){
	  for(int j=i;j>=1;j--){
	    for(int x=0;x<5;x++){
	      g[j][x]=g[j-1][x];
	    }
	  }
	}
      }
    }
    cout<<count(g[0],g[h-1],true)<<endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>

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;
}

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;
}