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[1000][5], m[5], maxn, sum;
int n;
int a, b, c;
int main() {
	while (true) {
		sum = 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 < 1000; 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 < 999; j++) {
						for (int k = 0; k < 5; k++) {
							x[j][k] = x[j + 1][k];
						}
					}
					x[999][0] = 0; x[999][1] = 0; x[999][2] = 0; x[999][3] = 0; x[999][4] = 0;
					i--;
					m[0]--; m[1]--; m[2]--; m[3]--; m[4]--;
				}
			}
		}
		for (int i = 0; i < 1000; i++) {
			for (int j = 0; j < 5; j++) {
				sum += x[i][j];
			}
		}
		cout << sum << endl;
	}
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int main()
{
	for(int n;cin>>n,n;){
		vvi field(5000,vi(5));
		vi top(5);
		rep(i,n){
			int d,p,q; cin>>d>>p>>q;
			if(d==1){
				int t=*max_element(top.begin()+q-1,top.begin()+q-1+p);
				rep(i,p){
					field[t][q-1+i]=1;
					top[q-1+i]=t+1;
				}
			}
			else{
				rep(i,p)
					field[top[q-1]+i][q-1]=1;
				top[q-1]+=p;
			}
		}
		int res=0;
		rep(i,field.size())
			res+=count(allof(field[i]),1)%5;
		cout<<res<<endl;
	}
	
	return 0;
}

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

typedef long long ll;
typedef pair<int, int> P;

#define pb push_back
#define rep(i, a, n) for(int i = (a); i < (n); i++)
#define dep(i, a, n) for(int i = (a); i >= (n); i--)
#define mod 1e9+7

__attribute__((constructor))
void initial() {
  cin.tie(0);
  ios::sync_with_stdio(false);
}

int main() {
  int n;
  while(cin >> n) {
    if(!n) return 0;
    int m[5][5005] = {};
    int gou = 0;
    rep(i, 0, n) {
      int d, p, q;
      cin >> d >> p >> q;
      gou += p;
      if(d == 1) {
        dep(j, 4998, 0) {
          int f = 0;
          rep(k, q - 1, p) {
            if(m[k][j]) {
              rep(l, q - 1, p) {
                m[l][j + 1] = 1;
              }
              f = 1;
              break;
            }
          }
          if(f) break;
          if(j == 0) {
            rep(l, q - 1, p) {
              m[l][j] = 1;
            }
            break;
          }
        }
      }else {
        dep(j, 4998, 0) {
          int f = 0;
          if(m[q - 1][j - (p - 1)]) {
            rep(k, 0, p) {
              m[q - 1][j + k] = 1;
              f = 1;
              break;
            }
          }
          if(f) break;
          if(j == 0) {
            rep(l, 0, p) {
              m[q - 1][j + l] = 1;
            }
            break;
          }
        }
      }
    }

    rep(i, 0, 5005) {
      int f = 0;
      rep(j, 0, 5) {
        if(!m[j][i]) {
          f = 1;
          break;
        }
      }
      if(!f) gou -= 5;
    }


    cout << gou << endl;
  }
}

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

int di[2] = {0, 1};
int dj[2] = {1, 0};
int mat[5010][5];

int main(){
    int n;
    while(cin >> n, n){
        memset(mat, 0, sizeof(mat));
        int ma = 1;
        for(int i = 0; i < n; i++){
            int d, p, q;
            cin >> d >> p >> q;
            d--, q--;
            int put;
            for(put = 5000; put >= 0; put--){
                bool ok = true;
                for(int j = 0; j < p; j++){
                    ok &= mat[put+j*di[d]][q+j*dj[d]] == 0;
                }
                if(!ok) break;
            }
            int h = put+1;
            for(int j = 0; j < p; j++){
                mat[h+j*di[d]][q+j*dj[d]] = 1;
            }
            int acc = 0;
            for(int hh = h; hh < 5000; hh++){
                int sum = 0;
                for(int j = 0; j < 5; j++)  sum += mat[hh][j];
                if(sum == 5){
                    for(int j = 0; j < 5; j++)  mat[hh][j] = 0;
                    acc++;
                }else{
                    for(int j = 0; j < 5; j++)  mat[hh-acc][j] = mat[hh][j];
                }
            }
        }
        int ans = 0;
        for(int i = 0; i < 5000; i++){
            for(int j = 0; j < 5; j++){
                ans += mat[i][j];
            }
        }
        cout << ans << endl;
    }
    return 0;
}