/**
* 以第i个元素结尾且和最大的连续子数组实际上,
* 要么是以第i-1个元素结尾且和最大的连续子数组加上这个元素,
* 要么是只包含第i个元素,
* sum[i] = max(sum[i-1] + a[i], a[i])
*/
public class Main {
private static long solve(int[] arr, int n) {
long maxSum = arr[0];
long sum = arr[0];
boolean flag = false;
for (int i = 0; i < n; i++) {
if (arr[i] < 0) {
maxSum = Math.max(maxSum,arr[i]);
}else {
flag = true;
break;
}
}
if (!flag) {
return maxSum;
}
maxSum = arr[0];
for (int i = 1; i < n; i++) {
if (sum > 0) {
sum += arr[i];
}else {
sum = arr[i];
}
maxSum = Math.max(maxSum,sum);
}
return maxSum;
}
public static void main(String[] arg) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = scan.nextInt();
}
System.out.println(solve(arr,n));
scan.close();
}
}
/**
* 餐桌容纳人数:从小到大排序
* 客人:按消费金额从大到小排序
* 遍历餐桌:每一桌选择消费金额最大且人数小于餐桌容纳人数
* ps:未在OJ测试,不知是否能AC
*/
public class Main {
static class Customer {
int b;
int c;
}
private static int getCost(Customer[] customers,boolean[] flag,int m,int x) {
for (int i = 0; i < m; i++) {
Customer customer = customers[i];
if (!flag[i] && customer.b < x) {
flag[i] = true;
return customer.c;
}
}
return 0;
}
private static long solve(Customer[] customers,int[] arr,int n,int m) {
Arrays.sort(arr);
Arrays.sort(customers, new Comparator<Customer>() {
@Override
public int compare(Customer c1, Customer c2) {
return c2.c - c1.c;
}
});
long ans = 0;
boolean[] flag = new boolean[m];
for (int i = 0; i < n; i++) {
ans += getCost(customers,flag,m,arr[i]);
}
return ans;
}
public static void main(String[] arg) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
int[] arr = new int[n];
Customer[] customers = new Customer[m];
for (int i = 0; i < n; i++) {
arr[i] = scan.nextInt();
}
for (int i = 0; i < m; i++) {
Customer c = new Customer();
c.b = scan.nextInt();
c.c = scan.nextInt();
customers[i] = c;
}
System.out.println(solve(customers,arr,n,m));
scan.close();
}
}