akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <functional>

#define REP(i, a, b) for (int i = int(a); i < int(b); i++)
#define dump(val) cerr << __LINE__ << ":\t" << #val << " = " << (val) << endl

using namespace std;

typedef long long int lli;

template<typename T>
vector<T> make_v(size_t a, T b) {
    return vector<T>(a, b);
}

template<typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v(ts...))>(a, make_v(ts...));
}

struct Edge {
    int to, cap, rev;
    Edge(int t, int c, int r)
        : to(t), cap(c), rev(r) {}
    Edge() {}
};

int gcd(int a, int b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}

int main() {
    int m, n;
    while (cin >> m >> n, m + n) {
        vector<int> cards(m + n);
        REP(i, 0, m) {
            cin >> cards[i];
        }
        REP(i, 0, n) {
            cin >> cards[i + m];
        }
        vector<vector<Edge>> G(m + n + 2);
        auto add_edge = [&](int from, int to, int cap) {
            G[from].push_back(Edge(to, cap, static_cast<int>(G[to].size())));
            G[to].push_back(Edge(from, 0, static_cast<int>(G[from].size() - 1)));
        };
        REP(i, 0, m) {
            REP(j, 0, n) {
                if (gcd(cards[i], cards[j + m]) != 1) {
                    add_edge(i, j + m, 1);
                }
            }
        }
        REP(i, 0, m) {
            add_edge(m + n, i, 1);
        }
        REP(i, 0, n) {
            add_edge(i + m, m + n + 1, 1);
        }
        vector<bool> used(m + n + 2);
        function<int(int, int, int)> dfs = [&](int v, int t, int f) -> int {
            if (v == t) return f;
            used[v] = true;
            REP(i, 0, G[v].size()) {
                Edge &e = G[v][i];
                if (!used[e.to] && e.cap > 0) {
                    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 ans = 0;
        while (true) {
            used.assign(m + n + 2, false);
            int f = dfs(m + n, m + n + 1, 1 << 29);
            if (f == 0) break;
            ans += f;
        }
        cout << ans << endl;
    }
    return 0;
}



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

int main(void)
{
    long long N;
    cin >> N;
    for (long long i = N; i > 0; i--) {
        if (static_cast<long long>(sqrt(i)) * static_cast<long long>(sqrt(i)) == i) {
            cout << i << endl;
            break;
        }
    }
}


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

int yaku(int n){
    int ans = 1;
    int i = 2;
    int x = 0;
    while(n > 1){
        if(n % i == 0){
            n /= i;
            x++;
        }else{
            ans *= (x + 1);
            i++;
            x = 0;
        }
    }
    ans *= (x + 1);
    return ans;
}

int main(){
    int n;
    cin >> n;

    int ans = 0;

    for(int i = 1; i <= n; i += 2){
        if(yaku(i) == 8) ans++;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<math.h>
#include<algorithm>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)

using namespace std;

int main(){
	int n, m;
    cin >> n >> m;
    int k[n], a[n][30];
    REP(i,n){
        REP(j,30){
            a[i][j] = 100;
        }
    }
    REP(i,n){
        cin >> k[i];
        FOR(j,0,k[i]){
            cin >> a[i][j];
        }
    }

    int c1, c2 = 0;
    REP(i,m){
        c1 = 0;
        REP(j,n){
            REP(l,30){
                if(a[j][l] == i+1){ 
                    c1++;
                    //cout << j << " " << l << endl;
                }
            }
        }
        if(c1 == n) c2++;
        //cout << "haha" << endl;
    }
    cout << c2 << endl;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define pii pair<int,int>
const long long MOD=1e9+7;

double mkaijo(int n){
  if(n<=1) return 1;
  return n*mkaijo(n-1);
}

int main() {
  long long n,a,b;
  cin>>n>>a>>b;
  vector<long long> X(n);
  rep(i,n) cin>>X[i];
  /*a,b,c(a<b<c)の部分について考えると
  aからbにテレポートしても、aからcにテレポートしても変わらない
  よって常に東に進む時を考える*/
  long long ans=0;
  rep(i,n-1) ans+=min(a*(X[i+1]-X[i]),b);
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 1024
import heapq

n,m=map(int,input().split())
a=list(map(lambda x: int(x)*(-1),input().split()))
heapq.heapify(a)

for i in range(m):
    tmp_min=heapq.heappop(a)
    if tmp_min%2==0:
        push=tmp_min/2
    else:
        push=tmp_min//2+1
    heapq.heappush(a,push)
print(int(-sum(a)))

Measured memory_bytes: 20444
#pragma GCC optimize("Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
template<class t> inline t read(t &x){
	char c=getchar();bool f=0;x=0;
	while(!isdigit(c)) f|=c=='-',c=getchar();
	while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar();
	if(f) x=-x;return x;
}
template<class t,class ...A> inline void read(t &x,A &...a){
	read(x);read(a...);
}
template<class t> inline void write(t x){
	if(x<0) putchar('-'),write(-x);
	else{if(x>9) write(x/10);putchar('0'+x%10);}
}

const int N=2e5+5,M=1e6+5;
long long ans=0;
int n,id,rest[30],ch[M][30],cnt[M];
string s[N];

signed main(){
	read(n);
	for(int i=1;i<=n;i++){
		cin>>s[i];
		reverse(s[i].begin(),s[i].end());
		int x=0;
		for(char c:s[i]){
			int k=c-'a';
			if(!ch[x][k]) ch[x][k]=++id;
			x=ch[x][k];
		}
		cnt[x]++;
	}
	for(int i=1;i<=n;i++){
		for(char c:s[i]) rest[c-'a']++;
		int x=0;
		for(char c:s[i]){
			for(int k=0;k<26;k++) if(rest[k]&&ch[x][k]) ans+=cnt[ch[x][k]];
			x=ch[x][c-'a'];
			rest[c-'a']--;
		}
		ans--;
	}
	write(ans);
}

Measured memory_bytes: 131248
n,m,q=map(int,input().split())
li=[list(map(int,input().split()))for _ in range(q)]

ans = 0
def dfs(A):
    #print(A)
    global ans 
    if len(A) == n+1:
        tmp = 0
        for a,b,c,d in li:
            if A[b] - A[a] == c:
                tmp += d
            ans = max(ans,tmp)
        return
    for i in range(A[len(A)-1],m+1):
        dfs(A+[i])
dfs([1])
print(ans)

Measured memory_bytes: 9132
using System;
using System.Linq;

public class Program
{
    static void Main()
    {
        int[] input = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
        int n = input[0];
        int m = input[1];
        int[] h = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
        bool[] isH = new bool[n];
        for (int i = 0; i < n; i++)
        {
            isH[i] = true;
        }
        int[,] ms = new int[m, 2];
        for (int i = 0; i < m; i++)
        {
            int[] tmp = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
            ms[i, 0] = tmp[0] - 1;
            ms[i, 1] = tmp[1] - 1;
        }

        for (int i = 0; i < m; i++)
        {
            if (!isH[ms[i, 0]])
                continue;
            if (h[ms[i, 0]] > h[ms[i, 1]])
                isH[ms[i, 0]] = true;
            else
                isH[ms[i, 0]] = false;
        }
        for (int i = 0; i < m; i++)
        {
            if (!isH[ms[i, 1]])
                continue;
            if (h[ms[i, 1]] > h[ms[i, 0]])
                isH[ms[i, 1]] = true;
            else
                isH[ms[i, 1]] = false;
        }
        int ans = 0;
        for (int i = 0; i < n; i++)
        {
            if (isH[i])
                ans++;
        }

        Console.WriteLine(ans);
    }
}

Measured memory_bytes: 48724
H, W = map(int, input().split())
grid = ['.' * (W + 2)]
for i in range(H):
    grid.append('.' + input() + '.')
grid += ['.' * (W + 2)]
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if grid[i][j] == "#":
            if not (grid[i][j + 1] == "#" or grid[i + 1][j] == "#"
                    or grid[i][j - 1] == "#" or grid[i - 1][j] == "#"):
                print('No')
                exit()
print('Yes')


Measured memory_bytes: 3064
#define _CRT_SECURE_NO_WARNINGS
//#define DEBUG
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

int main(void) {
#ifdef DEBUG
	printf("");
#endif

	int A, B, C; 
	int flag = 0;

	scanf("%d %d %d", &A, &B, &C);
	if (C >= A && C <= B) {
		flag = 1;
	}
	if (flag == 0){
		printf("No\n");
	}
	else {
		printf("Yes\n");
	}

	return 0;
}

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

int main(){
	int n;
	cin >> n;
	vector<int> vec(n);
	for (int i = 0; i < n; i++) cin >> vec.at(i);
	sort(vec.begin(), vec.end());
	reverse(vec.begin(), vec.end());
	int ans = 0;
	int flip = 0;
	for (int i = 0; i < n; i++) {
		if (flip == 0) {
			ans += vec.at(i);
			flip++;
		}
		else {
			ans -= vec.at(i);
			flip--;
		}
	}
	cout << ans << endl;
}


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

N = int(input().strip())+2
A = list(map(int,input().split()))+[N-1,N]
INF = N
dp = [[-INF for j in range(N)] for i in range(N)]
dpmax = -INF
dpmaxj = [-INF for i in range(N)]
score = 0
a,b,c,d,e = sorted(A[:5])
a -= 1
b -= 1
c -= 1
d -= 1
e -= 1

if a == b == c:
  dp[d][e] = 1
  dp[e][d] = 1
  dpmaxj[d] = 1
  dpmaxj[e] = 1
  dpmax = 1
elif b == c == d:
  dp[a][e] = 1
  dp[e][a] = 1
  dpmaxj[a] = 1
  dpmaxj[e] = 1
  dpmax = 1
elif c == d == e:
  dp[a][b] = 1
  dp[b][a] = 1
  dpmaxj[a] = 1
  dpmaxj[b] = 1
  dpmax = 1
else:
  L = [a,b,c,d,e]
  for i in range(5):
    for j in range(i+1,5):
      p,q = L[i],L[j]
      dp[p][q] = 0
      dp[p][q] = 0
      dpmaxj[p] = 0
      dpmaxj[q] = 0
  dpmax = 0 

def update(i,j):
  dpmaxj[i] = max(dpmaxj[i],dp[i][j])
  dpmaxj[j] = max(dpmaxj[j],dp[i][j])

for i in range(N-3):
  prevdpmax = dpmax
  prevdpmaxj = dpmaxj[:]
  dpd = [dp[k][k] for k in range(N)]
  a,b,c = A[i*3+5:i*3+8]
  a -= 1
  b -= 1
  c -= 1
  prevdpka = [dp[i][a] for i in range(N)]
  prevdpla = [dp[a][i] for i in range(N)] 
  prevdpkb = [dp[i][b] for i in range(N)]
  prevdplb = [dp[b][i] for i in range(N)] 
  if a == b == c:
    score += 1
    continue
  elif a == b:
    for k in range(N):
      dp[k][c] = max(dp[k][c],max(prevdpka[k],prevdpla[k]) + 1)
      dp[c][k] = max(dp[c][k],max(prevdpka[k],prevdpla[k]) + 1)
      dpmax = max(dpmax,dp[k][c],dp[c][k])
      update(k,c)
      update(c,k)
  elif a == c:
    for k in range(N):
      dp[k][b] = max(dp[k][b],max(prevdpka[k],prevdpla[k]) + 1)
      dp[b][k] = max(dp[b][k],max(prevdpka[k],prevdpla[k]) + 1)
      dpmax = max(dpmax,dp[k][b],dp[b][k])
      update(k,b)
      update(b,k)
  elif b == c:
    for k in range(N):
      dp[k][a] = max(dp[k][a],max(prevdpkb[k],prevdplb[k]) + 1)
      dp[a][k] = max(dp[a][k],max(prevdpkb[k],prevdplb[k]) + 1)
      dpmax = max(dpmax,dp[k][a],dp[a][k])
      update(k,a)
      update(a,k)
  dp[b][c] = max(dp[b][c],dpd[a] + 1)
  dp[c][b] = max(dp[c][b],dpd[a] + 1)
  dp[a][c] = max(dp[a][c],dpd[b] + 1)
  dp[c][a] = max(dp[c][a],dpd[b] + 1)
  dp[a][b] = max(dp[a][b],dpd[c] + 1)
  dp[b][a] = max(dp[b][a],dpd[c] + 1)
  dpmax = max(dpmax,dp[b][c],dp[a][c],dp[a][b],dp[c][b],dp[c][a],dp[b][a])
  
  dp[a][b] = max(0,dp[a][b],prevdpmax)
  dp[b][a] = max(0,dp[b][a],prevdpmax)
  dp[b][c] = max(0,dp[b][c],prevdpmax)
  dp[c][b] = max(0,dp[c][b],prevdpmax)
  dp[a][c] = max(0,dp[a][c],prevdpmax)
  dp[c][a] = max(0,dp[c][a],prevdpmax)
  update(a,b)
  update(a,c)
  update(b,a)
  update(b,c)
  update(c,a)
  update(c,b)
  for j in range(N):
    dp[j][a] = max(dp[j][a],prevdpmaxj[j])
    dp[j][b] = max(dp[j][b],prevdpmaxj[j])
    dp[j][c] = max(dp[j][c],prevdpmaxj[j])
    dp[a][j] = max(dp[a][j],prevdpmaxj[j])
    dp[b][j] = max(dp[b][j],prevdpmaxj[j])
    dp[c][j] = max(dp[c][j],prevdpmaxj[j])
    update(j,a)
    update(j,b)
    update(j,c)
    update(a,j)
    update(b,j)
    update(c,j)

print(max(dp[N-2][N-1],dp[N-1][N-2])+score)

Measured memory_bytes: 130712
#include <bits/stdc++.h>
#define INF INT_MAX
#define ll long long
using namespace std;
int main(){
    int N,A,B;
    cin >> N >> A >> B;
    int res = 0;
    for(int i=1;i<=N;i++){
        int t = i;
        int C = 0;
        for(int j=0;j<5;j++){
            C += t % 10;
            t /= 10;
        }
        if(A <= C && C <= B){
            res += i;
        }
    }
    cout << res << endl;
}

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

using namespace std;

typedef long long ll;
typedef double dd;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;

#define FOR(i,a,b) for (int (i) = (a); (i) < (b); (i)++)
#define DOW(i,b,a) for (int i = (b); i >= (a); i--)
#define pb(a) push_back(a)
#define ALL(a) (a).begin(),(a).end()
#define fi first
#define se second

template<typename T>
void print_array(T* arr, int num) {
   FOR(i, 0, num) cout << arr[i] << ' ';
   cout << endl;
}

template<typename T> 
void print_vector(vector<T> vec) {
   FOR(i,0,vec.size()) cout << vec[i] << ' ';
   cout << endl;
}

int n,arr[110];  

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    //freopen("test.txt", "r", stdin);
    cin >> n;
    int ans = 0, mi = 210;
    FOR(i,0,n) { 
        cin >> arr[i];
        ans += arr[i];
        if (arr[i] % 10 != 0) mi = min(mi,arr[i]);
    }
    if (ans % 10 == 0) {
        if (mi == 210) cout << 0;
        else cout << ans - mi;
        return 0;
    }
    cout << ans;
}


Measured memory_bytes: 256
use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();

    token.parse().ok().expect("failed to parse token")
}

fn alpha2num(c: char) -> usize {
    c as usize - 'A' as usize
}

fn shift_alpha(c: char, n: usize) -> char {
    let d = ((alpha2num(c) + n) % 26) as u8;
    (b'A' + d) as char
}

fn main() {
    let n: usize = read();
    let s: String = read();

    let ans: String = s.chars().map(|c| shift_alpha(c, n)).collect();
    println!("{}", ans);
}


Measured memory_bytes: 4352
import java.util.*;

public class Main{
  public static long dp[][];
  public static long func(int w[],long v[],int n,int W)
  {
    if(n==0 || W==0)
    {
      return 0;
    }
    if(dp[n][W]!=-1)
      return dp[n][W];
    if(W-w[n-1]>=0)
    return dp[n][W]=Math.max(v[n-1]+func(w,v,n-1,W-w[n-1]),
             func(w,v,n-1,W));
    else
    return dp[n][W]=func(w,v,n-1,W);   
  }
  public static void main(String[] args)
  {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int W=sc.nextInt();
    int w[]=new int[n];
    long v[]=new long[n];
    dp=new long[n+1][W+1];
    for(int i=0;i<n;i++)
    {
      w[i]=sc.nextInt();
      v[i]=sc.nextInt();
    }
    for(int i=0;i<=n;i++)
    {
      for(int j=0;j<=W;j++)
      dp[i][j]=-1;
    }
    System.out.println(func(w,v,n,W));
  }
}

Measured memory_bytes: 139120
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int MR=1e6+10;
typedef long long LL;
int a[MR];
int main() {
	int k,n;
	cin>>k>>n;
	for(int i=0;i<n;i++) cin>>a[i];
	sort(a,a+n);
    int maxd=k+a[0]-a[n-1];
    for(int i=0;i<n;i++){
    	maxd=max(maxd,a[i]-a[i-1]);
    }
    printf("%d\n",k-maxd);
    return 0;
}

Measured memory_bytes: 1024
import java.util.*;
 
public class Main
{
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		int d=0,c=0;
		int a=sc.nextInt();
		if(a<1200)
		{System.out.println("ABC");}
		else if(a<2800)
		{System.out.println("ARC");}
		else
		{System.out.println("AGC");}
	}
}

Measured memory_bytes: 21844
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "vector"
#include "string"
#include "map"
#include "algorithm"
#include "functional"
#include "set"
#include "numeric"

using namespace std;

const long long int MOD = 1000000007;

int N, M, K, H, W, L, R;

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

	string s, t;
	cin >> s >> t;
	bool a = false, b = false;
	for (auto i : s)if (i == '1')a = true;
	for (auto i : t)if (i == '1')b = true;
	if (!a && !b) {
		cout << 0 << endl;
		return 0;
	}
	if (!b) {
		cout << -1 << endl;
		return 0;
	}
	N = s.size();
	vector<int>l(N, MOD);
	vector<int>r(N, MOD);
	L = 0;
	R = -1;
	for (int i = 0; i < N; i++) {
		if (t[i] == '1') {
			if (R == -1)R = i;
			for (int j = L; j <= i; j++) {
				r[j] = i - j;
			}
			L = i + 1;
		}
	}
	for (int i = 0; i < N; i++) {
		if (r[i] == MOD)r[i] = R + N - i;
	}
	R = N - 1;
	L = -1;
	for (int i = N - 1; i >= 0; i--) {
		if (t[i] == '1') {
			if (L == -1)L = i;
			for (int j = R; j >= i; j--) {
				l[j] = j - i;
			}
			R = i - 1;
		}
	}
	for (int i = 0; i < N; i++) {
		if (l[i] == MOD)l[i] = i + N - L;
	}
	int ans = MOD;
	vector<vector<int>>lbox(N);
	for (int i = 0; i < N; i++)lbox[l[i]].push_back(r[i]);
	for (int i = 0; i < N; i++) {
		set<int>st;
		vector<vector<int>>llbox(N);
		int cnt = 0;
		for (int j = 0; j < N; j++) {
			if (t[j] != s[(i + j) % N]) {
				cnt++;
				llbox[l[(i+j)%N]].push_back((i+j)%N);
			}
		}
		for (int j = N - 1; j > 0; j--) {
			//cout << i << " " << j << " " << st.size() << endl;
			int box = j;
			if (st.empty()) {
				box += min(N - abs(i - j), abs(i - j));
				ans = min(ans, box+cnt);
				for (auto k : llbox[j])st.insert(-r[k]);
				//cout <<"a "<< i << " " << j << " " << ans << endl;
				continue;
			}
			auto it = st.upper_bound(j - N);
			if (it == st.end()) {
				box += min(N - abs(i - j), abs(i - j));
				ans = min(ans, box+cnt);
				for (auto k : llbox[j])st.insert(-r[k]);
				//cout <<"b "<< i << " " << j << " " << ans << endl;
			}
			box -= *it;
			//cout << i << " " << j << " " << box << endl;
			ans = min(ans, cnt+ box - *it + min(N - abs(i - j), abs(i - j)));
			ans = min(ans, cnt+ box + j + min(N - abs(i - (N + *it)), abs(i - (N + *it))));
			//cout <<"c "<< i << " " << j << " " << ans << endl;
			for (auto k : llbox[j])st.insert(-r[k]);
		}
		if (st.empty()) {
			ans = min(ans, cnt + min(N - i, i));
		//	cout << i << " 0 " << ans << endl;
			continue;
		}
		auto it = st.upper_bound(-N);
		ans = min(ans, cnt - *it + min(N - abs(i - (N + *it)), abs(i - (N + *it))));
		//cout << i << " 0 " << ans << endl;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i=ll(a);i<ll(b);i++)
#define irep(i,a,b) for(ll i=ll(a);i>=ll(b);i--)
#define pb push_back
#define mp make_pair
#define F .first
#define S .second 
using ll=long long;
using ld=long double;
using namespace std;
const ll mod=1e9+7;
ll l[110000],r[110000],g[110000];
int main(){
  ll q,x=0,y=2;
  cin>>q;
  rep(i,0,q)cin>>l[i]>>r[i];
  bool p=true;
  rep(i,3,100001){
    p=true;
    y=2;
    ll z=((i+(ll)1)/(ll)2);
    while(y<i){
      if(i%y==0){p=false;break;}
      if(y<z&&z%y==0){p=false;break;}
      if(y==2)y++;
      else y+=2;
    }
    if(p)x++;
    g[i]=x;
    i++;
  }
  rep(i,0,q)cout<<g[r[i]]-g[l[i]-(ll)2]<<endl;
}


Measured memory_bytes: 3072
#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 endl '\n'
#define pb push_back
#define fi first
#define se second
#define all(c) (c).begin(),(c).end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cerr << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif

const ll inf = 2e18;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;

int modpow(int x, int n, int m) {
	x %= m;
	int res = 1;
	while(n > 0) {
		if(n & 1)
			res = res * x % m;
		x = x * x % m;
		n >>= 1;
	}
	return res;
}

int fact[N], invfact[N];

void pre() {
	fact[0] = 1;
	for(int i = 1; i < N; i++) {
		fact[i] = fact[i - 1] * i % mod;
	}
	invfact[N - 1] = modpow(fact[N - 1], mod - 2, mod);
	for(int i = N - 2; i >= 0; i--) {
		invfact[i] = invfact[i + 1] * (i + 1) % mod;
	}
}

int ncr(int x, int y) {
	if(y < 0 || y > x)
		return 0;
	int res = fact[x] * invfact[y] % mod * invfact[x - y] % mod;
	return res;
}

signed main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	pre();
	int h, w, n;
	cin >> h >> w >> n;
	n++;
	vector<pii> v(n);
	v[0] = {h, w};
	for(int i = 1; i < n; i++) {
		cin >> v[i].fi >> v[i].se;
	}
	sort(all(v));
	int dp[n];
	for(int i = 0; i < n; i++) {
		dp[i] = ncr(v[i].fi + v[i].se - 2, v[i].fi - 1);
		for(int j = 0; j < i; j++) {
			dp[i] = (dp[i] - dp[j] * ncr(v[i].fi - v[j].fi + v[i].se - v[j].se, v[i].fi - v[j].fi) % mod + mod) % mod;
		}
	}
	cout << dp[n - 1];

	return 0;
}

Measured memory_bytes: 6824
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include <bitset>
#include<math.h>
using namespace std;
#define INF 11000000000
#define MOD 1000000007
typedef long long ll;
typedef pair<ll,int> P;


int main(){
  ll A,B;
  int K;

  cin>>A>>B>>K;
  for(int i=0;i<K;i++){
    if(A+i>B) break;
    cout<<A+i<<endl;
  }

  for(int i=K-1;i>=0;i--){
    if(B-i<=A+K-1) continue;
    else cout<<B-i<<endl;
  }
  
}


Measured memory_bytes: 256
# include <iostream>
# include <algorithm>
using namespace std;
int v[210],n,sol,i;
int main () {
    cin>>n;
    for(i=1;i<=2*n;i++)
        cin>>v[i];
    sort(v+1,v+2*n+1);
    for(i=1;i<=2*n;i+=2)
        sol+=v[i];
    cout<<sol<<"\n";
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#define MOD 1000000007
typedef long long ll;
using namespace std;

int main(){
  int n;
  cin>>n;
  string s;
  cin>>s;
  int r=0,b=0;
  for(int i=0;i<n;i++){
    if(s[i]=='R') r++;
    else b++;
  }

  if(r>b) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;

  return 0;
}


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

#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("-funroll-loops")
#pragma GCC target("avx,sse4")

#define mms(a,n) memset(a,0,sizeof((a)[0])*(n))
#define mmp(a,b,n) memcpy(a,b,sizeof((b)[0])*(n))
#define lowbit(x) ((x)&-(x))
#define pb push_back
#define fi first
#define se second
#define debug(...) fprintf(stderr,__VA_ARGS__)
#define fo(i,l,r) for(register int i=l,_lim_=r;i<=_lim_;i++)
#define fd(i,r,l) for(register int i=r,_lim_=l;i>=_lim_;i--)
#define fos(i,l,r,d) for(register int i=l,_lim_=r;i<=r;i+=d)
#define fol(i,l,r) for(register ll i=l,_lim_=r;i<=_lim_;i++)
#define fdl(i,r,l) for(register ll i=r,_lim_=l;i>=_lim_;i--)
#define fosl(i,l,r,d) for(register ll i=l,_lim_=r;i<=r;i+=d)
#define Clear(a) memset(a,0,sizeof(a))
#define Copy(a,b) memcpy(a,b,sizeof(b))
#define ALL(v) v.begin(),v.end()
#define SZ(v) ((int)v.size())

using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ldb;
typedef double db;
typedef pair<int,ll> pi;

namespace io{
	const int L=(1<<21)+1;
	char ibuf[L],*iS,*iT,obuf[L],*oS=obuf,*oT=obuf+L-1,c,st[55];int f,tp;
	#ifdef whzzt
		#define gc() getchar()
	#else
		#define gc() (iS==iT?(iT=(iS=ibuf)+fread(ibuf,1,L,stdin),(iS==iT?EOF:*iS++)):*iS++)
	#endif
	inline void flush(){fwrite(obuf,1,oS-obuf,stdout);oS=obuf;}
	inline void putc(char x){*oS++=x;if(oS==oT)flush();}
	template<class I>
	inline void gi(I&x){
		for(f=1,c=gc();c<'0'||c>'9';c=gc())if(c=='-')f=-1;
		for(x=0;c<='9'&&c>='0';c=gc())x=x*10+(c&15);x*=f;
	}
	template<class I>
	inline void print(I x){
		if(!x)putc('0');if(x<0)putc('-'),x=-x;
		while(x)st[++tp]=x%10+'0',x/=10;
		while(tp)putc(st[tp--]);
	}
	inline void gs(char*s,int&l){
		for(c=gc();c<'a'||c>'z';c=gc());
		for(l=0;c<='z'&&c>='a';c=gc())s[l++]=c;
	}
	inline char O(){
		for(c=gc();c!='C'&&c!='Q'&&c!='R';c=gc());
		return c;
	}
	inline void ps(const char*s){
		fo(i,0,strlen(s)-1)putc(s[i]);
	}
};
using io::putc;
using io::gi;
using io::gs;
using io::print;

const int N=100005;
int n,x[N],cnt[1<<4],u,v,w,ans,dp[1<<16],st;
bool vi[1<<16];

int dfs(int S){
	if(!S)return 0;if(vi[S])return dp[S];
	vi[S]=true;
	int ret=233;
	fo(i,1,15)if(S>>i&1)fo(j,1,15)if(i!=j&&(S>>j&1)){
		int T=S^(1<<i)^(1<<j)^(1<<(i^j));
		if(__builtin_popcount(T)<__builtin_popcount(S))ret=min(ret,dfs(T)+1+(~T>>(i^j)&1));
	}
	return dp[S]=ret;
}

int main(){
	gi(n);
	fo(i,2,n)gi(u),gi(v),gi(w),x[u+1]^=w,x[v+1]^=w;
	fo(i,1,n)if(x[i])cnt[x[i]]++;
	fo(i,0,15)ans+=cnt[i]>>1,st|=(cnt[i]&1)<<i;
	ans+=dfs(st);
	printf("%d\n",ans);
	return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

#define endl '\n'
#define len(x) (int)x.size()
#define rep(a, b, i) for (int i = a; i < b; i++)
#define repi(a, b, i) for (int i = a; i > b; i--)
#define min3(a, b, c) min(a, min(b, c))
#define max3(a, b, c) max(a, max(b, c))
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define binary_search
#define mem(a, b) memset(a, b, sizeof(a))
#define all(x) (x).begin(), (x).end()
#define allr(x) (x).rbegin(), (x).rend()
#define __                                                                                                             \
    ios_base::sync_with_stdio(false);                                                                                  \
    cin.tie(0);                                                                              
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vii;
typedef vector<pll> vll;

const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
const ld PI = 3.141592653589793;
const int MAXN = 1e5 + 5;

int32_t main() {
    __;

	int a, b, c; 
	cin >> a >> b >> c;
	swap(a, b);
	swap(a, c);
	
	cout << a << " " << b << " " << c;

    return 0;
}


















Measured memory_bytes: 256
n,m,c=map(int,input().split())
B=[int(i) for i in input().split()]
A=[[int(i) for i in input().split()] for j in range(n)]
ans=0
for i in range(n):
        x=0
        for j in range(len(A[i])):
                x+=A[i][j]*B[j]
        x+=c
        if x>0:
                ans+=1
print(ans)

Measured memory_bytes: 3064
N,K = list(map(int , input().split()))
D = []
A= []
for i in range(K):
    d = int(input())
    y = list(map(int, input().split()))
    D.append(d)
    A.extend(y)
S = []
for j in range (N):
    k = A.count((j+1))
    if k == 0:
        S.append(j+1)
print(len(S))

Measured memory_bytes: 9304
#include <bits/stdc++.h>
using namespace std;
double expectation(int x, int y, int z, int n, double*** dp){
    if(x < 0 || y < 0 || z < 0) return 0;
    if(x == 0 && y == 0 && z == 0) return 0;
    if(dp[x][y][z] > -0.9) return dp[x][y][z];
    double exp = n + x*expectation(x-1, y, z, n, dp) + y*expectation(x+1, y-1, z, n, dp) + z*expectation(x, y+1, z-1, n, dp);
    dp[x][y][z] = exp/(x + y + z);
    return dp[x][y][z];
}
int main() {
	int n;
	cin>>n;
	int x = 0, y = 0, z = 0;
	for(int i=0;i<n;i++){
	    int curr;
	    cin>>curr;
	    if(curr == 1) x++;
	    else if(curr == 2) y++;
	    else if(curr == 3) z++;
	}
	double*** dp = new double**[n+1];
	for(int i=0;i<=n;i++){
	    dp[i] = new double*[n+1];
	    for(int j=0;j<=n;j++){
	        dp[i][j] = new double[z+1];
	        for(int k=0;k<=z;k++){
	            dp[i][j][k] = -1;
	        }
	    }
	}
	cout<<fixed<<setprecision(10)<<expectation(x, y, z, n, dp);
	return 0;
}

Measured memory_bytes: 214784
//
//  main.cpp
//  Exercise
//
//

#include <algorithm>
#include <iostream>
#include <limits>
#include <math.h>
#include <tuple>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "string.h"
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <set>

using namespace std;

using int_t = int;

int main() {
    int A, B, C, X, Y;
    scanf("%d %d %d %d %d", &A, &B, &C, &X, &Y);
    if (A + B <= 2 * C) {
        printf("%d", A * X + B * Y);
    } else if (2 * C <= A && 2 * C <= B) {
        printf("%d", 2 * C * max(X, Y));
    } else if (2 * C <= A) { // and B < 2 * C
        if (X <= Y) {
            printf("%d", 2 * C * X + B * (Y - X));
        } else {
            printf("%d", 2 * C * X);
        }
    } else if (2 * C <= B) { // and A < 2 * C
        if (Y <= X) {
            printf("%d", 2 * C * Y + A * (X - Y));
        } else {
            printf("%d", 2 * C * Y);
        }
    } else {
        if (X >= Y) {
            printf("%d", Y * 2 * C + (X - Y) * A);
        } else {
            printf("%d", X * 2 * C + (Y - X) * B);
        }
    }
    printf("\n");
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep2(i, s, n) for (ll i = s; i < (ll)(n); i++)
#define repr(i, n) for (ll i = n; i >= 0; i--)
#define pb push_back
#define COUT(x) cout << (x) << endl
#define COUTF(x) cout << setprecision(15) << (x) << endl
#define ENDL cout << endl
#define DF(x) x.erase(x.begin())  // 先頭文字削除
#define ALL(x) x.begin(), x.end()
#define SORT(x) sort(ALL(x))
#define REVERSE(x) reverse(ALL(x))
#ifdef _DEBUG
#define debug(x) cout << "[debug] " << #x << ": " << x << endl
#else
#define debug(x)
#endif
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using P = pair<ll, ll>;
constexpr ll INF = 0x3f3f3f3f3f3f3f3f;
constexpr double PI = 3.141592653589793238462643383279;
ll getDigit(ll x) {
  return x == 0 ? 1 : log10(x) + 1;
}

ll gcd(ll x, ll y) {
  return y ? gcd(y, x % y) : x;
}

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

vector<P> factorize(ll n) {
  vector<P> result;
  for (ll i = 2; i * i <= n; ++i) {
    if (n % i == 0) {
      result.pb({i, 0});
      while (n % i == 0) {
        n /= i;
        result.back().second++;
      }
    }
  }
  if (n != 1) {
    result.pb({n, 1});
  }
  return result;
}

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

template <typename T>
void debugV(const vector<T> v) {
#ifdef _DEBUG
  rep(i, v.size()) {
    cout << i << ":" << v[i] << " ";
  }
  cout << endl;
#else
  (void)v;
#endif
}

signed main() {
  ll N, M, K;
  cin >> N >> M >> K;

  vector<vll> fri(N);
  vector<vll> blo(N);

  rep(i, M) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    fri[a].pb(b);
    fri[b].pb(a);
  }

  rep(i, K) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    blo[a].pb(b);
    blo[b].pb(a);
  }

  vll renketsu(N);
  ll label = 0;
  rep(i, N) {
    if (renketsu[i] != 0) {
      continue;
    } else {
      label++;
    }
    queue<P> q;
    q.push({i, label});
    while (!q.empty()) {
      P f = q.front();
      q.pop();
      ll index = f.first;
      ll num = f.second;
      if (renketsu[index] != 0) {
        continue;
      }
      renketsu[index] = num;
      for (auto fr : fri[index]) {
        q.push({fr, label});
      }
    }
  }

  // debugV(renketsu);

  map<ll, ll> m;
  rep(i, N) {
    m[renketsu[i]]++;
  }

  rep(i, N) {
    ll r_count = m[renketsu[i]];
    ll f_count = fri[i].size();

    ll b_count = 0;
    for (auto bl : blo[i]) {
      if (renketsu[i] == renketsu[bl]) {
        b_count++;
      }
    }

    ll ans = r_count - f_count - b_count - 1;
    if (i == 0) {
      cout << ans;
    } else {
      cout << " " << ans;
    }
  }
  ENDL;
  return 0;
}

Measured memory_bytes: 17348
import java.util.Scanner;
class Main{
	public static void main(String[]agrs){
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()){
			int n = sc.nextInt();
			int x = sc.nextInt();
			if(n == 0 && x == 0) break;
			int count = 0;
			for(int i = 1; i <= n; i++){
				for(int j = 1; j <= n; j++){
					for(int k = 1; k <= n; k++){
						if(i + j + k == x && i != j
								&& j != k && i != k){
							count++;
						}
					}
				}
			}
			System.out.println(count/6);
		}
	}
}

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

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n,m;
  cin>>n>>m;
  cout<<(n*(n-1)+m*(m-1))/2<<endl;
  return 0;
}


Measured memory_bytes: 256
import java.util.*;

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

		long N = sc.nextLong();
		long X = sc.nextLong();
		
		System.out.println(N+solve(X, N-X));
		
		sc.close();
	}
	
	static long solve(long a, long b) {
		if(a>b) {
			long temp = a;
			a = b;
			b = temp;
		}
		
		if(gcd(a,b)==a)
			return (b/a*2-1)*a;
		
		return 2*(b/a)*a + solve(b%a, a);
	}
	
	static long gcd(long a, long b) {
		if(b%a==0)
			return a;
		else
			return gcd(b%a, a);
	}
}


Measured memory_bytes: 9932
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <algorithm>
#define ll long long

using namespace std;

const long long mod = 1e9+7;

int main(){
	int n;
    int* a;
    int* a_copy;
    cin >> n;
    a = new int[n];
    a_copy = new int[n];

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

    sort(a, a+n);

    for(int i = 0; i < n; i++){
        if(a_copy[i] == a[n-1]){
            cout << a[n-2] << endl;
        }else{
            cout << a[n-1] << endl;
        }
    }
}

Measured memory_bytes: 3200
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>
using namespace std;
using ll = long long;
#define fst first
#define snd second

/* clang-format off */
template <class T, size_t D> struct _vec { using type = vector<typename _vec<T, D - 1>::type>; };
template <class T> struct _vec<T, 0> { using type = T; };
template <class T, size_t D> using vec = typename _vec<T, D>::type;
template <class T> vector<T> make_v(size_t size, const T& init) { return vector<T>(size, init); }
template <class... Ts> auto make_v(size_t size, Ts... rest) { return vector<decltype(make_v(rest...))>(size, make_v(rest...)); }
template <class T> inline void chmin(T &a, const T& b) { if (b < a) a = b; }
template <class T> inline void chmax(T &a, const T& b) { if (b > a) a = b; }
/* clang-format on */

int main() {
#ifdef DEBUG
  ifstream cin("in.txt");
#endif
  int N;
  while (cin >> N) {
    vec<int, 2> G(N);
    for (int i = 1; i < N; i++) {
      int a;
      cin >> a;
      --a;
      G[a].push_back(i);
    }
    function<int(int)> dfs = [&](int v) {
      vector<int> ch;
      for (int u : G[v]) {
        ch.push_back(dfs(u));
      }
      sort(ch.rbegin(), ch.rend());
      int res = 0;
      for (int i = 0; i < ch.size(); i++) chmax(res, ch[i] + i + 1);
      return res;
    };
    cout << dfs(0) << endl;
  }
  return 0;
}


Measured memory_bytes: 16640
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm> 

#define fo(i,j,l) for(int i=j;i<=l;++i)
#define fd(i,j,l) for(int i=j;i>=l;--i)

using namespace std;
typedef long long ll;
const ll N=12e4;

int a,b,c;

int main()
{
	scanf("%d%d%d",&a,&b,&c);
	if(a==b&&a==c)puts("Yes");
	else puts("No");
}

Measured memory_bytes: 256
#include<stdio.h>
int isPrime(int);
int main()
{
  int i,n,count=0,flag=0;
  scanf("%d",&n);
  int A[n];
  for(i=0;i<n;i++){
    scanf("%d",&A[i]);
  }
  for(i=0;i<n;i++){
    flag=isPrime(A[i]);
    if(flag==1){
      count++;
    }
      }
  printf("%d\n",count);
  return 0;
}
int isPrime(int x){
  int j;
  if(x==2)
    return 1;
  if(x<2 || x%2==0)
    return 0;
  j=3;
  while(j*j<=x){
    if(x%j==0)
      return 0;
      j=j+2;
  }
      return 1;
}

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

#define LL long long
#define VI vector<int>
#define VB vector<bool>
#define VL vector<long long>
#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define EPS 1e-14

const std::string YES = "YES";
const std::string Yes = "Yes";
const std::string NO = "NO";
const std::string No = "No";

using namespace std;


LL make_id(LL station, LL company) {
    return station * ((LL)1e6+10) + company;
}

int to_station(LL id) {
    return (int)(id / (1e6+10));
}

void dfs(LL start, vector<LL> &route, set<LL> &used, unordered_map<LL, vector<LL>> &graph) {
    FOE(id, graph[start]) {
        if (used.find(id) != used.end()) { continue; }
        used.insert(id);
        route.emplace_back(id);

        dfs(id, route, used, graph);
    }

}

int solve(int N, int M, const vector<int> &P, vector<int> &Q, vector<int> &C) {

    unordered_map<LL, vector<LL>> graph;    // (駅, 会社)から到達できる(駅, 会社)のリスト
    FOR(i, 0, P.size()) {
        graph[make_id(P[i], C[i])].emplace_back(make_id(Q[i], C[i]));
        graph[make_id(Q[i], C[i])].emplace_back(make_id(P[i], C[i]));
    }

    set<LL> used;

    // 駅とrouteの2部グラフを作成
    unordered_map<int, vector<int>> station_route_graph;
    int route_id = N + 2;
    FOE(p, graph) {
        LL station_company = p.first;

        if (used.find(station_company) != used.end()) {
            continue;
        }
        vector<LL> route = {station_company};
        used.insert(station_company);
        dfs(station_company, route, used, graph);

        FOE(id, route) {
            int station = to_station(id);
            station_route_graph[station].push_back(route_id);
            station_route_graph[route_id].push_back(station);
        }
        route_id++;
    }

    // 2部グラフ上で最短経路を求める
    unordered_map<int, int> dist;
    dist[1] = 0;
    queue<int> que;
    que.push(1);
    while (not que.empty()) {
        int x = que.front(); que.pop();
        FOE(e, station_route_graph[x]) {
            if(dist.find(e) == dist.end() or dist[e] > dist[x] + 1) {
                dist[e] = dist[x] + 1;
                que.push(e);
            }
        }
    }

    if (dist.find(N) == dist.end()) {
        return -1;
    }
    return dist[N] / 2;

}


int main() {
    int N, M;
    cin >> N >> M;

    vector<int> P(M), Q(M), C(M);
    FOR(i, 0, M) {
        cin >> P[i] >> Q[i] >> C[i];
    }

    cout << solve(N, M, P, Q, C) << endl;

    return 0;
}

Measured memory_bytes: 99060
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstring>
#include<deque>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for(int i = (x), i##_len = int(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = int(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = int(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = int(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = int(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << endl

const int IINF = (1 << 30) - 1;
const long long LLINF = 1LL << 61;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

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

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

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

    int h, w;
    cin >> h >> w;

    vector<string> c(h);
    vector<int> l(h, w), r(h, -1);
    rep(i, h){
        cin >> c[i];
        rep(j, w){
            if(c[i][j] == 'B'){
                chmin(l[i], j);
                chmax(r[i], j);
            }
        }
    }

    int ans = 0;
    rep(i, h){
        rep(j, h){
            if(0 <= r[j] && l[i] < w){
                chmax(ans, abs(i - j) + r[j] - l[i]);
            }
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 3572
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
#include <set>

#include <cassert>
using namespace std;

inline int GetBit(int mask, int num) {
    return (mask >> num) & 1;
}

int n, k;
vector<vector<int>> g;

const int MAX_LOG = 20;

vector<vector<int>> up;
vector<int> tin;
vector<int> tout;
vector<int> dist;
int curTimer;

vector<bool> used;

void Build(int v, int pr) {
    tin[v] = curTimer++;
    up[v].resize(MAX_LOG);
    if (pr == -1) {
        for (int i = 0; i < MAX_LOG; ++i) {
            up[v][i] = v;
        }
    } else {
        dist[v] = dist[pr] + 1;
        up[v][0] = pr;
        for (int i = 1; i < MAX_LOG; ++i) {
            up[v][i] = up[up[v][i - 1]][i - 1];
        }
    }
    for (int u : g[v]) {
        Build(u, v);
    }
    tout[v] = curTimer++;
}

int GetVertexOnDist(int v, int dist) {
    for (int i = MAX_LOG - 1; i >= 0; --i) {
        if (GetBit(dist, i)) {
            v = up[v][i];
        }
    }
    return v;
}

void FillUsed(int v) {
    used[v] = true;
    for (int u : g[v]) {
        if (!used[u]) {
            FillUsed(u);
        }
    }
}

int main() {
    // freopen("input.txt", "r", stdin);

    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> k;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        --a[i];
    }

    int result = 0;
    for (int i = 0; i < n; ++i) {
        if (a[i] != 0) {
            ++result;
        }
    }

    if (k == 1) {
        cout << result << endl;
        return 0;
    }

    int newResult = 0;
    if (a[0] != 0) {
        ++newResult;
    }

    g.resize(n);
    for (int i = 1; i < n; ++i) {
        g[a[i]].push_back(i);
    }

    tin.resize(n);
    tout.resize(n);
    curTimer = 0;
    up.resize(n);
    dist.resize(n);
    dist[0] = 0;
    Build(0, -1);

    set<pair<int, int>> s;
    for (int v = 0; v < n; ++v) {
        s.insert(make_pair(dist[v], v));
    }

    used.assign(n, false);

    while (!s.empty()) {
        int d = (*s.rbegin()).first;
        int v = (*s.rbegin()).second;
        s.erase(make_pair(d, v));

        if (d <= k || used[v]) {
            continue;
        }

        int u = GetVertexOnDist(v, k - 1);

        ++newResult;

        FillUsed(u);
    }

    result = min(result, newResult);

    cout << result << endl;

    return 0;
}

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

int dp[1003][1003];
const int mod = 1e9 + 7;
string a[1003];

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

  int h, w;
  cin >> h >> w;
  for (int i = 0; i < h; i++) cin >> a[i];

  for (int i = h - 1; i >= 0; i--) {
    for (int j = w - 1; j >= 0; j--) {
      if (a[i][j] == '#')
        dp[i][j] = 0;
      else if (i == h - 1 && j == w - 1)
        dp[i][j] = 1;
      else if (i == h - 1)
        dp[i][j] = dp[i][j + 1];
      else if (j == w - 1)
        dp[i][j] = dp[i + 1][j];
      else
        dp[i][j] = (dp[i + 1][j] + dp[i][j + 1]) % mod;
    }
  }

  cout << dp[0][0] << '\n';

  return 0;
}

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

int main() {
  string S,T;
  int a,b;
  string U;
  
  cin >> S >> T;
  cin >> a >> b;
  cin >> U;
  
  if(S == U) cout << a - 1 << " " << b << endl;
  else if(T == U) cout << a  << " " << b - 1 << endl;
}

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

typedef long long ll;
typedef unsigned long long ull;
//typedef long double ld;

typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double,double> pdd;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<int> vi;
typedef vector<pair<int, int > > vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vll> vvll;

constexpr ll MOD = 1e9+7;
constexpr int INF = 1<<30;
constexpr ll  LINF = 1LL<<60;

#define Sort(x) sort(x.begin(),x.end())
#define Reverse(x) reverse(x.begin(),x.end())
#define ABS(a,b) ((a)<(b)?(b)-(a):(a)-(b))
#define nbit(n) (1ll<<(n))

// sort(begin(x), end(x), greater<ll>());
// lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置
// upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置

// Returns minimum of a and b.
// If a is less b, a is set to b.
template<typename T>
T chmin(T& a, T b) {
  if (a > b) {
    a = b;
  }
  return a;
}

// Returns maximum of a and b.
// If a is less b, a is set to b.
template<typename T>
T chmax(T& a, T b) {
  if (a < b) {
    a = b;
  }
  return a;
}

// a = (a+b)%MOD;
void addmod(ll& a, ll b) {
  a = (a+b)%MOD;
}

template <typename T>
void dumpContents(const T& v, const string& msg)
{
  cerr << "### " << msg << " ###\n";
  for (const auto& x : v) {
    cerr << x << " ";
  }
  cerr << endl;
}

struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<setprecision(20)<<fixed;
    #define endl "\n"
  }
} before_main_function;

// ========== end of template ==========

int main(int argc, char** argv) {
  int N;
  cin >> N;

  vi A(N, INF);
  vi B(N-1);
  for (int i=0; i<N-1; i++) {
    cin >> B[i];
  }

  for (int i=0; i<N-1; i++) {
    A[i] = min({A[i], B[i]});
    A[i+1] = B[i];
  }

  cout << accumulate(begin(A), end(A), 0LL) << endl;

  return 0;
}


Measured memory_bytes: 256