akhauriyash/Code-Regression huggingface dataset:
import sys
input = sys.stdin.buffer.readline
import heapq

def dijkstra(n, start, edge):
    dist = [float("inf")]*n
    dist[start] = 0
    que = [(0, start)]
    while que:
        d, v = heapq.heappop(que)
        if dist[v] < d:
            continue
        for nv, nd in edge[v]:
            if dist[nv] > d + nd:
                dist[nv] = d + nd
                heapq.heappush(que, (dist[nv], nv))
    return dist
    
n, m, s, t = map(int, input().split())
G_yen = [[] for _ in range(n+1)]
G_snuuk = [[] for _ in range(n+1)]
for _ in range(m):
    u, v, a, b = map(int, input().split())
    G_yen[u].append((v, a))
    G_yen[v].append((u, a))
    G_snuuk[u].append((v, b))
    G_snuuk[v].append((u, b))
dijk_yen = dijkstra(n+1, s, G_yen)
dijk_snuuk = dijkstra(n+1, t, G_snuuk)
L = []
U = 10**15
now = U
for i in range(n, 0, -1):
    now = min(now, dijk_yen[i]+dijk_snuuk[i])
    L.append(U-now)
print(*L[::-1], sep="\n")

Measured memory_bytes: 130904
# include <iostream>
# include <string>
# include <algorithm>
# include <vector>
# include <cstring>
# include <stdio.h>
# include <map>
# include <queue>

# define ll long long
# define pii pair<int,int>
# define FOR(a,b) for(int a=1; a<=b; a++)
# define REP(a,b) for(int a=0; a<b; a++)
# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)

using namespace std;

bool flag[505];
int N, H, par[505], indeg[505], outdeg[505], ccsize[505];

int findpar(int x){
    return x == par[x] ? x : par[x] = findpar(par[x]);
}

void merge(int u,int v){
    u = findpar(u);
    v = findpar(v);

    ccsize[v]++;
    if(u != v){
        ccsize[v] += ccsize[u];
        par[u] = v;
    }
}

int main(){

    ios :: sync_with_stdio(false);
    cin >> N >> H;
    REP(i,2*H+1) par[i] = i;

    FOR(i,N){
        int a,b,c,d;
        cin >> a >> b >> c >> d;
        int u = c ? -c : a;
        int v = d ? d : -b;

        u += H;
        v += H;

        outdeg[u]++;
        indeg[v]++;
        merge(u, v);
    }

    bool ok = 1;
    REP(i,2*H+1){
        if(i<H && indeg[i] < outdeg[i]) ok = 0;
        if(i>H && indeg[i] > outdeg[i]) ok = 0;
        if(indeg[i] != outdeg[i]) flag[findpar(i)] = 1;
    }

    REP(i,2*H+1){
        if(ccsize[i]>0 && !flag[findpar(i)]) ok = 0;
    }

    cout << (ok ? "YES" : "NO") << endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <deque>
#include <climits>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
//#include <tuple>

#define INF INT_MAX>>1 
#define SIZE 100010
#define MOD 1000000007
#define rep(i, n) for(int i=0; i<(int)(n); i++)
#define rep2(i, a, b) for(int i=(a); i<(int)(b); i++)

typedef long long ll;

using namespace std;

struct Point { int x, y; };
struct Edge { int to, cost; };
struct Node { int from, to, cost; };

int dx[] ={-1, 0, 0, 1};
int dy[] ={0, -1, 1, 0};

int main(void){
	ll X, Y, Z, K; cin >> X >> Y >> Z >> K;
	vector<ll> x(X), y(Y), z(Z);
	rep(i, X) cin >> x[i];
	rep(i, Y) cin >> y[i];
	rep(i, Z) cin >> z[i];
	
	vector<ll> tmp;
	rep(i, X){
		rep(j, Y){
			tmp.push_back(x[i]+y[j]);
		}
	}
	sort(tmp.begin(), tmp.end(), greater<ll>());

	vector<ll> ans;
	ll size = min(K, (ll)tmp.size());
	rep(i, size){
		rep(j, Z){
			ans.push_back(tmp[i]+z[j]);
		}
	}
	sort(ans.begin(), ans.end(), greater<ll>());

	rep(i, K) cout << ans[i] << endl;

	return 0;
}


Measured memory_bytes: 43240
#include<iostream>
#include<vector>
#include<set>
#include<queue>
#include<map>
#include<algorithm>
#include<cstring>
#include<string>
#include<cassert>
#include<cmath>
#include<climits>
#include<iomanip>
#include<stack>
#include<unordered_map>
#include<bitset>
#include<limits>
using namespace std;
#define MOD 1000000007
#define rep(i,m,n) for(int (i)=(int)(m);i<(int)(n);i++)
#define REP(i,n) rep(i,0,n)
#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(),(hoge).end()
#define M_PI 3.1415926535897932LL;
typedef pair<ll, ll> P;
const long long INF = 1LL << 60;
const long double EPS = 1e-10;
typedef vector<ll> Array;

template<class T> inline bool chmin(T& a, T b) {
	if (a > b) {
		a = b;
		return true;
	}
	return false;
}
template<class T> inline bool chmax(T& a, T b) {
	if (a < b) {
		a = b;
		return true;
	}
	return false;
}

ll popcount3(ll x) {
	x = (x & 0x55555555) + (x >> 1 & 0x55555555);
	x = (x & 0x33333333) + (x >> 2 & 0x33333333);
	x = (x & 0x0F0F0F0F) + (x >> 4 & 0x0F0F0F0F);
	x = (x & 0x00FF00FF) + (x >> 8 & 0x00FF00FF);
	x = (x & 0x0000FFFF) + (x >> 16 & 0x0000FFFF);

	return x;
}

ll dp[1LL << 18][51];

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	ll n;
	cin >> n;
	Array a(n), b(n);
	REP(i, n)cin >> a[i];
	REP(i, n)cin >> b[i];
	REP(i, 1LL << 18)REP(j, 51)dp[i][j] = INF;
	dp[0][0] = 0;

	
	REP(i, 1LL << 18) {
		REP(j, 51) {
			if (dp[i][j] == INF)continue;
			
			ll t = popcount3(i);
			bitset<18> bit = i;
			ll cnt = 0;
			for (int k = n - 1; k >= 0;k--) {
				if (bit[k] == 1) {
					cnt++;
					continue;
				}
				if (abs(t - k) % 2 == 0) {
					if (a[k] >= j)chmin(dp[(i | (1LL << k))][a[k]], dp[i][j] + cnt);
				}
				else {
					if (b[k] >= j)chmin(dp[(i | (1LL << k))][b[k]], dp[i][j] + cnt);
				}
			}
		}
	}
	ll ans = INF;
	REP(i, 51)chmin(ans, dp[(1 << n) - 1][i]);
	if (ans == INF)cout << -1 << endl;
	else cout << ans << endl;

	return 0;
}


Measured memory_bytes: 104832
/*
0-1 ナップザック問題
重さと価値が それぞれ wi, vi であるような N 個の品物があります。
これらの品物から、重さの総和Wを超えないように選んだ時の、
価値の総和の最大値を求めなさい。

制約
1 ≤ N ≤ 100
1 ≤ vi ≤ 1,000
1 ≤ wi ≤ 1,000
1 ≤ W ≤ 10,000
*/
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <string.h>


using namespace std;

// #define for(i,a,b) for (int i=(a);i<(b);++i)
typedef long long ll;
typedef pair<int,int> point;
#define rep(i,n) for(i,0,n)

const int MAX_N = 1e6;
int N; 

// メモ化再帰
int memo[MAX_N+10]; // memo[i] が i についての答え
int rec(int n) {
    if (n == 0) return 0; // 終端条件
    if (memo[n] != -1) return memo[n]; // すでに探索済みならリターン

    int res = n; // INF の気持ち

    // 1, 6, 6^2, ..., を試す (n を超えない範囲で)
    for (int pow6 = 1; pow6 <= n; pow6 *= 6){
    	res = min(res, rec(n - pow6) + 1);
	}
	
    // 1, 9, 9^2, ..., を試す (n を超えない範囲で)
    for (int pow9 = 1; pow9 <= n; pow9 *= 9){
    	res = min(res, rec(n - pow9) + 1);
    }

    // メモしながらリターン
    return memo[n] = res;
}

int main() {
    cin >> N;

    // テーブルを -1 で初期化
    for (int i = 0; i < MAX_N; ++i) memo[i] = -1;
    cout << rec(N) << endl;
}


Measured memory_bytes: 7296
n, k = map(int, input().split())
s = list(input())
a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
b = "abcdefghijklmnopqrstuvwxyz"
for i in range(26):
    if s[k - 1] == a[i]:
        s[k - 1] = b[i]
        break
print(*s, sep="")

Measured memory_bytes: 2940
#include <iostream>
#include <vector>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <time.h>
#include <list>

using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define reps(x,c) for(auto x:c)
#define all(x) begin(x), end(x)
#define pb(a) emplace_back(a) 
#define mp(a,b) make_pair((a),(b))
#define pl pair<ll,ll>
#define INF 2147483600
#define MOD 1000000007
#define fi first
#define se second
#define dbg(x) cout << #x"="<< (x) << endl
#define dbgv(x) cout<<#x<<endl;reps(y,x){cout<<y<<" ";}cout<<endl
ll n,m;
ll mod(ll a) {
  return (a % m + m) % m;
}

int main(){
  cin>>n>>m;
  vector<ll> a(n),b(n+1);
  ll ans=0; 
  rep(i,n){
    cin>>a[i];
  }

  rep(i,n){
    b[i+1]=mod(b[i]+a[i]);
  }

  map<ll,ll> cnt;
  rep(i,n+1){
    cnt[b[i]]++;
  }
  reps(x,cnt){
    if(x.fi==0){
    }
    ans+=x.se*(x.se-1)/2;
  }
  cout<<ans<<endl;
  return 0;
}

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

int fac(int n, int p) {
  if (n < 2) {
    return 1;
  }

  return (n * fac(n - 1, p)) % p;
}

int comb(int n, int r, int p) {
  return (fac(n, p) / ((fac(r, p) * fac(n - r, p)) % p)) % p;
}

int main() {
  int p;
  cin >> p;
  vector<int> a(p);
  for (int i=0; i<p; i++) {
    cin >> a[i];
  }

  vector<int> c(p);
  int half = (p - 1) / 2;
  for (int i=0; i<p; i++) {
    if (i <= half) {
      c[i] = comb(p - 1, i, p);
    } else {
      c[i] = c[p - 1 - i];
    }
  }

  vector<int> b(p);
  b[0] = a[0];
  for (int i=0; i<p; i++) {
    int pw = 1;
    for(int j=0; j<p-1; j++) {
      if (a[i] == 1) {
        if (j % 2 == 0) {
          b[p-1-j] += p - ((c[j] * pw) % p);
        } else {
          b[p-1-j] += c[j] * pw;
        }
        b[p-1-j] %= p;
      }
      pw *= i;
      pw %= p;
    }
  }

  for (int i=0; i<p; i++) {
    if (i > 0) {
      cout << " ";
    }

    cout << b[i];
  }

  cout << endl;

  return 0;
}


Measured memory_bytes: 512
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<queue>
#define _USE_MATH_DEFINES
#include<math.h>
#include<cmath>
#include<ctime>
#include<map>
#include<typeinfo>
#define R(i, n) for(int i = 0;i < n; i++)
#define R1(i, n) for(int i = 1; i <= n; i++)
#define S(a) scanf("%d", &a)
#define S2(a, b) scanf("%d%d", &a, &b)
#define S3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SL(a) scanf("%lld", &a)
#define SL2(a, b) scanf("%lld%lld", &a, &b)
#define SL3(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)
#define SC(a) scanf("%c", &a)
#define P(a) printf("%d", a)
#define PL(a) printf("%lld", a)
#define PY printf("YES")
#define PN printf("NO")
#define Py printf("Yes")
#define Pn printf("No")
using namespace std;
using ll = long long;
using PR = pair<int, int>;
const ll MOD = 1e+9 + 7;
const int INF = 1e+9;
const double PI = acos(-1);

int main() {
	ll q, h, s, d, n;
	SL3(q, h, s);
	SL2(d, n);
	h = min(q * 2, h);
	s = min(h * 2, s);
	ll ans;
	if (d < s * 2) {
		ans = n / 2 * d + n % 2 * s;
	}
	else {
		ans = n * s;
	}
	PL(ans);
}

Measured memory_bytes: 3800
x, n = map(int,input().split())
ps = list(map(int,input().split()))
mtmp = x
ptmp = x
rt = True
r = []
if x not in ps:
  print(x)
elif len(ps) == 0:
  print(x)
else:
  while True:
    if mtmp+1 not in ps:
      r.append(mtmp+1)
      rt = False
    if ptmp-1 not in ps:
      r.append(ptmp-1)
      rt = False
    mtmp = mtmp+1
    ptmp = ptmp-1
    if rt:
      continue
    else:
      break
  print(min(r))

Measured memory_bytes: 61992
N, T = map(int,input().split())

cost_min = 1e9

for i in range(N):
  c, t = map(int,input().split())
  if t <= T:
    if c < cost_min:
      cost_min = c

if cost_min == 1e9:
  print("TLE")
else:
  print(cost_min)

Measured memory_bytes: 2940
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <numeric>
using namespace std;
#define IOS ios_base::sync_with_stdio(false);cin.tie(0)
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define int long long
const int MOD=1e9+7;
const int N=3e5+10;
int a[N];
int32_t main()
{
    IOS;
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    int lo=1;
    int hi=1e9;
    int ans=0;
    while(hi>=lo)
    {
        int mid=(hi+lo)/2;
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            sum+=(a[i]-1)/mid;
        }
        if(sum<=k)
        {
            ans=mid;
            hi=mid-1;
        }
        else
            lo=mid+1;
    }
    cout<<ans<<endl;


}

Measured memory_bytes: 5152
# include <bits/stdc++.h>
using namespace std;
 
int main () {
  int N, f, x;
  cin >> N;
  f = 0;
  x = N;
  
  for (int i = 0; i < 8; i++) {
    int a;
    a = x % 10;
    f += a;
    x /= 10;
    }
  
  if (N % f == 0) {
    cout << "Yes" << endl;
  }
  else {
    cout << "No" << endl;
  }
}

Measured memory_bytes: 256
import sys

s0=[]
for i,c in enumerate(sys.stdin.readline().strip()):
    if c=="L": s0.append(i-1)
    if c=="R": s0.append(i+1)
n= len(s0)
#
now= [ i for i in range(n) ]
prev= s0
for i in range(17):
    now = [ prev[i] for i in prev ]
    prev= now
#
ans= [0]*n
for i in range(n): ans[now[i]]+=1
for i in range(n): print( ans[i],end=" " )

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

using namespace std ;

#define N 20000

int n ;
char s[N] ;

int main()
{
    scanf("%d" , &n ) ;
    scanf("%s" , s + 1 ) ;
    int len = strlen( s + 1 ) ;
    for(int i = 1 ; i <= len ; i++ ){
        int t = ( s[i] - ( 'A' - 1 ) + n ) % 26 ;
        if( t == 0 ) t = 26 ;
        printf("%c" , 'A' + t - 1 ) ; 
    }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
long long cnt[200020],arr[200020];
bool vis[200020];
long long a[200020];
vector<int> v;
int main()
{
	int n;
	long long ans=0;
	cin>>n;
	for(int i=1;i<=n;++i)
	{
		cin>>arr[i];
		cnt[arr[i]]++;
		if(!vis[arr[i]])
		{
			vis[arr[i]]=true;
			v.push_back(arr[i]);
		}
	}
	for(auto c:v)
	{
		ans+=(cnt[c]*(cnt[c]-1))/2;
	}
	for(auto c:v)
	{
		a[c]=ans-cnt[c]+1;
	}
	for(int i=1;i<=n;++i)
	{
		cout<<a[arr[i]]<<endl;
	}
	return 0;
}

Measured memory_bytes: 7160
import sys
input = sys.stdin.readline

m = [1, 1] + [None]*50

def fibonacci(n):
    if m[n-2]:
        pass
    else:
        fibonacci(n-2)
    if m[n-1]:
        pass
    else:
        fibonacci(n-1)

    m[n] = m[n-1] + m[n-2]

N = int(input())

fibonacci(44)
print(m[N])




Measured memory_bytes: 5604
N, K = map(int, input().split())
MOD = 1000000007

fact = [1]
for i in range(1, N):
  fact.append(fact[-1] * i)

def comb(n, k):
  return (fact[n] // (fact[n - k] * fact[k])) % MOD

def f2(n, k):
  return comb(n + k - 1, k - 1)

def f(n, k):
  if n < k:
    return 0
  if n == 0 and k == 0:
    return 1
  if k < 1:
    return 0
  return f2(n - k, k)

for i in range(1, K + 1):
  blue = f(K, i)
  red = (f(N - K, i - 1) + 2 * f(N - K, i) + f(N - K, i + 1)) % MOD
  ans = blue * red
  print(ans % MOD)

Measured memory_bytes: 5748
#include <iostream>
using namespace std;
int main() {
    int n; cin >> n;
    int a[n]; for (int i = 0; i < n; i++) cin >> a[i];
    long long dp[402][402] = {}, sum[402][402] = {};
    for (int i = 0; i < n; i++)
        for (int j = i+1; j <= n; j++) {
            dp[i][j] = 1LL<<60;
            sum[i][j] = sum[i][j-1] + a[j-1];
        }
    for (int i = 0; i < n; i++) dp[i][i+1] = 0;
    for (int w = 2; w <= n; w++)
        for (int i = 0; i+w <= n; i++) {
            for (int j = i+1; j < i+w; j++) {
                dp[i][i+w] = min(dp[i][i+w], dp[i][j] + dp[j][i+w] + sum[i][j] + sum[j][i+w]);
            }
        }
    cout << dp[0][n] << endl;
}


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

int64_t ncr2(int64_t n);
int main(void) {
    int64_t N,M;

    cin >> N >> M;

    cout << ncr2(N) + ncr2(M) << "\n";
    return 0;
}

int64_t ncr2(int64_t n) {
    if(n < 2) return 0;
    if(n == 2) return 1;
    return (n * (n-1)) / 2;
}


Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>
using namespace std;
typedef long long ll;
ll const INF = 1LL << 60;

int main() {
  ll D;
  cin >> D;
  if (D == 25) {
    cout << "Christmas" << endl;
  } else if (D == 24) {
    cout << "Christmas Eve" << endl;
  } else if (D == 23) {
    cout << "Christmas Eve Eve" << endl;
  } else {
    cout << "Christmas Eve Eve Eve" << endl;
  }
  return 0;
}

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

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>  // Including tree_order_statistics_node_update

using namespace __gnu_pbds;
using namespace std;

#define sz(a) int((a).size())
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define minei(x) min_element(x.begin(), x.end()) - (x).begin()
#define maxei(x) max_element(x.begin(), x.end()) - (x).begin()
#define LSOne(S) (S & (-S))

#define uns(v) sort((v).begin(), (v).end()), v.erase(unique(v.begin(), v.end()), v.end())
#define acusum(x) accumulate(x.begin(), x.end(), 0)
#define acumul(x) accumulate(x.begin(), x.end(), 1, multiplies<int>());
#define bits(x) __builtin_popcount(x)
#define oo INT_MAX
#define inf 1000000000
#define MAXN 1000007
#define MOD 1000000007

const double pi = acos(-1.0);
const double eps = 1e-11;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;

int main() {
    std::ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    vector<int> v(3);
    for (int i = 0; i < 3; i++) cin >> v[i];
    sort(all(v));
    cout << ((v[0] == v[1] || v[1] == v[2]) && v[0] != v[2] ? "Yes" : "No") << '\n';
    return 0;
}


Measured memory_bytes: 256
using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        int count = int.Parse(ReadLine().Split(' ')[0]);
        string[] input = new string[count];

        for(int i = 0; i < count; i++)
        {
            input[i] = ReadLine();
        }

        List<string> str = new List<string>(input);
        str.Sort();

        string output = "";
        foreach(string s in str)
        {
            output = output + s;
        }

        Console.WriteLine(output);
        ReadKey();
    }
}

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

using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define re return
#define fi first
#define mp make_pair
#define se second
#define sz(a) (int)a.size()
#define prev previ
#define tm tmmm
#define div divv
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

const int ma = 1024 * 1024;
const ll mod = int(1e9) + 7;
int x = 4, y = 5, z = 8;
string s;

int main() {
	iostream::sync_with_stdio(0), cin.tie(0);
	cin >> x >> y >> z;
	s = "";
	vector<string> ss;
	forn (i, x) {
		string t = "a";
		ss.push_back(t);
	}
	forn (i, y) {
		string t = "b";
		ss.push_back(t);
	}
	forn (i, z) {
		string t = "c";
		ss.push_back(t);
	}
	while (sz(ss) > 1) {
		int k = 0;
		forn (i, sz(ss))
			if (ss[i] == ss[0]) k++;
		if (k == sz(ss)) break;
		for (int i = 0; i < k && sz(ss) > k; i++) {
			ss[i] += ss[sz(ss) - 1];
			ss.pop_back();
		}

		sort(ss.begin(), ss.end());
	}
	forn (i, sz(ss)) cout << ss[i];
}

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


int main(){
	int N, M;
	cin >> N >> M;
	vector<long long> v1;
	for (int i = 0; i < N; ++i){
		int a;
		cin >> a;
		v1.push_back(a);
	}
	sort(v1.begin(), v1.end(), greater<int>());

	vector<pair<long long, int>> v2;
	for (int i = 0; i < M; ++i){
		int b;
		long long c;
		cin >> b >> c;
		v2.push_back(make_pair(-c,b));
	}
	sort(v2.begin(), v2.end());

	long long ans = 0;
	int num = 0;
	int addr1 = 0;
	int addr2 = 0;
	while(num < N){
		if (v1[addr1] > -v2[addr2].first){
			ans += v1[addr1++];
			num += 1;
		}else{
			int l = min(N - num, v2[addr2].second);
			ans += -v2[addr2++].first * l;
			num += l;
		}
	}

    cout << ans << endl;
}


Measured memory_bytes: 4212
from fractions import gcd
A, B, C, D = map(int, input().split())
cnt_C = int(B // C) - int((A-1) // C)
cnt_D = int(B // D) - int((A-1) // D)
cnt_CD = int(B // int((C*D)/gcd(C,D))) - int((A-1) // int((C*D)/gcd(C,D)))
print(B - A + 1 - (cnt_C + cnt_D - cnt_CD))

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

int main() {
  int a[3];
  for(int i=0; i<3; i++) cin >> a[i];
  if((a[0]==1 && a[1]==1) || a[2]==1) cout << "Open" << endl;
  else cout << "Close" << endl;
  return 0;
} 


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

int main()
{
    int n;
    cin>>n;
    
    char c[n][3];
    for(int j=0;j<3;j++)
        for(int i=0;i<n;i++)
        cin>>c[i][j];
    
    int ans = 0;
    
    for(int i=0;i<n;i++)
    {
        if(c[i][0] == c[i][1] && c[i][1]==c[i][2]);
        else if((c[i][0]==c[i][1] && c[i][0] !=c[i][2] )|| (c[i][0]!=c[i][1] && c[i][0] ==c[i][2]) ||(c[i][0]!=c[i][1] && c[i][1]==c[i][2]))
            ans++;
        else
            ans+=2;
    }
    cout<<ans;
}

Measured memory_bytes: 256
// AOJ 1040 Chocolate with Heart Marks
// 2018.2.6 bal4u
 
#include <stdio.h>
#include <string.h>

#define INF 0x03030303
int s[10], sz;
int g[200][200];
int mv[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};

//#define getchar_unlocked()  getchar()
int in()
{
	int n = 0;
	int c = getchar_unlocked();
	while (c < '0') c = getchar_unlocked();
	do n = (n<<3)+(n<<1) + (c & 0xf), c = getchar_unlocked();
	while (c >= '0');
	return n;
}

int b[1024][200];
int minimumSteinerTree(int *s, int sz, int g[200][200], int gsz)
{
	int i, j, k, t, lim, ans;

    if (sz <= 1) return 0;

	for (k = 0; k < gsz; k++) for (i = 0; i < gsz; i++) for (j = 0; j < gsz; j++) {
		t = g[i][k] + g[k][j];
		if (t < g[i][j]) g[i][j] = t;
	}

	memset(b, INF, sizeof(b));
	for (t = 1, i = 0; i < sz; i++, t <<= 1) {
		for (j = 0; j < gsz; j++) b[t][j] = g[s[i]][j];
	}

	lim = (1<<sz) - 1;
	for (i = 1; i <= lim; i++) {
		if (!(i & (i-1))) continue;
		for (j = 0; j < gsz; j++) for (k = 0; k < i; k++) {
			if ((i | k) == i) {
				t = b[k][j] + b[i-k][j];
				if (t < b[i][j]) b[i][j] = t;
			}
		}
		for (j = 0; j < gsz; j++) for (k = 0; k < gsz; k++) {
			t = b[i][k] + g[j][k];
			if (t < b[i][j]) b[i][j] = t;
		}
	}

	ans = INF;
    for (i = 0; i <= lim; i++) for (j = 0; j < gsz; j++) {
		t = b[i][j] + b[lim-i][j];
		if (ans > t) ans = t;
	}
    return ans;
}

int main()
{
    int H, W, i, r, c, t, nr, nc, nt;

    while (H = in()) {
		W = in();
		memset(g, INF, sizeof(g));
		sz = 0;
		for (r = 0; r < H; r++) for (c = 0; c < W; c++) {
			t = r*W + c;
			if (in()) s[sz++] = t;

			for (i = 0; i < 4; i++) {
				nr = r + mv[i][0], nc = c + mv[i][1];
				if (nr < 0 || nr >= H || nc < 0 || nc >= W) continue;
				nt = nr*W + nc;
				g[t][nt] = 1;
			}
			g[t][t] = 0;
		}

		printf("%d\n", H*W - minimumSteinerTree(s, sz, g, H*W) - 1);
	}
	return 0;
}



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

using namespace std;
#define ll long long int

void solve(){

	int N , a;
	cin>>N;
	int p = N-1;

	vector<int>v;
	
	for(int i = 0 ; i < N ;i++){
		cin>>a;
		v.push_back(a);
	}

	sort(v.begin(),v.end());

	ll c = v[N-1];
	int count = 1;

	for(int i = N-2; i>=0 ; i--){

		if(count < p){

			c = c + v[i];
			count++;
		}
		if(count < p){

			c = c + v[i];	
			count++;
		}
	}

	cout<<c<<"\n";

}




int main() {
	ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("out.txt","w",stdout);
    #endif

   
    solve();
   
    return 0;
}


Measured memory_bytes: 4228
#include<bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >> N;
  vector<vector<int>> vec(N,vector<int>(N-1));
  for(int i=0; i<N; i++){
    for(int j=0; j<N-1; j++){
      cin >> vec.at(i).at(j);
    }
  }
  
  vector<vector<int>> day(N,vector<int>(N-1,-1));
  vector<int> nex(N,0);
  vector<int> over(N,-1);
  queue<int> Q;
  for(int i=0; i<N; i++){
    int a = vec.at(i).at(0);
    if(vec.at(a-1).at(0) == i+1){
      Q.push(i);
      nex.at(i)++;
      over.at(i)++;
      day.at(i).at(0) = 1;
    }
  }
 
  
  while(Q.size()){
    int x = Q.front();
    Q.pop();
    if(over.at(x) > 0){
      over.at(x)--;
      continue;
    }
    int y = nex.at(x);
    int z = vec.at(x).at(y); //次の対戦相手
    z--;
    if(vec.at(z).at(nex.at(z)) == x+1){
      if(nex.at(z) == 0){
        day.at(x).at(nex.at(x)) = day.at(x).at(nex.at(x)-1) + 1;
        day.at(z).at(nex.at(z)) = day.at(x).at(nex.at(x)-1) + 1;
      }
      else{
        day.at(x).at(nex.at(x)) = max(day.at(x).at(nex.at(x)-1),day.at(z).at(nex.at(z)-1)) + 1;
        day.at(z).at(nex.at(z)) = max(day.at(x).at(nex.at(x)-1),day.at(z).at(nex.at(z)-1)) + 1;
      } 
      nex.at(x)++;
      nex.at(z)++;
      if(nex.at(x) < N-1){
        Q.push(x);
      }
      if(nex.at(z) < N-1){
        Q.push(z);
      }
      over.at(z)++;
    }
    else{
      over.at(x) = -1;
    }
  }
  
  /*for(int i=0; i<N; i++){
    for(int j=0; j<N-1; j++){
      cout << day.at(i).at(j);
    }
    cout << endl;
  }*/
  
  int ans = 0;
  for(int i=0; i<N; i++){
    for(int j=0; j<N-1; j++){
      if(day.at(i).at(j) == -1){
        ans = -1;
        break;
      }
      ans = max(ans,day.at(i).at(j));
    }
    if(ans == -1){
      break;
    }
  } 
  cout << ans << endl; 
}



Measured memory_bytes: 8192
#include<iostream>
using namespace std;
int main(){
  int n,ans = 0,ss;
  cin >>n;
  string sign;
  cin >>sign;
  ss = sign.size();
  while(n--){
    string s;
    cin >>s;
    for(int i=0; i<s.size(); i++){
      if(s[i] == sign[0]){
	for(int j=1;;j++){
	  if(i+j*(ss-1)>=s.size()) break;
	  for(int k=1; k<ss; k++){
	    if(s[i+j*k] != sign[k]) break;
	    if(k == ss-1){ans++;goto f;}
	  }
	}
      }
    }
    f:;
  }
  cout <<ans<<endl;
}

Measured memory_bytes: 1208
#include <stdio.h>
#include <stdlib.h>

#define ORIGIN 1
#define SWAP(a, b, v) v = a, a = b, b = v
#define parent(i) ((i) / 2)
#define left(i) (2 * (i))
#define right(i) (2 * (i) + 1)

int a[2000001]; /* 8MB */

/* ヒントがないのでwikipedia参照 */

void
up_heap(int *a, int n, int i)
{
	int p = parent(i);
	int g;

	g = (ORIGIN <= p && a[p] < a[i]) ? p : i;

	if (g != i) {
		SWAP(a[i], a[g], p);
		up_heap(a, n, g);
	}
}

void
down_heap(int *a, int n, int i)
{
	int l = left(i);
	int r = right(i);
	int g;

	g = (l <= n && a[l] > a[i]) ? l : i;
	g = (r <= n && a[r] > a[g]) ? r : g;

	if (g != i) {
		SWAP(a[i], a[g], l);
		down_heap(a, n, g);
	}
}

int
main(void)
{
	int i, p, n = 0;
	int v;
	char cmd[16];

	while (scanf("%s", cmd) != EOF) {
		switch (cmd[2]) {
		case 's': /* in[s]ert */
			scanf("%d", &v);
			a[++n] = v;
			up_heap(a, n, n);
			break;
		case 't': /* ex[t]ract */
			printf("%d\n", a[ORIGIN]);
			a[ORIGIN] = a[n--];
			down_heap(a, n, ORIGIN);
			break;
		default: /* end */
			return 0;
		}
	}


	return 0;
}



Measured memory_bytes: 5584
def main():
    n = int(input())
    ss = [input() for _ in range(n)]

    m = [0, 0, 0, 0, 0]
    for s in ss:
        try:
           m['MARCH'.index(s[0])] += 1
        except ValueError:
            pass

    c = 0
    for i in range(len(m)):
        for j in range(i+1, len(m)):
           for k in range(j+1, len(m)):
               c += m[i] * m[j] * m[k]

    print(c)

if __name__ == '__main__':
    main()


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

#define int long long

typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>void chmax(T &t,U f){if(t<f)t=f;}

int N;
int A[100];

signed main(){
    cin>>N;
    rep(i,N)cin>>A[i];
    
    int cnt=0;
    rep(i,N){
        int mi=i;
        for(int j=i+1;j<N;j++)if(A[mi]>A[j])mi=j;
        swap(A[i],A[mi]);
        cnt+=i!=mi;
    }
    rep(i,N){
        if(i)cout<<" ";
        cout<<A[i];
    }cout<<endl;
    cout<<cnt<<endl;
    
    return 0;
}

Measured memory_bytes: 1160
# D - Make Them Even

H, W = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(H)]

ans_move = []

hitofude = []
y,x = 0,0
move_right_flg = 1
while y < H:
    hitofude.append([y,x])
    if move_right_flg == 1 and x < W-1:
        x += 1
    elif move_right_flg == 1 and x == W-1:
        y += 1
        move_right_flg = 0
    elif move_right_flg == 0 and x > 0:
        x -= 1
    else:
        y += 1
        move_right_flg = 1
        
hold_coin_flg = 0
tmp_move = -1
for current_place in hitofude:
    if a[current_place[0]][current_place[1]]%2 == 1 and hold_coin_flg == 0:
        hold_coin_flg = 1
    elif a[current_place[0]][current_place[1]]%2 == 1 and hold_coin_flg == 1:
        hold_coin_flg = 0
        tmp_move[-1].extend([current_place[0]+1, current_place[1]+1])
        ans_move.extend(tmp_move)
    if hold_coin_flg == 1:
        if tmp_move == -1 or len(tmp_move[-1]) == 4:
            tmp_move = [[current_place[0]+1, current_place[1]+1]]
        else:
            tmp_move[-1].extend([current_place[0]+1, current_place[1]+1])
            tmp_move.append([current_place[0]+1, current_place[1]+1])

print(len(ans_move))
for ans in ans_move:
    print(*ans)

Measured memory_bytes: 61780
#include <iostream>
#include <algorithm>
using namespace std;
using ll = long long;

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

  int r = 0;
  ll xr = 0, sum = 0, ans = 0;
  
  for(int l = 0; l < n; l++){
    while(r < n && ((xr ^ a[r]) == (sum + a[r]))){
      xr ^= a[r];
      sum += a[r];
      r++;
    }

    //集計
    ans += r-l;

    if(r == l){
      r++;
    } 
    else{
      sum -= a[l];
      xr ^= a[l];
    }
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 1024
#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<iostream>
#include<vector>
#include<cstdlib>
using namespace std;
 
template <class T> using V = vector<T>;
 
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
 
const long long INF = 1LL << 60;
 
using ll = long long;
using db = double;
using st = string;
using ch = char;
using bl = bool;
using vll = V<ll>;
using vpll =V<pair<ll,ll>>;
using vst = V<st>;
using vdb = V<db>;
using vch = V<ch>;
using vbl = V<bl>;
 
 
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define rFOR(i,a,b) for(ll i=(a);i>(b);i--)
#define oFOR(i,a,b) for(ll i=(a);i<(b);i+=2)
#define bgn begin()
#define en end()
#define SORT(a) sort((a).bgn,(a).en)
#define REV(a) reverse((a).bgn,(a).en)
#define M(a,b) max(a,b)
#define rM(a,b) min(a,b)
#define fi first
#define se second
#define sz size()
#define gcd(a,b) __gcd(a,b)
#define co(a) cout<<a<<endl;
#define ci(a) cin>>a;
 

vll dp(100100);

ll sum(ll n) {
  ll m=0;
  FOR(i,0,20){
    m+=n%10;
    n/=10;
    if(n==0){
      break;
    }
  }
  return m;
}
 
 
const int MAX = 510000;
const int MOD = 1000000007;
 
long long fac[MAX], finv[MAX], inv[MAX];
 
void Comuse() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
 
 
#define comuse Comuse()
 
ll combi(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
 
 
 
 
ll lcm(ll a,ll b){
  ll n;
  n=a/gcd(a,b)*b;
  return n;
}
 
 
/****************************************\
| Thank you for viewing my code:)        |
| Written by RedSpica a.k.a. RanseMirage |
| Twitter:@asakaakasaka                  | 
\****************************************/
 
 
signed main() {
  ll a;
    ci(a);
  co(a*a*3)
}

Measured memory_bytes: 1024
from fractions import gcd
def lcm(x,y):
    return (x*y)//gcd(x,y)
n,m=map(int,input().split())
s=input()
t=input()
d=lcm(n,m)
p=d//n
q=d//m
h=lcm(p,q)
l=d//h
for i in range(0,l):
    g=i*h
    if s[g//p]!=t[g//q]:
        print(-1)
        break
else:
    print(d)

Measured memory_bytes: 5344
n = int(input())
A = [int(i) for i in input().split()]
dp = [0] * n

dp[1] = abs(A[0] - A[1])
for i in range(2, n):
    route1 = abs(A[i] - A[i-2]) + dp[i-2]
    route2 = abs(A[i] - A[i-1]) + dp[i-1]
    dp[i] = min(route1, route2)
    
print(dp[-1])

Measured memory_bytes: 13928
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef long double ld;
const double pi=acos(-1.0);
const ll mod=pow(10,9)+7;
const ll INF=pow(2,31)-1;
using str=string;
typedef pair<int,int> P;
typedef vector<int> vi;

ll gcd(ll a,ll b){
    ll v0=a,v1=b,v2=a%b;
    while(v2!=0){
        v0=v1;
        v1=v2;
        v2=v0%v1;
    }
    return v1;
}

ll lcm(ll a,ll b){
    return (a*b/gcd(a,b));
}

int main(){
    ios::sync_with_stdio(false);
    int n; cin >> n;
    int h[n],d[n-1];
    rep(i,n) cin >> h[i];
    rep(i,n-1) d[i]=h[i]-h[i+1];
    int m;
    int ans=0;
    rep(i,n-1){
        if(d[i]>=0) m++;
        else m=0;
        ans=max(ans,m);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

template <typename T>
struct edge {
    int from, to;
    T cost;

    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}

    explicit operator int() const {return to;}
};

class UnionFind {
    vector<int> uni;
    int n;
public:
    explicit UnionFind(int n) : uni(static_cast<u32>(n), -1) , n(n){};

    int root(int a){
        if (uni[a] < 0) return a;
        else return (uni[a] = root(uni[a]));
    }

    bool unite(int a, int b) {
        a = root(a);
        b = root(b);
        if(a == b) return false;
        if(uni[a] > uni[b]) swap(a, b);
        uni[a] += uni[b];
        uni[b] = a;
        return true;
    }
};

template< typename T >
T kruskal(vector<edge<T>> &G, int V)
{
    sort(begin(G), end(G), [](const edge<T> &a, const edge<T> &b) { return (a.cost < b.cost); });
    UnionFind uf(V);
    T ret = 0;
    for(auto &e : G) {
        if(uf.unite(e.from, e.to)) ret += e.cost;
    }
    return ret;
}

using real = double;
real EPS = 1e-10;
struct Point3 {
    real x, y, z;
    Point3& operator+=(const Point3 a) { x += a.x; y += a.y; z += a.z;  return *this; }
    Point3& operator-=(const Point3 a) { x -= a.x; y -= a.y; z -= a.z; return *this; }
    Point3 operator+(const Point3 a) const {return Point3(*this) += a; }
    Point3 operator-(const Point3 a) const {return Point3(*this) -= a; }
    explicit Point3(real a = 0, real b = 0, real c = 0) : x(a), y(b), z(c) {};
};


inline real dot(Point3 a, Point3 b){ return a.x*b.x + a.y*b.y + a.z*b.z; }
inline Point3 cross(Point3 a, Point3 b){
    return Point3(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
}
inline real abs(Point3 a){ return sqrt(dot(a, a)); }

int main() {
    int n;
    while(cin >> n, n){
        vector<pair<Point3, double>> v(n);
        for (int i = 0; i < n; ++i) {
            cin >> v[i].first.x >> v[i].first.y >> v[i].first.z >> v[i].second;
        }
        vector<edge<double>> G;
        for (int i = 0; i < n; ++i) {
            for (int j = i+1; j < n; ++j) {
                G.emplace_back(i, j, max(0.0, abs(v[i].first-v[j].first)-v[i].second-v[j].second));
            }
        }
        printf("%.3f\n", kruskal(G, n));
    }
    return 0;
}


Measured memory_bytes: 3260
#include <iostream>
#include <vector>
  
int main(void) {
    u_int N;    // the number of integer
    std::vector<std::vector<u_long>> dp;
    std::vector<u_int> seq;
     
    std::cin >> N;
    seq.reserve(N);
    dp.resize(N);
    for (u_int i = 0; i < N; ++i) {
        u_int tmp;
        std::cin >> tmp;
        seq.push_back(tmp);
        dp[i].resize(21, 0);
    }
     
    // solve by dp
    dp[0][seq[0]] = 1;  // initialization
    for (u_int i = 1; i < N - 1; ++i) {
        for (u_int j = 0; j < 21; ++j) {
            if (dp[i - 1][j] > 0) {
                if (j + seq[i] <= 20) {
                    dp[i][j + seq[i]] += dp[i - 1][j];
                }
                if (j >= seq[i]) {
                    dp[i][j - seq[i]] += dp[i  -1][j];
                }
            }
        }
    }
    std::cout << dp[N - 2][seq[N - 1]] << std::endl;
  
    return 0;
}

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

int main() {
    using namespace std;
    int n,m;
    cin >> n >> m;
    int ans[n] = {1,};
    bool checked[n];
    if(n==1)ans[0]=0;
    fill(checked, checked+n, false);
    bool isExsist = true;
    for(int i=0; i<m; i++){
        int s,c;
        cin >> s >> c;
        if((s==1 && c==0 && n!=1) || (checked[s-1] && ans[s-1]!=c)) {
            isExsist = false;
            break;
        }
        ans[s-1]=c;
        checked[s-1]=true;
    }
    if(!isExsist)cout << "-1\n";
    else {
        for(int i=0; i<n; i++){
            cout << ans[i];
        }
        cout << endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()
template <class T> inline bool chmin(T &a, T b) {if (a > b){a = b;return true;}return false;}
template <class T> inline bool chmax(T &a, T b) {if (a < b){a = b;return true;}return false;}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,h,w;cin>>n>>h>>w;
    int ans = 0;
    rep(i,n){
        int a,b;cin>>a>>b;
        if(a>=h && b>=w) ans++;
    }
    cout<<ans<<endl;
    
}

Measured memory_bytes: 256
//XOR_IS_FRUSTRATED

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;


#define fastio ios_base::sync_with_stdio(false), cin.tie(NULL)
#define rep(a,x,b) for(a=x;a<b;a++)
#define repv(a,it) for(auto it=a.begin();it!=a.end();it++)
#define repr(a,x,b) for(a=x;a>=b;a--)
#define pb 
#define mm(a,x) memset(a,x,sizeof(a))
#define all(v) (v).begin(), (v).end()
#define pll pair<ll, ll>
#define mp map<pair<ll,ll>,ll>
#define ret(a) return a
#define br break
#define con continue

ll mod=pow(10,9)+7;
ll n,m,a[1000005],i,j,t,k;
ll prime[1000005]={0};
vector<ll>vprime;
ll max3(ll a, ll b, ll c)
{
        ret(max(a,max(b,c)));
}
ll min3(ll a, ll b, ll c)
{
        ret(min(a,min(b,c)));
}
ll inv(ll x1,ll y1)
{
        if(y1==0)
                ret(1);
        ll temp=inv(x1,y1/2);
        temp=(temp*temp)%mod;
        if(y1%2)
                temp=(temp*x1)%mod;
        ret(temp);        
}
ll inv1(ll a1,ll m1)
{
        ll m0=m1,x0=0,y0=1;
        if(m1==1)
                ret(0);
        while(a1>1)
        {
                ll q=a1/m1;
                ll temp=m1;
                m1=a1%m1;
                a1=temp;
                temp=y0;
                y0=x0-q*y0;
                x0=temp;
        }
        x0=(x0+m0)%m0;
        ret(x0);
}

void sieve()
{
        rep(i,2,1000)
        {
                if(prime[i]==0)
                {
                        
                        for(j=i*i;j<=1000000;j+=i)
                                prime[j]=1;
                }
        }
        rep(i,2,1000001)
        {
                if(prime[i]==0)
                    vprime.push_back(i);
        }
}

int main()
{
        
        fastio;
        cin>>n;
        ll b[n];
        rep(i,0,n-1)
                cin>>b[i];
        a[0]=b[0];
        a[1]=b[0];
        rep(i,1,n-1)
        {
                if(a[i]>b[i])
                {
                        a[i]=b[i];
                }
                a[i+1]=b[i];
        }
        ll ans=0;
        rep(i,0,n)
                ans+=a[i];
        cout<<ans;        
        
        
        
        
        
        
        
        ret(0);
}

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

int main()
{
    int N;
    cin >> N;
    vector<int> buttom;

    for (int i = 0; i < N; i++)
    {
        int next_button;
        cin >> next_button;
        next_button--;
        buttom.push_back(next_button);
    }
    int now = 0, c = 0;

    for (;;)
    {
        if(now == 1)//ボタン2が光る
        {
            cout << c << endl;
            break;
        }
        if(c >= N)
        {
            cout << -1 << endl;
            break;
        }
        c++;
        now = buttom.at(now);
    }
    
}

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

// 型定義
typedef long long ll;
typedef pair<ll, ll> P;
bool operator>(const P a,const P b){return a.second > b.second;};//演算子オーバーロード

// forループ
#define REP(i,n) for(ll i=0; i<(ll)(n); ++i)

// 定数宣言
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;

// グラフ表現
using Graph = vector<vector<int>>;

// グラフの辺表現
using Edge = map<pair<int,int>,int>;

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

// 最大公約数
ll gcd(ll a,ll b){
   if (a%b == 0) return(b);
   else return(gcd(b, a%b));
}

// 最小公倍数
ll lcm(ll a, ll b){
    return a*b/gcd(a, b);
}

vector<ll> C;
vector<ll> ans;
vector<bool> seen; 
queue<ll> que;
ll score = 0;
ll max_score = 0;
ll max_id = -1;

// DFSのための再帰関数
void dfs(const Graph &G, ll v, ll step) {
    seen[v] = true;
    ans[v] = que.front();
    que.pop();
    if(step != 0) score += ans[v];
    for (auto next_v : G[v]) { 
        if (seen[next_v]) continue;
        dfs(G, next_v, step+1); // 再帰的に値を追加していく
    }
}

int main()
{
    cout << fixed << setprecision(15);
    ll N;
    cin >> N;

    C.resize(N);
    ans.resize(N);
    
    // グラフ
    Graph G(N);

    REP(i, N-1){
        int a, b;
        cin >> a >> b;
        G[a-1].push_back(b-1);
        G[b-1].push_back(a-1);
    }
    REP(i, N){
        cin >> C[i];
    }
    sort(C.begin(), C.end(), greater<ll>());

    // REP(i, 1){
    //     seen.assign(N, false);
    //     REP(j, N){
    //         que.push(C[j]);
    //     }
    //     score = 0;
    //     dfs(G, i, i, 0);

    //     if(score > max_score){
    //         max_score = score;
    //         max_id = i;
    //     }
    // }
    seen.assign(N, false);
    REP(j, N){
        que.push(C[j]);
    }
    score = 0;
    dfs(G, 0, 0);

    if(score > max_score){
        max_score = score;
        max_id = 0;
    }

    cout << max_score << endl;
    REP(i, N){
        if(i == N-1){
            cout << ans[i] << endl;
        }
        else{
            cout << ans[i] << " ";
        }
    }
    return 0;
}

Measured memory_bytes: 1664
<?php
while (true) {
  list($h, $w) = explode(" ", trim(fgets(STDIN)));
  if (!$h) break;

  for ($i = 1; $i <= $h; $i++) {
    for ($j = 1; $j <= $w; $j++) {
      if ($i == 1 || $i == $h || $j == 1 || $j == $w) echo "#";
      else echo ".";
    }
    echo "\n";
  }

  echo "\n";
}



Measured memory_bytes: 9220
n=gets.chomp.to_i
h=gets.split.map(&:to_i)
ans=0
100.times{|hh|
  t=0
  s=false
  n.times{|nn|
    if h[nn]>hh then
      if !s then
        t+=1
        s=true
      end
    else
      s=false        
    end
  }
  break if t==0
  ans+=t
}
puts ans

Measured memory_bytes: 1788
//*
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <numeric>
#include <functional>
#define MOD 1000000007
#define MAX 0x3f3f3f3f
#define MAX2 0x3f3f3f3f3f3f3f3fll
#define ERR 1e-10
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#pragma warning(disable:4996)
using namespace std;

typedef long long ll;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

int n, m;
int a[2000][2000];
int d[2000][2000];

char in[2000];

vector<pii> q;

int main()
{
	int i, j, k, l;
	cin>>n>>m;
	for(i=0;i<n;i++)
	{
		scanf("%s", in);
		for(j=0;j<m;j++) a[i][j]=in[j]=='#'?1:0;
	}
	memset(d, -1, sizeof(d));
	for(i=0;i<n;i++) for(j=0;j<m;j++)
	{
		if(a[i][j] == 1) q.emplace_back(i, j), d[i][j]=0;
	}
	int dir[4][2]={0, 1, 1, 0, 0, -1, -1, 0};
	for(i=0;i<q.size();i++)
	{
		for(j=0;j<4;j++)
		{
			int x, y, nx, ny;
			x=q[i].first, y=q[i].second, nx=q[i].first+dir[j][0], ny=q[i].second+dir[j][1];
			if(nx < 0 || ny < 0 || nx >= n || ny >= m) continue;
			if(d[nx][ny] != -1) continue;
			d[nx][ny]=d[x][y]+1;
			q.emplace_back(nx, ny);
		}
	}
	cout<<d[q.back().first][q.back().second]<<endl;
	return 0;
}
//*/

Measured memory_bytes: 35432
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
bool prime[1000000];
void eratos(void)
{
	memset(prime,true,sizeof(prime));
	prime[0]=false;
	prime[1]=false;
	for(int i=2;i*i<1000000;i++)
	{
		if(!prime[i])continue;
		for(int j=i*2;j<1000000;j+=i)
		{
			prime[j]=false;
		}
	}
	return;
}
int main()
{
	eratos();
	ll ans;
	int n,count;
	while(1)
	{
		count=0;
		ans=0;
		cin >> n;
		if(n==0)break;
		for(int i=0;i<1000000;i++)
		{
			if(prime[i])
			{
				count++;
				ans += i;
			}
			if(count==n)break;
		}
		cout << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 2140
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int)n; i++)
typedef long long ll;

int main(){
  int n;
  cin >> n;
  ll a[n+1];
  rep(i, n) cin >> a[i+1];
  ll mp[n+1] = {};
  ll pm[n+1] = {};
  for(int i = 1; i <= n; i++){
    if(i % 2) {
      mp[i] = mp[i-1] + a[i];
      pm[i] = pm[i-1] - a[i];
    } else {
      mp[i] = mp[i-1] - a[i];
      pm[i] = pm[i-1] + a[i];
    }
  }
  ll ans[n+1];
  for(int i = 1; i <= n; i++){
    if(i % 2) ans[i] = mp[i-1] + pm[n] - pm[i-1];
    else ans[i] = pm[i-1] + mp[n] - mp[i-1];
  }
  for(int i = 1; i <= n; i++) cout << -ans[i] << endl;
  return 0;
}


Measured memory_bytes: 4480
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; (i) < int(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < int(n); ++ (i))
#define REP_R(i, n) for (int i = int(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = int(n) - 1; (i) >= int(m); -- (i))
#define ALL(x) begin(x), end(x)
using ll = long long;
using namespace std;
template <class T> inline void chmax(T & a, T const & b) { a = max(a, b); }
template <class T> inline void chmin(T & a, T const & b) { a = min(a, b); }

ll solve(int n, vector<int> a, vector<int> b) {
    if (a == b) return 0;
    ll answer = 0;
    ll acc = 0;
    vector<int> ge;
    REP (i, n) {
        if (a[i] < b[i]) {
            answer += b[i];
            acc += b[i] - a[i];
            a[i] = 0;
            b[i] = 0;
        } else {
            if (b[i] != 0) {
                ge.push_back(i);
            }
        }
    }
    sort(ALL(ge), [&](int i, int j) { return b[i] > b[j]; });
    for (int i : ge) {
        if (acc < a[i] - b[i] + 1) continue;
        ll delta = min<ll>(acc, a[i] - b[i] + 1);
        a[i] -= delta;
        acc -= delta - 1;
        answer += b[i];
    }
    return answer;
}

/*
map<tuple<vector<int>, vector<int>, bool>, ll> memo;
ll go(int n, vector<int> a, vector<int> b, bool turn) {
    if (a == b) return 0;
    auto key = make_tuple(a, b, turn);
    if (memo.count(key)) return memo[key];
    if (turn) {
        ll acc = LLONG_MIN;
        REP (i, n) if (a[i]) {
            a[i] -= 1;
            chmax(acc, go(n, a, b, not turn) + 1);
            a[i] += 1;
        }
        return memo[key] = acc;
    } else {
        ll acc = LLONG_MAX;
        REP (i, n) if (b[i]) {
            b[i] -= 1;
            chmin(acc, go(n, a, b, not turn) + 1);
            b[i] += 1;
        }
        return memo[key] = acc;
    }
}
ll solve(int n, vector<int> a, vector<int> b) {
    return go(n, a, b, true) / 2;
}
*/

int main() {
    int n; scanf("%d", &n);
    vector<int> a(n), b(n);
    REP (i, n) {
        scanf("%d%d", &a[i], &b[i]);
    }
    ll answer = solve(n, a, b);
    printf("%lld\n", answer);
    return 0;
}


Measured memory_bytes: 768