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;

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

#define chmax(a,x) a=max(a,x)

vector<bool> F[5];

void dump() {
  rep(j, 10) {
    rep(i, 5) {
      if(j>=int(F[i].size())) cout << 0;
      else cout << F[i][j];
    }
    cout << endl;
  }
}

bool eraser(int idx) {
  
  rep(i, 5) {
    if((int)F[i].size() <= idx) { return false; }
    if(!F[i][idx]) { return false; }
  }
  
  rep(i, 5)
    F[i].erase(F[i].begin()+idx);
  
  rep(i, 5) {
    while(1) {
      if(!F[i].empty() && !F[i].back()) { F[i].pop_back(); }
      else { break; }
    }
  }
  
  return true;
}

int main() {
  
  for(int N; cin>>N && N;) {
    rep(i, 5) F[i].clear();
    rep(i, N) {
      int type, len, pos; cin >> type >> len >> pos; pos --;
      if(type == 1) {
	int mx = -1;
	REP(i, pos, pos+len) { chmax(mx, int(F[i].size())); }
	REP(i, pos, pos+len) {
	  REP(j, F[i].size(), mx) {
	    F[i].push_back(false);
	  }
	  F[i].push_back(true);
	}
	eraser(mx);
      }
      else {
	rep(i, len) { F[pos].push_back(true); }
	int cnt = 0;
	rep(i, len) { if(!eraser((int)F[pos].size()-1-cnt)) cnt++; }
      }
    }
    int sum = 0;
    rep(i, 5) rep(j, F[i].size()) sum += F[i][j];
    cout << sum << endl;
  }
  return 0;
}

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

using namespace std;

int n;

typedef struct Block
{
	int d;
	int p;
	int q;
} B;

int F[5000][5];
int H[5];

B blocks[1000];

int highest(int r, int l)
{
	int max = 0;
	for(int i=r; i<l; i++)
	{
		if(max < H[i])
		{
			max =H[i];
		}
	}
	return max;
}

void yoko_set(B b)
{
	int hp = highest(b.q, b.p+b.q);
	for(int i=b.q; i<b.p+b.q; i++)
	{
		F[hp][i] =1;
		H[i] = hp+1;
	}
}

void tate_set(B b)
{
	int hp = H[b.q];
	for(int i=hp; i<hp+b.p; i++)
	{
		F[i][b.q] = 1;
	}
	H[b.q] = hp+b.p;
}

void col_clear()
{
	
	int hp = highest(0, 5);
	bool is_ok = false;
	vector<int> cols;
	for(int i=0; i<=hp; i++)
	{
		is_ok =true;
		for(int j=0; j<5; j++)
		{
			if(F[i][j]==1) continue;
			is_ok = false;
			break;
		}
		
		if(is_ok)
		{
			cols.push_back(i);
		}
	}
	
	if(cols.empty()) return;
	
	int c = 0;
	for(int i=0; i<hp; i++)
	{
		if(cols[c]==i)
		{
			c++;
			continue;
		}
		
		for(int j=0; j<5; j++)
		{
			F[i-c][j] = F[i][j];
		}
	}
	
	for(int i=hp-cols.size(); i<hp; i++)
	{
		for(int j=0; j<5; j++)
		{
			F[i][j] = 0;
		}
	}
	
	for(int j=0; j<5; j++)
	{
		int max = 0;
		for(int i=0; i<hp; i++)
		{
			if(F[i][j]==1)
			{
				max = i;
			}
		}
		H[j] =max+1;
	}
}

int main()
{
	while(cin >> n, n)
	{
		for(int i=0; i<n; i++)
		{
			cin >> blocks[i].d;
			cin >> blocks[i].p;
			cin >> blocks[i].q;
			blocks[i].q--;
		}
		
		memset(&F[0][0], 0, sizeof(int)*25000);
		memset(&H[0], 0, sizeof(int)*5);
		
		for(int i=0; i<n; i++)
		{
			if(blocks[i].d == 2)
			{
				tate_set(blocks[i]);
			}
			else
			{
				yoko_set(blocks[i]);
			}
			
			col_clear();
		}
		
		int hp = highest(0, 5);
		int c = 0;
		for(int i=0; i<hp; i++)
		{
			for(int j=0; j<5; j++)
			{
				if(F[i][j]==1) c++;
			}
		}
		cout << c<< endl;
	}
	
	return 0;
}

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

int  hei;
char fld[5002];

void typeW(int s, int l)
{
	int line = 0;
	for(int x=s; x<s+l; x++)
	{
		line |= (1<<x);
	}

	for(int y=hei; y>=0; y--)
	{
		if(fld[y]&line)
		{
			fld [y+1] |= line;
			hei = (y+1>hei?y+1:hei);
			break;
		}
	}
}

void typeH(int s, int l)
{
	int line = (1<<s);
	for(int y=hei; y>=0; y--)
	{
		if(fld[y]&line)
		{
			for(int y2=0; y2<l; y2++)
			{
				fld [y+y2+1] |= line;
				hei = (y+y2+1>hei?y+y2+1:hei);
			}
			break;
		}
	}
}

void eraseline()
{
	for(int y=1; y<=hei; )
	{
		if(fld[y] == 31)
		{
			hei--;
			for(int y2=y; y2<=hei+1; y2++)
			{
				fld[y2] = fld[y2+1];
			}
		}
		else
		{
			y++;
		}
	}
}

int main()
{
	int n,d,p,q,c;
	while(cin >> n, n)
	{
		hei = 0;
		fld[0] = 31;
		for(int y=1; y<5002; y++)
		{
			fld[y] = 0;
		}

		while(n--)
		{
			cin >> d >> p >> q;		
			if(d==1) typeW(q-1,p);
			if(d==2) typeH(q-1,p);
			eraseline();
		}
	
		c=0;
		for(int y=1; y<=hei; y++)
		for(int x=0; x<5;    x++)
		{
			if(fld[y]&(1<<x)) c++;
		}

		cout << c << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <utility>
#include <tuple>
#include <math.h>
#include <iomanip>

using namespace std;

class Sover {
};

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

		int length = n * 5 + 10;
		vector<vector<bool>> grid(length, vector<bool>(5, false));

		for (int i = 0; i < n; i++) {
			int d, p, q;
			cin >> d >> p >> q;
			q--;
			int bottom = length - 1;
			if (d == 1) {
				for (; bottom >= 0; bottom--) {
					bool interrupt = false;
					for (int j = 0; j < p; j++) {
						if (grid[bottom][q+j])interrupt = true;
					}
					if (interrupt)break;
				}

				for (int j = 0; j < p; j++)grid[bottom + 1][q + j] = true;
			}
			else {
				while (bottom >= 0 && !grid[bottom][q])bottom--;

				for (int j = 0; j < p; j++)grid[bottom + 1 + j][q] = true;
			}

			int shift_cnt = 0;
			for (int j = bottom + 1; j < length; j++) {
				bool full = true;
				for (int k = 0; k < 5; k++) {
					if (!grid[j][k]) {
						full = false;
						break;
					}
				}
				if (full) {
					shift_cnt++;
					for (int k = 0; k < 5; k++) {
						grid[j][k] = false;
					}
				}
				else {
					for (int k = 0; k < 5; k++) {
						grid[j - shift_cnt][k] = grid[j][k];
						if (shift_cnt > 0)grid[j][k] = false;
					}
				}
			}
		}

		int res = 0;
		for (int i = 0; i < length; i++) {
			for (int j = 0; j < 5; j++)if (grid[i][j])res++;
		}
		cout << res << endl;
	}
}