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 <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

void solve(int n){
    vector<vector<int>> grid(5, vector<int>(5*n+1));
    for (int i = 0; i < 5; ++i) grid[i].back() = n+1;
    vector<int> alive(5*n+1);
    vector<int> cnt(5*n+1);
    iota(alive.begin(),alive.end(), 0);
    stack<int> X;
    for (int _ = 0; _ < n; ++_) {
        int d, p, q;
        cin >> d >> p >> q; q--;
        if(d == 1){
            for (int i = 1; i < grid[0].size(); ++i) {
                int ok = 1;
                for (int j = q; j < q+p; ++j) {
                    if(grid[j][alive[i]]) ok = 0;
                }
                if(!ok){
                    for (int j = q; j < q+p; ++j) {
                        int a = alive[i-1];
                        grid[j][a] = _+1;
                        cnt[a]++;
                        if(cnt[a] == 5) X.emplace(a);
                    }
                    break;
                }
            }
        }else {
            for (int i = 1; i+p-1 < grid[0].size(); ++i) {
                int ok = 1;
                for (int j = i; j < i+p; ++j) {
                    if(grid[q][alive[j]]) ok = 0;
                }
                if(!ok){
                    for (int j = i; j < i+p; ++j) {
                        int a = alive[j-1];
                        grid[q][a] = _+1;
                        cnt[a]++;
                        if(cnt[a] == 5) X.emplace(a);
                    }
                    break;
                }
            }
        }
        while(!X.empty()){
            alive.erase(lower_bound(alive.begin(),alive.end(), X.top())); X.pop();
        }
    }
    int ans = 0;
    for (auto &&j : alive) {
        for (int i = 0; i < 5; ++i) {
            if(grid[i][j] && grid[i][j] <= n) ans++;
        }
    }
    cout << ans << "\n";
}

int main() {
    int n;
    while(cin >> n, n){
        solve(n);
    }
    return 0;
}

C++: // 2011/08/10 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		int T[6000][5];
		for(int i=0; i<5000; i++)
			for(int j=0; j<5; j++)
				T[i][j] = 0;
		int I1 = 0;
		int I2 = 0;

		int N = 0;

		for(int k=0; k<n; k++){
			int d, p, q;
			cin >> d >> p >> q;

			if(d==1){
				int i;
				bool flg = true;
				for(i=I1-1; i>=I2; i--){
					for(int j=0; j<p; j++){
						if(T[i][q-1+j]!=0){
							flg = false;
							break;
						}
					}
					if(!flg)
						break;
				}
				for(int j=0; j<p; j++)
					T[i+1][q-1+j] = 1;
				if(I1<i+2)
					I1 = i+2;
			}
			else if(d==2){
				int i;
				bool flg = true;
				for(i=I1-1; i>=I2; i--){
					for(int j=0; j<p; j++){
						if(T[i+j][q-1]!=0){
							flg = false;
							break;
						}
					}
					if(!flg)
						break;
				}
				for(int j=0; j<p; j++)
					T[i+1+j][q-1] = 1;
				if(I1<i+1+p)
					I1 = i+1+p;
			}

			N += p;

			for(int i=I2; i<I1; i++)
				if(T[i][0]==1&&T[i][1]==1&&T[i][2]==1&&T[i][3]==1&&T[i][4]==1){
					N -= 5;
					for(int j=i; j<I1-1; j++){
						T[j][0] = T[j+1][0];
						T[j][1] = T[j+1][1];
						T[j][2] = T[j+1][2];
						T[j][3] = T[j+1][3];
						T[j][4] = T[j+1][4];
					}
					I1--;
					i--;
				}

		}
/*
		for(int i=I2; i<I1; i++){
			for(int j=0; j<5; j++)
				cout << T[i][j];
			cout << endl;
		}				
*/
		cout << N << endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N;
int W[10000];
int H;

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  while (cin >> N) {
    if (N == 0) break;
    rep(i, 10000) W[i] = 0;

    H = 1;
    rep(i, N) {
      int d, p, q;
      cin >> d >> p >> q;
      int mask = 1;
      if (d == 1) {
        mask = (1<<p)-1;
        p = 1;
      }
      mask <<= (q-1);
      rep(_, p) {
        int h = H;
        for (int y=H; y>=0; y--) {
          if ((W[y] & mask) == 0) h = min(h, y);
          else break;
        }
        if (h == H) H++;
        W[h] |= mask;
      }
    }
    int s = 0;
    rep(i, 10000) {
      //cout<<bitset<5>(W[i])<<"\n";
      if (W[i] == 31) continue;
      s += __builtin_popcount(W[i]);
    }
    cout << s << "\n";
  }
  return 0;
}