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 <vector>
#include <iostream>

using namespace std;

int n, d, p, q;

int main()
{
    cin >> n;
    
    vector<vetcor<bool> > c(5000, vector<int>(5, false));
    
    for(int i = 0; i < n; i++)
    {
        cin >> d >> p >> q;
        
        if(d == 1)
        {
            int maxh = -1;
            
            for(int j = p - 1; j < p + q - 1; j++)
            {
                int r = 5000;
                
                while(r--)
                {
                    if(c[r][j] == true)
                    {
                        break;
                    }
                }
                
                maxh = max(maxh, r + 1);
            }
            
            for(int j = p - 1; j < p + q - 1; j++)
            {
                c[maxh][j] = true;
            }
        }
        else
        {
            int r = 5000;
            
            while(r--)
            {
                if(c[r][p] == true)
                {
                    break;
                }
            }
            
            for(int j = r + 1; j < r + q + 1; j++)
            {
                c[j][p] = true;
            }
        }
    }
    
    int sum = 0;
    
    for(int i = 0; i < 5000; i++)
    {
        for(int j = 0; j < 5; j++)
        {
            sum += (c[j][p] ? 1 : 0);
        }
    }
    
    cout << sum << endl;
    
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <array>
#include <list>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
#include <cassert>
#include <random>
enum class Direction {
	Horizontal, Vertical
};
Direction as_direction(int d) {
	switch (d) {
	case 1: return Direction::Horizontal;
	case 2: return Direction::Vertical;
	default: throw 0;
	}
}
struct Block {
	Direction direction;
	int length;
	int position;
	int height() const {
		switch (direction) {
		case Direction::Horizontal: return 1;
		case Direction::Vertical: return length;
		default: throw 0;
		}
	}
	int width() const {
		switch (direction) {
		case Direction::Horizontal: return length;
		case Direction::Vertical: return 1;
		default: throw 0;
		}
	}
};
struct Line {
	int state = 0;
	void write(Block block) {
		state |= (1 << (block.position + block.width())) - (1 << block.position);
	}
	bool can_put(Block block) const {
		return (state & ((1 << (block.position + block.width())) - (1 << block.position))) == 0;
	}
	bool is_filled() const {
		return state + 2 == (1 << 6);
	}
};
struct Field {
	std::list<Line> list;
	int block_rest = 0;
	void drop_block(Block block) {
		block_rest += block.height() * block.width();
		auto last = std::find_if(list.rbegin(), list.rend(), [block](const Line& line) {return !line.can_put(block); });
		if (last == list.rend()) {
			while (list.size() < block.height()) {
				list.push_back(Line());
			}
			for (auto itr = list.begin(); std::distance(list.begin(), itr) < block.height(); ++itr) {
				itr->write(block);
			}
			auto itr = list.begin();
			for (auto i = 0; i < block.height(); ++i) {
				if (itr->is_filled()) {
					itr = list.erase(itr);
					block_rest -= 5;
				}
				else {
					++itr;
				}
			}
		}
		else if (last == list.rbegin()) {
			for (auto i = 0; i < block.height(); ++i) {
				auto line = Line();
				line.write(block);
				list.push_back(line);
			}
			for (auto itr = std::prev(list.end(), block.height()); itr != list.end();) {
				if (itr->is_filled()) {
					itr = list.erase(itr);
					block_rest -= 5;
				}
				else {
					++itr;
				}
			}
		}
		else {
			auto rev_count = std::distance(list.rbegin(), last);
			for (; rev_count < block.height(); ++rev_count) {
				list.push_back(Line());
			}
			auto itr = std::prev(list.end(), rev_count);
			for (auto i = 0; i < block.height(); ++i) {
				itr->write(block);
				if (itr->is_filled()) {
					itr = list.erase(itr);
					block_rest -= 5;
				}
				else {
					++itr;
				}
			}

		}
	}
	int block_count() const { return block_rest; }
};
int main() {
	while (true) {
		int n; std::cin >> n; if (n == 0) break;
		Field field;
		for (auto i = 0; i < n; ++i) {
			int d, l, p; std::cin >> d >> l >> p;
			Block block{ as_direction(d), l, p };
			field.drop_block(block);
		}
		std::cout << field.block_count() << '\n';
	}
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <bitset>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF 1145141919
#define MOD 1000000007

int N;
int W[10000];
int H;

signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  while (cin >> N) {
    if (N == 0) break;
    rep(i, 10000) W[i] = 0;

    H = 0;
    rep(i, N) {
      int d, p, q;
      cin >> d >> p >> q;
      int mask = 1;
      if (d == 1) {
        mask = (1<<p)-1;
        p = 1;
      }
      mask <<= (q-1);
      rep(_, p) {
        int h = H;
        for (int y=H; y>=0; y--) {
          if ((W[y] & mask) == 0) h = min(h, y);
          else break;
        }
        if (h == H) H++;
        W[h] |= mask;
      }
    }
    int s = 0;
    rep(i, H) {
      //cout<<bitset<5>(W[i])<<"\n";
      if (W[i] == 31) continue;
      s += __builtin_popcount(W[i]);
    }
    cout << s << "\n";
  }
  return 0;
}