#include<bits/stdc++.h>
using namespace std;
#define int long long
const int mod = 1e9+7;
#define inf 1e18
#define spc " "
#define vi vector<int>
#define pb push_back
#define ff first
#define ss second
#define pi pair<int,int>
#define all(v) (v).begin(),(v).end()
#define mk(arr,n,type)  type *arr=new type[n];
#define rrep(i, n) for(int i=n-1;i>=0;i--)
#define rep(i,n) for(int i=0;i<n;i++)
#define repm(i,a,b) for(int i=a ; i<b ; i++)
#define endl "\n"
#ifndef ONLINE_JUDGE
#define debug(x) cerr << #x <<" "; _print(x); cerr << endl;
#else
#define debug(x)
#endif
typedef long long ll;
//#include<ext/pb_ds/assoc_container.hpp>
//#include<ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>

template<class T>void _print(T t) {cerr << t;}
template <class T, class V> void _print(pair <T, V> p) {cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}";}
template <class T> void _print(vector <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(set <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T> void _print(multiset <T> v) {cerr << "[ "; for (T i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
template <class T, class V> void _print(unordered_map <T, V> v) {cerr << "[ "; for (auto i : v) {_print(i); cerr << " ";} cerr << "]";}
vector<ll> sieve(int n) {int*arr = new int[n + 1](); vector<ll> vect; for (int i = 2; i <= n; i++)if (arr[i] == 0) {vect.push_back(i); for (int j = 2 * i; j <= n; j += i)arr[j] = 1;} return vect;}

void cp(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
        freopen("error.txt","w",stderr);
        cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    #endif
}

void solve(){
    
    int n;
    cin>>n;

    vi nums(n);

    unordered_map<int,int> unmap;

    rep(i,n)cin>>nums[i];

    int ans = 0;

    unmap[nums[0]]++;

    for(int i = 1 ; i<n ; i++){
        ans+=(i - unmap[nums[i]]);
        unmap[nums[i]]++;
    }

    cout<<ans<<endl;
}                   


int32_t main(){
    cp();
    int t = 1;
    // cin>>t;

    while(t--)
        solve();

        return 0;
}



//ll min_len[1<<8];
//void __(){
//    _(ll,n);
//    _(ll,m);
//    _(vl,a,n);
//    _(vpll,b,m);
//    rep(bm,1<<n){
//        ll w = 0;
//        rep(i,n)
//            if(bm&(1<<i))
//                w += a[i];
//        rep(i,m){
//            if(b[i].Y < w)
//                min_len[bm] = max(min_len[bm],b[i].X);
//        }
//    }
//    rep(i,n)
//        if(min_len[1<<i] != 0){
//            print -1;
//            return;
//        }
//    vi order(n);
//    iota(all(order),0);
//    ll ans = INF;
//    do{
//        vl x(n);
//        rep(i,1,n-1){
//            x[i] = x[i-1];
//            ll bm = 1<<order[i];
//            per(j,i){
//                bm |= 1<<order[j];
//                x[i] = max(x[i],min_len[bm]+x[j]);
//            }
//        }
//        ans = min(ans,x.back());
//    }while(next_permutation(all(order)));
//    print ans;
//}
//
#include <numeric>
#include <iomanip>
#include <algorithm>
#include <cassert>
#include <vector>
#include <utility>
#include <iostream>
#include <string>
#define REP_INT(i,l,r) for(int i = (l); i <= (r); ++i)
#define REP_ZERO_INT(i,r) for(int i = 0; i < (r); ++i)
#define GET_REP_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define rep(...) GET_REP_MACRO(__VA_ARGS__,REP_ANY,REP_INT,REP_ZERO_INT)(__VA_ARGS__)
#define PER_ZERO_INT(i,r) for(int i = (r)-1; i >= 0; --i)
#define GET_PER_MACRO(_1,_2,_3,_4,NAME,...) NAME
#define per(...) GET_PER_MACRO(__VA_ARGS__,PER_ANY,PER_INT,PER_ZERO_INT)(__VA_ARGS__)
#define all(v) (v).begin(), (v).end()
#define sz(v) ll(v.size())
#define X first
#define Y second
#define T1 template<typename T> static
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<int> vi;
const ll INF = ll(2e18) + 666;
T1 ostream& operator<<(ostream& stream, const vector<T>& t);
template <typename F, typename S> static
istream& operator>>(istream& stream, pair<F, S>& t){
    return stream >> t.first >> t.second;
}
T1 istream& read(T, T, istream& = cin);
T1 istream& operator>>(istream& stream, vector<T>& t){
    return read(all(t), stream);
}
T1 istream& read(T b, T e, istream& stream){
    for(T it = b; it != e; ++it)
        stream >> *it;
    return stream;
}
struct _print {
    string sep,end;
    bool space;
    ostream &stream;
    _print(string _sep=" ",string _end="\n",
            ostream &_stream = cout) 
        : sep(_sep),end(_end),space(false),
            stream(_stream) {}
    ~_print() { stream << end; }

    template <typename T>
        _print &operator , (const T &t) {
            if (space) stream << sep;
            space = true;
            stream << t;
            return *this;
        }
};
#define print _print(),
#define INPUT_WITHOUT_INIT(type,name) type name; cin >> name
#define _IWI(type,name,...) type name(__VA_ARGS__); cin >> name
#define GET_INPUT_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME
#define _(...) GET_INPUT_MACRO(__VA_ARGS__,_IWI,_IWI,_IWI,_IWI,_IWI,_IWI,INPUT_WITHOUT_INIT)(__VA_ARGS__)
ll min_len[1<<8];
void __(){
    _(ll,n);
    _(ll,m);
    _(vl,a,n);
    _(vpll,b,m);
    rep(bm,1<<n){
        ll w = 0;
        rep(i,n)
            if(bm&(1<<i))
                w += a[i];
        rep(i,m){
            if(b[i].Y < w)
                min_len[bm] = max(min_len[bm],b[i].X);
        }
    }
    rep(i,n)
        if(min_len[1<<i] != 0){
            print -1;
            return;
        }
    vi order(n);
    iota(all(order),0);
    ll ans = INF;
    do{
        vl x(n);
        rep(i,1,n-1){
            x[i] = x[i-1];
            ll bm = 1<<order[i];
            per(j,i){
                bm |= 1<<order[j];
                x[i] = max(x[i],min_len[bm]+x[j]);
            }
        }
        ans = min(ans,x.back());
    }while(next_permutation(all(order)));
    print ans;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
        __();
}

