akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,K;
    cin >> n >> K;
    vector<int> vec;
    vector<int> u(n),v(n);
    rep(i,n){
        cin >> u[i] >> v[i];
    }
    int bf = K;
    while(bf){
        if(bf & 1){
            vec.pb(1);
        }else{
            vec.pb(0);
        }
        bf >>= 1;
    }
    ll ans = 0;
    rep(i,n){
        if(!(u[i]&(~K))){
            ans += v[i];
        }
    }
    rep(i,vec.size()){
        if(vec[i] == 1){
            bf = K;
            ll res = 0;
            bf >>= i;
            bf ^= 1;
            bf <<= i;
            if(i != 0){
                bf += (1 << i)-1;
            }
            rep(j,n){
                if(!(u[j]&(~bf))){
                    res += v[j];
                }
            }
            ans = max(ans,res);
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1152
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		int A = scanner.nextInt();
		int B = scanner.nextInt();
		int C = scanner.nextInt();
		scanner.close();

		int[] bunsetsu = new int[3];
		bunsetsu[0] = A;
		bunsetsu[1] = B;
		bunsetsu[2] = C;

		Arrays.sort(bunsetsu);

		if (bunsetsu[0] != 5 || bunsetsu[1] != 5 || bunsetsu[2] != 7) {
			System.out.println("NO");
			return;
		}

		System.out.println("YES");

	}

}


Measured memory_bytes: 35508
#include<stdio.h>
#include<math.h>
 
int main(){
  int s[1000];
  int n,i;
  double a,m,z;
 
  while(1){
    scanf("%d",&n);
    if(!n)
      return 0;
 
    m=0;
    for(i=0;i<n;i++){
      scanf("%d",&s[i]);
      m+=s[i];
    }
 
    m=m/n;
    z=0;
    for(i=0;i<n;i++)
      z+=(s[i]-m)*(s[i]-m);
 
    a=sqrt(z/n);
    printf("%lf\n",a);
  }
}

Measured memory_bytes: 636
#include <cstdio>
using namespace std;
long long N, K, i, t, a, b;
long long arr[100001] = {0};
int main() {
    scanf("%ld %ld", &N, &K);
    for(i = 0; i < N; i++) {
        scanf("%ld %ld", &a, &b);
        arr[a] += b;
    }
    for(i = 1, t = 0; i <= 100001; i++) {
        t += arr[i];
        if(t >= K) {printf("%ld", i); break;}
    }
}

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

int main()
{
    long long N;
    cin >> N;
    long long A, B, C, D, E;
    cin >> A >> B >> C >> D >> E;
    long long MinMove = min({A, B, C, D, E});
    long long ans = (N + MinMove - 1) / MinMove + 4;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, l;
    cin >> n >> l;
    int sum = 0;
    for(int i = 1; i <= n; ++i) sum += l + i - 1;

    int j = -l + 1;
    if(j < 1) j = 1;
    if(j > n) j = n;
    sum -= l + j - 1;
    cout << sum << endl;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(){
    int c[3][3];
    int x[3];
    int y[3];
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            scanf("%d", &c[i][j]);
        }
    }
    x[0] = 0;
    for(int i = 0; i < 3; i++){
        y[i] = c[0][i] - x[0];
    }
    for(int i = 0; i < 3; i++){
        x[i] = c[i][0] - y[0];
    }
    bool good = true;
    for(int i = 0; i < 3; i++){
        for(int j = 0; j < 3; j++){
            if(x[i] + y[j] != c[i][j])
                good = false;
        }
    }
    if(good == true)
        printf("Yes");
    else
        printf("No");
    return 0;
}

Measured memory_bytes: 256
import sys
import numpy as np

sys.setrecursionlimit(10 ** 9)

K = int(input())

def gcd(x,y):
    while y:
        x, y = y, x % y
    return x

ans = 0
for i in range(K):
    for j in range(K):
        tmp = gcd(i+1,j+1)
        if tmp == 1:
            ans += 1*K
        else:
            for k in range(K):
                ans += gcd(gcd(i+1,j+1), k+1)

print(ans)

Measured memory_bytes: 27052
def calc(data)
  mc   = 0
  mx   = 0
  acal = 0
  f = Array.new(N+1){0}
  g = Array.new(N+1){0}
  data.each_with_index do |(x,v), i|
    acal += v
    nowc = acal - x
    if nowc > mc
      mc = nowc
      mx = x
    end
    f[i+1] = mc
    g[i+1] = mx
  end
  [f, g]
end

N, C = gets.split.map(&:to_i)
data = $<.map{|l|l.split.map(&:to_i)} # ex) [[1,2],[3,4],...]

# clockwise
fa, ga = calc(data)
# counter-clockwise
fb, gb = calc(data.reverse.map{|x,v|[C-x,v]})

maxa = fa.max
maxb = fb.max

maxa2b = 0
1.upto(N-1) do |i|
  maxa2b = [maxa2b, fa[i] - ga[i] + fb[-(i+1)]].max
end

maxb2a = 0
1.upto(N-1) do |i|
  maxb2a = [maxb2a, fb[i] - gb[i] + fa[-(i+1)]].max
end

p [maxa, maxb, maxa2b, maxb2a].max



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

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

#define mod ((ll)(1e9)+7)
#define MAX 200000

ll d[MAX+1], s[MAX+1];

int main(){
	int n;
	cin >> n;
	vector<int> v;
	for(int i=0; i<n; i++){
		int c;
		cin >> c;
		if(v.size()==0 || *(v.end()-1)!=c) v.push_back(c);
	}
	n=v.size();

	d[0]=1;
	for(int i=0; i<n; i++){
		d[i+1]=(d[i]+s[v[i]])%mod;
		s[v[i]]+=d[i];
	}
	cout << d[n] << endl;
	return 0;
}

Measured memory_bytes: 4340
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();

		System.out.println(Math.min(C, B / A));

		sc.close();
	}
}

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


int main(void){
    string n;
    cin >>n;
    rep(i,n.length()){
        if(n[i] == '1'){
            cout << 9;
        }
        else{
            cout << 1;
        }
    }
    cout <<endl;
    
    return 0;
}











































Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
#define MOD 1000000007
#define INF LLONG_MAX/2

const int NMAX=1000;
const int MMAX=2000;
int a[MMAX],b[MMAX];
ll c[MMAX],d[NMAX];
int N,M;

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

  for(int i=0;i<N;i++) d[i]=INF;
  d[0]=0;
  for(int loop=0;loop<N-1;loop++){
    for(int i=0;i<M;i++){
      if(d[a[i]]==INF) continue;
      d[b[i]]=min(d[b[i]], d[a[i]]+c[i]);
    }
  }
  ll ans=-d[N-1];

  bool negative[NMAX];
  for(int i=0;i<N;i++) negative[i]=0;
  for(int loop=0;loop<N;loop++){
    for(int i=0;i<M;i++){
      if(d[a[i]]==INF) continue;
      if(d[b[i]]>d[a[i]]+c[i]){
        d[b[i]]=d[a[i]]+c[i];
        negative[b[i]]=1;
      }
      if(negative[a[i]]) negative[b[i]]=1;
    }
  }
  if(negative[N-1]) cout<<"inf"<<endl;
  else cout<<ans<<endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>

#define rep(i, n) for(int i = 0; i < (n); i++)
#define MP make_pair

using namespace std;

typedef pair<int, int> P;
typedef long long ll;
typedef vector<int> vi;

int main(){
    int n, k;
    while(cin >> n >> k, n|k){
        vector<int> v(k);
        rep(i, k) cin >> v[i];
        sort(v.begin(), v.end());
        int prev = 0, seq = 0;
        int ans = 0;
        for(int i = 1+!v[0]; i <= k; i++){
            if(i != k && v[i] == v[i-1]+1){
                seq++;
            }else{
				seq++;
				if(!v[0]) ans = max(ans, prev+seq+1);
				else ans = max(ans, seq);
                prev = seq;
                seq = 0;
            }
        }
        cout << ans << endl;
    }
	return 0;
}

Measured memory_bytes: 1272
#include <bits/stdc++.h>
#define PI 3.1415926535897
using namespace std;
typedef long long ll;
const int INF = 1000000000;
const ll LINF = 1000000000000000000;//1e18
const double EPS = 1e-10;

int main(void)
{
    int D, T, S;
    cin >> D >> T >> S;

    if (T * S >= D)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
    return (0);
}

Measured memory_bytes: 3592
using System;
using System.Collections.Generic;
using System.Text;
using System.Numerics;
using System.IO;


namespace SolutionCS
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split(' ');
            int N = int.Parse(s[0]);
            int K = int.Parse(s[1]);

            int[] v = new int[N];

            s = Console.ReadLine().Split(' ');
            for (int i = 0; i < N; i++)
            {
                v[i] = int.Parse(s[i]);
            }

            int ans = int.MaxValue;
            for (int i = 0; i <= N - K; i++)
            {
                int l = i;
                int r = i + K - 1;


                if(Math.Sign(v[l]) == Math.Sign(v[r]))
                {
                    ans = Math.Min(ans, Math.Max(Math.Abs(v[l]), Math.Abs(v[r])));
                }
                else
                {
                    int d0 = Math.Abs(v[l]);
                    int d1 = Math.Abs(v[r]);

                    if ( d0 < d1)
                    {
                        d0 *= 2;
                    }
                    else
                    {
                        d1 *= 2;
                    }

                    ans = Math.Min(ans, d0 + d1);
                }
            }

            Console.WriteLine(ans);
        }
    }
}


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

def total_cost(a):
    before = 0
    total = 0
    for x in a:
        total += abs(x - before)
        before = x
    return total

def main():
    input()
    A = [0] + [int(c) for c in input().split()] + [0]
    total = total_cost(A)
    for i, x in enumerate(A):
        if i == 0 or i == len(A)-1: continue
        prev_cost = abs(x - A[i-1])
        succ_cost = abs(A[i+1] - x)
        print(total - (prev_cost+succ_cost) + abs(A[i+1]-A[i-1]))

if __name__ == "__main__":
    main()

Measured memory_bytes: 14044
// https://atcoder.jp/contests/abc134/tasks/abc134_c

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

#define REP(i,n)   for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for(int i=(b); i<=(int)(e); i++)
#define DUMP(a) REP(_i, a.size()) cout << a[_i] << (_i + 1 == a.size() ? "\n" : " ")
#define DUMP2D(a) REP(_i, a.size()) DUMP(a[_i]); cout << endl

const int N_MAX = 200000;
const int A_I_MAX = 200000;

int N;
vector<int> A;

int main() {
  cin >> N;
  A = vector<int>(N);
  REP(i, N) cin >> A[i];

  int v1 = 0;
  int v2 = 0;
  REP(i, N) {
    if (A[i] >= v1) {
      v2 = v1;
      v1 = A[i];
    } else if (A[i] >= v2) {
      v2 = A[i];
    }
  }

  REP(i, N) {
    if (A[i] == v1) cout << v2 << endl;
    else cout << v1 << endl;
  }
}


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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<")"<< endl
#define BINARY(x) static_cast<bitset<16> >(x)

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

#define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()

typedef long long ll;
const int INF   = 1e9;
const ll  INFLL = 1e18;
typedef pair<int, int> PII;
typedef vector<vector<int>> Graph;
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};

int main()
{
    ios::sync_with_stdio(false);

    int N; cin >> N;
    vector<ll> A(N), sum(N+1);
    map<ll, ll> cnt;
    cnt[0]++;
    rep(i, N) {
        cin >> A[i];
        sum[i+1] = sum[i] + A[i];
        cnt[sum[i+1]]++;
    }

    ll ans = 0;
    for (auto m : cnt) {
        ans += m.second * (m.second - 1) / 2;
    }
    cout << ans << endl;
}

Measured memory_bytes: 15872
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
using namespace std;

int main() {
        int n;
        scanf("%d", &n);
        map<long long, int> cnt;
        for (int i = 0; i < n; i ++) {
                int a;
                scanf("%d", &a);
                cnt[a] ++;
        }
        long long ans = 0;
        for (auto c : cnt) {
                if (c.first > c.second) {
                        ans += c.second;
                } else {
                        ans += c.second - c.first;
                }
        }
        printf("%lld\n", ans);
        return 0;
}



Measured memory_bytes: 6272
#include <algorithm>
#include <array>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include <cassert>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
using namespace std;

int main() {
    string S;
    string T = "CODEFESTIVAL2016";
    cin >> S;
    int ret = 0;
    rep(i, 16) {
        if(S[i] != T[i]) ret++;
    }
    cout << ret << endl;
    return 0;
}



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

#define REP(i,n) for(int i=0; i < (n); ++i)
#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)

using ll = long long;
#define INF 1e9

int main(){
  int n;
  cin >> n;
  vector<string> S(n);
  vector<int> cntm('z'-'a'+1,INF);
  REP(i, n){
    cin >> S[i];
  }
  REP(i, n){
    vector<int> cnt('z'-'a'+1, 0);
    REP(j, S[i].length()){
      cnt[S[i][j]- 'a']++;
    }
    REP(j, 'z'-'a'+1){
      cntm[j] = min(cntm[j], cnt[j]);
    }
  }

  REP(i, 'z'-'a'+1){
    REP(j, cntm[i]){
      cout << (char)('a'+i);
    }
  }
  cout << endl;
  return 0;
}


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

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

int A, B, C;

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

  std::cin >> A >> B >> C;

  int sum = A + B + C;
  if(sum <= 21){
      std::cout << "win" << std::endl;
  }else{
      std::cout << "bust" << std::endl;
  }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int n;
long long h[300000],k,v[300000],tree[1000000],ans;
pair<int,int> b[300000];
void upd(int node,int l,int r,int s,long long val)
{
    if (l==r)
    {
        tree[node]=val;
        return ;
    }
    if (s<=(l+r)/2) upd(node*2,l,(l+r)/2,s,val);
    else upd(node*2+1,(l+r)/2+1,r,s,val);
    tree[node]=max(tree[node*2],tree[node*2+1]);
    return;

}
long long que(int node,int l,int r,int s)
{
    if (s>=r) return tree[node];
    if (s>(l+r)/2) return max(que(node*2,l,(l+r)/2,s),que(node*2+1,(l+r)/2+1,r,s));
    else return que(node*2,l,(l+r)/2,s);
}
int main()
{

    cin>>n;
    for (int i=0;i<n;i++)
    {
        cin>>h[i];
        b[i].first=h[i];
        b[i].second=i;
    }
    sort(b,b+n);
    for (int j=0;j<n;j++)
    {
        cin>>v[j];
    }
    for (int i=0;i<n;i++)
    {
        long long cur=que(1,0,n-1,b[i].second)+v[b[i].second];
        ans=max(ans,cur);
       // cout<<b[i].second<<"  "<<que(1,0,n-1,b[i].second)+v[b[i].second]<<endl;
        upd(1,0,n-1,b[i].second,cur);

    }
     cout<<ans;

}


Measured memory_bytes: 14080
N, M = map(int, input().split())

a = [0] * N

# 各スイッチがどの電球に繋がっているか
for i in range(M):
    l = list(map(int, input().split()))
    k = l[0]
    S = l[1:]
    for j in range(k):
        s = S[j]
        s -= 1
        # s番目のスイッチにi番目の電球が繋がっている
        a[s] |= 1 << i

p = 0
l = list(map(int, input().split()))
for i in range(M):
    p |= l[i] << i

ans = 0
# スイッチの押し方全通り
for s in range(1 << N):
    t = 0
    for i in range(N):
        # iビット目が立っているか
        if s >> i & 1:
            t ^= a[i]
    if t == p:
        ans += 1

print(ans)


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

int main() {
  int n;
  cin >> n;
  int a[n];
  for(int i=0;i<n;i++) cin >> a[i];
  int ans=1<<29;
  for(int j=0;j<a[0]+1;j++) {
    int c[n],x=j;
    for(int i=0;i<n;i++) c[i]=a[i];
    c[0]-=j;
    for(int i=0;i<n-1;i++) {
      int y=max(0,c[i]);
      c[i]-=y;
      c[i+1]-=y;
      x+=y*2;
    }
    ans=min(ans,x+max(0,c[n-1]));
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 3132
import sys
input = sys.stdin.readline
n,q=map(int,input().split())
import heapq
S=[]
D=dict()
for i in range(n):
  s,t,x=map(int,input().split())
  S.append((1,x,max(0,s-x)))
  S.append((-1,x,max(0,t-x)))
  D[x]=False
S.sort(key=lambda x:x[2])
X=[]
cur=0
for i in range(q):
  d=int(input())
  while cur<2*n:
    a,b,c=S[cur]
    if c>d:
      break
    else:
      cur+=1
      if a==1:
        heapq.heappush(X,b)
        D[b]=True
      else:
        D[b]=False
  while X:
    ans=heapq.heappop(X)
    if D[ans]:
      print(ans)
      heapq.heappush(X,ans)
      break
  if not X:
    print(-1)


Measured memory_bytes: 125324
n,m=map(int,input().split())
height = list(map(int,input().split()))
lst = [0] * n
for j in range(m):
  a,b=map(int,input().split())
  if height[a-1]<height[b-1]:
    if height[b-1] <0:
      lst[a-1]=-100
    else:
      lst[a-1]=-100
      lst[b-1]+=1
  elif height[a-1]>height[b-1]:
    if height[a-1]<0:
      lst[b-1]=-100
    else:
      lst[a-1]+=1
      lst[b-1]=-100
  elif height[a-1]==height[b-1]:
    lst[a-1] = -100
    lst[b-1] = -100
cnt = 0
for k in range(n):
  if lst[k]>=0:
    cnt+=1
print(cnt)

Measured memory_bytes: 20164
#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n,m,a[11111],sum=0;
  cin>>n>>m;
  for(int i=0;i<m;i++){
    cin>>a[i];sum+=a[i];
  }
  if(sum>n)cout<<-1;
  else cout<<n-sum;
  return 0;
}

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

int main() {
  long L, R;
  cin >> L >> R;

  if (R - L > 2019) return cout << 0 << "\n", 0;

  long ans = 9e18;
  for (long i = L; i <= R; i++) {
    for (long j = i + 1; j <= R; j++) {
      ans = min(ans, i * j % 2019);
    }
  }

  cout << ans << "\n";
}

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

using namespace std;

int get(const vector<int>& a, int k) {
	int ans = 0;
	for (int x : a) {
		ans += (x + k - 1) / k - 1;
	}
	return ans;
}


int main() {
	int n, k;
	cin >> n >> k;
	vector<int>  a(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int l = 0;
	int r = *max_element(a.begin(), a.end());
	while (r - l > 1) {
		int mid = (l + r) / 2;
		if (get(a, mid) > k) {
			l = mid;
		} else {
			r = mid;
		}
	}
	cout << r << endl;
}

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

signed main(void)
{
	int n, k, score, sum;
	double ans;

	cin >> n >> k;
	ans = 0;
	rep(i, n)
	{
		score = i + 1;
		sum = n;
		while(1 <= score && score <= k - 1)
		{
			score *= 2;
			sum *= 2;
		}
		ans += (double)1 / sum;
	}
	cout << fixed << setprecision(10) << ans << endl;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for(int i = a; i < b; i++)
#define  all(x) (x).begin(),(x).end()     // 昇順ソート
#define  rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define  FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
using P = pair<int,int>;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; }

int main(){
    ll n;
    cin >> n;
    ll ans = 0;
    for(ll r = 1; r * r < n; r++){
        if (((n-r) % r) == 0) {
            ll m = (n-r)/r;
            if (n % m != r) continue;
            ans += (n-r)/r;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3652
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;

typedef long long ll;
//typedef pair<int,int> Pint;
typedef pair<ll, ll> P;
//typedef pair<int, pair<int, int>> P;
//typedef tuple<int,int,int> T;
typedef vector<ll> vec;
typedef vector<vec> mat;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define revrep(i, n) for(ll i = n-1; i >= 0; i--)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll Mypow(ll x, ll k){
  ll res = 1;
  while(k > 0){
    if(k % 2) res *= x;
    x *= x;
    k /= 2;
  }
  return res;
}

ll gcd(ll a, ll b){//計算量はO(log(max(a,b))
    if(b == 0) return a;
    return gcd(b, a % b);
}

ll INFL = 1LL << 60;//10^18 = 2^60
int INF = 1 << 30;//10^9
ll MOD  = 1000000007;
//vector<int> dy = {0,0,1,-1};
//vector<int> dx = {1,-1,0,0};

ll kai(ll x){
  if(x == 1) return 1;
  return x * kai(x-1) % MOD;
}

int N;
struct edge{int to; ll cost;};

vector<edge> G[100010];

ll d[100010];

void dfs(int now, int from){
  rep(i, G[now].size()){
    edge e = G[now][i];
    if(e.to == from) continue;
    d[e.to] = d[now] + e.cost;
    dfs(e.to, now);
  }
}


int main(){
  cin >> N;
  rep(i, N-1){
    int a, b;
    ll c;
    cin >> a >> b >> c;
    a--, b--;
    edge e; e.to = b, e.cost = c;
    G[a].push_back(e);
    e.to = a;
    G[b].push_back(e);
  }
  int Q, K; cin >> Q >> K;
  K--;
  dfs(K, -1);
  rep(i, Q){
    int x, y;
    cin >> x >> y;
    x--, y--;
    cout << d[x] + d[y] << endl;
  }
}


Measured memory_bytes: 15872
import java.io.*;
import java.util.*;

public class Main {

    private static FastReader in = new FastReader(System.in);
    private static PrintWriter out = new PrintWriter(System.out);

    private static final long MOD = (long) 1e9 + 7;

    public static void main(String[] args) {
        solve();
        out.flush();
    }

    private static void solve() {
        int n = in.nextInt();
        int[] b = in.nextIntArray(n-1);

        long ans = 0;
        ans += b[0];
        ans += b[n-2];
        for (int i=1; i < n-1; i++) {
            ans += Math.min(b[i-1], b[i]);
        }
        out.println(ans);
    }

    // https://www.geeksforgeeks.org/fast-io-in-java-in-competitive-programming/
    static class FastReader {
        BufferedReader br;
        StringTokenizer st;

        public FastReader(InputStream inputStream) {
            br = new BufferedReader(new
                    InputStreamReader(inputStream));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException  e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        // https://qiita.com/masakinpo/items/5f77f2a879578a85a0bb
        public int[] nextIntArray(int n) {
            int[] a = new int[n];
            for (int i = 0; i < n; i++)
                a[i] = nextInt();
            return a;
        }

        public long[] nextLongArray(int n) {
            long[] a = new long[n];
            for (int i = 0; i < n; i++)
                a[i] = nextLong();
            return a;
        }
    }

}


Measured memory_bytes: 21972
#include <iostream>
#include <string>
#include <queue>
#include <numeric>
#include <algorithm>
using namespace std;

int main(){

	int n, p;
	while(cin >> n >> p, n|p){

		vector<int> people(n);
		int stone = p;
		bool finish = false;
		for(int i=0; !finish; i++){
			i = i%n;
			if(stone != 0){
				people[i]++;
				stone--;
			}else{
				stone += people[i];
				people[i] = 0;
			}
			if(people[i] == p){
				cout << i << endl;
				finish = true;
			}
		}
	}
	return 0;
}

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

int main() {
	int n,d;
	cin >> n;
	d = n%10;
	if((d==2)||(d==4)||(d==5)||(d==7)||(d==9)){
		cout << "hon" << endl;
	}
	else if((d==0)||(d==1)||(d==6)||(d==8)){
		cout << "pon" << endl;
	}
	else cout << "bon" << endl;
	return 0;
}

Measured memory_bytes: 3592
#include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(a)-1;i>=b;i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define printV(_v) for(auto _x:_v){cout<<_x<<" ";}cout<<endl
#define printVS(_vs) for(auto _x : _vs){cout << _x << endl;}
#define printVV(_vv) for(auto _v:_vv){for(auto _x:_v){cout<<_x<<" ";}cout<<endl;}
#define printP(_p) cout << _p.first << " " << _p.second << endl
#define printVP(_vp) for(auto _p : _vp) printP(_p);

typedef long long ll;
typedef pair<int, int> Pii;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<vector<int>> Graph;
const int inf = 1e9;
const int mod = 1e9 + 7;

struct edge { int to, cap, rev; };

const int MAX_V = 300;
vector<edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];

void add_edge(int from, int to, int cap) {
    G[from].emplace_back((edge){to, cap, (int)G[to].size()});
    G[to].emplace_back((edge){from, 0, (int)G[from].size() - 1});
}

void bfs(int s) {
    memset(level, -1, sizeof(level));
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while (!que.empty()) {
        int v = que.front(); que.pop();
        for (int i = 0; i < G[v].size(); i++) {
            edge &e = G[v][i];
            if (e.cap > 0 && level[e.to] < 0) {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
}

int dfs(int v, int t, int f) {
    if (v == t) return f;
    for (int &i = iter[v]; i < G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && level[v] < level[e.to]) {
            int d = dfs(e.to, t, min(f, e.cap));
            if (d > 0) {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s, int t) {
    int flow = 0;
    for (;;) {
        bfs(s);
        if (level[t] < 0) return flow;
        memset(iter, 0, sizeof(iter));
        int f;
        while ((f = dfs(s, t, inf)) > 0) {
            flow += f;
        }
    }
}


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

    int n, m;
    cin >> n >> m;
    map<Pii, int> mp;
    rep(i, m) {
        int a, b;
        cin >> a >> b;
        a--; b--;
        mp[Pii(a, b)] = i;
        add_edge(a, b, 1);
        add_edge(b, a, 1);
    }
    int s, t;
    cin >> s >> t;
    s--; t--;

    int F = max_flow(s, t);
    int R = 0;
    set<int> st;
    rep(i, n) {
        for (auto e : G[i]) {
            if (mp.count(Pii(i, e.to)) && e.cap != 0 && G[e.to][e.rev].cap == 0 && !st.count(mp[Pii(i, e.to)])) {
                R++;
                st.insert(mp[Pii(i, e.to)]);
            }
        }
    }

    cout << F << endl;
    cout << R << endl;
    for (auto i : st) {
        cout << i + 1 << endl;
    }

}

Measured memory_bytes: 3436
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	ll n;
	cin>>n;
	ll a[n+5];
	for(ll i=0;i<n;i++)
	{
		cin>>a[i];
	}
	ll ans = 0;
	for(ll i=0;i<n;i++)
	{
		for(ll j=0;j<i;j++)
		{
			ans+=(a[i]*a[j]);
		}
	}
	cout<<ans;
}

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

#define int long long 
#define double long double
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define RE(i,n) for (int i = 1; i <= n; i++)
#define RED(i,n) for (int i = n; i > 0; i--)
#define REPS(i,n) for(int i = 1; (i*i) <= n; i++)
#define REP(i,n) for (int i = 0; i < n; i++)
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REPD(i,n) for (int i = n-1; i >= 0; i--)
#define FORD(i,a,b) for (int i = a; i >= b; i--)
#define remax(a,b) a = max(a,b)
#define remin(a,b) a = min(a,b)
#define all(v) v.begin(),v.end()
#define pii pair<int,int>
#define mii map<int,int>
#define vi vector<int>
#define vvi vector<vi>
#define WL(t) while(t --)
#define gcd(a,b) __gcd((a),(b))
#define lcm(a,b) ((a)*(b))/gcd((a),(b))
#define print(arr) for (auto it = arr.begin(); it != arr.end(); ++it) cout << *it << " "; cout << endl;
#define debug(x) cout << x << endl;
#define debug2(x,y) cout << x << " " << y << endl;
#define debug3(x,y,z) cout << x << " " << y << " " << z << endl;
const int INF = 1e18+1;
const int MOD = 1e9+7;
const double PI = 3.14159265358979323846264338;

int fast_ex(int a,int n,int m){
  if(n == 0)return 1;
  if(n == 1)return a;
  int x = 1;
    x *= fast_ex(a,n/2,m);
    x %= m;
    x *= x;
    x %= m;
    if(n%2)x*= a;
    x %= m;
    return x;
}

string s;

int comp(int i,int j){
	stack<int> q;
    FOR(k,i,j+1){
    	q.push(s[k]-'0');
    }
    int cur = 1;
    int res = 0;
    while(!q.empty()){
       res += cur*q.top();
       q.pop();
       cur *= 10;
    }
    return res;
}

void solve(){
	 cin >> s;
	 int n = s.size();
	 int ans = 0;
	 RE(i,n){
        RE(j,i){
        	int po = max(n-n,(j-2))+max((n-i-1),n-n);
        	//debug2(comp(j-1,i-1),po);
        	ans += comp(j-1,i-1)*fast_ex(2,po,MOD);
        }
	 } 
	 cout << ans;
}

signed main(){
  ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  //freopen(".in","r",stdin);freopen(".out","w",stdout);
  int t = 1;//cin >> t;
  WL(t)solve();
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
bool lucky7(int);

int main(){

    int n;
    cin>>n;
    bool r = lucky7(n);
    if(r)
    cout<<"Yes"<<endl;
    else
    cout<<"No"<<endl;
    return 0;
}
bool lucky7(int n){
    while(n>0){
        int rem = n%10;
        if(rem==7)
        return true;
        n/=10;
    }
    return false;
}

Measured memory_bytes: 3636
require 'pp'

# 空白区切の入力値を数値の配列で返却する
def gets_i_list()
  gets.chomp.split(" ").map(&:to_i)
end

N, M = gets_i_list

students = []
N.times do
  students.push(gets_i_list)
end

checkpoints = []
M.times do
  checkpoints.push(gets_i_list)
end

students.each do |s|
  sx, sy = s
  min_point    = nil
  min_distance = nil
  checkpoints.each_with_index do |c, i|
    cx, cy = c
    distance = (sx - cx).abs + (sy - cy).abs
    if min_point.nil?
      min_point = i + 1
      min_distance = distance
    elsif !min_point.nil? && min_distance > distance
      min_point = i + 1
      min_distance = distance
    end
  end
  puts min_point
end


Measured memory_bytes: 2044
#include<iostream>
using namespace std;
bool f(int a)
{
    for(int i=2;i*i<=a;i++)if(a%i<1)return 0;
    return 1;
}
int n;
main()
{
    while(cin>>n,n)
    {
        int ans=0;
        for(int i=2;n;i++)if(f(i))n--,ans+=i;
        cout<<ans<<endl;
    }
}


Measured memory_bytes: 3044
#include <bits/stdc++.h>
using namespace std;
const int maxn=(1<<21)+10;
int n,m,f[21][maxn],lg[maxn],tr[maxn][2];
int main()
{
	scanf("%d%d",&n,&m);
	for(int j=0;j<=n;j++)
		for(int i=0;i<(1<<j);i++)
			scanf("%1d",&f[j][i^(1<<j)]);
	lg[0]=-1;
	for(int i=1;i<(1<<(n+1));i++) lg[i]=lg[i>>1]+1;
	for(int i=2;i<(1<<(n+1));i++)
		if((i>>(lg[i]-1))&1)
			tr[i][1]=i^(1<<lg[i]),tr[i][0]=tr[tr[i][1]][0];
		else
			tr[i][0]=i^(3<<(lg[i]-1)),tr[i][1]=tr[tr[i][0]][1];
	for(int j=n;j;j--)
		for(int i=(1<<j);i<(1<<(n+1));i++)
		{
			int k = i >> j, l = (i & ((1 << j) - 1)) ^ (1 << j);
			if(tr[l][0]) f[lg[tr[l][0]]][(((k << 1) ^ 1) << lg[tr[l][0]]) ^ tr[l][0]] += f[j][i];
			if(tr[l][1]) f[lg[tr[l][1]]][(((k << 1) ^ 0) << lg[tr[l][1]]) ^ tr[l][1]] += f[j][i];
			f[0][k] += f[j][i];
		}
	int ans=1;
	for(int i=2;i<(1<<(n+1));i++)
		if(f[0][i]>=m && lg[i]>lg[ans]) ans=i;
	int j=n;
	while((~ans>>j)&1) j--;
	while(j--) putchar(((ans>>j)&1)+'0');
	putchar('\n');
	return 0;
}

Measured memory_bytes: 192188
n=int(input())
lists=list(map(int,input().split()))
lists=sorted(lists,reverse=True)

mod=10**9+7
dp=[0 for i in range(n+1)]
dp[1]=2*lists[0]
for i in range(1,n):
    x=lists[i]
    y=pow(2,i,mod)
  
    dp[i+1]=4*dp[i]+(i+2)*(y**2)*lists[i]
    dp[i+1]%=mod

print(dp[n]%mod)

Measured memory_bytes: 25740
#include<iostream>
#include<map>
#include<cstdio>
#include<vector>
#include<algorithm>
#define F first
#define S second

using namespace std;

class Pox
{
public:
  int rlen,llen,rnext,lnext;
  Pox(int rlen = 0,int llen = 0,int rnext = 0,int lnext = 0):rlen(rlen),llen(llen),rnext(rnext),lnext(lnext){}
};

int lcm(int a,int b)
{
  return a/__gcd(a,b)*b;
}

int rec(vector<Pox>& G,int p)
{

  int rcost = !G[p].rnext?1:rec(G,G[p].rnext);
  int lcost = !G[p].lnext?1:rec(G,G[p].lnext);

  int lc = lcm(rcost*G[p].rlen,lcost*G[p].llen);

  return lc/G[p].rlen + lc/G[p].llen;

}

int main()
{
  int n;
  while(cin >> n,n)
    {
      vector<Pox> G;
      G.resize(n+1);
      bool TPO[n+1];
      for(int i=0;i<n+1;i++)
	TPO[i] = false;
      for(int i=1;i<=n;i++)
	{
	  int a,b,c,d;
	  scanf("%d %d %d %d",&a,&b,&c,&d);
	  G[i] = Pox(a,b,c,d);
	  TPO[c] = TPO[d] = true;
	}

      int st = -1;
      for(int i=1;i<=n;i++)
	if(!TPO[i])
	  {
	    st = i;
	    break;
	  }
	
      cout << rec(G,st) << endl;


    }
  return 0;
}

Measured memory_bytes: 1244
/*
これを入れて実行
g++ code.cpp
./a.out
 */
 
#include <iostream>
#include <cstdio>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>
#include <random>

#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;
 
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}
 
ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll conbinationMemo[100][100];

void cmemoInit(){
    rep(i, 100){
        rep(j, 100){
            conbinationMemo[i][j] = -1;
        }
    }
}
 
ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
 
ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
//-----------------------ここから-----------
int main(void){
    ll n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];

    vector<pair<ll,ll>> vec;
    rep(i, n){
        vec.push_back({a[i], i});
    }

    sort(all(vec));
    ll ans = 0;
    rep(i, n){
        if(i % 2 != vec[i].second % 2) ans++;
    }
    cout << ans / 2 << endl;
}



Measured memory_bytes: 6136
from collections import Counter

n = int(input())
v = list(map(int, input().split()))

odd = v[::2]
even = v[1::2]

ac = Counter(odd).most_common(2)
bc = Counter(even).most_common(2)

if ac[0][0] != bc[0][0]:
    print(n-ac[0][1]-bc[0][1])
else:
    if len(ac) == 1 and len(bc) == 1:
        print(n//2)
    else:
        print(n-max(ac[0][1]+bc[1][1], ac[1][1]+bc[0][1]))

Measured memory_bytes: 15972
#include<iostream>
#include<string>
using namespace std;
int main(){
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	string s;
	cin>>s;
	int ans=0;
	for(int i=0,j=s.size()-1;i<=j;i++,j--){
		if(s[i]!=s[j]){
			ans++;
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
    int n, t;
    cin >> n >> t;
    
    int x, y = 0, sum = 0;
    for (int i = 0; i < n; i++) {
        cin >> x;
        
        if (x - y < t) {
            sum += x - y;
        }else{
            sum += t;
        }
        y = x;
        //cout << sum << endl;
    }
    //  i = 0 のときの t 
    sum += t;
    cout << sum << endl;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <set>
#include <algorithm>

using namespace std;

#define ll long long
#define _ABS(x) (((x) < 0) ? (-(x)):(x))

int main()
{
	int N, X;

	scanf("%d %d", &N, &X);

	int cur = 0;
	int ans = 1;

	for (int i = 0; i < N; ++i)
	{
		int L;

		scanf("%d", &L);
		cur += L;

		if (cur <= X) ans++;
	}

	printf("%d\n", ans);

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long
#define rep(i, a, n) for(int (i) = (int)(a);(i) < (int)(n);++(i))
#define repe(i, a, n) for(int (i) = (int)(a);(i) <= (int)(n);++(i))
#define repif(i, a, x) for(int (i) = (int)(a);(x);++(i))
#define repr(i, a, n) for(int (i) = ((int)(a)-1);(i) >= (int)(n);--(i))
#define reper(i, a, n) for(int (i) = (int)(a);(i) >= (int)(n);--(i))
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define RNG(x, a, n) &((x)[a]), &((x)[n])
#define ADD(a, b) ((((a+MOD)%MOD)+((b+MOD)%MOD))%MOD)
#define MUL(a, b) ((((a)%MOD)*((b)%MOD))%MOD)
#define CHADD(a, b) (a = ADD(a, b))
#define CHMUL(a, b) (a = MUL(a, b))
#define CEIL(x) ((int)ceil(((double)x)))
#define POW(x, y) ((int)pow(x, y))
#define UNIQUE(x) (x).erase(unique(ALL((x))), (x).end())
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define fcout cout << fixed << setprecision(10)
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define PI (acos(-1))
#define EPS (1e-10)
#define INF LLONG_MAX
#define MOD 1000000007
using namespace std;
using VI = vector<int>;
using PII = pair<int, int>;
using VPII = vector<PII>;
using Complex = complex<double>;
template<typename T, template<typename> typename C> using PQ = priority_queue<T, vector<T>, C<T>>;
template<typename T> bool chmax(T& a, const T& b){ if(a<b){ a=b; return true; } return false; }
template<typename T> bool chmin(T& a, const T& b){ if(a>b){ a=b; return true; } return false; }


int N, M;
int A[100010];
PII BC[100010];


signed main(){
    cin >> N >> M;
    rep(i, 0, N) cin >> A[i];
    rep(i, 0, M){
        int B, C;
        cin >> B >> C;
        BC[i] = { C, B };
    }

    sort(RNG(A, 0, N));
    sort(RNG(BC, 0, M), greater<PII>());

    int ans = 0;
    int idx = 0, pidx = 0;
    rep(i, 0, M){
        int C = BC[i].first;
        int B = BC[i].second;
        idx = lb(RNG(A, pidx, N), C)-&A[0];
        if(idx == 0) break;
        if(pidx+B <= idx) ans += B*C, idx = pidx+B;
        else ans += (idx-pidx)*C;
        pidx = idx;
    }

    rep(i, idx, N) ans += A[i];

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 2560
n, k = map(int, input().split())
mod = 10 ** 9 + 7
def comb(n, r):
    if n < r:return 0
    if n < 0 or k < 0:return 0
    return fa[n] * fi[r] % mod * fi[n - r] % mod
fa = [1] * (n + 1)
fi = [1] * (n + 1)
for i in range(1, n + 1):
    fa[i] = fa[i - 1] * i % mod
    fi[i] = pow(fa[i], mod - 2, mod)
ans = 0
for i in range(min(k, n - 1) + 1):
    ans += comb(n, i) * comb(n - 1, i) % mod
print(ans % mod)

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

int main() {
  
  int N, X, Y;
  cin >> N >> X >> Y;
  X--;
  Y--;
  vector<int> klist(N-1);
  for (int i = 0; i < N-1; i++) {
    for (int j = i+1; j < N; j++) {
      const int inf = 100000;
      int ans = inf;
      if (j<=X || Y <= i) ans= j-i;
      else if (i<=X && X<=j && j<=Y) ans=min(X-i+1+Y-j, j-i);
      else if (X<=i && i<=Y && X<=j && j<=Y) ans=min(j-i, i-X+Y-j+1);
      else if (X<=i && i<=Y && Y<=j) ans=min(j-i, i-X+1+j-Y);
      else if (i<X && Y<=j) ans=X-i+j-Y+1;
      
      if (ans>=N-1) continue;
      else klist.at(ans-1)++;
    }
  }
  
  for (int i = 0; i < N-1; i++) {
    cout << klist.at(i) << endl;
  }
  
}


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

#define pb push_back
#define ff first
#define ss second
#define endl '\n'
#define fast ios::sync_with_stdio(false); cin.tie(NULL);
#define int long long

signed main(){
	
	int n , m;
	cin>>n>>m;

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

		for(int j = i+1; j < n ; ++j){

			int d = 0;

			for(int k = 0 ; k < m ; ++k){

				d = d+(a[i][k]-a[j][k])*(a[i][k]-a[j][k]);
			}

			int s = sqrt(d);

			if(s*s==d)
				ans++;
		}
	}

	cout<<ans<<endl;
}



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

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef long long ll;

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int INT_INF = 2147483647;
const long long LL_INF = 1LL<<60;
const long long MOD = 1000000007;

#define CLR(a) memset((a), 0 ,sizeof(a))

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

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

int main(void){
    ll S; cin >> S;
    ll X1=0,Y1=0,X2=1e9,Y2=1,X3,Y3;
    Y3 = (S+X2-1)/X2;
    X3 = X2*Y3 - S;
    cout << X1 << " " << Y1 << " " << X2 << " " << Y2 << " " << X3 << " " << Y3 << endl;
    return 0;
}

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

int main()
{
	ios::sync_with_stdio(false);
	
	int K, T; 
	cin >> K >> T;

	vector<int> a(T);
	for(int i = 0; i < T; ++i)
		cin >> a[i];
	sort(a.begin(), a.end(), greater<int>());
	if(T == 1)
	{
		cout << a[0]-1 << endl;
		return 0;
	}

	int ind1 = 0, ind2 = 1;
	while(ind1 != ind2)
	{
		while(ind1 < T && a[ind1] == 0) ++ind1;
		while(ind2 < T && (a[ind2] == 0 || ind2 == ind1)) ++ind2;
		if(ind1 == T || ind2 == T) break;
		--a[ind1];
		--a[ind2];
		//cout << ind1 << " " << ind2 << endl;
	}
	if(ind2 == T && ind1 < T)
		--a[ind1];
	int res = 0;
	for(int i = 0; i < T; ++i)
	{
		//cout << a[i] << " ";
		res += a[i];
	}
	//cout << endl;
	cout << res << endl;
	return 0;
}

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

typedef long long int ll;
typedef vector<int> vi;            typedef vector<ll> vl;
typedef vector<vi> vvi;         typedef vector<vl> vvl;
typedef pair<int, int>     pii;    typedef pair<ll, ll> pll;
typedef vector<pii> vii;         typedef vector<pll> vll;
typedef map<int,int> mii;
typedef vector<char> vc;
typedef vector<bool> vb;
#define endl '\n'
#define forn(i,n) for(int i=0;i<n;i++)
#define rforn(i,n) for(int i=n-1;i>=0;i--)
#define forne(i,n) for(int i=1;i<=n;i++)
#define rforne(i,n) for(int i=n;i>=1;i--)
#define forse(i,s,e) for(int i=s;i<e;i++)
#define rforse(i,s,e) for(int i=e-1;i>=s;i--)
#define all(x) x.begin(),x.end()
#define sz(x) (ll)x.size()
#define MOD 1000000007
#define F first
#define S second
#define pb push_back
#define pf push_front
#define rb pop_back
#define rf pop_front
#define in insert
#define mp make_pair
#define fill(a,x) memset(a,x,sizeof a);
#define trav(a,x) for(auto &a:x)
#define min3(a, b, c) min(min(a, b), c)
#define min4(a, b, c, d) min(min(a, b), min(c, d))
#define max3(a, b, c) max(max(a, b), c)
#define max4(a, b, c, d) max(max(a, b), max(c, d))
const ll INFL = 1e18;
const int INFI = 1e9;
#ifdef KARAN
    #include "../../trace.h"
#else
    #define trace(args...)
#endif

ll div_floor(const ll &a,const ll &b) { return a / b - (((a ^ b) < 0) and a % b);}
ll div_ceil(const ll &a,const ll &b) { return a / b + (((a ^ b) >= 0) and a % b);}
ll modpow(ll a, ll b, ll m = MOD){a %= m; ll res = 1; 
    while (b) {if (b & 1)res = (res * a) % m; a = (a * a) % m; b >>= 1;} return res;}
ll bpow(ll a, ll b){ll res = 1; while (b) {if (b & 1)res = res * a; a = a * a; b >>= 1;} return res;}
ll modinv(ll a, ll m = MOD) {return modpow(a, m - 2, m);}
void graph(vvi &adj, int m) {int x, y; forn(i, m){cin>>x>>y; adj[--x].pb(--y); adj[y].pb(x);}}

void solve(){
    string s, t;
    cin>>s>>t;
    int n = s.size(), m = t.size();
    s = " " + s;
    t = " " + t;
    vvl dp(n+1, vl(m+1, 0));
    forne(i, n)
        forne(j, m)
            if(s[i]==t[j])
                dp[i][j] = dp[i-1][j-1] + 1;
            else
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
    int len = dp[n][m];
    int i = n, j = m;
    string ans = "";
    while(len>0){
        if(s[i]==t[j]) ans += s[i], i--, j--, len--;
        else if(dp[i][j]==dp[i-1][j]) i--;
        else j--; 
    }
    reverse(all(ans));
    cout<<ans<<endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifdef KARAN
        freopen("inp.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
        freopen("err.txt", "w", stderr);
    #endif
    int t=1;
    while(t--)
        solve();
}

Measured memory_bytes: 70784