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 <bits/stdc++.h>

using namespace std;

char field[5010][5];

void add_block(bool const isPortrait, int const len, int const pos) {
  if(isPortrait) {
    for(int i=0; i<len; i++) {
      field[5000+len-1-i][pos] = '*';
    }
  }
  else {
    for(int i=0; i<len; i++) {
      field[5000][pos+i] = '*';
    }
  }
}

void simulate() {
  for(int k=0; k<5005; k++) {
    for(int j=0; j<5; j++) {
      for(int i=0; i<5005; i++) {
	if(field[i][j] == '.' && field[i+1][j] == '*') {
	  field[i][j] = '*';
	  field[i+1][j] = '.';
	}
      }
    }
  }
}

void erase_blocks() {
  for(int i=0; i<5005; i++) {
    bool ok = true;
    for(int j=0; j<5; j++) {
      ok = ok && field[i][j] == '*';
    }
    if(ok) {
      for(int j=0; j<5; j++) {
	field[i][j] = '.';
      }
    }
    else {
      break;
    }
  }
}

int main() {
  
  int N;
  
  while(cin >> N && N) {
    
    fill(field[0], field[0]+5010*5, '.');
    
    for(int i=0; i<N; i++) {
      int d, p, q; cin >> d >> p >> q; d--; q--;
      add_block((bool)d, p, q);
      simulate();
      erase_blocks();
      simulate();
    }
    
    int ans = 0;
    for(int i=0; i<5000; i++) {
      for(int j=0; j<5; j++) {
	ans += field[i][j] == '*';
      }
    }
    
    cout << ans << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef tuple<int, int> duo;

#define TT template<typename T>
#define ET(T) typename T::value_type
TT inline T sq(T x){return x*x;}
TT inline T In(){T x;cin>>x;return x;}
TT inline int ubnd(T&v,ET(T) x){return upper_bound(begin(v),end(v),x)-begin(v);}
TT inline int lbnd(T&v,ET(T) x){return lower_bound(begin(v),end(v),x)-begin(v);}
TT inline void sort(T&v){sort(begin(v),end(v));}
TT inline void revs(T&v){reverse(begin(v),end(v));}
TT inline void uniq(T&v){sort(v);v.erase(unique(begin(v),end(v)),end(v));}
TT inline void inpt(T&v,int n,function<ET(T)()>f=In<ET(T)>){for(v.reserve(n);n--;v.emplace_back(f()));}
TT inline void show(T&v,string d=" ",string e="\n"){int i=0;for(auto&x:v)i++&&(cout<<d),cout<<x;cout<<e;}
#undef TT
#undef ET

inline void fast_io(){ios::sync_with_stdio(0); cin.tie(0);}
inline int in(){int x;scanf("%d",&x);return x;}
inline string instr(){string x;cin>>x;return x;}
inline ll pow_mod(ll a, ll k, ll m){ll r=1;for(;k>0;a=a*a%m,k>>=1)if(k&1)r=r*a%m;return r;}
inline ll mod_inv(ll a, ll p){return pow_mod(a,p-2,p);}

const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1};
const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1};
const int Mod = 1000000000;

char field[10000][8];
int main()
{
  int n;
  while (n = in()){
    int height[8] = {};
    memset(field, '.', sizeof(field));
    for (int i = 0; i < 10000; i++) field[i][0] = '0', field[i][6] = 0;
    for (int i = 0; i < n; i++){
      int d, p, q;
      cin >> d >> p >> q;
      if (d == 1){
        int h = 0;
        for (int j = 0; j < p; j++) h = max(h, height[q + j]);
        for (int j = 0; j < p; j++) field[h][q + j] = '#';
        for (int j = 0; j < p; j++) height[q + j] = h + 1;
        field[h][0] += p;
      }
      else {
        int h = height[q];
        for (int j = 0; j < p; j++){
          field[h + j][q] = '#';
          field[h + j][0]++;
        }
        height[q] += p;
      }
      bool line_delete = false;
      for (int i = 0; i < 5000; i++){
        if (field[i][0] == '0') break;
        if (field[i][0] != '5') continue;
        memmove(field[i], field[i + 1], 40000);
        i--;
        line_delete = true;
      }
      if (!line_delete) continue;
      for (int i = 1; i <= 5; i++){
        int h = height[i];
        while (h >= 0 && field[h][i] != '#') h--;
        height[i] = h + 1;
      }
    }
    int tot = 0;
    for (int i = 0; i < 5000; i++) tot += field[i][0] - '0';
    cout << tot << "\n";
  }
  return (0);
}

// priority_queue queue deque front top push pop map set stringstream max_element min_element insert count find get tie make_tuple

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

int board[6000][6];

int main() {
	int n;
	while(cin >> n, n) {
		int top[8];

		for(int i = 0; i < 6000; i++)
			for(int j = 0; j < 6; j++)
				board[i][j] = 0;
		for(int i = 0; i < 8; i++)
			top[i] = 0;

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

			if(d == 1) {
				int h_max = -1;
				for(int j = 0; j < p; j++) {
					h_max = max(top[q - 1 + j], h_max);
				}
				for(int j = 0; j < p; j++) {
					board[h_max][q - 1 + j] = 1;
					top[q - 1 + j] = h_max + 1;
				}

				bool flg = true;
				for(int j = 0; j < 5; j++)
					flg &= board[h_max][j];
				if(flg) {
					for(int j = 0; j < 5; j++) {
						board[h_max][j] = 0;
						for(int k = h_max + 1; k < top[j] + 10; k++) {
							board[k - 1][j] = board[k][j];
						}
					}
				}
			}

			else {
				for(int j = 0; j < p; j++) {
					board[top[q - 1] + j][q - 1] = 1;
				}

				int h = top[q - 1];
				top[q - 1] += p;
				int del = 0;
				for(int j = 0; j < p; j++) {
					bool flg = true;
					for(int k = 0; k < 5; k++) {
						flg &= board[h + j][k];
					}
					if(flg) {
						for(int k = 0; k < 5; k++)
							board[h + j - del][k] = board[h + j + 1][k];
						del++;
					}
				}
				for(int j = 0; j < 5; j++) {
					for(int k = h + p - del; k < top[j]; k++) {
						board[k][j] = board[k + del][j];
					}
				}
				for(int j = 0; j < 5; j++)
					top[j] -= del;
			}
			/*
			for(int i = 10; i >= 0; i--) {
				for(int j = 0; j < 5; j++) {
					cout << (board[i][j] ? '*' : '.');
				}
				cout << endl;
			}
			cout << endl;
			// */
		}

		int ans = 0;
		for(int i = 0; i < 6000; i++) {
			for(int j = 0; j < 6; j++) {
				ans += board[i][j];
			}
		}

		cout << ans << endl;
	}
}