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 <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

template<class T> void debug(T begin, T end){ for(T i = begin; i != end; ++i) cerr<<*i<<" "; cerr<<endl; }
inline bool valid(int x, int y, int W, int H){ return (x >= 0 && y >= 0 && x < W && y < H); }

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int main(){
  int N;
  while(cin >> N && N){
    const int H = 5010;
    const int W = 5;
    bool block[H][W] = {};
    int height[W] = {};
    int n_h = 0;
    REP(iter, N){
      int d, l, bx;
      cin >> d >> l >> bx;
      bx --;
      if(d == 1){
        // yoko
        int h = 0;
        for(int x = bx; x < bx + l; x++) h = max(h, height[x]);
        for(int x = bx; x < bx + l; x++) block[h][x] = true;
        bool line = true;
        for(int x = 0; x < W; x++) if(!block[h][x]) line = false;
        if(line){
          for(int x = 0; x < W; x++) block[h][x] = false;
        }
      }else if(d == 2){
        // tate
        for(int y = height[bx]; y < height[bx] + l; y++)
          block[y][bx] = true;
        for(int y = height[bx]; y < height[bx] + l; y++){
          bool line = true;
          for(int x = 0; x < W; x++) if(!block[y][x]) line = false;
          if(!line) continue;
          for(int x = 0; x < W; x++) block[y][x] = false;
        }
      }
      //bool new_block[H][W] = {};
      int new_height[W] = {};
      int new_nh = 0;
      int curr = 0;
      /*
      for(int y = n_h + 5; y >= 0; y--){
        debug(block[y], block[y] + 5);
      }
      cout << endl;
      */
      for(int y = 0; y < n_h + 5; y++){
        bool clear = true;
        for(int x = 0; x < W; x++) if(block[y][x]) clear = false;
        if(clear) continue;
        for(int x = 0; x < W; x++)if(block[y][x]){
          block[y][x] = false;
          block[curr][x] = true;
          new_height[x] = curr + 1;
        }
        new_nh = ++curr;
      }
      //memcpy(block, new_block, sizeof(block));
      memcpy(height, new_height, sizeof(height));
      n_h = new_nh;
    }
    int ans = 0;
    REP(y, H)REP(x, W) ans += block[y][x];
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  while(true){
    int n;
    int ans=0;
    scanf("%d",&n);

    if(n==0)break;
    int row[10000]={0,};
    int h[6]={0,};
    for(int i = 0; i < n; ++i){
      int mh = 0;
      int d,p,q;
      scanf("%d%d%d", &d, &p, &q);

      if(d==1){
	int maxh = 0;
	for(int j = q; j < q + p; ++j){
	  maxh = max( maxh, h[j] );
	}
	for(int j = q; j < q + p; ++j){
	  h[j] = maxh + 1;
	}
	row[maxh]+=p;
      }else{
	for(int j = h[q]; j < p + h[q]; ++j){
	  row[ j ]++;
	}
	h[q]+=p;
      }

      // cout << " DROPPED : " << i << endl;
      for(int j = 1; j < 6; ++j){
	mh = max( mh , h[j] );
      }

      for(int j = 0; j < mh; ++j){
	// cout << " row["<<j<<"] = " << row[j] << endl;
	if( row[j] == 5 ){
	  for(int k = 1; k < 6; ++k){
	    --h[k];
	  }
	  for(int k = j; k < mh; ++k){
	    row[k] = row[k + 1];
	  }
	  j = -1;
	  continue;
	}
      }
    }
    for(int i = 0; i < 10000; ++i){
      ans += row[i];
    }
    printf("%d\n",ans);
  }
  return 0;
}

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

using namespace std;

void solve()
{
	bool isUsed[5002][5];
	int n;
	while (cin >> n, n)
	{
		for (int i = 1; i < 5002; ++i)
		{
			for (int j = 0; j < 5; ++j)
			{
				isUsed[i][j] = false;
			}
		}
		for (int i = 0; i < 5; ++i)
		{
			isUsed[0][i] = true;
		}
		int highest = 1;
		for (int i = 0; i < n; ++i)
		{
			int d, p, q;
			cin >> d >> p >> q;
			--q;
			int level = 0;
			if (d == 1)
			{
				bool isOK = true;
				while (isOK)
				{
					for (int j = 0; j < p; ++j)
					{
						if (isUsed[highest - level][q + j])
						{
							isOK = false;
							--level;
							break;
						}
					}
					++level;
				}
				--level;
				for (int j = 0; j < p; ++j)
				{
					isUsed[highest - level][q + j] = true;
				}
				highest = max(highest, highest - level + 1);
			}
			else if (d == 2)
			{
				while (!isUsed[highest - level][q])
				{
					++level;
				}
				--level;
				for (int j = 0; j < p; ++j)
				{
					isUsed[highest - level + j][q] = true;
				}
				highest = max(highest, highest - level + p);
			}
		}
		int ans = 0;
		for (int i = 1; i < highest; ++i)
		{
			int rowCount = 0;
			for (int j = 0; j < 5; ++j)
			{
				if (isUsed[i][j])
				{
					++ans;
					++rowCount;
				}
				if (rowCount == 5)
				{
					ans -= 5;
				}
			}
		}
		cout << ans << endl;
	}
}

int main()
{
	solve();
	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]++;
				}
				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[h][j]=0;
						for(int k=h;k<4999;k++)dat[k][j]=dat[k+1][j];
						m[j]--;
					}
					count-=5;
				}
			}
		}
		printf("%d\n",count);
	}
}