#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<ll, ll>;
using Pld = pair<ld, ld>;
using Vec = vector<ll>;
using VecP = vector<P>;
using VecB = vector<bool>;
using VecC = vector<char>;
using VecD = vector<ld>;
using VecS = vector<string>;
template <class T>
using Vec2 = vector<vector<T>>;
#define REP(i, m, n) for(ll i = (m); i < (n); ++i)
#define REPN(i, m, n) for(ll i = (m); i <= (n); ++i)
#define REPR(i, m, n) for(ll i = (m)-1; i >= (n); --i)
#define REPNR(i, m, n) for(ll i = (m); i >= (n); --i)
#define rep(i, n) REP(i, 0, n)
#define repn(i, n) REPN(i, 1, n)
#define repr(i, n) REPR(i, n, 0)
#define repnr(i, n) REPNR(i, n, 1)
#define all(s) (s).begin(), (s).end()
#define pb push_back
#define fs first
#define sc second
template <class T1, class T2>
bool chmax(T1 &a, const T2 b){if(a < b){a = b; return true;} return false;}
template <class T1, class T2>
bool chmin(T1 &a, const T2 b){if(a > b){a = b; return true;} return false;}
ll pow2(const int n){return (1LL << n);}
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for (const T &i : v) os << i << ' ';
    return os;
}
void co() { cout << '\n'; }
template <class Head, class... Tail>
void co(Head&& head, Tail&&... tail) {
    cout << head << ' ';
    co(forward<Tail>(tail)...);
}
void ce() { cerr << '\n'; }
template <class Head, class... Tail>
void ce(Head&& head, Tail&&... tail) {
    cerr << head << ' ';
    ce(forward<Tail>(tail)...);
}
void sonic(){ios::sync_with_stdio(false); cin.tie(0);}
void setp(const int n){cout << fixed << setprecision(n);}
constexpr int INF = 1000000001;
constexpr ll LINF = 1000000000000000001;
constexpr ll MOD = 1000000007;
constexpr ll MOD_N = 998244353;
constexpr ld EPS = 1e-11;
const double PI = acos(-1);

struct S {
    ll l, v;
    
    bool operator>(const S &rhs) const {
        return v > rhs.v;
    }
    bool operator<(const S &rhs) const {
        return v < rhs.v;
    }
    bool operator==(const S &rhs) const {
        return v == rhs.v;
    }

    void input() {
        cin >> l >> v;
    }


};


int main(void) {
    ll n, m;
    cin >> n >> m;
    Vec w(n);
    rep(i, n) cin >> w[i];
    ll max_w = *max_element(all(w));

    vector<S> ss(m);
    rep(i, m) ss[i].input();
    rep(i, m) {
        if(max_w > ss[i].v) {
            co(-1);
            return 0;
        }
    }

    sort(all(ss));
    Vec max_l(m);
    rep(i, m) {
        max_l[i] = ss[i].l;
        if (i) chmax(max_l[i], max_l[i - 1]);
    }

    Vec p(n);
    rep(i, n) p[i] = i;
    ll ans = LINF;
    do {
        Vec cs(n);
        rep(i, n) {
            cs[i] = w[p[i]];
            if (i) cs[i] += cs[i - 1];
        }
        // ce(cs);

        Vec q(n, 0);
        rep(i, n) {
            REP(j, i + 1, n) {
                ll pos = lower_bound(all(ss), S{0, cs[j]}) - ss.begin();
                if (pos == 0) continue;
                chmax(q[j], q[i] + max_l[pos - 1]);
            }
            REP(j, i + 1, n) cs[j] -= w[p[i]];
        }
        chmin(ans, q.back());
        // ce(ans);
    } while (next_permutation(all(p)));
    co(ans);

    return 0;
}
//include <atcoder>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <tuple>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#define flush fflush(stdout)
#define endl '\n'
#define all(v) v.begin(), v.end()
using namespace std;
//using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pl;
const int mod1 = (int)1e9 + 7, mod2 = (int)998244353;
const int INF = (int)1e9;
const ll LINF = (ll)1e18;
const int di[8] = {1, 0, -1, 0, 1, 1, -1, -1}, dj[8] = {0, 1, 0, -1, -1, 1, -1, 1};

#define rep0(i, n) for (i = 0; i < n; i++)
#define rep1(i, a, b) for (i = a; i < b; i++)

template <typename T>
T my_abs(T x){
    return (x >= 0)? x : -x;
}

template <typename T>
void chmax(T &a, T b){
    a = max(a, b);
}

template <typename T>
void chmin(T &a, T b){
    a = min(a, b);
}

ll gcd(ll a, ll b){
    if (a > b) return gcd(b, a);
    if (a == 0) return b;
    return gcd(b % a, a);
}

// --------------------------------------------------------------------------------

int main(void){
    int i, j;
    int n, a[200003];

    cin >> n;
    rep0(i, n){
        cin >> a[i];
    }

    ll amax, asum, basesum;
    amax = 0;
    asum = 0;
    basesum = 0;
    rep0(i, n){
        chmax(amax, (ll)a[i]);
        asum += a[i];
        basesum += asum;
        cout << basesum + amax * (i + 1) << endl;
    }

    return 0;
}