Lose it!

Link: CodeForces-565-C
Description:
You are given an array a consisting of n integers. Each ai is one of the six following numbers: 4,8,15,16,23,42.

Your task is to remove the minimum number of elements to make this array good.

An array of length k is called good if k is divisible by 6 and it is possible to split it into k6 subsequences 4,8,15,16,23,42.

Examples of good arrays:

[4,8,15,16,23,42] (the whole array is a required sequence);
[4,8,4,15,16,8,23,15,16,42,23,42] (the first sequence is formed from first, second, fourth, fifth, seventh and tenth elements and the second one is formed from remaining elements);
[] (the empty array is good).
Examples of bad arrays:

[4,8,15,16,42,23] (the order of elements should be exactly 4,8,15,16,23,42);
[4,8,15,16,23,42,4] (the length of the array is not divisible by 6);
[4,8,15,16,23,42,4,8,15,16,23,23] (the first sequence can be formed from first six elements but the remaining array cannot form the required sequence).
Input
The first line of the input contains one integer n (1≤n≤5⋅105) — the number of elements in a.

The second line of the input contains n integers a1,a2,…,an (each ai is one of the following numbers: 4,8,15,16,23,42), where ai is the i-th element of a.

Output
Print one integer — the minimum number of elements you have to remove to obtain a good array.

Examples
input

5
4 8 15 16 23

output

5

input

12
4 8 4 15 16 8 23 15 16 42 23 42

output

0

input

15
4 8 4 8 15 16 8 16 23 15 16 4 42 23 42

output

3

Intentional analysis:
The most difficult path of this problem is the order of the numbers is fixed.So you can not just find the minimum number of 4,8,15,16,23,42,and I had to say that is my first try,without doubt,wrong answer.But how to make sure that the order in which you find the number is fixed?My method is to use some numbers to mark the appearance of 4,8,15,16,23,42.Only 4 has appeared, the number of 8 can be increased by one, and the number of 4 should reduced by one.And so on.The final number of 42 is the answer.See the code.

Click to see Chinese Intentional analysis 要保证找出来的最长序列的顺序一定是`4,8,15,16,23,42`,这个一定要看清。我的办法就是用几个数标记他们的出现次数。只有当4出现的次数不为0的时候8出现,那么8出现的次数才能加一,而且此时4出现的次数就得减一。以此类推,最后得到的42出现的次数代表的就是能找到的最长序列的长度的1/6。

Code:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n, a, mid = 0;
    cin >> n;
    ll f1 = 0, f2 = 0, f3 = 0, f4 = 0, f5 = 0, f6 = 0;
    for (ll i = 0; i < n; i++)
    {
        cin >> a;
        if (a == 4)
            f1++;
        if (a == 8 && f1)
        {
            f2++;
            f1--;
        }
        if (a == 15 && f2)
        {
            f3++;
            f2--;
        }
        if (a == 16 && f3)
        {
            f4++;
            f3--;
        }
        if (a == 23 && f4)
        {
            f5++;
            f4--;
        }
        if (a == 42 && f5)
        {
            f6++;
            f5--;
        }
    }
    cout << n - f6 * 6 << endl;
}