#include <bits/stdc++.h>

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
using ld = long double;
const ul mod = 1000000007;

struct edge {
    ll to, cost;
    edge(ll to = 0, ll cost = 0) : to(to), cost(cost) {}
};

// p : (cost, position)
using p = pair<ll, ll>;

void djikstra(vector<ll> &d, vector<vector<edge>> &g, ll start) {
    ll inf = 1LL << 60;
    fill(d.begin(), d.end(), inf);
    d[start] = 0;

    priority_queue<p, vector<p>, greater<p>> q;
    q.push(p(0, start));

    while (!q.empty()) {
        p now = q.top();
        q.pop();
        ll now_cost = now.first, now_position = now.second;
        if (d[now_position] < now_cost)
            continue;
        for (edge e : g[now_position]) {
            if (d[e.to] > d[now_position] + e.cost) {
                d[e.to] = d[now_position] + e.cost;
                q.push(p(d[e.to], e.to));
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll n, m;
    cin >> n >> m;
    vector<ll> w(n);
    rep(i, n) { cin >> w[i]; }
    vector<p> vl(m);
    rep(i, m) {
        ll l, v;
        cin >> l >> v;
        vl[i] = p(v, l);
    }
    sort(vl.begin(), vl.end(), [](const p &a, const p &b) {
        if (a.first == b.first) {
            return a.second > b.second;
        }
        return a.first < b.first;
    });
    ll l_max = vl[0].second;
    rep(i, m) {
        l_max = max(l_max, vl[i].second);
        vl[i].second = l_max;
    }

    ll ans = mod * mod;
    sort(w.begin(), w.end());
    if (w[n - 1] > vl[0].first) {
        cout << -1 << endl;
        return 0;
    }

    do {
        vector<ll> sum(n + 1);
        sum[0] = 0;
        rep(i, n) { sum[i + 1] = sum[i] + w[i]; }
        vector<ll> d(n);
        vector<vector<edge>> g(n);
        rep(i, n) {
            repd(j, i + 1, n) {
                ll weight = sum[j + 1] - sum[i];
                if (weight < vl[0].first) {
                    g[i].push_back(edge(j, 0));
                    continue;
                }
                if (weight == vl[0].first) {
                    g[i].push_back(edge(j, 0));
                    continue;
                }
                ll length = 0;
                auto iter_lower =
                    upper_bound(vl.begin(), vl.end(), p(weight, 0));
                length = (iter_lower - 1)->second;
                g[i].push_back(edge(j, -length));
            }
        }
        djikstra(d, g, 0);
        ll temp = -d[n - 1];
        ans = min(ans, temp);
    } while (next_permutation(w.begin(), w.end()));

    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define st first
#define nd second
#define pii pair<int,int>
#define mp make_pair
#define pll pair<long long,long long>

using namespace std;

const int nax = 105;
int n;
char s[nax];
int a[nax];

vector<vector<int> > cols;

void solve(){
    cin >> n;
    string x; cin >> x;
    for(int i=1;i<=n;i++) s[i] = x[i - 1];
    for(int i=0;i<=n;i++) cin >> a[i];
    int ile = 1e9;
    for(int i=1;i<=n;i++){
        ile = min(ile, abs(a[i] - a[i - 1]));
    }
    vector<int> fi;
    for(int i=1;i<=ile;i++){
        fi.pb(a[0] / ile);
        if(i <= a[0] % ile) fi.back()++;
    }
    cols.pb(fi);
    for(int i=1;i<=n;i++){
        if(s[i] == '<'){
            // jestesmy wieksi
            vector<int> last = cols.back();
            vector<int> now;
            int rest = a[i];
            for(int j=1;j<=ile;j++){
                now.pb(last[j - 1] + 1);
                rest -= (last[j - 1] + 1);
            }
            int prefix = -1;
            if(now[0] == now[ile - 1]) prefix = ile - 1;
            else{
                for(int j=1;j<ile;j++){
                    if(now[j] < now[j - 1]){
                        prefix = j - 1;
                        break;
                    }
                }
            }
            while(rest--){
                prefix++;
                if(prefix == ile) prefix = 0;
                now[prefix]++;
            }
            cols.pb(now);
        }
        else{
            vector<int> last = cols.back();
            vector<int> now;
            int rest = a[i];
            for(int j=1;j<=ile;j++){
                now.pb(last[j - 1] - 1);
                rest -= (last[j - 1] - 1);
            }
            int prefix = -1;
            if(now[0] == now[ile - 1]) prefix = ile - 1;
            else{
                for(int j=1;j<ile;j++){
                    if(now[j] < now[j - 1]){
                        prefix = j - 1;
                        break;
                    }
                }
            }
            while(rest < 0){
                rest++;
                now[prefix]--;
                prefix--;
                if(prefix == -1) prefix = ile - 1;
            }
            cols.pb(now);
        }
    }
    cout << ile <<"\n";
    for(int j=0;j<ile;j++){
        for(int i=0;i<=n;i++){
            cout << cols[i][j] <<" " ;
        }
        cout << "\n";
    }


}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int tt = 1;
    // cin >> tt;
    while(tt--) solve();
    return 0;
}
