akhauriyash/Code-Regression huggingface dataset:
import scala.io.Source.stdin

object Main {
  val r = Array(2,3,5,7)

  def twoPow(d:Int,s:Int):(Int,Int) = {
    if(d%2 == 1) (d,s)
    else twoPow(d>>1,s+1)
  }

  def maybePrime(i:Int,d:Int,s:Int, n:Int):Boolean = {
    if(i>3) true
    else {
      val a = r(i)
      val x = BigInt(a).modPow(d,n).toLong

      if(x==1 || x==n-1) maybePrime(i+1,d,s,n)
      else repeat(s-1,BigInt(x).modPow(2,n).toLong, i, d, s, n)
    }
  }

  def repeat(j:Int, x:Long, i:Int, d:Int, s:Int, n:Int):Boolean = {
    if(j==0) false
    else if(x==1) false
    else if(x==n-1) maybePrime(i+1,d,s,n)
    else repeat(j-1,BigInt(x).modPow(2,n).toLong,i,d,s,n)
  }

  def isPrime(n:Int):Boolean = {
    if(n<5000) (2 to Math.sqrt(n).toInt).forall(n%_ != 0)
    else if(n%2==0) false
    else {
      val (d,s) = twoPow(n-1,0)
      maybePrime(0,d,s,n)
    }
  }

  def main(args:Array[String]):Unit = {
    val n = for{
      i<-stdin.getLines.drop(1)
      if isPrime(i.toInt)
    } yield i
    println(n.length)
  }
}

Measured memory_bytes: 67520
#include<cstdio>
#include<algorithm>
long long sum[200005];
long long value[200005];
long long map[200005];
int bit[200005];

int getId(long long u,int n){
    int L = 1, R = n;
    int res = -1;
    while(L<=R){
        int M = (L+R)/2;
        if(map[M]<=u){
            res = M;
            L = M+1;
        }
        else R = M-1;
    }
    return res;
}

int preWork(int n){
    int size = 0;
    for(int i = 0; i <= n; i++) map[++size] = value[i];
    std::sort(map+1,map+1+size);
    int p = 1, nSize = 0;
    while(p<=size){
        int np = p;
        while(np+1<=size && map[np+1]==map[p]) np++;
        map[++nSize] = map[p];
        p = np+1;
    }
    for(int i = 0; i <= n; i++) value[i] = getId(value[i],nSize);
    return nSize;
}

void change(int p,int v,int n){
    while(p<=n){
        bit[p] += v;
        p += p&-p;
    }
}

int ask(int p){
    int res = 0;
    while(p!=0){
        res += bit[p];
        p -= p&-p;
    }
    return res;
}

int getSegSum(int l,int r){
    if(l>r) return 0;
    else return ask(r)-ask(l-1);
}

int main(){
    int n; long long k;
    scanf("%d%lld",&n,&k);
    sum[0] = value[0] = 0;
    for(int i = 1; i <= n; i++){
        long long u;
        scanf("%lld",&u);
        sum[i] = sum[i-1]+u;
        value[i] = sum[i]-k*(long long)i;
    }
    int size = preWork(n);
    long long ans = 0;
    for(int i = n; i >= 1; i--){
        change(value[i],1,size);
        ans += (long long)getSegSum(value[i-1],size);
    }
    printf("%lld\n",ans);
    return 0;
}


Measured memory_bytes: 7204
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string S, T;
	cin >> S;
	cin >> T;

	int ans = 0;
	for (size_t i = 0; i < S.size(); i++) {
		if (S[i] != T[i])
			ans++;
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 3920
#include <iostream>
#include <string>
using namespace std;

int main()
{   
    string b;
    cin >> b;
    if(b=="A"){
        cout << "T" << endl;
    return 0;
    }else if(b=="T"){
        cout << "A" << endl;
    return 0;
    }else if(b=="C"){
        cout << "G" << endl;
    return 0;
    }else if(b=="G"){
        cout << "C" << endl;
    return 0;
    }
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;


int main() {
    int d, g;
    cin >> d >> g;
    vector<int> p(d, 0), c(d, 0);
    for (int i = 0; i < d; ++i) {
        cin >> p[i] >> c[i];
    }

    int minc = 1010;
    for (int bit = 0; bit < 1 << d; ++bit) {
        int score = 0, cnt = 0;
        for (int i = 0; i < d; ++i) {
            if (bit >> i & 1) {
                score += 100 * (i + 1) * p[i] + c[i];
                cnt += p[i];
            }
        }

        for (int i = d - 1; i >= 0; --i) {
            if (score >= g) break;
            if (bit >> i & 1) continue;

            int pt = 100 * (i + 1);
            int num = min((g - score + pt - 1) / pt, p[i] - 1);
            score += pt * num;
            cnt += num;
        }


        if (score >= g) minc = min(minc, cnt);
    }

    cout << minc << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long
int dp[100005][4];
int a[100005][3];
int n;

int func(int i, int prev)
{
    if(i==n) return 0;
    if(dp[i][prev]!=-1) return dp[i][prev];
    int ans=0;
    if(prev!=0) ans=max(ans, a[i][0]+func(i+1, 0));
    if(prev!=1) ans=max(ans, a[i][1]+func(i+1, 1));
    if(prev!=2) ans=max(ans, a[i][2]+func(i+1, 2));
    dp[i][prev]=ans;
    return dp[i][prev];
}

main()
{
    fastio;
    cin>>n;
    for(int i=0; i<n; i++) {
        for(int j=0; j<3; j++) {
            cin>>a[i][j];
        }
    }
    memset(dp, -1, sizeof(dp));
    cout<<func(0, 3);
}


Measured memory_bytes: 10368
#include<iostream>
#include <algorithm>
#include <string>
#include<vector>
#include <array>
#include<tuple>
#include<queue>
#include<stack>
#include<iomanip>
//AtCoderC++.cpp

using namespace std;


int main() {
	string s; cin >> s;
	string answer = "";
	for (size_t i = 0; i < s.length(); i++) {
		switch (s[i]) {
			case '0':
			case '1':
				answer += s[i];
				break;
			default:
				if (answer != "")answer = answer.erase(answer.length() - 1);
				break;
		}
	}
	cout << answer << endl;
}

Measured memory_bytes: 256
fun main(args: Array<String>) {
    val n = readLine()?.toInt() ?: return
    val L = readLine()?.split(" ")?.map(String::toInt)?.sortedDescending() ?: return
    var total = 0L
    0.until(n - 2).forEach { i ->
        (i + 1).until(n - 1).forEach loop@{ j ->
            val a = L[i]
            val b = L[j]
            var l = j + 1
            var r = n
            while (l + 1 < r) {
                val m = (l + r) / 2
                val c = L[m]
                if (a < b + c) {
                    l = m
                } else {
                    r = m
                }
            }
            if (a < b + L[l]) {
                total += l - j
            }
        }
    }
    println(total.toString())
}


Measured memory_bytes: 40580
n=int(input())
for _ in range(n):
    s=input()
    s=s.replace("-","")
    train=["" for i in range(60)]
    pointer=30
    for i in s:
        if i=="<":
            pointer-=1
        elif i==">":
            pointer+=1
        else:
            train[pointer]=i
    print("".join(train))



Measured memory_bytes: 5620
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<cstdio>
#include<stack>
#include<queue>
#include<fstream>
#include<deque>
#include<iomanip>
#include <numeric>
#include<tuple>
#include <functional>
//#include<bits/stdc++.h>

using namespace std;
#define ok1 printf("ok1\n");
#define ok2 printf("ok2\n");
#define M 1000000000000000000LL
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,s,n) for(int i=(s);i<(n);++i)
#define repr(i,n) for(int i=n-1;i>=0;--i)
#define REPR(i,s,n) for(int i=(s);i>=(n);--(i))
#define all(a) (a).begin(),(a).end()
#define reall(a) (a).rbegin(),(a).rend()
#define pb push_back
#define pf push_front
#define MIN(a,b) a=min((a),(b))
#define MAX(a,b) a=max((a),(b))
#define SIZE(v) (int)v.size()
#define fi first
#define se second
const double pi = acos(-1.0);
typedef vector<int> vi;
typedef vector<string> vs;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef vector<char> vc;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef deque<ll> dll;
typedef pair<ll, ll> P;
typedef vector<P> vP;
using Edge = pair<int, ll>;
using Graph = vector<vector<Edge> >;
const ll mod = 1e9 + 7;
ll dx[4] = { 1,0,-1,0 };
ll dy[4] = { 0,1,0,-1 };
template <typename T>
bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
};
template <typename T>
bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
};
void Pvll(vll v) {
	cout << "------------------------------------------------\n";
	rep(i, v.size()) cout << v[i] << " ";
	cout << endl;
	cout << "------------------------------------------------\n";
}
void Pvvll(vvll v) {
	cout << "------------------------------------------------\n";
	rep(i, v.size()) {
		rep(j, v[i].size()) {
			cout << v[i][j] << " ";
		}
		cout << endl;
	}
	cout << "------------------------------------------------\n";
}

void Ps(string s) {
	cout << "------------------------------------------------\n";
	rep(i, s.size()) cout << s[i] << " ";
	cout << endl;
	cout << "------------------------------------------------\n";
}

void Pvs(vs s) {
	cout << "------------------------------------------------\n";
	rep(i, s.size()) {
		rep(j, s[i].size()) {
			cout << s[i][j] << " ";
		}
		cout << endl;
	}
	cout << "------------------------------------------------\n";
}

void Yes(bool x) {
	if (x) cout << "Yes\n";
	else cout << "No\n";
}

void YES(bool x) {
	if (x) cout << "YES\n";
	else cout << "NO\n";
}

void yes(bool x) {
	if (x) cout << "yes\n";
	else cout << "no\n";
}

void Yay(bool x) {
	if (x) cout << "Yay!\n";
	else cout << ":(\n";
}

ll gcd(ll x, ll y)
{
	ll r;
	while ((r = x % y) != 0)
	{
		x = y;
		y = r;
	}
	return y;
}
ll lcm(ll x, ll y)
{
	x /= gcd(x, y);
	y /= gcd(x, y);
	return (x * y);
}



ll  d, n, m, r, l, k, h, w, ans = 0, ret = M;
bool flag = false, flag2 = false, flag3 = false;
string s,t;


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll x, y;
	cin >> w >> h >> x >> y;
	cout << (double)h*w / 2 << endl;
	if (2 * x == w && 2 * y == h)ans++;
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;

int main(){
  int N;
  cin >> N;
  vector <int> cards(N);
  for (int i = 0; i < N; i++){
    cin >> cards.at(i);
  } 
  
  sort(cards.begin(), cards.end());
  reverse(cards.begin(), cards.end());
  int winner_point = 0, looser_point = 0;
  for (int i = 0; i < cards.size(); i += 2){
    winner_point += cards.at(i);
  }
  for (int j = 1; j < cards.size(); j += 2){
    looser_point += cards.at(j);
  }
  cout << winner_point - looser_point << endl;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main()
{
	int n;
	cin >> n;
	int b, f, r, v;
	int build[4][3][10] = { {{}} };
	for (int i = 0; i < n; i++)
	{
		cin >> b >> f >> r >> v;
		build[b - 1][f - 1][r - 1] += v;
	}
	for (int i = 0; i < 4; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			for (int k = 0; k < 10; k++)
			{
				cout << " ";
				cout << build[i][j][k];
			}
			cout << endl;
		}
		if (i < 3)
		{
			cout<< "####################" << endl;
		}
	}
}


Measured memory_bytes: 3088
#include<string>
using namespace std;
#include <stdio.h>
#include <limits.h>
#include "bits/stdc++.h"
char S[3];
int sum=0;
int x=0;
int y=0;
int z=0;
string str("No");
int main(){
	cin>>S;
	for(int i = 0; i<3; i++){
		if(S[i]=='a'){
			x=1;
		}	
	}
	for(int i = 0; i<3; i++){
		if(S[i]=='b'){
			y=1;
		}	
	}
	for(int i = 0; i<3; i++){
		if(S[i]=='c'){
			z=1;
		}	
	}
	sum=x+y+z;
	if (sum==3){
		str="Yes";
	}
	cout << str;
	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
int main(void){
    int N,A[1000],cou=0;
    scanf("%d",&N);
    for(int i=0;i<N;i++){
       scanf("%d",&A[i]);
       if(A[i]%2==0){
          if((A[i]%3==0)||(A[i]%5==0)){

          }else{
             cou++;
          }
       }
    }
    if(cou!=0){
       printf("DENIED");
    }else{
       printf("APPROVED");
    }



   return 0;
}


Measured memory_bytes: 128
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
int main(){
    while(1){
        int N,M;
        cin>>N>>M;
        if(N==0&&M==0)break;
        int lis1[10002][2];
        int lis2[501]={0};
        int lis3[501]={0};
        lis3[1]=1;
        for(int i=0;i<M;i++)cin>>lis1[i][0]>>lis1[i][1];
        for(int i=0;i<M;i++){
            if(lis1[i][0]==1)lis2[lis1[i][1]]=1,lis3[lis1[i][1]]=1;
            else if (lis1[i][1]==1)lis2[lis1[i][0]]=1,lis3[lis1[i][0]]=1;
        }
        for(int i=1;i<=N;i++){
            if(lis2[i]==1){
                for(int j=0;j<M;j++){
                    if(lis1[j][0]==i)lis3[lis1[j][1]]=1;
                    else if (lis1[j][1]==i)lis3[lis1[j][0]]=1;
                }
            }
        }
        int ans=0;
        for(int i=0;i<=N;i++){
            if(lis3[i]==1)ans++;
        }
        cout<<ans-1<<endl;
    }
    return 0;
}

Measured memory_bytes: 1240
#include<bits/stdc++.h>

using namespace std;
typedef long long int ll;

#define endl '\n'


int main()
{
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    ll n;cin>>n;

    ll arr[n];

    for(ll i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    ll val = arr[0];
    for(ll i=1;i<n-1;i++){
        if(i%2 == 1){
            val-=arr[i];
        }
        else{
            val += arr[i];
        }
    }

    ll ans[n+1];

    ans[0] = (val+arr[n-1])/2;
    //cout<<val<<endl;
    for(ll i=0;i<n;i++){
        ans[i+1] = arr[i]-ans[i];
    }

    for(ll i=0;i<n;i++){
        cout<<2*ans[i]<<" ";
    }cout<<endl;


}









Measured memory_bytes: 2944
#include <stdio.h>
#include <assert.h>
int n,i,j,x,a[10][10],list[200][3],size;
void push(int x,int y,int s){list[size][0]=i;list[size][1]=j;list[size][2]=s;size++;}

int check(int x,int y,int s){
	int d;
	if(s<3 && (x<1 || x>9 || y<1 || y>9)) return 0;
	if(s==3 && (x<2 || x>8 || y<2 || y>8)) return 0;
	d=a[x][y];
	if(a[x-1][y]<d)d=a[x-1][y];
	if(a[x+1][y]<d)d=a[x+1][y];
	if(a[x][y-1]<d)d=a[x][y-1];
	if(a[x][y+1]<d)d=a[x][y+1];
	if(s==1 || d==0)return d;
	if(a[x-1][y-1]<d)d=a[x-1][y-1];
	if(a[x+1][y+1]<d)d=a[x+1][y+1];
	if(a[x+1][y-1]<d)d=a[x+1][y-1];
	if(a[x-1][y+1]<d)d=a[x-1][y+1];
	if(s==2 || d==0)return d;
	if(a[x-2][y]<d)d=a[x-2][y];
	if(a[x+2][y]<d)d=a[x+2][y];
	if(a[x][y-2]<d)d=a[x][y-2];
	if(a[x][y+2]<d)d=a[x][y+2];
	return d;
}

void draw(int x,int y,int s,int d){
	if(size==3 && (x<2 || x>8 || y<2 || y>8)) return;
	a[x][y]+=d;
	a[x-1][y]+=d;
	a[x+1][y]+=d;
	a[x][y-1]+=d;
	a[x][y+1]+=d;
	if(s==1)return;
	a[x-1][y-1]+=d;
	a[x+1][y+1]+=d;
	a[x+1][y-1]+=d;
	a[x-1][y+1]+=d;
	if(s==2)return;
	a[x-2][y]+=d;
	a[x+2][y]+=d;
	a[x][y-2]+=d;
	a[x][y+2]+=d;
}

int search(int nn,int from){
	int i,x,y,s,t;
	for(i=from;i>=0;i--){
		x=list[i][0];y=list[i][1];s=list[i][2];
		//if(a[x][y]==0 && a[x+1][y]>0)return 0;
		if(x<8){
		if(s<3 && a[x+2][y]>0)return 0;
		if(y==1 && a[x+2][y-1]>0)return 0;
		}
		if(y==1 && s<2 && a[x+1][y-1]>0)return 0;
        if(y==8 && s<2 && a[x+1][y+1])return 0;
		t=check(x,y,s);
		if(t>0){
			draw(x,y,s,-t);
			for(;t>0;t--){
				if(nn-t>0){
					if(search(nn-t,i-1)>0){goto answer;}
				}else{
					int p,q;
					for(p=0;p<10;p++)for(q=0;q<10;q++){{if(a[p][q]!=0)goto miss;}}
					goto answer;
					miss:;
				}
				draw(x,y,s,+1);
			}
		}
	}
	return 0;

	answer:
	for(;t>0;t--)printf("%d %d %d\n",y,x,s);
	return 1;
}

int main(void){
	scanf("%d",&n);size=0;
	for(i=0;i<10;i++){for(j=0;j<10;j++){scanf("%d",&a[i][j]);}}
	for(i=1;i<9;i++){
		for(j=1;j<9;j++){
			x=check(i,j,1); if(x==0)continue; push(i,j,1);
			x=check(i,j,2); if(x==0)continue; push(i,j,2);
			x=check(i,j,3); if(x==0)continue; push(i,j,3);
		}
	}
	/*
	 if(a[i][j]>5){
	 fprintf(stderr,"%3d\n",n);
	 for(i=0;i<10;i++){for(j=0;j<10;j++){fprintf(stderr,"%3d",a[i][j]);}fprintf(stderr,"\n");}fprintf(stderr,"\n");
	 assert(0);
	 }*/
	//for(i=0;i<size;i++)printf("%d %d %d\n",list[i][0],list[i][1],list[i][2]);
	search(n,size-1);
	return 0;
}

Measured memory_bytes: 600
#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;
//
// Created by karayuu on 2018-12-28.
//

int main () {
    int h, w;
    cin >> h >> w;

    while (true) {
        string s;
        cin >> s;

        if (s.empty()) {
            break;
        }

        for (int i = 0; i < 2; i++) {
            cout << s << endl;
        }
    }
    cout << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct range {
    int s, e;
};

bool cmp(range a, range b) {
    if(a.e!=b.e) {
        return a.e<b.e;
    } else {
        return a.s<b.s;
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    
    vector<range> vec;
    for(int m=0; m<M; m++) {
        int start, end;
        cin >> start >> end;
        vec.push_back({start, end});
    }

    sort(vec.begin(), vec.end(), cmp);

    int ans=0;
    int cur=0;
    for(int m=0; m<M; m++) {
        if(vec[m].s>=cur) {
            ans++;
            cur = vec[m].e;
        }
    }
    cout << ans << endl;
    
    
    return 0;
    
}

Measured memory_bytes: 1400
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int x[200],y[200];
int n,m,xx,yy;

bool check(int z){
    if(x[n]<z && y[1]>=z)   return true;
    return false;
}

int main(){

    cin >>n>>m>>xx>>yy;
    for(int i=1;i<=n;i++)   cin >> x[i];
    for(int i=1;i<=m;i++)   cin >>y[i];
    sort(x+1,x+1+n);
    sort(y+1,y+1+m);
    for(int z=xx+1;z<=yy;z++){
        if(check(z)){
            cout <<"No War"<<"\n";
            return 0;
        }
    }
    cout <<"War"<<"\n";
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <set>
#include <math.h>
#include <cmath>
#define ll long long
#define lli long long int

using namespace std;
using P = pair<int,int>;

int main(){

  ll A, B, C, K;
  cin >> A >> B >> C >> K;

  ll BC = B+C;
  ll AC = A+C;
  if(K%2 != 0) cout << (B+C)-(A+C) << endl;
  else cout << -1*((B+C)-(A+C)) << endl;

  return 0;
}

Measured memory_bytes: 3640
#include<bits/stdc++.h>
using namespace std;
long long n,m,ans;
inline int read()
{
	int num=0,flag=1;
	char c=getchar();
	for (;c<'0'||c>'9';c=getchar())
	if (c=='-') flag=-1;
	for (;c>='0'&&c<='9';c=getchar())
	num=(num<<3)+(num<<1)+c-48;
	return num*flag;
}
int main()
{
	n=read();
	m=read();
	if (n==1&&m==1)
	{
		printf("1\n");
		return 0;
	}
	if (n==1||m==1)
	{
		ans=max(n,m);
		printf("%lld\n",ans-2);
		return 0;
	}
	ans=n*m-2*(n+m-2);
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)

typedef long long ll;

class PieceOfCake {
 public:
  int id;
  bool is_strowberry;
  PieceOfCake() : id(-1), is_strowberry(false) {}
};
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<PieceOfCake> > cake_map(h, vector<PieceOfCake>(w));
  int id = 0;
  FOR(i, 0, h) {
    string str;
    cin >> str;
    const char *chars = str.c_str();
    FOR(j, 0, w) {
      if (chars[j] == '#') {
        id++;
        PieceOfCake piece;
        piece.id = id;
        piece.is_strowberry = true;
        cake_map[i][j] = piece;
      }
    }
  }
  FOR(i, 0, h) {
    int temp_id = -1;
    FOR(j, 0, w) {
      if (temp_id == -1) {
        if (cake_map[i][j].is_strowberry) {
          temp_id = cake_map[i][j].id;
          for (int jj = 0; jj < j; jj++) {
            cake_map[i][jj].id = temp_id;
          }
        }
        continue;
      }
      if (cake_map[i][j].is_strowberry) {
        temp_id = cake_map[i][j].id;
      } else {
        cake_map[i][j].id = temp_id;
      }
    }
  }
  int ref_i = 0;
  FOR(i, 0, h) {
    if (ref_i == 0 && cake_map[0][0].id == -1) {
      FOR(ii, 0, h) {
        if (cake_map[ii][0].id != -1) {
          ref_i = ii;
          break;
        }
      }
      FOR(ii, 0, ref_i) {
        FOR(j, 0, w) {
          if (j != 0) cout << " ";
          cout << cake_map[ref_i][j].id << " ";
        }
        cout << endl;
        i++;
      }
    }
    if (cake_map[i][0].id != -1) {
      ref_i = i;
    }
    FOR(j, 0, w) {
      if (j != 0) cout << " ";
      cout << cake_map[ref_i][j].id << " ";
    }
    cout << endl;
  }

  return 0;
}

Measured memory_bytes: 1536
/* ****devACE**** */

#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define    endl        '\n'
#define pii         pair<ll int,ll int>
#define vi          vector<ll int>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define sz(x)       (ll int)x.size()
#define hell        1000000007
#define rep(i,a,b)    for(ll int i=a;i<b;i++)
#define lbnd        lower_bound
#define ubnd        upper_bound
#define bs          binary_search
#define mp          make_pair
using namespace std;

#define N  100005

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int TESTS=1;
//    cin>>TESTS;
    while(TESTS--)
    {
        int n,i,j,k;
        cin>>n;
        int a[n],b[n],c[n-1],sum=0;

        for(i=0;i<n;i++){
            cin>>a[i];
            a[i]--;
        }
        for(i=0;i<n;i++){
            cin>>b[i];
            sum+=b[i];
        }
        for(i=0;i<n-1;i++)cin>>c[i];

        for(i=0;i<n-1;i++){
            if(a[i+1]==a[i]+1)sum+=c[a[i]];
        }
        cout<<sum;
    }
    return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define ll long long
int main() {
	//code


	    int n;
	    cin>>n;
	    int arr[n];
	    for(int i=0;i<n;i++)
	    {
	        cin>>arr[i];
	    }
	vector<int>dp(n+1,0);

	dp[1]=0;
	dp[0]=INT_MAX;
	for(int i=2;i<=n;i++)
	{
	int op1=abs(arr[i-1]-arr[i-2])+dp[i-1];
	int op2=(i==2)?INT_MAX:abs(arr[i-1]-arr[i-3])+dp[i-2];
	dp[i]=min(op1,op2);


	}
	cout<<dp[n]<<endl;
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sqr(x) ((x)*(x))
#define int long long
#define mp make_pair
#define uint unsigned
inline char gc(){
    static char buf[100000],*p1=buf,*p2=buf;
    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
#define gc getchar
inline int read(){
	int x = 0; char ch = gc(); bool positive = 1;
	for (; !isdigit(ch); ch = gc())	if (ch == '-')	positive = 0;
	for (; isdigit(ch); ch = gc())	x = x * 10 + ch - '0';
	return positive ? x : -x;
}
inline void write(int a){
    if(a>=10)write(a/10);
    putchar('0'+a%10);
}
inline void writeln(int a){
    if(a<0){
    	a=-a; putchar('-');
	}
	write(a); puts("");
}
const int N=100005;
int n,jb,ans=-4e18,dq,a[N],sum,top,tot,s[N],ss[N];
signed main(){
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;;i++){
		if(i>n){
			cout<<sum<<endl; return 0;
		}
		if(a[i]<0){
			top=i; break;
		}
		sum+=a[i];
	}
	for(int i=top;i<=n;i++)if(a[i]<0){
		s[++tot]=-a[i];
	}else{
		ss[tot]+=a[i];
	}
	for(int i=top;i<=n;i++)jb+=abs(a[i]);
	for(int i=1;i<=tot;i++){
		jb-=s[i]+ss[i];
		ans=max(ans,jb-s[i]-ss[i]+dq);
		dq+=ss[i]-s[i];
	}
	cout<<ans+sum<<endl;
}


Measured memory_bytes: 1792
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;

int main() {
	string s; cin >> s; int k; cin >> k;
	vector<string>a;
	for (int i = 0; i <= k; i++) {
		for (int j = 0; i+j <= s.size(); j++) {
			a.push_back(s.substr(j, i));
		}
	}
	sort(a.begin(), a.end());
	unique(a.begin(), a.end());
	cout << a[k] << endl;

	return 0;
}

Measured memory_bytes: 1660
#include<bits/stdc++.h>
using namespace std;

#define int long long
#define endl "\n"
#define REP(i,a,n) for(int i=a;i<n;++i)
#define REPR(i,a,n) for(int i=a;i>n;--i)
#define RUP(a,b) ((a+b-1)/(b))
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define MOD 1000000007
#define INF LLONG_MAX/2

typedef long long ll;
typedef pair<int,int> Pii;
typedef tuple<int,int,int> Tiii;
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef vector<Pii> VPii;
typedef vector<string> Vs;

template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T> void YesNo(T a) {cout<<(a?"Yes":"No")<<endl;}
template<class T> void YESNO(T a) {cout<<(a?"YES":"NO")<<endl;}
void vin(Vi &v){REP(i,0,(v).size()) cin>>v[i];}
void vin(Vi &v,Vi &v2){REP(i,0,(v).size()) cin>>v[i]>>v2[i];}
void vout(Vi &v){for(int i=0;i<(v).size();i++) cout<<v[i]<<" ";cout<<endl;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}
void uniq(Vi &v){sort(v.begin(),v.end()); v.erase(unique(v.begin(),v.end()),v.end());}
int ctoi(char c){if(c>='0'&&c<='9'){return c-'0';}return 0;}
void accum(Vi &v){REP(i,1,(v).size()) v[i]+=v[i-1];}
bool comp(Pii a,Pii b){
    if(a.second != b.second) return a.second<b.second;
    else return a.first<b.first;
}


void solve(long long N, std::vector<long long> d){
    int ans=0;
    REP(i,0,N){
        REP(j,i+1,N){
            ans += d[i]*d[j];
        }
    }
    cout<<ans<<endl;
}

// Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
signed main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> d(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&d[i]);
    }
    solve(N, std::move(d));
    return 0;
}


Measured memory_bytes: 256
//动态规划青蛙跳
#include <stdio.h>
#include <math.h>
#include <algorithm>

using namespace std;

int n,h[100005],dp[100005];

int main()
{
	scanf("%d",&n);
	for(int i = 1; i <= n; i++)
	{
		scanf("%d",&h[i]);
	}
	for(int i = 1; i <= n; i++)
	{
		if(i == 1)
		{
			dp[i] = 0;
		}
		else if(i == 2)
		{
			dp[i] = abs(h[2] - h[1]);
		}
		else
		{
			dp[i] = min(dp[i-2]+abs(h[i]-h[i-2]),dp[i-1]+abs(h[i]-h[i-1]));
		}
	}
	printf("%d\n",dp[n]);
	return 0;
}

Measured memory_bytes: 896
void main()
{
    long n, k, l;
    rdVals(n, k, l);

    auto road = UnionFind(n);
    foreach (i; 0 .. k)
    {
        long p, q;
        rdVals(p, q);
        --p, --q;
        road.unite(p, q);
    }

    auto rail = UnionFind(n);
    foreach (i; 0 .. l)
    {
        long r, s;
        rdVals(r, s);
        --r, --s;
        rail.unite(r, s);
    }

    long[Pair] cnt;
    foreach (i; 0 .. n)
    {
        ++cnt[Pair(road.root(i), rail.root(i))];
    }

    foreach (i; 0 .. n)
    {
        cnt[Pair(road.root(i), rail.root(i))].write;
        if (i == n - 1) writeln;
        else " ".write;
    }
}

struct Pair
{
    long a, b;
}

struct UnionFind
{
    long[] par, cnt;

    this(long n)
    {
        par.length = n, cnt.length = n;
        foreach (i; 0 .. n)
        {
            par[i] = i;
        }
        cnt[] = 1L;
    }

    long root(long x)
    {
        if (par[x] == x) return x;
        else return par[x] = root(par[x]);
    }

    bool same(long x, long y)
    {
        return root(x) == root(y);
    }

    void unite(long x, long y)
    {
        x = root(x), y = root(y);
        if (x == y) return;
        if (cnt[x] > cnt[y]) swap(x, y);
        cnt[y] += cnt[x];
        par[x] = y;
    }

    long size(long x)
    {
        return cnt[root(x)];
    }
}

enum long mod = 10^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;

T rdElem(T = long)()
if (!is(T == struct))
{
    return readln.chomp.to!T;
}

alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);

T rdElem(T)()
if (is(T == struct))
{
    T result;
    string[] input = rdRow!string;
    assert(T.tupleof.length == input.length);
    foreach (i, ref x; result.tupleof)
    {
        x = input[i].to!(typeof(x));
    }
    return result;
}

T[] rdRow(T = long)()
{
    return readln.split.to!(T[]);
}

T[] rdCol(T = long)(long col)
{
    return iota(col).map!(x => rdElem!T).array;
}

T[][] rdMat(T = long)(long col)
{
    return iota(col).map!(x => rdRow!T).array;
}

void rdVals(T...)(ref T data)
{
    string[] input = rdRow!string;
    assert(data.length == input.length);
    foreach (i, ref x; data)
    {
        x = input[i].to!(typeof(x));
    }
}

void wrMat(T = long)(T[][] mat)
{
    foreach (row; mat)
    {
        foreach (j, compo; row)
        {
            compo.write;
            if (j == row.length - 1) writeln;
            else " ".write;
        }
    }
}

import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;

Measured memory_bytes: 28924
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define pii pair<int,int>
#define pll pair<ll,ll>
#define PB push_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
using namespace std;
const int N=1505;
int n,a[N][N];
short L[N],R[N];
short cnt[N],S[N];
char s[N];
ll ans;
//120692274455805
void Mx(short &x,short y){
	x<y?x=y:233;
}
void Mn(short &x,short y){
	x>y?x=y:233;
}
int main(){
	scanf("%d",&n);
	For(i,1,n){
		scanf("%s",s+1);
		For(j,1,n) a[i][j]=(s[j]=='#'?0:s[j]-'0');
	}
	Rep(i,n,1){
		For(j,1,n)
			S[j]=S[j-1]+a[i][j];
		Rep(j,n+1,1)
			if (a[i][j]){
				L[j]=j;
				R[j]=(R[j+1]?R[j+1]:j);
				ans+=a[i][j]*(S[R[j]]-S[j]);
			}
			else
				L[j]=n+1,R[j]=0;
		Rep(j,i-1,1){
			memset(cnt,0,2*(n+1));
			Rep(k,n,1)
				if (a[j][k]){
					L[k]=(L[k]==n+1?L[k+1]:L[k]);
					R[k]=(R[k+1]==0?R[k]:R[k+1]);
					if (R[k]){
						cnt[L[k]]++; cnt[R[k]+1]--;
						ans+=a[j][k]*(S[R[k]]-S[L[k]-1]);
					}
				}
				else
					L[k]=n+1,R[k]=0;
			For(k,1,n){
				cnt[k]+=cnt[k-1];
				if (!cnt[k]&&a[i][k]){
					For(L,k,n) S[L]-=a[i][k]; 
					a[i][k]=0;
				}
			}
		}
	}
	printf("%lld\n",ans);
} 

Measured memory_bytes: 4352
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

ll nCr[62][62];
void build(){
  nCr[1][0] = 1;
  for (int i = 2; i < 62; i++){
    nCr[i][0] = 1;
    for (int j = 1; j < i; j++){
      nCr[i][j] = nCr[i - 1][j] + nCr[i - 1][j - 1];
    }
  }
}

int main()
{
  int N;
  ll M;

  build();
  while (cin >> N >> M, N){
    int one = 0;
    ll cnt = 0;
    while (cnt + nCr[N + 1][one] < M){
      cnt += nCr[N + 1][one];
      one++;
    }
    ll d = M - cnt;
    while (N > 0){
      if (nCr[N][one] >= d){
        printf("0");
      }
      else {
        printf("1");
        d -= nCr[N][one];
        one--;
      }
      N--;
    }
    puts("");
  }
  return 0;
}

Measured memory_bytes: 1148
#include<bits/stdc++.h>
//g++ -std=c++11 

using namespace std;
typedef long long ll;

#define rep(i,n) for (long long i = 0; i < (n); ++i)
#define DIV 1000000007 //10^9+7

ll N;
string S;

class RollingHash {
	private:
		vector<long long> mod; // ハッシュなので、複数種類のmodで強化する
		const long long b = 1000000007LL; // 素数が望ましい
		int n;
		void init() {
			mod.push_back(1000000009LL);
			mod.push_back(1000003341LL);
		}

		// hs: 文字列のローリングハッシュ値
		// hs: hs[i+1] = hs[i] * b + s[i] (mod m)
		// s = "xyzw"
		// ""     hs[0] = 0 
		// "x"    hs[1] = x * b^0 
		// "xy"   hs[2] = x * b^1 + y * b^0
		// "xyz"  hs[3] = x * b^2 + y * b^1 + z * b^0
		// "xyzw" hs[4] = x * b^3 + y * b^2 + z * b^1 + w * b^0
		// "yz"   y * b^1 + z * b^0 = hs[3] - hs[1] * b^2
		vector<vector<long long> > hs;
		// pw: b^n (mod m)
		vector<vector<long long> > pw;

	public:
		// ローリングハッシュ構築
		// O(n)
		RollingHash(const string &s) : n(s.size()) {
			init();
			hs.resize(mod.size()), pw.resize(mod.size());
			for (int m = 0; m < mod.size(); m++) {
				hs[m].assign(n + 1, 0);//0で初期化
				pw[m].assign(n + 1, 0);//0で初期化
				hs[m][0] = 0;
				pw[m][0] = 1;
				for (int j = 0; j < n; j++) {
					pw[m][j + 1] = pw[m][j] * b % mod[m];
					hs[m][j + 1] = (hs[m][j] * b + s[j]) % mod[m];
				}
			}
		}

		// 部分文字列[l, r)のローリングハッシュ値計算
		// O(1)
		inline long long hash1(int l, int r) {
		return  ((hs[0][r] - hs[0][l] * pw[0][r - l]) % mod[0] + mod[0]) % mod[0];
		}

		inline long long hash2(int l, int r) {
		return  ((hs[1][r] - hs[1][l] * pw[1][r - l]) % mod[1] + mod[1]) % mod[1];
		}
};


int main(){
	cin >> N >> S;

	RollingHash rh(S);


	ll start = 0;
	ll end = S.size();

	while(start + 1 < end) {
		ll mid = (start + end)/2;

		//hash, minipos
		map<pair<ll, ll>, ll> memo;

		bool ok = false;
		rep(i, N - mid + 1) {
			ll hash1 = rh.hash1(i, i + mid);
			ll hash2 = rh.hash2(i, i + mid);
			if(memo.count(make_pair(hash1, hash2)) > 0 && memo[make_pair(hash1, hash2)] <= i - mid) {
				ok = true;
				break;
			}
			if(memo.count(make_pair(hash1, hash2)) == 0) {
				memo[make_pair(hash1, hash2)] = i;
			}
		}

		if(ok) {
			start = mid;
		} else {
			end = mid;
		}
	}
	cout << start << endl;

}



Measured memory_bytes: 3948
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,o,n) for(long long i = o;i<n;i++)
#define oneforall ios::sync_with_stdio(false);cin.tie(0);
#define all(v) (v).begin(),(v).end()
#define ini(...) int __VA_ARGS__; in(__VA_ARGS__)
#define inl(...) long long __VA_ARGS__; in(__VA_ARGS__)
#define ins(...) string __VA_ARGS__; in(__VA_ARGS__)
#define int long long 
const long long inf=1e18;
void in(){} template <typename T,class... U> void in(T &t,U &...u){ cin >> t; in(u...);}
void out(){cout << "\n";} template <typename T,class... U> void out(const T &t,const U &...u){ cout << t; if(sizeof...(u)) cout << " "; out(u...);}
typedef vector<int> vi;
typedef vector<long long> vl;
typedef long long ll;
typedef vector<pair<long, long > > vpll;
typedef vector<pair<int, int > > vpii;
#define FORR(x,arr) for(auto& x:arr)
#define ZERO(a) memset(a,0,sizeof(a))





//3111111111111111111111111111111

	void solve(){



		ini(n);
		ini(k);
		vi morty(n);
		FOR(i,0,n)in(morty[i]);
		int tmp = inf;
		FOR(i,0,1<<n){
			int tmp1 = 0;
			FOR(i1,0,21){
				if(1<<i1 & i)tmp1++;
			}
			if(tmp1 != k)continue;
			int tmpp = 0;
			int tmp_ = 0;
			tmp1 = 0;
			FOR(i1,0,n){
				if(1<<i1 & i){
					tmpp = max(tmp_+1,morty[i1]);
					tmp1+=tmpp-morty[i1];
					tmp_ = tmpp;
					//out(i1,i,tmpp,tmp1,tmp_);
				}
				else{
					tmp_ = max(tmp_,morty[i1]);
				}
			}
			//out(tmp1,31);
			tmp = min(tmp,tmp1);
		}
		out(tmp);			







	}	























int32_t main() {
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall
	oneforall 
	oneforall

	solve();
	


	return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

#define swap(a,b)   do { int c = a; a = b;  b = c; }while(0)

int gcd(int x, int y)
{
    if(y == 0) return x;
    else return gcd(y,x%y);
}

int main(void)
{
    int x, y;

    scanf("%d %d", &x, &y);
    if(y>x) swap(x,y);

    printf("%d\n", gcd(x,y));

    return 0;
}

Measured memory_bytes: 592
#include<bits/stdc++.h>
using namespace std;
int w[108],v[108],n;
long long int dp[105][100008]={0},W;
long long int solve(int i, long long int c)
{
    long long int ans1,ans2;
    //ans1=0;ans2=0;
    if(i==n+1){return 0;}
    if(dp[i][c]!=-1)
    {return dp[i][c];}
    ans1=solve(i+1,c);
    if((c-w[i])>=0){ans2=solve(i+1,c-w[i])+v[i];}
    else{ans2=0;}
    dp[i][c]=max(ans1,ans2);
    return max(ans1,ans2);
 
}
int main()
{
    int i;
    //long long int a;
    cin>>n>>W;
    for(i=0;i<=101;i++)
        for(int j=0;j<=100004;j++)
            dp[i][j]=-1;
        
    for(i=1;i<=n;i++)
    {
        cin>>w[i]>>v[i];
        //cout<<dp[1][i];
    }
    //a=solve(1,W,N);
    cout<<solve(1,W);
}

Measured memory_bytes: 80128
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>


using namespace std;

#define ll long long int

int main(){
    ll h; cin >>h;
    double h1 = log2(h);
    h1++;
   // cout << "h1: " << h1 << endl;
    


    ll ans = 0;
    ans = pow(2, (int)h1) - 1;


    

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

Measured memory_bytes: 256
#include<stdio.h>

void trace(int A[],int N)
{
    int i;
    for(i=0;i<N;i++)
    {
        if(i>0) printf(" ");
        printf("%d",A[i]);
    }
    printf("\n");
}

void insertionSort(int A[],int N)
{
    int i,j,v;
    for(i=1;i<N;i++)
    {
        v=A[i];
        j=i-1;
        while(j>=0 && A[j]>v)
        {
            A[j+1]=A[j];
            j--;
        }
        A[j+1]=v;
        trace(A,N);
    }
}

int main()
{
    int N,i,j;
    int A[100];
    
    scanf("%d",&N);
    for(i=0;i<N;i++) scanf("%d",&A[i]);
    
    trace(A,N);
    insertionSort(A,N);
}


Measured memory_bytes: 2048
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto nuv = readln.split.to!(int[]);
    auto N = nuv[0];
    auto u = nuv[1]-1;
    auto v = nuv[2]-1;

    int[][] T;
    T.length = N;
    foreach (_; 0..N-1) {
        auto ab = readln.split.to!(int[]);
        auto A = ab[0]-1;
        auto B = ab[1]-1;
        T[A] ~= B;
        T[B] ~= A;
    }
    auto LS = new int[](N);
    void walk(int i, int p, int l) {
        LS[i] = l;
        foreach (j; T[i]) if (j != p) walk(j, i, l+1);
    }
    walk(v, -1, 0);

    int solve(int i, int p, int l) {
        if (l >= LS[i]) return LS[i];
        auto r = LS[i]-1;
        foreach (j; T[i]) if (j != p) {
            r = max(r, solve(j, i, l+1));
        }
        return r;
    }
    writeln(solve(u, -1, 0));
}

Measured memory_bytes: 18556
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <cstdlib>
#include <algorithm>
using namespace std;
/*
int dfs(int w,int h,int noww,int nowh,int n,int b){
	++n;
	if(noww>w || nowh>h) return 0;
	if(noww==w && nowh==h) return 1;
	int ret=0;
	n=0;
	if(n)
	ret+=dfs(w,h,++noww,nowh,n,b);
	ret+=dfs(w,h,noww,++nowh,n,b);
	ret+=dfs(w,h,++noww,nowh,n,b);
	ret+=dfs(w,h,noww,++nowh,n,b);
	return ret;
}

int main()u{
	int w,h;
	cin >> w >> h;
	cout << dfs(w,h,1,1,2,0) << endl;
	return 0;
}
*/

int main(){
	int n,k;
	while(cin >> n >> k&&n!=0&&k!=0){
		int maxi=0,pre=0;
		vector<int> seq(n);
		for(int i=0; i<n; ++i){
			cin >> seq[i];
		}
		for(int i=0; i<k; ++i){
			maxi+=seq[i];
			pre+=seq[i];
		}
		for(int i=1; i<n-k+1; ++i){
			pre=pre-seq[i-1]+seq[i+k-1];
			maxi=max(maxi,pre);
		}
		cout << maxi << endl;
	}
	return 0;
}

Measured memory_bytes: 1532
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<list>
#include<cstdint>
#include<bits/stdc++.h>
#include<queue>
#include<set>

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep_r(i, n) for(int i = n-1; i >= 0; i--)
#define rep1(i, n) for(int i = 1; i <= (int)(n); i++)
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((ll)(x).size())
#define bit(n) (1LL<<(n))
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

#define INF bit(60)
#define pb push_back
#define mod 1000000007

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

using namespace std;
using uif=uint_fast64_t;
using ll=long long int;

int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mod;a=1ll*a*a%mod;}while(b>>=1);return ans;}
int qp(int a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};


#define FACSIZE 2002

ll invfac[FACSIZE];
ll fac[FACSIZE];

ll mod_pow(ll ,ll );
ll mod_add(ll ,ll );
ll mod_mul(ll ,ll );
ll mod_div(ll ,ll );
void make_fact(ll );
void make_invfact(ll );



int main(void){
  using pa=pair<ll,ll>;
  ll n,m;
  cin>>n>>m;
  vector<ll> a(n);
  rep(i,n) cin>>a[i];
  vector<ll> b(m);
  vector<ll> c(m);
  priority_queue< pa,vector<pa>,greater<pa> > pq;
  unordered_map<ll,ll> um;
  rep(i,m){
    cin>>b[i]>>c[i];
  }
  for(auto x:a){
    um[x]++;
  }
  for(auto p:um) pq.push(p);
  
  for(ll i=0;i<m;i++){
    ll rep_num=c[i];
    ll rep_cnt_max=b[i];
    ll cnt=0;
    while( !pq.empty() ){
      if(pq.top().second + cnt > rep_cnt_max ) break;
      else if( pq.top().first >= rep_num ) break;
      auto p=pq.top();
      pq.pop();
      cnt+=p.second;
    }
    auto diff = rep_cnt_max - cnt;
    if(!pq.empty() && pq.top().first < rep_num ){
      auto p=pq.top();
      pq.pop();
      p.second -= diff;
      cnt+=diff;
      if(p.second>0) pq.push(p);
    }
    if(cnt>0) pq.push( make_pair( rep_num ,cnt) );
  }
  ll ans=0;
  while(!pq.empty()){
    auto p=pq.top();
    pq.pop();
    //cout << p.first << "," << p.second << endl;
    ans+=p.first*p.second;
  }
  cout << ans << endl;
  return 0;

}

ll mod_pow(ll x,ll r){
  if(r==0) return 1;
  else if(r==1) return x%mod;
  else if(r%2==0){
    ll t=mod_pow(x,r/2)%mod;
    return t*t %mod;
  } 
  else{
    ll t=mod_pow(x,r/2)%mod;
    ll k= t*t %mod;
    return k*x%mod;
  } 
}
ll mod_add(ll a,ll b){
  return (a+b)%mod;
}

ll mod_mul(ll a,ll b){
  return (a*b)%mod;
}

ll mod_div(ll a,ll b){
  return mod_mul(a,mod_pow(b,mod-2));
}

void make_fact(ll n){
  fac[0]=1;
  rep(i,n){
    fac[i+1]=mod_mul(fac[i],i+1);
  }  
}

void make_invfact(ll n){
  invfac[n]= mod_pow( fac[n] ,mod-2);
  for(int i=n-1;i>=0;i--){
    invfac[i]=mod_mul(invfac[i+1],i+1);
  }

}

Measured memory_bytes: 9248
#include<bits/stdc++.h>
typedef long long ll;

using namespace std;

#define REP(i,a,b) for (int i = a; i <= b; i++)
#define ALL(a)              a.begin(), a.end()
#define ALLN(a, n)          (a, a+n)
#define MAX                 10000007
#define MIN                 -10000007



              
int main()
{
  ios::sync_with_stdio(0);
    cin.tie(0);
  #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);
    #endif
      
    int t;
    t=1;
     //  cin>>t;
    while(t--){
            ll n,k=0;
            cin>>n;
            string s;
            cin>>s;
            bool value=false;
            int j=n-1;
            REP(i,0,n-1){
                if(s[i]=='W'){
                    if(value) continue;
                    else{
                        while(s[j]!='R'){
                        j--;
                        if(j<i) {value=true; k--;break;}
                        }k++;swap(s[i],s[j]);
                    }
                }
            }
            cout<<k;

       


             }
             cerr<<"Time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;
    return 0;
}


Measured memory_bytes: 3996
#include "bits/stdc++.h"
 
using namespace std;

using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using vvvll = vector<vector<vector<ll>>>;
using vvvvll = vector<vector<vector<vector<ll>>>>;
using pl4 = pair<ll,ll>;
using str = string;
using vpl4 = vector<pair<ll,ll>>;

#define sz size()
#define be begin()
#define en end()
#define fi first
#define se second
#define pb push_back
#define mp make_pair

#define llin(x) ll (x);cin >>(x);
#define stin(x) str (x);cin >>(x);
#define vllin(x,n) vll (x)(n);FOR(i,0,n-1){cin >>(x)[i];}
#define vllin2(a,b,n) vll (a)(n);vll (b)(n);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i];}
#define vllin3(a,b,c,n) vll (a)(n);vll (b)(n);vll (c)(n);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i]>>(c)[i];}
#define vlling(x,n) (x).assign(n,0);FOR(i,0,n-1){cin >>(x)[i];}
#define vlling2(a,b,n) (a).assign(n,0);(b).assign(n,0);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i];}
#define vlling3(a,b,c,n) (a).assign(n,0);(b).assign(n,0);(c).assign(n,0);FOR(i,0,n-1){cin >>(a)[i]>>(b)[i]>>(c)[i];}
#define vpl4in(x,n) vpl4 (x)((n),mp(0,0));FOR(i,0,n-1){cin >>x[i].fi>>x[i].se;}
#define FOR(i,a,b) for(ll i = a ; i <= b ; i++)
#define rFOR(i,b,a) for(ll i = a; i >= b ; i--)
#define SORT(x) sort(x.be, x.en)
#define rSORT(x) sort(x.rbegin(), x.rend())
#define say(x) cout<<(x);
#define sal(x) cout<<(x)<<endl;
#define says(x) cout<<(x)<<(' ');
#define sas cout<<(' ');
#define sayR(x) cout<<fixed<<setprecision(10)<<(x);
#define salR(x) cout<<fixed<<setprecision(10)<<(x)<<endl;
#define yn(a) cout <<((a)?"yes":"no")<<endl;
#define Yn(a) cout <<((a)?"Yes":"No")<<endl;
#define YN(a) cout <<((a)?"YES":"NO")<<endl;
#define Imp(a) cout <<((a)?"Possible":"Impossible")<<endl;
#define IMP(a) cout <<((a)?"POSSIBLE":"IMPOSSIBLE")<<endl;
#define pow(a,b) ll(pow(a,b))

ll MOD=1000000007;

signed main(){
	llin(n);
	stin(s);
	vll a(n);
	if(s[0]=='.') a[0]=1;
	FOR(i,1,n-1){
		a[i]=a[i-1];
		if(s[i]=='.'){
			a[i]++;
		}
	}
	ll k=n;
	ll l=0;
	FOR(i,1,n-1){
		l=(i-a[i-1])+a[n-1]-a[i-1];
		k=min(k,l);
	}
	k=min(k,min(a[n-1],n-a[n-1]));
	sal(k);
}


Measured memory_bytes: 2180
#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define all(a) a.begin(), a.end()
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,int> pli;
typedef pair<int,int> pii;
#define rep(i,a,b) for(ll i=a ; i<b ; i++)
#define qrep(que, ite) for(auto ite=begin(que) ; ite!=end(que) ; ite++)
const int max_n = 1e5;
const ll mod = 1e9+7;
const ll INF = 1LL<<60;
const int inf = 1e5;
//typedef long double ld;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
int dx[4] = {0,0,1,-1};
int dy[4] = {1,-1,0,0};
ll mo = 998244353;
map<ll,ll> ma;
ll gcd(ll a, ll b) { return a ? gcd(b%a, a) : b; }
int main(){
  int n,m;  cin >> n >> m;
  vector<vector<int> > p(n);
  vector<int> x(m);
  string s[m];
  rep(i,0,m){
    cin >> x[i] >> s[i];
    x[i]--;
    p[x[i]].pb(i);
  }
  int a=0, b=0;
  bool ok[n];
  rep(i,0,n){
    ok[i] = false;
  }
  rep(i,0,m){
    if(s[i]=="AC" && !ok[x[i]]){
      a++;
      b += lower_bound(all(p[x[i]]),i) - p[x[i]].begin();
      ok[x[i]] = true;
    }
  }
  cout << a <<" " << b << endl;
  return 0;
}


Measured memory_bytes: 11648
# -*- coding: utf-8 -*-


def linerSearch(S, key):
    i = 0
    S.append(key)
    while S[i] != key:
        i += 1
    if i == len(S)-1:
        S.pop()
        return False
    S.pop()
    return True

n = int(input())
S = list(map(int, input().split()))
q = int(input())
T = list(map(int, input().split()))

count = 0

for i in range(q):
    if linerSearch(S, T[i]):
        count += 1

print (count)

Measured memory_bytes: 8544
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

const int MAX_N = 1e5+1;

int a[MAX_N], b[MAX_N], c[MAX_N];

int main() {
    int N;
    cin >> N;
    for(int i = 0; i < N; i++) cin >> a[i];
    for(int i = 0; i < N; i++) cin >> b[i];
    for(int i = 0; i < N; i++) cin >> c[i];

    sort(a, a+N);
    sort(b, b+N);
    sort(c, c+N);

    long long ans = 0;
    for(int i = 0; i < N; i++) {
        long long up = lower_bound(a, a+N, b[i]) - a;
        long long down = (c+N)-upper_bound(c, c+N, b[i]);
        ans += up * down;
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 1408
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int main() {
  char *text, *keyword, eof[12]="END_OF_TEXT";
  int count = 0, i;
  text = (char *)malloc(sizeof(char) * 1000);
  keyword = (char *)malloc(sizeof(char) * 100);

  scanf("%s\n", keyword);

  while (1) {
    scanf("%s", text);
    if (!strcmp(text, eof)) break;
    for(i = 0; *(text + i) != '\0'; i++){
      *(text + i)  = tolower(*(text + i));
  } 
    if (!strcmp(text, keyword)) count++;
  } 

  free(text);
  free(keyword);
  
  printf("%d\n", count);
  
  return 0;
}


Measured memory_bytes: 2068
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <map>

using namespace std;

int main() {
	string S;
	cin >> S;
	int num = 0;
	for (auto s : S) {
		if (s == '+') num++;
		else if (s == '-') num--;
	}
	cout << num << endl;
	return 0;
}

Measured memory_bytes: 3580
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする
#include <math.h> //数学関数と数学定数を利用する
#define rep(i,n) for (int i = 0; i < (n); i++)
#define PI 3.141592653589793
using namespace std;
typedef long long ll;
template<class T> void chmax(T &a,T b) { if (a<b) a=b;}
template<class T> void chmin(T &a,T b) { if (a>b) a=b;}
int gcd(int a, int b){//ユークリッドの互除法
  if (a < b) gcd(b,a); //aの方がbよりでかいのが前提
  if (b == 0) return a; //aをbで割り切れたらreturn
  else gcd(b, a % b);
}
const int INF = 1<<30;
const int MOD = 1000000007;
int dx[4] = {0,0,-1,1};
int dy[4] = {-1,1,0,0};

int main(){
  int h,w; cin >> h >> w;
  vector<string> s(h);
  vector<vector<int>> d(h,vector<int>(w,-1));
  rep(i,h) cin >> s[i];
  queue<pair<int,int>> q;

  rep(i,h)rep(j,w){
    if(s[i][j] == '#'){
      q.push({j,i});
      d[i][j] = 0;
    }
  }
  
  while(!q.empty()){
    int x,y;
    tie(x,y) = q.front(); q.pop();
    rep(i,4){
      int nx = x + dx[i], ny = y + dy[i];
      if(nx < 0 || ny < 0 || nx >= w || ny >= h) continue;
      if(d[ny][nx] != -1) continue;
      d[ny][nx] = d[y][x] + 1;
      q.push({nx,ny});
    }
  }
  
  int ans = 0;
  rep(i,h)rep(j,w){
    chmax(ans, d[i][j]);
  }
  
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 13456