Lose it!

You are given an array aa consisting of nn integers. Each aiai is one of the six following numbers: 4,8,15,16,23,424,8,15,16,23,42.

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

An array of length kk is called good if kk is divisible by 66 and it is possible to split it into k6k6 subsequences 4,8,15,16,23,424,8,15,16,23,42.

Examples of good arrays:

  • [4,8,15,16,23,42][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][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][4,8,15,16,42,23] (the order of elements should be exactly 4,8,15,16,23,424,8,15,16,23,42);
  • [4,8,15,16,23,42,4][4,8,15,16,23,42,4] (the length of the array is not divisible by 66);
  • [4,8,15,16,23,42,4,8,15,16,23,23][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 nn (1n51051≤n≤5⋅105) — the number of elements in aa.

The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (each aiai is one of the following numbers: 4,8,15,16,23,424,8,15,16,23,42), where aiai is the ii-th element of aa.

Output

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

Examples
input
Copy
5
4 8 15 16 23
output
Copy
5
input
Copy
12
4 8 4 15 16 8 23 15 16 42 23 42
output
Copy
0
input
Copy
15
4 8 4 8 15 16 8 16 23 15 16 4 42 23 42
output
Copy
3


 

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <unordered_set>
#include <unordered_map>
#include <xfunctional>
#define ll long long
#define mod 998244353
using namespace std;
int dir[4][2] = { {0,1},{0,-1},{-1,0},{1,0} };
const int maxn = 1e5 + 5;
const long long inf = 0x7f7f7f7f7f7f7f7f;

int main()
{
    vector<int> p({ 4, 8, 15, 16, 23, 42 });

    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
        a[i] = lower_bound(p.begin(), p.end(), a[i]) - p.begin();
    }

    vector<int> seq(6);
    for (int i = 0; i < n; ++i)
    {
        if (a[i] == 0) 
        {
            ++seq[0];
        }
        else 
        {
            if (seq[a[i] - 1] > 0)
            {
                --seq[a[i] - 1];
                ++seq[a[i]];
            }
        }
    }

    cout << n - seq[5] * 6 << endl;
    return 0;
}

 

 

posted @ 2020-02-21 18:39  DeaL57  阅读(190)  评论(0编辑  收藏  举报