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>
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]; else d--; 
		}
		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 <bits/stdc++.h>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)

int n, hmax;
bool maps[5000][5];

int serch(int x, int l) {
	for (int i = hmax; i >= 0; i--) {
		REP(j, l) if (maps[i][x + j]) return i + 1;
	}
	return 0;
}

int check(int y, int l) {
	REP(i, l) {
		bool flag = true;
		REP(j, 5) if (!maps[y + i][j]) flag = false;
		if (flag) return y + i;
	}
	return -1;
}

void bset(int d, int p, int q) {
	int hl = d == 2 ? p : 1;
	int xl = d == 1 ? p : 1;
	int hpos = serch(q - 1, xl);
	
	if (d == 1) REP(i, p) maps[hpos][q + i - 1] = true;
	else REP(i, p) maps[hpos + i][q - 1] = true;
	hmax = max(hmax, hpos + hl);
	
	int tmp;
	while ((tmp = check(hpos, hl)) != -1) {
		REP(i, 5) maps[tmp][i] = false;
		FOR(i, tmp, hmax) {
			REP(j, 5) swap(maps[i][j], maps[i + 1][j]);
		}
		hmax--;
	}
}

int main() {
	while (cin >> n, n) {
		memset(maps, 0, sizeof(maps));
		hmax = 0;
		
		while (n--) {
			int d, p, q;
			scanf("%d %d %d", &d, &p, &q);
			bset(d, p, q);
		}
		cout << count(maps[0], maps[5000], true) << endl;
	}
	return 0;
}

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<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    while(cin>>n,n){
        queue<int> que[6];
        for(int i=0,num[3];i<n;i++){
            for(int j=0;j<3;j++)cin>>num[j];
            if(num[0]==1)for(int j=0;j<num[1];j++)que[num[2]+j].push(1);
            else for(int j=0;j<num[1];j++)que[num[2]].push(1);
            bool kawa=1;
            while(kawa){
                for(int j=1;j<6&&kawa;j++)if(que[j].empty())kawa=0;
                if(kawa)for(int j=1;j<6;j++)que[j].pop();
            }
        }
        int ans=0;
        for(int i=1;i<6;i++)ans+=(int)que[i].size();
        cout<<ans<<endl;
    }
}