import std.stdio;
import std.c.stdio;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.regex;
import std.typecons;

const INF = int.max / 2;
 
void main() {
    int M, N;
    scanf("%d %d\n", &M, &N);
    int[] P = new int[M];
    foreach (i; 0 .. M) {
        scanf("%d\n", &P[i]);
    }
    int[] C = new int[N];
    int[] E = new int[N];
    foreach (i; 0 .. N) {
        scanf("%d %d\n", &C[i], &E[i]);
    }
    int[][] dp = new int[][](N + 1, M + 1); // N逡ェ逶ョ縺セ縺ァ縺ョ邂ア縺ァ蜷郁ィ亥ョケ驥上′M縺ィ縺ェ繧狗ョア縺ョ縺上∩縺ゅｏ縺帙?縺?■,譛?ｂ螳峨＞縺上∩縺ゅｏ縺帙?蛟、谿オ
    foreach (ref l; dp) l[] = INF;
    dp[0][0] = 0;
    foreach (i; 1 .. N + 1) {
        foreach (j; 0 .. M + 1) {
            dp[i][j] = min(dp[i][j], dp[i - 1][j]);
            dp[i][j] = min(dp[i][j], dp[i - 1][max(0, j - C[i - 1])] + E[i - 1]);
        }
    }
    P.sort!"a > b";
    int[] S = new int[M + 1];
    S[0] = 0;
    foreach (i; 1 .. M + 1) {
        S[i] = S[i - 1] + P[i - 1];
    }
//    writeln(S);
//    foreach (l; dp) l.writeln;
    int Ans = 0;
    foreach (i; 0 .. M + 1) {
        Ans = max(Ans, S[i] - dp[N][i]);
    }
    writeln(Ans);
}