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 <cstdio>
#include <ctime>
using namespace std;

int main(){
	clock_t c1 = clock();
	int s = 0, n;
	while(scanf("%d", &n), n){
		s += n;
		for(int i = 0; i < n; ++i){
			scanf("%*d%*d%*d");
		}
	}
	
	clock_t c2 = c1 + CLOCKS_PER_SEC * (s / 100000.0);
	while(clock() < c2);
}

C++: #include <iostream>
#include <vector>
#include <cmath>

using namespace std;

int main() {
	int n;
	while(cin >> n) {
		if (n == 0) {
			break;
		}
	vector<int> retu[5];
	for (int i = 0; i < n; i++) {
		int d, p, q;
		cin >> d >> p >> q;
		q--;
		if (d == 1) {
			int MAX = 0;
			for (int j = q; j < q+p; j++) {
				int k = retu[j].size();
				MAX = max(MAX, k);
			}
			for (int j = q; j < q+p; j++) {
				for (int k = retu[j].size(); k < MAX; k++) {
					retu[j].push_back(0);
				}
				retu[j].push_back(1);
			}
		} else {
			for (int j = 0; j < p; j++) {
				retu[q].push_back(1);
			}
		}
		int MIN = retu[0].size();
		for (int j = 1; j < 5; j++) {
			int k = retu[j].size();
			MIN = min(MIN, k);
		}
		for (int j = MIN-1; j >= 0; j--) {
			if (retu[0][j]+retu[1][j]+retu[2][j]+retu[3][j]+retu[4][j] == 5) {
				for (int k = 0; k < 5; k++) {
					for (int l = j+1; l < retu[k].size(); l++) {
						retu[k][l-1] = retu[k][l];
					}
					vector<int>::iterator it = retu[k].end();
					it--;
					retu[k].erase(it);
				}
			}
		}
		for (int j = 0; j < 5; j++) {
			vector<int>::iterator it = retu[j].end();
			vector<int>::iterator itt = retu[j].begin();
			it--;
			for (int k = retu[j].size()-1; k >= 0; k--) {
				if (retu[j][k] == 0) {
					retu[j].erase(it);
				} else {
					break;
				}
				it--;
			}
		}
	}
	int counter = 0;
	for (int i = 0; i < 5; i++) {
		for (int j = 0; j < retu[i].size(); j++) {
			if (retu[i][j] == 1) {
				counter++;
			}
		}
	}
	cout << counter << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cmath>
#include <queue>
#include <map>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1<<30;

int main() {
  int n;
  while(cin >> n, n) {
    int height[5] = {}; // ツ各ツ療アツづ可堕カツ催敖つキツづゥツブツδ債ッツクツづ個暗ェツ氾板湘」ツづ個暗ェツ古つ湘」
    bool ba[5][5010] = {};

    while(n--) {
      int d, p, q;
      cin >> d >> p >> q;
      q--;
      if (d == 1) {
        int ma = 0;
        for (int i=q; i<p+q; ++i) {
          ma = max(ma, height[i]);
        }
        for (int i=q; i<p+q; ++i) {
          ba[i][ma] = 1;
          height[i] = ma + 1;
        }
        bool f = 1;
        REP(i,5)
          if (!ba[i][ma])
            f = 0;
        if (f) {
          REP(i,5) {
            for (int j=ma+1; j<height[i]; ++j)
              if (j>0)
                ba[i][j-1] = ba[i][j];
            ba[i][height[i]-1] = 0;
            while(height[i] > 0 && !ba[i][height[i]-1])
              height[i]--;
          }
        }
      } else {
        REP(i,p) {
          ba[q][height[q]+i] = 1;
        }
        height[q] += p;
        for (int k=height[q]-p; k<height[q]; ++k) {
          bool f = 1;
          REP(i,5)
            if (!ba[i][k])
              f = 0;
          //    cout << k << " " << f << endl;
          if (f) {
            REP(i,5) {
              for (int j=k+1; j<height[i]; ++j) {
                if (j > 0)
                  ba[i][j-1] = ba[i][j];
              }
              ba[i][height[i]-1] = 0;
              while(height[i] > 0 && !ba[i][height[i]-1])
                height[i]--;
            }
            // REP(i,10) {
            //   REP(j,5) {
            //     cout << ba[j][9-i];
            //   }
            //   cout << endl;
            // }
            // cout << endl;
            k--;
          }
        }
      }
      // REP(i,10) {
      //   REP(j,5) {
      //     cout << ba[j][9-i];
      //   }
      //   cout << endl;
      // }
      // REP(i,5)
      //   cout << height[i] << " ";
      // cout << endl << endl;;
    }
    int ans = 0;
    REP(i,5)
      REP(j,5010)
        if (ba[i][j])
          ans++;
    cout << ans << endl;
  }
}

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

using namespace std;

const int limit=5000;

int main(){

	int n;
	char field[limit][5];
	while(cin>>n&&n!=0){
		for(int i = 0; i < limit; i++)
			for(int j = 0; j < 5; j++)
				field[i][j]='0';
		int d,p,q;
		for(int i = 0; i < n; i++){
			cin>>d>>p>>q;
			if(d==1){
				int init=q-1;
				int at=10000000;
				for(int j = init; j < p+init; j++){
					for(int k = 0; k < limit; k++){
						if(field[k][j]=='1'){
							at=min(at,k-1);
							break;
						}
						else if(k==limit-1){
							at=min(at,k);
							break;
						}
					}
				}
				// atÌÀWÌ³ÉubNð¨­
				for(int j = init; j < p+init; j++){
					field[at][j]='1';
				}
			}
			else{
				int at=10000000;
				for(int k = 0; k < limit; k++){
					if(field[k][q-1]=='1'){
						at=k-1;
						break;
					}
					else if(k==4999)
						at=k;
				}
				//ubNð¨­
				for(int j = at; j!=at-p; j--){
					field[j][q-1]='1';
				}
			}

			// ubNÌÁ
			for(int j = 0; j < limit; j++){
				bool f=false;
				for(int k = 0; k < 5; k++){
					if(field[j][k]!='1'){
						f=true;
						break;
					}
				}
				if(!f){
					for(int k = 0; k < 5; k++)
						field[j][k]='0';
					// Á¦½sæèãÉ éubNðº
					for(int k = j-1; k >= 0; k--){
						for(int l = 0; l < 5; l++){
							field[k+1][l]=field[k][l];
						}
					}
				}
			}
			//for(int j = 0; j < 10; j++){
			//	for(int k = 0; k < 5; k++){
			//		cout<<field[j][k]<<flush;
			//	}
			//	cout<<endl;
			//}
			//cout<<endl;
		}
		int cnt=0;
		for(int i = 0; i < limit; i++){
			for(int j = 0; j < 5; j++){
				if(field[i][j]=='1')
					cnt++;
			}
		}
		cout<<cnt<<endl;
	}

	return 0;
}