akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector< vector<ll> >dp(3001,vector<ll>(3001,-1));
string s1,s2;
ll k;
void solve(ll n,ll m)
{
    for(ll i=0;i<=n;i++) dp[i][0]=0;
    for(ll i=0;i<=m;i++) dp[0][i]=0;
    for(ll i=1;i<=n;i++)
    {
        for(ll j=1;j<=m;j++)
        {
            if(s1[i-1]==s2[j-1])
            {
                dp[i][j]=1+dp[i-1][j-1];
            }
            else
            {
                dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
            }
        }
    }
    ll p=n,q=m;
    string res="";
    while(p>0 && q>0)
    {
        if(s1[p-1]==s2[q-1])
        {
            res.push_back(s1[p-1]);
            p--;
            q--;
        }
        else
        {
            if(dp[p-1][q]>dp[p][q-1])
            {
                p--;
            }
            else
            {
                q--;
            }
        }
    }
    reverse(res.begin(),res.end());
    cout<<res;
}
int main()
{
    cin>>s1>>s2;
    solve(s1.length(),s2.length());
}


Measured memory_bytes: 73564
#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <string>
#include <math.h>
typedef long long ll;
using namespace std;

int main() {
	ll a,b,c;
	cin>>a>>b>>c;
	ll ma=max(a,max(b,c));
	cout << a+b+c+ma*9<<endl;
	return 0;
}


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

Measured memory_bytes: 256
import sys


H, W, M = map(int, input().split())
bomb = [tuple(map(lambda x: int(x) - 1, s.split())) for s in sys.stdin.readlines()]
X = [0] * H  # X:各行の爆破対象の個数
Y = [0] * W  # Y:各列の爆破対象の個数
for h, w in bomb:
    X[h] += 1
    Y[w] += 1
maxX = max(X)
maxY = max(Y)

R = [h for h, x in enumerate(X) if x == maxX]  # R:爆破対象の数が最大となる行の番号
C = [w for w, y in enumerate(Y) if y == maxY]  # C:爆破対象の数が最大となる列の番号

bomb = set(bomb)
for r in R:
    for c in C:
        if (r, c) not in bomb:
            # (r, c)に爆破対象が存在しないとき, maxX + maxY が答えとなることが確定するため, 
            # 即座に探索を終了する. これによりループの回数は最大でもM+1回となる.
            print(maxX + maxY)
            exit()
print(maxX + maxY - 1)

Measured memory_bytes: 92520
import sys
input = sys.stdin.readline
from collections import defaultdict, deque
from itertools import permutations

n = int(input())
s, ps = [tuple(map(int, input().split())) for i in range(n)], list(permutations([i for i in range(1, n + 1)]))

def dist(i, j):
    x, y = s[i][0] - s[j][0], s[i][1] - s[j][1]
    return (x ** 2 + y ** 2) ** 0.5

print(sum(sum(dist(p[i]-1, p[i+1]-1) for i in range(n-1)) for p in ps) / len(ps))

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

//関数
long ue(long a){//真に大きい最小の2べきをO(log a)で返す。
    long result=2;
    while(result<=a){
        result*=2;
    }
    return result;
}

void display(vector<long> A){
    for(int i=0;i<A.size();i++){
        cout<<A.at(i)<<" ";
    }
    cout<<endl;
}

void displays(multiset<long> s){
    for(auto i=s.begin();i!=s.end();++i){
        cout<<*i<<" ";
    }
    cout<<endl;
}


    
//main関数
int main() {
    
    
    //入力
    long N;
    cin>>N;
    vector<long> A(N);
    for(int i=0;i<N;i++){
        cin>>A.at(i);
    }
    
    //display(A);
    
    
    
    //計算
    sort(A.begin(),A.end());
    vector<long> waiting(N);
    for(int i=0;i<N;i++){
        waiting.at(i)=ue(A.at(i))-A.at(i);
    }
    
    //display(waiting);
    
    //cout<<"kokokara"<<endl;
    long ans=0;
    multiset<long> waitingset;
    multiset<long> :: iterator ite;;
    for(int i=N-1;i>=0;i--){
        ite=waitingset.find(A.at(i));
        if(ite!=waitingset.end()){
            ans++;
            waitingset.erase(ite);
        }else{
            waitingset.insert(waiting.at(i));
        }
        //displays(waitingset);
    }
    
    
    
    //出力
    cout<<ans<<endl;
}


Measured memory_bytes: 12800
#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

#include <limits.h>

using namespace std;

typedef long long ll;

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;
}

const ll MOD = 998244353LL;

// auto mod int
// ref. https://github.com/atcoder-live/library/blob/master/mint.cpp
// https://youtu.be/L8grWxBlIZ4?t=9858
// https://youtu.be/ERZuLAxZffQ?t=4807 : optimize
// https://youtu.be/8uowVvQ_-Mo?t=1329 : division
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%MOD+MOD)%MOD){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += MOD-a.x) >= MOD) x -= MOD;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const {
    return pow(MOD-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

vector<int> as;
vector<vector<mint>> dp1;
vector<mint> dp2;

ll solve(const int N, const int S) {
    for (int i = 1; i <= N; i++) {
        for (int j = S; j >= 0; j--) {
            dp1[i][j] += dp1[i - 1][j];
            if (j + as[i] <= S) {
                // if (j + as[i] == S) {
                //     dp1[i][j + as[i]] += dp1[i][j] * mint(2).pow(N - i);
                // } else {
                //     dp1[i][j + as[i]] += dp1[i][j];
                // }
                dp1[i][j + as[i]] += dp1[i][j];
                dp2[j + as[i]] += dp2[j];
            }
            dp2[j] *= 2;
        }
    }
    return dp1[N][S].x;
}

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

    as.resize(N + 1, 0);
    for (int i = 0; i < N; i++) {
        cin >> as[i + 1];
    }

    dp1.resize(N + 1, vector<mint>(S + 1, 0));
    dp1[0][0] = 1;

    dp2.resize(S + 1, 0);
    dp2[0] = 1;

    solve(N, S);

    // printf("dp1: \n");
    // for (int i = 0; i <= N; i++) {
    //     for (int j = 0; j <= S; j++) {
    //         cout << dp1[i][j].x << " ";
    //     }
    //     cout << endl;
    // }

    // printf("dp2: \n");
    // for (int j = 0; j <= S; j++) {
    //     cout << dp2[j].x << " ";
    // }
    // cout << endl;

    cout << dp2[S].x << endl;

    return 0;
}


Measured memory_bytes: 74068
#include "bits/stdc++.h"
#include <random>

using namespace std;
#define REP(i, n) for(ll i = 0;i < n;i++)
#define ll long long
#define MOD 1000000007LL
#define llMAX 9223372036854775807LL
#define llMIN -9223372036854775808LL
using vi = vector<ll>; // intの1次元の型に vi という別名をつける
using vvi = vector<vi>; // intの2次元の型に vvi という別名をつける

ll n,k,x,y;

int main(){
  ll ans=0;
  ll D=365;
  vi c(26);
  vi last(26);
  vvi data(365, vi(26));  // N * M の2次元配列
  for(ll i=0;i<26;i++){
    cin>>c[i];
  }
  for(ll i=0;i<365;i++){
    for(ll j=0;j<26;j++){
      cin>>data[i][j];
    }
  }


  ll sc=0;
  ll sct=0;
  ll maxid=-1;
  for(ll i=0;i<365;i++){
    sc=-922337203685477580LL;
    maxid=0;
    for(ll j=0;j<26;j++){
      sct=data[i][j]-(last[j]*last[j]-last[j])/2*c[i];
      if (sc<sct){
        sc=sct;
        maxid=j;
      }
    }
    last[maxid]=i;
    cout<<(maxid+1)<<endl;
  }



  
  /*
  random_device rnd;     // 非決定的な乱数生成器
  for (int i = 0; i < n; ++i) {
    auto aa=rnd();
      cout << aa << "\n";
  }
  */

  return 0;
}

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

using namespace std;

typedef long long ll;

const int INF = 1e9;

struct Node {
  int key_;
  Node *left_, *right_;
  Node *parent_;

  Node(int key) : key_(key), left_(0), right_(0) {}
  Node() : key_(0), left_(0), right_(0) {}
  ~Node()
  {
    if (left_) { delete left_; left_ = 0; }
    if (right_) { delete right_; right_ = 0; }
    parent_ = 0;
    //fprintf(stderr, "DEBUG: deleting %d\n", key_);
  }

  void print_inorder()
  {
    if (left_) { left_->print_inorder(); }
    fprintf(stdout, " %d", key_);
    if (right_) { right_->print_inorder(); }
  }

  void print_preorder()
  {
    fprintf(stdout, " %d", key_);
    if (left_) { left_->print_preorder(); }
    if (right_) { right_->print_preorder(); }
  }
};

class BST {
public:
  BST() : root_(0) {}
  ~BST() { delete root_; }

  void insert(Node *node)
  {
    Node *x = root_, *y = 0;
    while (x != 0) {
      y = x;
      if (node->key_ < x->key_) {
        x = x->left_;
      }
      else if (node->key_ > x->key_) {
        x = x->right_;
      }
      else {
        // duplicate node
        return;
      }
    }
    node->parent_ = y;

    if (y == 0) { root_ = node; }
    else if (node->key_ < y->key_) {
      y->left_ = node;
    }
    else if (node->key_ > y->key_) {
      y->right_ = node;
    }
  }

  Node *find(int key)
  {
    Node *p = root_;
    while (p) {
      if (key == p->key_) { return p; }
      else if (key < p->key_) { p = p->left_; }
      else { p = p->right_; }
    }
    return 0;
  }

  void print_inorder()
  {
    if (root_) {
      root_->print_inorder();
      fprintf(stdout, "\n");
    }
  }

  void print_preorder()
  {
    if (root_) {
      root_->print_preorder();
      fprintf(stdout, "\n");
    }
  }

private:
  Node *root_;
};

int main()
{
  int n = 0;
  fscanf(stdin, "%d", &n);
  char buf[64] = { '\0' };
  int key = -1;
  BST tree;
  for (int i = 0; i < n; ++i) {
    fscanf(stdin, "%s", buf);
    if (buf[0] == 'i') {
      fscanf(stdin, "%d", &key);
      tree.insert(new Node(key));
    }
    else if (buf[0] == 'p') {
      tree.print_inorder();
      tree.print_preorder();
    }
    else if (buf[0] == 'f') {
      fscanf(stdin, "%d", &key);
      if (tree.find(key)) { fprintf(stdout, "yes\n"); }
      else { fprintf(stdout, "no\n"); }
    }
  }
  return 0;
}

Measured memory_bytes: 19956
import java.util.Scanner;
 
public class Main {
    static Scanner scan = new Scanner(System.in);
    static boolean[][] map;
    static boolean[][] check;
    static int island ;
    public static void main(String[] args) {
        while(scan.hasNext()) {
            map = new boolean[12][12];
            check = new boolean[12][12];
            island = 0;
            for(int i = 0; i < 12; i++) {
                int count = 0;
                for( String s : scan.next().split("",0)) {
                    if(s.equals("1")) {
                        map[i][count] = true;
                    }
                    else {
                        map[i][count] = false;
                    }
                    count++;
                }
            }
            for(int i = 0; i < 12 ; i++) {
                for(int u = 0; u < 12; u++) {
                    if(map[i][u] && !check[i][u]) {
                        fun(i,u);
                    }
                }
            }
            System.out.println(island);
        }
    }
 
    static void fun(int x, int y) {
        flag(x,y);
        island++;
    }
 
    static void flag(int x, int y) {
        check[x][y] = true;
     
        if(y + 1 < 12 && map[x][y+1] && !check[x][y+1]) {
            flag(x,y+1);
        }
        if(x + 1 < 12 && map[x+1][y] && !check[x+1][y]) {
            flag(x+1,y);
        }
        if(y - 1 > -1 && map[x][y-1] && !check[x][y-1]) {
            flag(x,y-1);
        }
        if(x - 1 > -1 && map[x-1][y] && !check[x-1][y]) {
            flag(x-1,y);
        }           
    }
}

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

#define Long long long

#define fori(i,a,b)       for( __typeof(a) i = a ; i <= b ; ++i )
#define forn(i,n)         fori(i,0,n-1)
#define ford(i,n)         for( __typeof(n) i = n-1 ; i >= 0 ; --i )

#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define deb(x) cout << "[ " << #x << " = " << x << " ]\n"

const int M = 1e5 + 5;
int v[M] , dp[M];

int recursive( int idx ){
    if( idx == 0 ) return 0;
    if( idx == 1 ) return abs (v[idx]-v[idx-1] );
    if( dp[idx] != -1 ) return dp[idx];
    int ans = min( recursive(idx-1) + abs(v[idx]-v[idx-1]) , recursive(idx-2) + abs(v[idx]-v[idx-2]) );
    dp[idx] = ans;
    return ans;
}

void solve(){
    int n;
    cin >> n;
    forn(i,n) cin >> v[i];
    memset( dp , -1 , sizeof dp );
    cout << recursive(n-1) <<endl;
}

int main(){
    int t; t = 1 ; while(t--) solve();
    return 0;
}


Measured memory_bytes: 2560
#include <stdio.h>
int main()
{
    int a[1000];
    int b ,i, j;
    int max;
    int min;
    
    scanf("%d\n" ,&b);
    
    for(i=0;b>=i;i++)
    {
        scanf("%d" ,&a[i]);
    }
    
    max=a[0];
    min=a[0];
    
    for(j=0;b>j;j++)
    {
        if(max<=a[j])
        {
            max=a[j];
        }
    
        if(min>a[j])
        {
            min=a[j];
        }
    }
    
    //printf("%d\n" ,max);
    //printf("%d\n" ,min);
    printf("%d\n" ,max - min);
    
    
    return 0;
}


Measured memory_bytes: 128
import java.util.Scanner;

public class Main {	
	
	// JAG Team Coding Source (一部改訂)
	
	static class Team {
		int id;
		int slove;
		boolean flag[];
		int pena[];
		int time;
		Team(int nid,int P){
			this.id = nid;
			this.slove = 0;
			this.flag = new boolean[P+1];
			this.pena = new int[P+1];
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true){
			int t = sc.nextInt();
			int p = sc.nextInt();
			int r = sc.nextInt();
			if((t|p|r)==0){
				break;
			}
			Team[] team = new Team[t+1];		// チーム配列の生成
			for(int i=0;i<=t;i++){
				team[i] = new Team(i,p);		// チーム配列でチームクラス生成
			}
			for(int i=0;i<r;i++){		// Main Input
				int tid = sc.nextInt();
				int pid = sc.nextInt();
				int ttime = sc.nextInt();
				String str = sc.next();			// message
				if(str.equals("CORRECT")){
					if(!(team[tid].flag[pid])){		// 1st AC
						team[tid].slove++;
						team[tid].time += team[tid].pena[pid]*1200+ttime;
						team[tid].flag[pid] = true;
					}
				}
				else{
					team[tid].pena[pid]++;
				}
			}
			// Sorting (最小値選択ソート)
			for(int i=1;i<=t;i++){
				int max = i;
				for(int j=i;j<=t;j++){
					if(team[max].slove<team[j].slove){		// より問題を解いた
						max=j;
					}
					else if(team[max].slove==team[j].slove){
						if(team[max].time>team[j].time){	// ペナルティ
							max=j;
						}
						else if(team[max].time==team[j].time){
							if(team[max].id>team[j].id){	// チームIDの若い
								max=j;
							}
						}
					}
				}
				// Swap
				Team temp = team[i];
				team[i] = team[max];
				team[max] = temp;
			}
			// Output
			for(int i=1;i<=t;i++){
				System.out.println(team[i].id+" "+team[i].slove+" "+team[i].time);
			}
		}
	}
}

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

int cnt=0;

void bubbleSort(int *A, int N){
  bool flag=true;
  while(flag){
    flag=false;
    for(int i=N-1;i>0;i--){
      if(A[i-1]>A[i]){
        swap(A[i-1], A[i]);
        flag=true;
        cnt++;
      }
    }
  }
}

int main(){

  int n, a[110];

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

  bubbleSort(a, n);

  for(int i=0;i<n;i++){
    cout<<(i?" ":"");
    cout<<a[i];
  }
  cout<<endl<<cnt<<endl;

  return 0;
}




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

#define LL long long
#define mod 1000000007
LL fac[100005], n, a[100005], d, mp[100005];

LL power(LL x, LL y) { 
    LL res = 1;
    x = x % mod;

    while (y > 0) { 
        if (y & 1) 
            res = (res*x) % mod; 

        y = y>>1;
        x = (x*x) % mod; 
    } 
    return res; 
} 

LL modInverse(LL n) { 
    return power(n, mod-2); 
} 
 
LL nCr(LL n, LL r) { 
    if (r==0) 
        return 1; 
    return (fac[n]* modInverse(fac[r]) % mod * modInverse(fac[n-r]) % mod) % mod; 
}  

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	
    fac[0] = 1LL; 
    for (LL i=1;i<=100004;++i) 
        fac[i] = fac[i-1] * i % mod; 

    cin >> n;
    for(int i=0;i<n+1;i++) {
    	cin >> a[i];
    	if(!mp[a[i]])
    		mp[a[i]] = i + 1;
    	else
    		d = i + 1 - mp[a[i]] + 1;
    }

    for(int k=1;k<=n+1;k++)
    	cout << ( nCr(n+1, k) - (n+1-d < k-1 ? 0 : nCr(n+1-d, k-1)) + mod ) % mod << '\n';

}

Measured memory_bytes: 3584
#include <bits/stdc++.h>
typedef long long ll;
typedef long double ld;
using namespace std;

int main(){
    int N, M;  cin >> N >> M;
    vector<int> A(N), B(M);
    for(int i=0; i<N; i++)
        cin >> A[i];
    for(int i=0; i<M; i++)
        cin >> B[i];
    
    sort(A.begin(), A.end());
    for(int i=1; i<N; i++){
        if(A[i-1] == A[i]){
            cout << 0 << endl;
            return 0;
        }
    }
    sort(B.begin(), B.end());
    for(int i=1; i<M; i++){
        if(B[i-1] == B[i]){
            cout << 0 << endl;
            return 0;
        }
    }

    ll ans = -1;
    ll mod = 1e9 + 7;
    for(int n = N*M; n >= 1; n--){
        ll h_min = lower_bound(A.begin(), A.end(), n) - A.begin();
        ll w_min = lower_bound(B.begin(), B.end(), n) - B.begin();
        ll dh = N - h_min;
        ll dw = M - w_min;

        if(dh*dw <= N*M - n){
            cout << 0 << endl;
            return 0;
        }

        ll temp = 1;
        if(A[h_min] == n && B[w_min] == n){
            temp = 1;    
        }else if(A[h_min] == n){
            temp = dw;
        }else if(B[w_min] == n){
            temp = dh;
        }else{
            temp = dh*dw - (N*M - n);
        }
        // cout << n << " " << temp << endl;


        temp %= mod;

        if(ans == -1){
            ans = temp;
        }else{
            ans = ans * temp;
            ans %= mod;
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
/* How many ways? */
#include <stdio.h>

int main()
{
    int n, x, i, j, k, l, count=0;

    for(l=0;; l++){
        scanf("%d %d", &n, &x);
        if(n == 0 && x == 0){
            break;
        }
        for(i=1; i <= n; i++){
            for(j=i+1; j <= n; j++){
                for(k=j+1; k <= n; k++){
                    if(i+j+k == x){
                        count++;
                    }
                }
            }
        }
        printf("%d\n", count);
        count = 0;
    }

}


Measured memory_bytes: 2100
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define PI 3.14159265358979323846264338327950L

int main() {
    FAST
    int a,b,h,m;
    cin>>a>>b>>h>>m;
    long double hour_angle = 0.5 * (h * 60 + m);  
    long double minute_angle = 6 * m;  
    long double angle = abs(hour_angle - minute_angle);  
    angle = min(360 - angle, angle); 
    long double radian = (2*PI*angle)/360;
    long double ans = a*a + b*b -2*a*b*cosl(radian);
    cout<<fixed<<setprecision(15)<<sqrt(ans);
}

Measured memory_bytes: 4164
import sys

n, y = map(int, sys.stdin.readline().split())
y //= 1000

def main():
    for i in range(n, -1, -1):
        for j in range(n-i, -1, -1):
            k = n - i - j
            if 10*i + 5*j + k == y:
                return i, j, k
    return -1, -1, -1

if __name__ == '__main__':
    ans = main()
    print(*ans, sep=' ')

Measured memory_bytes: 3064
def sum_digit(num):
  sumsum = 0
  while(num > 0):
    sumsum += int(num%10)
    num /= 10

  return sumsum


if __name__ == '__main__':
  n, a, b = map(int, input().split())
  ans = 0
  for i in range(1, n+1):
    sum_num = sum_digit(i)
    if sum_num >= a and sum_num <= b:
      # print(sum_num)
      ans += i
  print(ans)

Measured memory_bytes: 3064
from itertools import accumulate
H,W = map(int, input().split(' '))
n = int(input())
a = list(map(int, input().split(' ')))
ac = list(accumulate(a))
ansinv = [[0 for i in range(W)] for i in range(H)]
r = 1
k = 1
for i in range(H*W):
    if k > ac[r-1]:
        r += 1
    x = i//W
    y = i % W
    ansinv[x][y] = r
    k += 1
ans = ansinv
for i in range(H):
    if i % 2:
        ans[i] = ans[i][::-1]
for i in ans:
    print(*i, sep = " ")

Measured memory_bytes: 4332
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
    int N, K;  cin >> N >> K;
    vector<vector<ll>> sushi;
    sushi = vector<vector<ll>>(N, vector<ll>());
    for(int i=0; i<N; i++){
        ll t, d;  cin >> t >> d;
        sushi[t-1].push_back(d);
    }
    for(int i=0; i<N; i++){
        sort(sushi[i].begin(), sushi[i].end(), greater<ll>());
    }

    vector<ll> good, other;
    for(int i=0; i<N; i++){
        if(sushi[i].size() != 0){
            good.push_back(sushi[i][0]);
            int j = 1;
            while(j < sushi[i].size()){
                other.push_back(sushi[i][j]);
                j++;
            }
        }
    }

    sort(good.begin(), good.end(), greater<ll>());
    sort(other.begin(), other.end(), greater<ll>());
    for(int i=1; i<good.size(); i++){
        good[i] += good[i-1];
    }
    for(int i=1; i<other.size(); i++){
        other[i] += other[i-1];
    }

    ll ans = 0;
    for(ll x=1; x<=K; x++){
        if(x > good.size()) break;
        if(K - x > other.size()) continue;

        if(x==K) ans = max(ans, good[x-1] + x*x);
        else ans = max(ans, good[x-1] + other[K-x-1] + x*x);
    }
    cout << ans << endl;
}


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

#define ll long long
#define ld double
#define pii pair<ll,ll>
#define vl vector<ll>
#define vb vector<bool>
#define vs vector<string>
#define vp vector<pii>
#define pb push_back
#define mp make_pair
#define sc(a) scanf("%lld",&a);
#define sd(a) scanf("%lf",&a);
#define test() ll tt;cin>>tt;for(ll testcase=1;testcase<=tt;testcase++)
#define all(v) v.begin(),v.end()
#define vivek_ghosh ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define MAX 1000055
#define mod 1000000007
#define ff first
#define ss second
const int INF=0x3f3f3f3f;

ll power(ll x,ll n,ll M)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)
        return power((x*x)%M,n/2,M);
    else
        return (x*power((x*x)%M,(n-1)/2,M))%M;
}

ll modinv(ll a,ll m)
{
    ll g =__gcd(a, m);
    ll res=power(a, m-2, m);
    return res;
}

vb prime(MAX,true);
vl prm;
void sieve()
{
	prime[0] = prime[1] = false;
	for(ll p=2;p*p<MAX;p++)
	{
		if(prime[p])
		{
			for(ll i=p*p;i<MAX;i+=p)
				prime[i] = false;
		}
	}
	for(ll p=2;p<MAX;p++)
	{
		if(prime[p])
			prm.pb(p);
	}
}

ll n,m,t,i,j,k,q,x,y,ans=0,cnt=0,sum=0,mx=-1,mn=INT_MAX;
string s;

int main()
{
    ll l,r;
    cin>>l>>r;
    
    ans = 2020;
    
    if((r-l)>2019)
        ans = 0;
    else
    {
        for(i=l;i<r;i++)
        {
            for(j=i+1;j<=r;j++)
            {
                ans = min(ans,(i*j)%2019);
            }
        }
    }
    
    cout<<ans<<endl;
    
    
    
    
    
    return 0;
}






Measured memory_bytes: 384
#include<iostream>
#include<iomanip>
#include<string>
#include<queue>
#include<vector>
#include<algorithm>
#include<tuple>
#include<numeric>
#include<map>
#include<cmath>
#define ll long long
#define mod 1000000000+7
#define rep(i,n) for(ll i=0;i<(int) (n);i++)
ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b)*b; }
using namespace std;
int main() {
	int n, k;
	cin >> n >> k;
	vector<ll>h(n);
	ll cnt = 0;
	rep(i, n)cin >> h[i];

	rep(i, n)cnt += h[i];

	ll m = 0;
	sort(h.begin(), h.end());
	reverse(h.begin(), h.end());
	if (n > k) {
		rep(i, k)m += h[i];
		cout << cnt - m;
	}else {
		cout << 0;
	}

}

Measured memory_bytes: 1792
import math

N = int(input())
Arr = list(map(int, input().split()))

sum = 0
mArr = []

for n in Arr:
    m = n
    sum += m
    mArr.append(m)

half = sum / 2

l = 0
last = 0

for n in mArr:
    l += n
    if l >= half:
        last = n
        break

if l == half:
    print(0)
else:
    d = l - half
    if d > (last / 2):
        if l == mArr[0]:
            left = l
            right = sum - left
            print(left - right)
        else:
            left = l - last
            right = sum - left
            print(right - left)
    else:
        if l == sum:
            left = l - last
            right = sum - left
            print(right - left)
        else:
            left = l
            right = sum - left
            print(left - right)



Measured memory_bytes: 99532
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
	    Scanner scanner = new Scanner(System.in);

	    int N = Integer.parseInt(scanner.next());
	    int a=N+N*N+N*N*N;



	    System.out.println(a);
	}

	public static int uclid(int m, int n) {
		if (m<n) {
			m ^= n;
			n^=m;
			m^=n;
		}

		while (true) {
			int t=m%n;
			if (t==0) {
				return n;
			} else {
				m=n;
				n=t;
			}
		}
	}

}

Measured memory_bytes: 35760
#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>;

constexpr ll MOD = 1'000'000'007;
int N, K;
ll dp[51][51][5001];

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

    std::cin >> N >> K;

    dp[0][0][0] = 1;

    for(int i=0;i<N;++i){
        for(int j=0;j<N;++j){
            for(int k=0;k<=5000;++k){
                if(j > 0){
                    if(k+2*(j-1) <= 5000){
                        dp[i+1][j-1][k+2*(j-1)] = (dp[i+1][j-1][k+2*(j-1)] + dp[i][j][k] * j * j) % MOD;
                    }
                }
                if(k+2*j <= 5000){
                    dp[i+1][j][k+2*j] = (dp[i+1][j][k+2*j] + dp[i][j][k] * (2 * j + 1)) % MOD;
                }
                if(k+2*(j+1) <= 5000){
                    dp[i+1][j+1][k+2*(j+1)] = (dp[i+1][j+1][k+2*(j+1)] + dp[i][j][k]) % MOD;
                }
            }
        }
    }

    ll res = dp[N][0][K];
    std::cout << res << std::endl;
}


Measured memory_bytes: 99840
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(n);++i)
#define ALL(v)   (v).begin(),(v).end()
#define debug(x) cerr<<#x<<": "<<(x)<<endl
#define INF      (int)1e9
#define EPS      (double)1e-9
#define MOD      ((int)1e9+7)
using namespace std;
typedef long long     llong;
typedef vector<int>   vi;
typedef vector<vi >   vvi;
typedef vector<vvi >  vvvi;
typedef pair<int,int> pii;
template<class Type> void line(const Type &a){int cnt=0;for(const auto &elem:a){if(cnt++)cerr<<' ';cout<<elem;}cerr<<endl;}

//解説参考. 解法2.
int main(){
	int n,k;
	cin>>n>>k;
	
	vector<llong> a,b;
	REP(i,n){
		llong in;
		cin>>in;
		if(in<0) a.push_back(in);
		else b.push_back(in);
	}
	sort(ALL(a));
	sort(ALL(b),greater<llong>());
	
	if(k==n){
		llong anss=1;
		REP(i,a.size()) anss=anss*(MOD+a[i])%MOD;
		REP(i,b.size()) anss=anss*b[i]%MOD;
		cout<<anss<<endl;
		return 0;
	}
	
	if(a.size()==n and k%2==1){
		llong anss=1;
		for(int i=a.size()-1;i>=a.size()-k;--i) anss=anss*(MOD+a[i])%MOD;
		cout<<anss<<endl;
		return 0;
	}
	
	llong ans=1;
	int pa=0, pb=0;
	while(k>0){
		if(k<=1){
			ans=ans*b[pb++]%MOD;
			k--;
		}else{
			if(a.size()-pa>=2 and (b.size()-pb<=1 or (b.size()-pb>=2 and a[pa]*a[pa+1]>=b[pb]*b[pb+1]))){
				REP(i,2) ans=ans*(MOD+a[pa++])%MOD;
				k-=2;
			}else{
				ans=ans*b[pb++]%MOD;
				k--;
			}
		}
	}
	
	cout<<ans<<endl;
}

Measured memory_bytes: 5484
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define vi vector<int>
#define vvi vector<vector<int>>
#define pb push_back
#define pii pair<ll,ll>
#define tii tuple<ll,ll,ll>
#define all(v) (v).begin(),(v).end()
#define ll long long
#define ull unsigned long long
#define fastio ios_base::sync_with_stdio(false);cin.tie(nullptr);
#define ld long double
#define mod 1000000007
#define PI 3.141592653589793238
#define phi 1.638
#define eps 1e-9
//#define endl "\n"
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;   

ll num[100] = {0};
ll num2[100] = {0};

int32_t main() 
{ 
    fastio
    ll n;
    cin>>n;
    num[0] = 1;
    for(ll i = 1; i<=15; ++i) {
        num[i] = num[i-1] * 26;
    }

    for(ll i = 1; i<=15; ++i) {
        num2[i] += num2[i-1] + num[i];
    }

    int len = 0;
    for(int i = 1; i<=15; ++i) {
        if(num2[i]>=n) {
            len = i;
            break;
        }
    }

    n -= num2[len-1]+1;
    string s(len, '?');
    for(int i = 0; i<len; ++i) {
        s[i] = n%26+'a';
        n /= 26;
    }
    reverse(all(s));
    cout<<s<<endl;
}

Measured memory_bytes: 3652
import math

def tri(x1, y1, x2, y2, x3, y3):
    return math.fabs((x2-x1)*(y3-y1) - (y2-y1)*(x3-x1)) / 2

while True:
    try:
        x1, y1, x2, y2, x3, y3, xp, yp = map(float, input().split())
    except:
        break
    abc = tri(x1, y1, x2, y2, x3, y3)
    abp = tri(x1, y1, x2, y2, xp, yp)
    acp = tri(x1, y1, x3, y3, xp, yp)
    bcp = tri(x2, y2, x3, y3, xp, yp)
    x, y = int(abc*pow(10, 5)), int((abp+acp+bcp)*pow(10, 5))
    print("YES" if x >= y else "NO")

Measured memory_bytes: 7620
N = int(input())
total = int(1/2 * N *  (N+1))
wa3 =  (N//3 * (N//3 + 1)) // 2 * 3
wa5 =  (N//5 * (N//5 + 1)) // 2 * 5
wa15 =  (N//15 * (N//15 + 1)) // 2 * 15
print(total - wa3 - wa5 + wa15)

Measured memory_bytes: 61900
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int x = scan.nextInt();
		int p = x;
		while(!isPrime(p)) {
			p++;
		}
		System.out.println(p);
	}
	
	static boolean isPrime(int x) {
		if(x <= 1) {
			return false;
		}
		for(int i = 2; i * i <= x; i++) {
			if(x % i == 0) {
				return false;
			}
		}
		return true;		
	}
}

Measured memory_bytes: 22996
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>
#include <sstream>
#include <complex>
#include <fstream>
#include <bitset>
#include <time.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = (1LL << 31) - 1;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1
#define MAX_N 100100 * 3

int n, m;
ll h[100100];
ll w[100100];
ll cnt[2000000][2];

int main() {
	while (cin >> n >> m&&n + m) {
		REP(i, 2000000)REP(j, 2)cnt[i][j] = 0;
		REP(i, n) {
			cin >> h[i];
			if (i != 0)h[i] += h[i - 1];
		}
		REP(i, m) {
			cin >> w[i];
			if (i != 0)w[i] += w[i - 1];
		}
		REP(i, n) {
			cnt[h[i]][0]++;
			FOR(j, i + 1, n) {
				cnt[h[j] - h[i]][0]++;
			}
		}
		REP(i, m) {
			cnt[w[i]][1]++;
			FOR(j, i + 1, m) {
				cnt[w[j] - w[i]][1]++;
			}
		}
		ll ans = 0;
		REP(i, 2000000) {
			//cout << cnt[i][0] << " " << cnt[i][1] << endl;
			ans += cnt[i][0] * cnt[i][1];
		}
		cout << ans << endl;
	}

}

Measured memory_bytes: 34384
#include<cstdio>
#include<algorithm>
int n,a[200005],c[200005],b[200005],x;
long long ans;
int find(int x)
{
	int l=1,r=n,mid;
	while(l<=r)
	  {
	  	mid=l+r>>1;
	  	if(x==c[mid])return mid;
	  	else if(x>c[mid])l=mid+1;
	  	else r=mid-1;
	  }
	return 0;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)scanf("%d",&a[i]),c[i]=i+a[i];
	std::sort(c+1,c+n+1);
	++b[find(1+a[1])];
	for(int i=2;i<=n;++i)
	  {
	  	x=find(i-a[i]);
	  	if(x)ans+=b[x];
	  	++b[find(i+a[i])];
	  }
	printf("%lld",ans);
}

Measured memory_bytes: 4012
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define PB push_back
#define MP make_pair
#define ll long long

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();}

int main(){
    std::ios::sync_with_stdio(false);
    int n;
    cin >> n;
    ll a[n];
    vector<ll> negs;
    vector<ll> poss;
    for(int i=0;i<n;++i) {
        ll tmp;
        cin >> tmp;
        if (tmp > 0) {
            poss.push_back(tmp);
        } else {
            negs.push_back(tmp);
        }
    }
    ll ret = 0;
    if((negs.size() % 2) == 0)
    {
        for(int i=0;i<negs.size();++i)
        {
            ret += negs[i] * -1;
        }

        for(int i=0;i<poss.size();++i)
        {
            ret += poss[i];
        }    
    }
    else
    {
        sort(negs.begin(), negs.end());
        sort(poss.begin(), poss.end());
        if(poss.empty())
        {
            ret += negs.back();
        }
        else
        {
            ll last = negs.back() + poss.front();
            if (last < 0) {
                last *= -1;
            }
            ret += last;
        }

        for(int i=0;i<negs.size()-1;++i)
        {
            ret += negs[i] * -1;
        }

        for(int i=1;i<poss.size();++i)
        {
            ret += poss[i];
        }

    }

    cout << ret << endl;
}

Measured memory_bytes: 1656
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<functional>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define M (998244353)
#define INF 1e16
typedef long long ll;
typedef pair<ll,ll > P;
typedef pair<P,ll>Q;



int main(void){
    int n;
    cin>>n;
    vector<ll>a(n);
    map<ll,set<P> >s;
    for(int i=0;i<n;i++){
        cin>>a[i];
        s[i+1-a[i]].insert(P(i+1,a[i]));
    }
    ll ans = 0;
    for(int i=0;i<n;i++){
        ll tmp = a[i]+i+1;
        ans+=s[tmp].size();
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 51492
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>

using namespace std;



int main(void){
    int S,W;

    cin>>S>>W;

    if(S>W){
        cout<<"safe"<<endl;
    }else{
        cout<<"unsafe"<<endl;
    }



    return 0;
}



Measured memory_bytes: 3636
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<time.h>
#include<tuple>
#include<set>
#include<map>
#include<cctype>
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define vint vector<int> 
#define vvint vector<vint>
#define ct(a) cout<<a<<endl
#define tget(a,b) get<a>(b)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e16;
const int MOD = 1e9 + 7;

struct edge { ll to, cost; };

ll V; vector<edge> G[100005];
vector<edge> B[100005];
ll d[2][100005];
ll dijkstra(int s,int flag){
	priority_queue<P, vector<P>, greater<P>> que;
	FOR(i, 0, 100005) { d[flag][i] = INF; }
	d[flag][s] = 0;
	que.push(P(0, s));
	while (!que.empty()) {
		
		P p = que.top(); que.pop();
		int v = p.second;//頂点の番号
		
		if (d[flag][v] < p.first) {continue; }
		if (flag==0) {
			FOR(i, 0, G[v].size()) {
				edge e = G[v][i];
				
				if (d[flag][e.to] > d[flag][v] + e.cost) {
					d[flag][e.to] = d[flag][v] + e.cost;
					que.push(P(d[flag][e.to], e.to));
				}
			}
		}
		else {
			FOR(i, 0, B[v].size()) {
				edge e = B[v][i];
				if (d[flag][e.to] > d[flag][v] + e.cost) {
					d[flag][e.to] = d[flag][v] + e.cost;
					que.push(P(d[flag][e.to], e.to));
				}
			}
		}
	}
	return 0;
}
ll m, s, t;
ll di[100005];
int main(void) {
	cin >> V >> m >> s >> t;
	FOR(i, 0, m) {
		ll u, v, x, y; cin >> u >> v >> x >> y;
		G[u].push_back(edge{ v,x });
		G[v].push_back(edge{ u,x });
		B[u].push_back(edge{ v,y });
		B[v].push_back(edge{ u,y });
	}
	dijkstra(s, 0);
	dijkstra(t, 1);
	
	for (int i = V ; i >= 1; i--) {
		di[i] += d[0][i] + d[1][i];
		
		if (i < V)di[i] = min(di[i], di[i + 1]);
	}
	FOR(i, 1, V+1) {
		ct(ll(1e15) - di[i]);
	}
	return 0;
}

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

using namespace std;

#define int long long
#define endl "\n"

const long long INF = (long long)1e18;
const long long MOD = 1'000'000'007; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}



signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int x = 0, y = 0;
	string s;
	int n;
	
	cin>>n;
	
	cin>>s;
	
	for(int i = 0; i < s.size(); i++){
		if('a' <= s[i] && s[i] <= 'm'){
			x++;
		}
		if('m' < s[i] && s[i] <= 'z'){
			x--;
		}
		if('A' <= s[i] && s[i] <= 'M'){
			y++;
		}
		if('M' < s[i] && s[i] <= 'Z'){
			y--;
		}
	}
	
	cout<<(int)(abs(x) + abs(y))<<endl;
	
//	cout<<x<<" "<<y<<endl;
	if(x < 0){
		x = abs(x);
		
		for(int i = 0; i < x; i++){
			cout<<"z";
		}
	} else {
		for(int i = 0; i < x; i++){
			cout<<"a";
		}
	}
	
	
	if(y < 0){
		y = abs(y);
		
		for(int i = 0; i < y; i++){
			cout<<"Z";
		}
	} else {
		for(int i = 0; i < y; i++){
			cout<<"A";
		}
	}
	
	cout<<endl;
	
	return 0;
}


Measured memory_bytes: 3344
w = [1, 2]
for i in range(6):
    w.append(w[-2] + w[-1])
H, W, K = map(int,input().split())
mod = 10 ** 9 + 7
dp = [[0 for i in range(W)] for j in range(H+1)]
dp[0][0] = 1
for i in range(H):
    for j in range(W):
        if j >= 1:
            k = 1
            if j - 2 >= 0:
                k *= w[j - 2]
            if W - j - 2 >= 0:
                k *= w[W - j - 2]
            dp[i+1][j-1] += k * dp[i][j]
            dp[i+1][j-1] %= mod
        k = 1
        if j - 1 >= 0:
            k *= w[j - 1]
        if  W - j - 2 >= 0:
            k *= w[W - j - 2]
        dp[i+1][j] += k * dp[i][j]
        dp[i+1][j] %= mod
        if j <= W - 2:
            k = 1
            if j - 1 >= 0:
                k *= w[j - 1]
            if W - j - 3 >= 0:
                k *= w[W - j - 3]
            dp[i+1][j+1] += k * dp[i][j]
            dp[i+1][j+1] %= mod

print(dp[H][K-1] % mod)

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

template<class T> void vin(vector<T>& v, int n) {
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
    }
}

int A[50][50];

int main()
{
    int N, M, C;
    cin >> N >> M >> C;
    vector<int> B(M);
    rep(i, M) cin >> B[i];
    int ans = 0;
    rep(i, N)
    {
        int tmp = C;
        rep(j, M)
        {
            cin >> A[i][j];
            tmp += A[i][j]*B[j];
        }
        if (tmp > 0) ans++;
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
k=j=0
a=[];b=[];d=[]
for i,x in enumerate(input()):
    if x=='\\':a+=[i]
    elif x=='/' and a:
        j=a.pop()
        c=i-j;k+=c
        while b and b[-1]>j:c+=d.pop();b.pop()
        b+=[j];d+=[c]
print(k)
print(len(b),*(d))



Measured memory_bytes: 6244
/*input
9
1 3 6 13 15 18 19 29 31
10
4
1 8
7 3
6 7
8 5
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#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 < (int)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 all(v) v.begin(),v.end()
#define pii pair<int,int>
#define vi vector<int>
#define vvi vector<vi>
#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;

typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;

const int INF = 1e18+1;
const int MOD = 1e9+7;
const double PI = 3.14159265358979323846264338;

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

int floor1(int n,int k){
    if(n%k == 0 || n >= 0)return n/k;
    return (n/k)-1;
}

int ceil1(int n,int k){
    return floor1(n+k-1,k);
}

const int LG = 17;
const int N = 1e5+1;
int x[N];
int pre[N][LG];
int n,q,l;

void solve(){
  	cin >> n;
  	RE(i,n){
  		cin >> x[i];
  	}
  	cin >> l >> q;

  	for(int i = 1;i <= n;i++){
  		int lo = 1;
  		int hi = i-1;
  		while(lo < hi){
  			int mid = (lo+hi)/2;
  			if(x[i]-x[mid] > l)lo = mid+1;
  			else hi = mid;
  		}
  		pre[i][0] = lo;
  		for(int j = 1;j < LG;j++)pre[i][j] = pre[pre[i][j-1]][j-1];
  	}
  	while(q--){
  		int a,b;cin >> a >> b;
  		if(a < b)swap(a,b);
  		int ans = 0;
  		for(int i = LG-1;i > -1;i--){
  			if(pre[a][i] > b){
  				ans += 1<<i;
  				a = pre[a][i];
  			}
  		}
  		cout << ans+1 << endl;
  	}
}

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;
  while(t--){
    solve();
  }
  return 0;
}

Measured memory_bytes: 14848
// review

#include <iostream>

using namespace std;
const int MOD = 1e9+7;

int h, w, k;
int dp[110][10];

void solve() {
  dp[0][0] = 1;
  for (int i=0;i<h;i++) {
    for (int j=0;j<w;j++) {
      for (int mask=0;mask<(1 << (w-1));mask++) {
        bool satisfy = true;
        for (int k=0;k<w-2;k++) {
          if ((mask >> k) & 1 && (mask >> (k+1)) & 1) {
            satisfy = false;
            break;
          }
        }
        if (!satisfy) {
          continue;
        }
        if ((mask >> j) & 1) {
          dp[i+1][j+1] = (dp[i+1][j+1] + dp[i][j]) % MOD;
        } else if (j > 0 && (mask >> (j - 1)) & 1) {
          dp[i+1][j-1] = (dp[i+1][j-1] + dp[i][j]) % MOD;
        } else {
          dp[i+1][j] = (dp[i+1][j] + dp[i][j]) % MOD;
        }
      }
    }
  }
  cout << dp[h][k-1] << endl;
}

int main() {
  cin >> h >> w >> k;
  solve();
}


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


int main()
{
  string s,t;
  char t1;
  cin >> s >> t;
  int i = 0;
  while(i < s.size()){
    s.insert(s.begin(),s[s.size() - 1]);
    s.pop_back();
    i++;
    if(s == t) i = s.size();
  }
  if(s == t) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}


Measured memory_bytes: 256
s = input() + "T"
x, y = map(int, input().split())
N = len(s)



for i in range(N):
    if s[i] == "F":
        x -= 1
    else:
        cnt_Y = True
        index = i + 1
        break

steps_X = []
steps_Y = []

while index < N:
    cnt = 0
    for j in range(index, N):
        if s[j] == "F":
            cnt += 1
        else:
            if cnt_Y and cnt != 0:
                steps_Y.append(cnt)
            elif not cnt_Y and cnt != 0:
                steps_X.append(cnt)
            cnt_Y = not cnt_Y
            index = j
            break
    index += 1
    
K = 10000
dp = [set() for _ in range(K)]
dp[0].add(0)

for i, t in enumerate(steps_X):
    for tmp in dp[i]:
        dp[i+1].add(tmp+t)
        dp[i+1].add(tmp-t)

# print(dp[len(steps_X)])
if x not in dp[len(steps_X)]:
    print("No")
    import sys
    sys.exit()

dp = [set() for _ in range(K)]
dp[0].add(0)
for i, t in enumerate(steps_Y):
    for tmp in dp[i]:
        dp[i+1].add(tmp+t)
        dp[i+1].add(tmp-t)

if y not in dp[len(steps_Y)]:
    print("No")
    import sys
    sys.exit()


print("Yes")


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

int main() {
  int a,b,c;
  cin>>a>>b>>c;
  if(a==b&&a!=c) cout<<"Yes"<<endl;
  else if(a==c&&b!=a) cout<<"Yes"<<endl;
  else if(b==c&&a!=b) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
 }

Measured memory_bytes: 256
n, m, L = map(int, input().split())
  
a = [list(map(int, input().split())) for _ in range(n)]
b = [list(map(int, input().split())) for _ in range(m)]
c = [[sum(ak * bk for ak, bk in zip(ai,bj)) for bj in zip(*b)] for ai in a]
  
for ci in c:
    print(*ci)

Measured memory_bytes: 9140
fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let n: usize = sc.read();
    let m: usize = sc.read();
    let mut graph = vec![vec![]; n];
    for _ in 0..m {
        let v = sc.read::<usize>() - 1;
        let u = sc.read::<usize>() - 1;

        graph[u].push(v);
        graph[v].push(u);
    }

    let mut bipartite: usize = 0;
    let mut single: usize = 0;
    let mut others: usize = 0;
    let mut color = vec![0; n];
    for i in 0..n {
        if color[i] == 0 {
            color[i] = 1;
            if graph[i].is_empty() {
                single += 1;
            } else if color_dfs(i, &graph, &mut color) {
                bipartite += 1;
            } else {
                others += 1;
            }
        }
    }

    let ans1 = single * n * 2 - single * single;
    let ans2 = others * (others + bipartite) * 2 - others * others;
    let ans3 = bipartite * bipartite * 2;
    println!("{}", ans1 + ans2 + ans3);
}

fn color_dfs(v: usize, graph: &[Vec<usize>], color: &mut [usize]) -> bool {
    let mut ok = true;
    for &next in graph[v].iter() {
        if color[next] == 0 {
            color[next] = if color[v] == 1 { 2 } else { 1 };
            ok &= color_dfs(next, graph, color);
        } else if color[next] == color[v] {
            ok = false;
        }
    }
    ok
}

pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}


Measured memory_bytes: 19196
#include <bits/stdc++.h>
#define MOD 1000000007
#define INF 1000000000
#define LINF 1000000000000000000
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define bit(n) (1LL<<(n))
using namespace std;

typedef pair<int, int> P;
typedef pair<long long, long long> LLP;

int main() {

    long long X;
    cin >> X;

    double M = 100;
    int count = 0;

    while (M < X) {
        count++;
        M *= 101;
        M /= 100;
        M = floor(M);
    }

    cout << count << endl;
    
    return 0;
}

Measured memory_bytes: 3636
#coding:utf-8
import sys,os
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    H,N = LMIIS()
    A = LMIIS()
    if sum(A) < H:
        print('No')
    else:
        print('Yes')
    

if __name__ == '__main__':
    main()

Measured memory_bytes: 52676