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<cstring>
#include<algorithm>
using namespace std;
int x[10000][5], m[5], maxn, sum;
int n;
int a, b, c;
int main() {
	while (true) {
		sum = 0;
		maxn = 0;
		memset(x, 0, sizeof(x));
		memset(m, 0, sizeof(m));
		cin >> n;
		if (n == 0) { break; }
		for (int h = 0; h < n; h++) {
			cin >> a >> b >> c;
			c--;
			if (a == 1) {
				maxn = 0;
				for (int j = c; j < c + b; j++) {
					maxn = max(maxn, m[j]);
				}
				for (int j = c; j < c + b; j++) {
					x[maxn][j] = 1;
					m[j] = maxn + 1;
				}
			}
			else {
				for (int j = 0; j < b; j++) {
					x[m[c] + j][c] = 1;
				}
				m[c] += b;
			}
			for (int i = 0; i < 10000; i++) {
				if (x[i][0] + x[i][1] + x[i][2] + x[i][3] + x[i][4] == 5) {
					x[i][0] = 0; x[i][1] = 0; x[i][2] = 0; x[i][3] = 0; x[i][4] = 0;
					for (int j = i; j < 9999; j++) {
						for (int k = 0; k < 5; k++) {
							x[j][k] = x[j + 1][k];
						}
					}
					x[9999][0] = 0; x[9999][1] = 0; x[9999][2] = 0; x[9999][3] = 0; x[9999][4] = 0;
					i--;
					for (int j = 0; j < 5; j++) {
						for (int k = m[j]; k >= 0; k--) {
							if (x[k][j] == 1) {
								m[j] = k + 1; goto E;
							}
						}
						m[j] = 0;
					E:;
					}
				}
			}
		}
		for (int i = 0; i < 10000; i++) {
			for (int j = 0; j < 5; j++) {
				sum += x[i][j];
			}
		}
		cout << sum << endl;
	}
}

C++: #include <iostream>
#include <cstring>
using namespace std;

#define MAX 5000

int t[MAX][5];

void destroy(void){
	for(int i=MAX-1;i>=0;i--){
		int count = 0;
		for(int j=0;j<5;j++){
			count += t[i][j];
		}

		if(count == 0) break;

		else if(count == 5){
			for(int k=i;k>0;k--){
				count = 0;
				for(int j=0;j<5;j++){
					t[k][j] = t[k-1][j];
					t[k-1][j] = 0;
					count += t[k][j];
				}
				if(count == 0) break;
			}
			i++;
		}
	}
}

void setBlock(int d,int p,int q){
	int sj = q - 1;
	int w = d == 1 ? p : 1;
	int h = -1;

	for(int i=0;i<MAX-1;i++){
		for(int j=sj;j<sj+w;j++){
			if(t[i+1][j]){
				h = i;
				break;
			}
		}
		if(h != -1) break;
	}

	if(h == -1) h = MAX-1;

	if(d == 1){
		for(int j=sj;j<sj+p;j++) t[h][j] = 1;
	}
	else{
		for(int i=h;i>h-p;i--) t[i][sj] = 1;
	}
}

int countBlock(void){
	int res = 0;
	for(int i=MAX-1;i>=0;i--){
		int count = 0;
		for(int j=0;j<5;j++){
			count += t[i][j];
			res += t[i][j];
		}
		if(count == 0) break;
	}
	return res;
}

int main(void){
	int n;

	while(cin>>n && n){
		memset(t,0,sizeof(t));
		for(int i=0;i<n;i++){
			int d,p,q;
			cin>>d>>p>>q;
			setBlock(d,p,q);
			destroy();
		}

		cout<<countBlock()<<endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int fld[5005][5];
void put1(int l,int p){
    int curr=5000;
    while(true){
        bool flag=true;
        for(int j=p;j<p+l;j++){
            if(fld[curr][j])flag=false;
        }
        if(flag)curr--;
        else break;
    }

    for(int j=p;j<p+l;j++)fld[curr+1][j]=1;
}
void put2(int l,int p){
    int curr=5000;
    while(true){
        bool flag=true;
        for(int i=curr;i<curr+l;i++)if(fld[i][p])flag=false;
        if(flag)curr--;
        else break;
    }
    for(int i=curr+1;i<curr+1+l;i++)fld[i][p]=1;
}

void del(){
    int curr=0;
    for(int i=0;i<5000;i++){
        bool flag=true;
        for(int j=0;j<5;j++)if(fld[i][j]==0)flag=false;
        if(flag){
            for(int j=0;j<5;j++)fld[i][j]=0;
            continue;
        }
        for(int j=0;j<5;j++)fld[curr][j]=fld[i][j];
        curr++;
    }
}

void debug(int h){
    for(int i=h;i>=0;i--){
        for(int j=0;j<5;j++){
            printf("%d",fld[i][j]);
        }
        puts("");
    }
}

int main(){
    int n;
    while(scanf("%d",&n),n){
        fill_n(*fld,5005*5,0);
        while(n--){
            int d,l,p;
            scanf("%d%d%d",&d,&l,&p);
            p--;
            if(d==1)put1(l,p);
            else put2(l,p);

            del();


            //debug(10);
        }


        int ans=0;
        for(int i=0;i<5000;i++){
            for(int j=0;j<5;j++)ans+=fld[i][j];
        }

        printf("%d\n",ans);
    }
    return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;

int fall(vector<vector<int>>&fi, vector<vector<int>>&pi,int fall_l) {
	int fall_u = 0;
	for (; fall_u < fi.size() - pi.size(); ++fall_u) {
		bool ok = true;
		for (int i = 0; i < pi.size(); ++i) {
			for (int j = 0; j < pi[i].size(); ++j) {
				if (pi[i][j]) {
					if (fi[fall_u + i+1][fall_l + j])ok = false;
				}
			}
		}
		if (!ok) {
			break;
		}
	}
	for (int i = 0; i < pi.size(); ++i) {
		for (int j = 0; j < pi[i].size(); ++j) {
			if (pi[i][j]) {
				fi[fall_u + i][fall_l + j] = true;
			}
		}
	}
	int delline = 0;
	for (int i = fall_u + pi.size() - 1; i >= fall_u; --i) {
		while (all_of(fi[i].begin(), fi[i].end(), [](const int a) {return a == 1; })) {
			transform(fi[i].begin(), fi[i].end(), fi[i].begin(), [](const int a) {return false; });
			rotate(fi.begin(), fi.begin() + i, fi.begin() + i+1); 
			delline += 1;
		}
	}
	return delline;
}

int main() {
	int N;
	while (cin>>N,N) {
		int sum = 0;
		vector<vector<int>>field(5*N, vector<int>(5));
		while(N--){
			int d, p, q; cin >> d >> p >> q; q--;
			sum += p;
			vector<vector<int>>pino(d == 1 ? 1 : p, vector<int>(d == 2 ? 1 : p, 1));
			sum -= fall(field, pino, q) * 5;
		}
		cout << sum << endl;
	}
	return 0;
}