Language: C++

use std::cmp;
use std::collections::BTreeMap;
use std::collections::BinaryHeap; // 優先度付きキュー
use std::collections::HashMap; // 連想配列
use std::collections::HashSet; // 連想配列 キーのみ
use std::collections::VecDeque; // キュー or スタック
use std::io::Read;
use std::usize::MAX;

macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() };input_inner!{next, $($r)*}};($($r:tt)*) => {let stdin = std::io::stdin();let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));let mut next = move || -> String{bytes.by_ref().map(|r|r.unwrap() as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect()};input_inner!{next, $($r)*}};}
macro_rules! input_inner {($next:expr) => {};($next:expr, ) => {};($next:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($next, $t);input_inner!{$next $($r)*}};}
macro_rules! read_value {($next:expr, ( $($t:tt),* )) => {( $(read_value!($next, $t)),* )};($next:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()};($next:expr, chars) => {read_value!($next, String).chars().collect::<Vec<char>>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, [ $t:tt ]) => {{let len = read_value!($next, usize);(0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()}};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};}

fn solve() {
    input! {
        n: usize,
        m: usize,
        a: [usize;n],
        operation: [(usize,usize);m]
    }

    let mut a = a;
    a.sort();

    let mut operation = operation;
    operation.sort_by(|x, y| {
        if x.1 > y.1 {
            std::cmp::Ordering::Less
        } else if x.1 == y.1 {
            y.0.cmp(&x.0)
        } else {
            std::cmp::Ordering::Greater
        }
    });

    let mut ans = 0;
    let mut replaced = 0;

    for o in &operation {
        let b = o.0;
        let c = o.1;
        let r_index = bin_search(&a[replaced..n - 1], c);
        if a[r_index + replaced] <= c {
            let new_replaced = cmp::min(b, r_index + 1) + replaced;
            if new_replaced > replaced {
                for i in replaced..new_replaced {
                    ans += c;
                }
            }
            replaced = new_replaced;
        }
    }

    if replaced < n {
        for i in replaced..n {
            ans += a[i];
        }
    }

    println!("{:?}", ans);
}

fn bin_search(slice: &[usize], num: usize) -> usize {
    let mut min = 0;
    let mut max = slice.len() - 1;
    while max - min > 1 {
        let mid = (max + min) / 2;
        if slice[mid] > num {
            max = mid;
        } else {
            min = mid;
        }
    }
    if slice[max] > num {
        return min;
    } else {
        return max;
    }
}

fn main() {
    let thd = std::thread::Builder::new().stack_size(104_857_600);
    thd.spawn(|| solve()).unwrap().join().unwrap();

    /*
    // 入力を一括で読み込む場合
    let mut buf = String::new();
    std::io::stdin().read_to_string(&mut buf).unwrap();
    let mut input = buf.split_whitespace();

    // inputに対しnext()で読み込んでいく
    let q: usize = input.next().unwrap().parse().unwrap();
    */
}

const LARGE_PRIME: u64 = 1_000_000_007;

// @〜でsnippet


Compiler output:
a.cc:12:133: error: too many decimal points in number
   12 | macro_rules! read_value {($next:expr, ( $($t:tt),* )) => {( $(read_value!($next, $t)),* )};($next:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()};($next:expr, chars) => {read_value!($next, String).chars().collect::<Vec<char>>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, [ $t:tt ]) => {{let len = read_value!($next, usize);(0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()}};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};}
      |                                                                                                                                     ^~~~~~~
a.cc:12:400: error: too many decimal points in number
   12 | macro_rules! read_value {($next:expr, ( $($t:tt),* )) => {( $(read_value!($next, $t)),* )};($next:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()};($next:expr, chars) => {read_value!($next, String).chars().collect::<Vec<char>>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, [ $t:tt ]) => {{let len = read_value!($next, usize);(0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()}};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};}
      |                                                                                                                                                                                                                                                                                                                                                                                                                ^~~~~~
a.cc:30:14: error: too many decimal points in number
   30 |             y.0.cmp(&x.0)
      |              ^~~~~~
a.cc:1:1: error: 'use' does not name a type
    1 | use std::cmp;
      | ^~~
a.cc:2:1: error: 'use' does not name a type
    2 | use std::collections::BTreeMap;
      | ^~~
a.cc:3:1: error: 'use' does not name a type
    3 | use std::collections::BinaryHeap; // 優先度付きキュー
      | ^~~
a.cc:4:1: error: 'use' does not name a type
    4 | use std::collections::HashMap; // 連想配列
      | ^~~
a.cc:5:1: error: 'use' does not name a type
    5 | use std::collections::HashSet; // 連想配列 キーのみ
      | ^~~
a.cc:6:1: error: 'use' does not name a type
    6 | use std::collections::VecDeque; // キュー or スタック
      | ^~~
a.cc:7:1: error: 'use' does not name a type
    7 | use std::io::Read;
      | ^~~
a.cc:8:1: error: 'use' does not name a type
    8 | use std::usize::MAX;
      | ^~~
a.cc:10:1: error: 'macro_rules' does not name a type
   10 | macro_rules! input {(source = $s:expr, $($r:tt)*) => {let mut iter = $s.split_whitespace();let mut next = || { iter.next().unwrap() };input_inner!{next, $($r)*}};($($r:tt)*) => {let stdin = std::io::stdin();let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));let mut next = move || -> String{bytes.by_ref().map(|r|r.unwrap() as char).skip_while(|c|c.is_whitespace()).take_while(|c|!c.is_whitespace()).collect()};input_inner!{next, $($r)*}};}
      | ^~~~~~~~~~~
a.cc:11:1: error: 'macro_rules' does not name a type
   11 | macro_rules! input_inner {($next:expr) => {};($next:expr, ) => {};($next:expr, $var:ident : $t:tt $($r:tt)*) => {let $var = read_value!($next, $t);input_inner!{$next $($r)*}};}
      | ^~~~~~~~~~~
a.cc:12:1: error: 'macro_rules' does not name a type
   12 | macro_rules! read_value {($next:expr, ( $($t:tt),* )) => {( $(read_value!($next, $t)),* )};($next:expr, [ $t:tt ; $len:expr ]) => {(0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()};($next:expr, chars) => {read_value!($next, String).chars().collect::<Vec<char>>()};($next:expr, usize1) => {read_value!($next, usize) - 1};($next:expr, [ $t:tt ]) => {{let len = read_value!($next, usize);(0..len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()}};($next:expr, $t:ty) => {$next().parse::<$t>().expect("Parse error")};}
      | ^~~~~~~~~~~
a.cc:14:1: error: 'fn' does not name a type
   14 | fn solve() {
      | ^~
a.cc:63:1: error: 'fn' does not name a type
   63 | fn bin_search(slice: &[usize], num: usize) -> usize {
      | ^~
a.cc:81:1: error: 'fn' does not name a type
   81 | fn main() {
      | ^~
a.cc:96:18: error: found ':' in nested-name-specifier, expected '::'
   96 | const LARGE_PRIME: u64 = 1_000_000_007;
      |                  ^
      |                  ::
a.cc:96:7: error: 'LARGE_PRIME' does not name a type
   96 | const LARGE_PRIME: u64 = 1_000_000_007;
      |       ^~~~~~~~~~~