akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
#include <chrono>
using namespace std::chrono;
using namespace std;
#define fr(x,i,n) for(int x = i; x < n; ++x)
#define fb(x,i,n) for(int x = i; x >= n; --x)
#define pr pair<int,int>
#define ll long long int
const ll mod=998244353;
/*    auto start = high_resolution_clock::now();
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start);
    cout << "\nduration="<<duration.count()/1e6 << endl;
*/
ll f(ll x,ll n){
    ll r=1;
    while(n){
        if(n%2==1)
        r=(r*x)%mod;
        x=(x*x)%mod;
        n/=2;
    }
    return r;

}
vector<ll> fact(2e5+10,1),ifact(2e5+10);
void build(){
    for(ll i=2;i<fact.size();i++)
    fact[i]=(i*fact[i-1])%mod;
    fr(i,0,fact.size()){
        ifact[i]=f(fact[i],mod-2);
    }
}
ll C(int n, int r){
    if(n<r)
    return 0;
    return (((fact[n]*ifact[r])%mod)*ifact[n-r])%mod;
}
void solve(){
    int n,m,k;
    cin>>n>>m>>k;
    // let l blocks are colored same color as their left adjacent
    ll ans=0;
    fr(l,0,k+1){
        ll x;
        x=(C(n-1,l)*m)%mod;
        x=(x*f(m-1,n-l-1))%mod;
        x=(x+mod)%mod;
        ans=(ans+x)%mod;
    }
    cout<<ans;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    build();
    int t=1;
    //cin>>t;
    while(t--)
    solve();

}


Measured memory_bytes: 6284
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <utility>
#include <iomanip>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
ll dp[101][8];
int w;
bool valid(int bit)
{
	//正当性チェック
	bool f=true;
	for(int b=0;b<w-1;b++)
	{
		if(bit&(1<<b))
		{
			if(b>0 && (bit&(1<<(b-1))))
			{
				f=false;
				break;
			}
			if(b<w-2 && (bit&(1<<(b+1))))
			{
				f=false;
				break;
			}				
		}
	}
	return f;
}
void update(int i,int j)
{
	ll cnt=0;
	//落下する場合を調べる
	for(int bit=0;bit<(1<<(w-1));bit++)
	{
		if(!valid(bit))
			continue;
		//jの周辺チェック
		if(!((j>0 && (bit&(1<<(j-1))))||(j<w-1 && (bit&(1<<j)))))
			cnt++;
	}
	dp[i][j]+=dp[i-1][j]*cnt;
	dp[i][j]%=MOD;
	cnt=0;
	//左からくる
	if(j>0)
	{
		for(int bit=0;bit<(1<<(w-1));bit++)
		{
			if(!valid(bit))
				continue;
			//jの周辺チェック
			if(bit&(1<<(j-1)))
				cnt++;
		}
	}
	dp[i][j]+=dp[i-1][j-1]*cnt;
	dp[i][j]%=MOD;
	cnt=0;
	//右からくる
	if(j<w-1)
	{
		for(int bit=0;bit<(1<<(w-1));bit++)
		{
			if(!valid(bit))
				continue;
			//jの周辺チェック
			if(bit&(1<<j))
				cnt++;
		}
	}
	dp[i][j]+=dp[i-1][j+1]*cnt;
	dp[i][j]%=MOD;
}
int main()
{
	int h,k;
	ll ans=0;
	cin>>h>>w>>k;
	k--;
	if(w==1)
	{
		cout<<1<<endl;
		return 0;
	}
	fill(dp[0],dp[101],0);
	dp[0][0]=1;
	for(int i=1;i<h+1;i++)
	{
		for(int j=0;j<w;j++)
		{
			update(i,j);
		}
	}
	cout<<dp[h][k]<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int main()
{
    int m, f, r;
        scanf("%d %d %d", &m, &f, &r);
        // printf("%d %d %d\n", m, f, r);
    while (!(m == -1 && f == -1 && r == -1))
    {
        if (m == -1 || f == -1)
            printf("F\n");
        else if (m + f >= 80)
            printf("A\n");
        else if (m + f >= 65)
            printf("B\n");
        else if (m + f >= 50)
            printf("C\n");
        else if (m + f >= 30)
        {
            if (r >= 50)
                printf("C\n");
            else
                printf("D\n");
        }
        else if (m + f < 30)
            printf("F\n");
        scanf("%d %d %d", &m, &f, &r);
    }
}


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

int main()
{
  long long n,z,w;
  
  cin>>n>>z>>w;
  
  long long arr[n];
  
  for(long long i=0;i<n;i++)cin>>arr[i];
  long long a,b;
  if(n==1)
    cout<<abs(arr[0]-w)<<endl;
  else
  {
   	a=abs(arr[n-1]-w);
    b=abs(arr[n-2]-arr[n-1]);
    if(a>b)cout<<a<<endl;
    else cout<<b<<endl;
  }
  
  
  
}


Measured memory_bytes: 256
/*
17D8101031C 杉山　祥吾 ID:keisei334 C++
*/

#include <iostream>

using namespace std;

#define N 10000
int a[N],b[N];

bool LinearSearch(int key,int n){
  int i=0;
  
  a[n] = key;
  while(a[i]!=key){
    i++;
  }

  return i!=n;
}

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

  cin>>q;
  for(int i=0;i<q;i++){
    cin>>b[i];
  }

  for(int i=0;i<q;i++){
    count+=LinearSearch(b[i],n);
  }

  cout<<count<<"\n";

  return 0;
}

/*実行結果
5
1 2 3 4 5
3
3 4 1
3

3
3 1 2
1
5
0

5
1 1 2 2 3
2     
1 2
2
*/



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

int main() {
  string s, t;
  cin >> s >> t;
  int ssz = s.size(), tsz = t.size();
  int ans = tsz;
  for (int i = 0; i <= ssz - tsz; i++) {
    int tmp = tsz;
    for (int j = 0; j < tsz; j++) if (s[i + j] == t[j]) tmp--;
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3628
//38
#include<iostream>
#include<string>
#include<set>
#include<cstdlib>

using namespace std;

int main(){
  cout<<"?z"<<endl;
  set<string> s;
  s.insert("z");
  char p='z';
  for(;;){
    string r;
    cin>>r;
    if(r[0]!=p||s.count(r)){
      cout<<"!OUT"<<endl;
      return 0;
    }
    s.insert(r);
    string n=string(1,r.end()[-1]);
    for(int i=0;i<8;i++){
      n+=char('a'+rand()%26);
    }
    n+='z';
    s.insert(n);
    p=n.end()[-1];
    cout<<'?'<<n<<endl;
  }
  return 0;
}

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

#define int long long
#define ii pair<int, int>

signed main(){
    ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int n;
    cin >> n;
    map<string, int> M;
    for(int i = 0; i < n; i++){
        string S;
        cin >> S;
        M[S]++;
    }

    cout << "AC x " << M["AC"] << endl;
    cout << "WA x " << M["WA"] << endl;
    cout << "TLE x " << M["TLE"] << endl;
    cout << "RE x " << M["RE"] << endl;
}


Measured memory_bytes: 3664
#include<stdio.h>

int main(){
  int k,a,b;
  scanf("%d %d %d",&k,&a,&b);
  
  int i;
  int res = 0;
  for(i = 1;i < 1000;i++){ 
    if(k*i >= a && k*i <= b)
     	res = 1;
  }
  if(k*0 >= a && k*0 <=b)
    res =1;
  if(res == 1)
    printf("OK");
  else
    printf("NG");
 
  return 0;
}


Measured memory_bytes: 1724
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define mst(a,b) memset((a),(b),sizeof(a))
#define mp(a,b) make_pair(a,b)
#define pi acos(-1)
#define pii pair<int,int>
#define pb push_back
#define lowbit(x) ((x)&(-x))
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int maxn = 1e5 + 10;
const int maxm = 1e6 + 10;
const ll mod =  998244353;

int a[maxn];

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    int n;
    scanf("%d",&n);
    ll ans = 0;
    for(int i = 0; i < n; i++) {
        int x,y;
        scanf("%d%d",&x,&y);
        a[i] = x + y;
        ans -= y;
    }
    sort(a, a + n);
    int cur = 1;
    for(int i = n - 1; i >= 0; i--) {
        if(cur) ans += a[i];
        cur ^= 1;
    }
    printf("%lld",ans);
    return 0;
}


Measured memory_bytes: 640
#include<cstdio>
#include<algorithm>
#define MAXN 100006
#define MAXK 2006
using namespace std;
int n,k;
char c;
int map[MAXK][MAXK],a,b,black,maxone,minone=MAXN;
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d %c",&a,&b,&c);
		black=0;
		if(c=='B') black=1;
		a%=k*2;
		b%=k*2;
		if(a>=k)
		{
			a%=k;
			black^=1;
		}
		if(b>=k)
		{
			b%=k;
			black^=1;
		}
		if(black)
		{
			map[a][k-1]+=1;
			map[k-1][b-1]+=1;
			map[a][b-1]-=2;
		}
		else
		{
			map[k-1][k-1]+=1;
			map[a][k-1]-=1;
			map[k-1][b-1]-=1;
			map[a][b-1]+=2;
		}
	}
	for(int i=k-1;i>=0;i--)
		for(int j=k-1;j>=0;j--)
		{
			map[i][j]+=map[i+1][j]+map[i][j+1]-map[i+1][j+1];
			maxone=max(maxone,map[i][j]);
			minone=min(minone,map[i][j]);
		}
	printf("%d",max(maxone,(n-minone)));
}

Measured memory_bytes: 8320
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <bitset>

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

#define FALSE printf("false\n");
#define TRUE printf("true\n");
#define all(x) (x).begin(),(x).end()
#define print(x) cout<<(x)<<endl
#define rep(i,n) for(int i = 0;i < ((int)(n));++i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define SUM(vec) accumulate(all(vec),0LL)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define test(a) cout<<"line:"<<__LINE__<<"["<<(#a)<<": "<<(a)<<"]"<<endl

const int INF = 1e9+7;
const ll INFL = 9*1e18;

ll inline digit(ll num){int tmp = 0;while(num){tmp++;num/=10;}return tmp;}
template<typename T>inline T digitSum(T num){T sum = 0;while(num){sum+=num%10;num/=10;}return sum;} // 各桁の和
template<typename T>inline T gcd(T a,T b){if(b == 0)return a;return gcd(b,a%b);}
template<typename T>inline T lcm(T a, T b){T g = gcd(a,b);return a/g*b;}
template<typename T>inline T power(T a,T b){T tmp=1;rep(i,b){tmp*=a;}return tmp;}

int ctoi(char c){return c - '0';}

bool isOk(ll key,ll value){
  return key >= value;
}

ll binary_search(vector<ll> a,ll key){
  ll ok = a.size()-1;
  ll ng = 0;

  while(abs(ok - ng) > 1){
    ll mid = (ok + ng) / 2;
    if(isOk(key,a[mid]))ok = mid;
    else ng = mid;
  }
  return ok;
}

bool isPrime(ll n) {
	for (int i = 2; n >= i * i; ++i) {
		if(!(n % i))return false;
	}
  return true;
}
vector<ll> primes(ll n){
  vector<ll> v;
  for (ll i = 2; i*i <= n; ++i) {
    while(n%i==0){
      n/=i;
      v.pb(i);
    }
  }
  if(n != 1)v.pb(n);
  return v;
}


int main() {
  cin.tie(0); ios::sync_with_stdio(0);
  string s;cin>>s;
  ll ans = 0;
  rep(i,1<<(s.size()-1)){
    bitset<10> bs(i);
    ll num = 0;
    rep(j,s.size()){
      num = num*10 + ctoi(s[j]);
      if(bs[j]){
        ans += num;
        num = 0;
      }
    }
    ans += num;
  }

  print(ans);
  return 0;
} 

Measured memory_bytes: 256
import java.util.Scanner;

public class Main{

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		int m = in.nextInt();
		int[] p = new int[n];
		int[] union = new int[n];
		for(int i=0;i<n;i++) {
			p[i] = in.nextInt()-1;
			union[i] = i;
		}
		for(int i=0;i<m;i++) {
			int x = in.nextInt()-1;
			int y = in.nextInt()-1;
			unite(union,x,y);
		}
		long res = 0;
		for(int i=0;i<n;i++) {
			if(root(union,p[i])==root(union,i)) res++;
		}
		System.out.println(res);
		in.close();
	}
	
	private static void unite(int[] union,int x,int y) {
		union[root(union,y)] = root(union,x);
	}
	
	private static int root(int[] union,int x) {
		if(union[x]==x) {
			return x;
		}else {
			return union[x] = root(union,union[x]);
		}
	}

}


Measured memory_bytes: 92472
x1, y1, x2, y2 = map(int, input().strip().split())

v1x, v1y = x2 - x1, y2 - y1
v2x, v2y = -v1y, v1x

x3, y3 = x2 + v2x, y2 + v2y
x4, y4 = x3 - v1x, y3 - v1y

print(x3, y3, x4, y4)


Measured memory_bytes: 2940
#include <bits/stdc++.h>
using namespace std;
 
int main() {
 
  int N;
  cin >> N;
  
  int p = N / 1.08;

  
  if((int)p * 1.08 == N){
    cout <<  p << endl;
  }else if((int)((p-1) * 1.08) == N){
    cout <<  p-1 << endl;
  }else if((int)((p+1) * 1.08) == N){
    cout <<  p+1 << endl;
  }else{
    cout <<  ":(" << endl;
  }
 
}

Measured memory_bytes: 3636
#include <iostream>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <bitset>
#include <utility>
#include <numeric>
#include <queue>
#include <stack>

using ll = long long;
using namespace std;

constexpr int MOD = 1e9 + 7;
constexpr ll MOD_LL = ll(1e9 + 7);

template<typename T>
T gcd(T a, T b) {
	if( a < b ) swap(a, b);
	
	T r;
	while( r = a % b ) {
		a = b;
		b = r;
	}
	
	return b;
}

template<typename T>
T lcm(T a, T b) {
	return (a / gcd(a, b)) * b;
}

int main(void) {
	int n;
	cin >> n;
	
	int ans = 1;
	for(int i = 0; i < n; ++i) {
		int a;
		cin >> a;
		ans = lcm(ans, a); 
	}
	
	cout << ans << endl;
	
	return 0;
}



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

const long long mod = 1000000007;
const int maxn = 200005;

int h, w, a, b;
long long ans;
long long fact[maxn];

long long mul_mod (long long x, long long y) {
	return (x * y) % mod;
}

long long add_mod (long long x, long long y) {
	if (x + y >= mod)
		return x + y - mod;
	return x + y;
}

long long pow_mod (long long x, long long y) {
	long long res = 1, t = x;
	while (y) {
		if (y & 1)
			res = mul_mod(res, t);
		t = mul_mod(t, t);
		y >>= 1;
	}
	return res;
}

long long div_mod (long long x, long long y) {
	return mul_mod(x, pow_mod(y, mod - 2));
}

long long ncr_mod (int n, int r) {
	return div_mod(fact[n], mul_mod(fact[n - r], fact[r]));
}

void precalc () {
	fact[0] = 1LL;
	for (long long i = 1; i < maxn; ++i)
		fact[i] = mul_mod(i, fact[i - 1]);
}

signed main () {
	precalc();	
	scanf("%d %d %d %d", &h, &w, &a, &b);
	for (int i = 0; i < h - a; ++i) 
		ans = add_mod(ans, mul_mod(ncr_mod(i + b - 1, b - 1), ncr_mod(w - b - 1 + h - i - 1, w - b - 1)));
	printf("%lld\n", ans);
}


Measured memory_bytes: 5404
// https://atcoder.jp/contests/ddcc2020-qual/tasks/ddcc2020_qual_c
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) FOR(i, 0, n)
#define REPR(i, n) FORR(i, 0, n)
#define FOR(i, s, n) for (int i = (s), i##_len = (n); i < i##_len; ++i)
#define FORR(i, s, n) for (int i = (n - 1); i >= s; --i)
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()
#define DIV(a, b) ((a - 1) / b + 1)
using pi = pair<int, int>;

signed main() {
    int H, W, K;
    cin >> H >> W >> K;
    vector<string> v(H);
    for (auto &x : v) {
        cin >> x;
    }
    int num = 0;
    vector<vector<int>> ans(H, vector<int>(W));
    REP(i, H) {
        REP(j, W) {
            if (v[i][j] == '#') {
                ans[i][j] = ++num;
            }
        }
    }
    FOR(i, 1, H) REP(j, W) {
        if (ans[i][j] == 0) {
            ans[i][j] = ans[i - 1][j];
        }
    }
    REPR(i, H - 1) REP(j, W) {
        if (ans[i][j] == 0) {
            ans[i][j] = ans[i + 1][j];
        }
    }
    REP(i, H) FOR(j, 1, W) {
        if (ans[i][j] == 0) {
            ans[i][j] = ans[i][j - 1];
        }
    }
    REP(i, H) REPR(j, W - 1) {
        if (ans[i][j] == 0) {
            ans[i][j] = ans[i][j + 1];
        }
    }
    REP(i, H) REP(j, W) { cout << ans[i][j] << (j == W - 1 ? "\n" : " "); }

    return 0;
}


Measured memory_bytes: 1664
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc=new Scanner(System.in);
		int x=sc.nextInt();
		if(x<1200)System.out.println("ABC");
		else System.out.println("ARC");
		
		}
		
	}


Measured memory_bytes: 10320
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define rrep(i,n) for (int i = n-1; i >= 0; i--)
#define rep2(i,s,n) for (int i = s; i < n; ++i)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define tmax(a,b,c) max(a,max(b,c))
#define tmin(a,b,c) min(a,min(b,c))
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int,int>;
using LP = pair<ll,ll>;
template<class T> istream& operator>>(istream& is,vector<T>& v) { for(T& t:v){is>>t;} return is; }
template<class T> ostream& operator<<(ostream& os,const vector<T>& v) { for(T t:v){os<<t<<" ";} os<<"\n"; return os; }
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template<class T> inline bool chmin(T& a,T b) {if(a > b){a = b; return true;} return false;}
template<class T> inline bool chmax(T& a,T b) {if(a < b){a = b; return true;} return false;}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    string s;
    cin >> s;
    Yes(s != "AAA" && s != "BBB");
}

Measured memory_bytes: 256
# Setup
import sys
sys.setrecursionlimit(4100000)

h, w = [int(i) for i in input().split()]
a = [[str(i) for i in input()] for _ in range(h)]

def check(row,col):
    if row < 0 or col < 0 or row >= h or col >= w:
        return False
    if a[row][col] == ".":
        return False
    return True

def bfs(row, col):
    if any([check(row-1,col), check(row+1, col), check(row,col-1), check(row,col+1)]):
        return True
    return False
end = 0
for i in range(h):
    if end == 0:
        for k in range(w):
            if end == 0:
                if a[i][k] == "#":
                    if bfs(i,k) == False:
                        end = 1
if end == 0:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 3064
n,m = map(int, input().split())
A = [[]for i in range(n)]
b = [0 for i in range(m)]

for i in range(n):
   A[i] = list(map(int, input().split()))

for i in range(m):
    b[i] = int(input())

for i in range(n):
    tmp = 0
    for j in range(m):
        tmp += A[i][j]*b[j]
    print(tmp)



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

using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define REP(i, l, n) for(int i=(l), i##_len=(n); i<i##_len; ++i)
#define ALL(x) (x).begin(),(x).end()
#define pb push_back

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

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

char alpha[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

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

int main(){
    int h, w; cin >> h >> w;
    int x, y, gx, gy; cin >> x >> y >> gx >> gy;
    x--; y--; gx--; gy--;
    vector<string> lis(h);
    vector<vector<int> > anslis(h, vector<int>(w, 1e9)), flag(h, vector<int>(w, 0));
    anslis[x][y] = 0;
    REP(i, 0, h){cin >> lis[i];}
    queue<Pii> que;
    queue<Pii> subq;
    que.push(Pii(x, y));
    while(!(subq.empty() && que.empty())){
        while(!que.empty()){
            Pii a = que.front();
            que.pop();
            int nx = a.first, ny = a.second;
            int f = 0;
            REP(i, 0, 4){
                int xx = nx + dx[i], yy = ny + dy[i];
                if(xx < h && xx >= 0 && yy >= 0 && yy < w && anslis[xx][yy] > anslis[nx][ny] && lis[xx][yy] == '.'){
                    anslis[xx][yy] = anslis[nx][ny];
                    que.push(Pii(xx, yy));
                }else if(xx < h && xx >= 0 && yy >= 0 && yy < w && lis[xx][yy] == '#' && flag[nx][ny] == 0){
                    f = 1;
                    flag[nx][ny] = 1;
                }
            }
            if(f){
                subq.push(Pii(nx, ny));
            }
        }
        if(anslis[gx][gy] != 1e9){
            cout << anslis[gx][gy] << endl;
            return 0;
        }
        if(!subq.empty()){
            Pii a = subq.front();
            subq.pop();
            int nx = a.first, ny = a.second;
            REP(i, -2, 3){
                REP(l, -2, 3){
                    int xx = nx + i, yy = ny + l;
                    if(xx < h && xx >= 0 && yy >= 0 && yy < w && anslis[xx][yy] > anslis[nx][ny]+1  && lis[xx][yy] == '.'){
                        anslis[xx][yy] = anslis[nx][ny] + 1;
                        que.push(Pii(xx, yy));
                    }
                }
            }
        }
        if(anslis[gx][gy] != 1e9){
            cout << anslis[gx][gy] << endl;
            return 0;
        }
    }
    if(anslis[gx][gy] == 1e9){
        cout << -1 << endl;
        return 0;
    }else{
        cout << anslis[gx][gy] << endl;
    }
}


Measured memory_bytes: 13448
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int MAXN = 100010;
int n,m,sr;
vector<pair<int,ll>> grf[MAXN];
ll dis[MAXN];

void dijkastra(int src)
{
    memset(dis,63,sizeof dis);
    ll inf = dis[0];
    dis[sr]=0;
    // weigth  : node
    set<pair<ll,int>> q;
    q.insert({0,src});
    while(!q.empty()){
        ll len = q.begin()->first;
        int node = q.begin()->second;
        q.erase({len,node});
        for(auto nbr: grf[node]){
            // node : weight
            if( dis[nbr.first]> nbr.second + len ){
                q.erase({dis[nbr.first],nbr.first});
                dis[nbr.first] = nbr.second + len;            
                q.insert({dis[nbr.first],nbr.first});
            }
        }
    }
    for(int i=0; i<n; i++){
        if(dis[i]==inf){
            cout<<"INF"<<"\n";
        }
        else{
            cout<<dis[i]<<"\n";
        }
    }
}
int main() {
    // freopen("./input.txt","r",stdin);
    // freopen("./output.txt","w",stdout);
    ios::sync_with_stdio(false); cin.tie(nullptr);
    cin>>n>>m>>sr;
    for(int i=0; i<m; i++){
        int a,b,c;
        cin>>a>>b>>c;
        grf[a].push_back({b,c});
        // grf[b].push_back({a,c});
    }
    dijkastra(sr);
}


Measured memory_bytes: 20652
#include <bits/stdc++.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; }

/* attention
    long longのシフト演算には気をつけよう
    タイポした時のデバッグが死ぬほどきつくなるので変数名は最低3字くらい使った方がいいかも
*/

ll N, A, B;
ll h[100005];

bool check(int k){
    ll cnt = 0;
    for(int i=0; i<N; i++){
        if(h[i]-k*B <= 0) continue;
        cnt += (h[i]-k*B-1)/(A-B) + 1;
    }

    if(cnt <= k) return true;
    else return false;
}

int solve(){
    ll left = 0, right = 1000000001;

    while(right - left > 1){
        ll mid = (left+right)/2;
        if(check(mid)) right = mid;
        else left = mid;
    }

    return right;
}


int main(){
    cin >> N >> A >> B;
    for(int i=0; i<N; i++) cin >> h[i];

    cout << solve() << endl;
}

Measured memory_bytes: 1024
import sys
sys.setrecursionlimit(20000000)


def dfs(node, distance):
    for next_node, edge_size in G[node]:
        if ans[next_node] == -1:
            ans[next_node] = ((distance+edge_size) % 2)
            dfs(next_node, distance+edge_size)


N = int(input())

G = [[] for i in range(N)]
for i in range(N-1):
    u, v, w = map(int, input().split())
    G[u-1].append([v-1, w])
    G[v-1].append([u-1, w])

ans = [-1]*N
dfs(0, 0)
print(*ans, sep='\n')

Measured memory_bytes: 89740
l=a=0;r=len(s:=input())-1;c='x'
while l<r:
  L,R=s[l],s[r]
  X=(L==R)
  L=(L==c)
  R=(R==c)
  l+=(L+X>0)
  r-=(R+X>0)
  a+=(X<1 and (L+R>0))
  if X+L+R<1:a=-1;break
print(a)

Measured memory_bytes: 9240
#include<bits/stdc++.h>
		using namespace std;
		#define sz(x) (int)(x).size()
		//#define int long long int
		#define loop(i,a,b) for(int i=a;i<b;i++)
		#define scan(arr,n) for (int i = 0; i < n; ++i) cin >> arr[i]
		#define vi vector<int>
		#define si set<int>
		#define pii pair <int, int>
		#define sii set<pii>
		#define vii vector<pii>
		#define mii map <int, int>
		#define pb push_back
		#define all(aa) aa.begin(), aa.end()
		#define faster ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
		//Constants
		const int mod = 1000000007; /* 1e9 + 7*/
		const int MAXN = 1000005; /*1e6 +5 */
		
		int spf[1000009]; // stores the smallest prime factor of a number
		const int siz=200001;
		vi adj[siz];
			
			int colr[siz];
		
		void print(vi v)
		{
			for(auto x:v)
			cout<<x+1<<" ";
			cout<<endl;
		}
		 const int maxx=2E5+1;
		
	
		void dfs(int curr, int& cnt, int*  
             visited, vector<int>& duringdfs) 
{ 
    visited[curr] = 1; 
  
    // Number of nodes in this component 
    ++cnt; 
  
    // Stores the nodes which belong  
    // to current component 
    duringdfs.push_back(curr); 
    for (auto& child : adj[curr]) { 
  
        // If the child is not visited 
        if (visited[child] == 0) { 
            dfs(child, cnt, visited, duringdfs); 
        } 
    } 
} 
  
// Function to return the desired  
// count for every node in the graph 
void MaximumVisit(int n, int k) 
{ 
    // To keep track of nodes we visit 
    int visited[maxx]; 
  
    // Mark every node unvisited 
    memset(visited, 0, sizeof visited); 
    int ans[maxx]={0}; 
  
    // No of connected elements for each node 
    memset(ans, 0, sizeof ans); 
    vector<int> duringdfs;  
    for (int i = 1; i <= n; ++i) { 
        duringdfs.clear(); 
        int cnt = 0; 
  
        // If a node is not visited, perform a DFS as  
        // this node belongs to a different component 
        // which is not yet visited 
        if (visited[i] == 0) { 
            cnt = 0; 
            dfs(i, cnt, visited, duringdfs); 
        } 
  
        // Now store the count of all the visited 
        // nodes for any particular component. 
        for (auto& x : duringdfs) { 
            ans[x] = cnt; 
        } 
    } 
     
    // Print the result 
    sort(ans,ans+maxx,greater<int>());
    cout<<ans[0]<<endl;
    //return; 
} 
		
		 
		 
		 
		
		
		void solve()
		{
			int n,m;
			cin>>n>>m;
			sii s;
			s.clear();
			int cc=0;
			loop(i,0,m)
			{
				int x,y;
				cin>>x>>y;
				x--;y--;
				if(s.find({x,y})==s.end() && s.find({y,x})==s.end())
				{
					adj[x].pb(y);
					adj[y].pb(x);
					s.insert({x,y});s.insert({y,x});
					cc++;
				}
			}
			//cout<<cc<<endl;
			 MaximumVisit(n, cc);
			
		//	cout<<last;
					
			
		}

		 
		int32_t main()
		{
			faster;int t=1;//cin>>t;
			while(t--)
				solve();		 
			return 0;
		}


Measured memory_bytes: 47808
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
    int n;
    cin>>n;
    int a[n];
    rep(i,n)cin>>a[i];
    sort(a,a+n);
    cout<<a[n-1]-a[0]<<endl;    
    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll          long long int
#define lld          long double
#define pb          push_back
#define pii         pair<ll,ll>
#define mi          map<ll,ll>
#define vec         vector<ll>
#define all(a)     (a).begin(),(a).end()
#define F           first
#define S           second
#define mod         1000000007
#define nmod         -1000000007
#define N            1000000//31630=sqrt(pow(10,9))
#define rep(i,a,b)	for(ll  i=a;i<b;i++)
#define repi(i,a,b)	for(ll  i=a;i<=b;i++)
#define per(i,a,b)  for(ll i=a;i>=b;i--)
#define mp          make_pair
#define mit         map<ll,ll>::iterator
#define sit         set<ll>::iterator
#define pit         pair<ll,ll>::iterator
#define tr(container, it) for(__typeof(container.begin()) it = container.begin(); it != container.end(); it++)
using namespace std;
ll xo(ll x, ll y) 
{ 
   return (x | y) & (~x | ~y); 
}  
ll bin_Expo(ll x,ll n)
{
		if(x==0)
		return 0;
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return bin_Expo(x*x,n/2);
    else                             //n is odd
        return x*bin_Expo(x*x,(n-1)/2);
}
ll mod_Expo(ll x,ll n,ll M)
{
		if(x==0)
		return 0;
    if(n==0)
        return 1;
    else if(n%2 == 0)        //n is even
        return mod_Expo((x*x)%M,n/2,M);
    else                             //n is odd
        return (x*mod_Expo((x*x)%M,(n-1)/2,M))%M;
 
}
//bool pf[N+1ll];
//vec prim;
//void sieve()
//{
//    repi(i,0,N)
//    pf[i]=true;
//	pf[0] = pf[1] = false;
//    for (int i = 2; i <= N; i++) {
//        if (pf[i]) 
//		{
//            prim.push_back(i);
//            for (int j = i + i; j <= N; j+=i) {
//                pf[j] = false;
//            }
//        }
//    }
//}
bool prime_check(ll x)
{
    bool prime = (x >= 2);
    for (ll i = 2; i * i <= x; i++) 
    {
        if (x % i == 0) 
        {
            prime = false;
            break;
        }
    }
    return prime;
}
ll logg(ll base,ll x) 
{
    return (ll)(log(x) / log(base));
}
ll a[200005];
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int TESTS=1;
//	cin>>TESTS; 
    while(TESTS--)
    {
    	ll m,i,l,h,j,mx,my,ans,d,n,r,g,b,e,f,k;
    	cin>>n>>h;
    	repi(i,0,n)
    	a[i]=i;
    	repi(i,1,n)
		a[i]+=a[i-1];
		ans=0;
		repi(k,h,n+1)
		{
			l=a[k-1];
			r=a[n]-a[n-k];
			ans=(ans+r-l+1)%mod;
		}
		cout<<ans;
	}
	return 0;
} 

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

//マクロ
//forループ関係
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i,n) for(lol i=0;i<lol(n);i++)
#define REPD(i,n) for(lol i=n-1;i>=0;i--)
#define FOR(i,a,b) for(lol i=a;i<=lol(b);i++)
#define FORD(i,a,b) for(lol i=a;i>=lol(b);i--)
//xにはvectorなどのコンテナ
#define ALL(x) x.begin(),x.end() //sortなどの引数を省略したい
#define SIZE(x) lol(x.size()) //sizeをsize_tからllに直しておく
//定数
#define PI 3.1415926535897932385 //pi
#define INF 1000000000000 //10^12:極めて大きい値,∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange(素数列挙などで使用)
//最大値最小値
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
int gcd(int a,int b){return b?gcd(b,a%b):a;}

int main()
{
    lol result{0};
    lol x, y, an, bn, cn; cin >> x >> y >> an >> bn >> cn;
    vector<lol> a(an+2, INF), b(bn+2, INF), c(cn+2, INF);
    FOR(i, 1, an)
    {
        cin >> a[i];
    }
    FOR(i, 1, bn)
    {
        cin >> b[i];
    }
    FOR(i, 1, cn)
    {
        cin >> c[i];
    }
    a[0] = -1; b[0] = -1; c[0] = -1;
    sort(ALL(a));
    sort(ALL(b));
    sort(ALL(c));
    lol ci = cn, bi = bn + 1 - y, ai = an + 1 - x;
    while(ci != 0)
    {
        if(a[ai] < b[bi] && a[ai] < c[ci])
        {
            a[ai] = c[ci];
            ai++; ci--;
            //cout << c[ci] << endl;
        }
        else if(a[ai] >= b[bi] && b[bi] < c[ci])
        {
            b[bi] = c[ci];
            bi++; ci--;
            //cout << c[ci] << endl;
        }
        else
        {
            break;
        }
        
    }
    FOR(i, bn + 1 - y, bn)
    {
        result += b[i];
        //cout << i << ":" << b[i] << " ";
    }
    FOR(i, an + 1 - x, an)
    {
        result += a[i];
    }

    cout << result << endl;
    return 0;
}

Measured memory_bytes: 5640
#include <stdio.h>
long long int lcm(long long int n1,long long int n2)
{
    long long int c1=n1;
    long long int c2=n2;
    long long int t;
    while(n2!=0)
    {
        t=n2;
        n2=n1%n2;
        n1=t;
    }
         return (c1*c2)/n1;
}
int main()
{
    long long int a,b,c,d,count1,count2,s1,s2,sum,r1,r;
    scanf("%lld %lld %lld %lld", &a,&b,&c,&d);
    s1=(b/c)-(a-1)/c;
    s2=(b/d)-(a-1)/d;
    sum=s1+s2;
    r=lcm(c,d);
     if(r>0)
    {
         r1=(b/r)-(a-1)/r;
    }
    printf("%lld\n", b-a-sum+r1+1);
    return 0;
}


Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define x first
#define y second
#define ll long long
#define dl double
#define pii pair<int,int>
#define pb push_back
#define all() begin(),end()
#define nl '\n'
#define adm(a,b,m) ((a+b)%m)
#define gum(a,b,m) ((a*1LL*b)%m)
#define lo(i,j,n) for(int i=j;i<n;i++)


int main(){
	int n;
	cin>>n;
	vector<int>v(n);
	for(auto &x:v)cin>>x;
	sort(v.begin(),v.end());
	cout<<v[n-1]-v[0]<<nl;
}

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

int main() {
    int n,m;
    cin >> n >> m;
    vector<P> p(m);
    rep(i,m) cin >> p[i].first >> p[i].second;
    rep(x,1000){
        int keta = 1;
        int nx = x/10;
        vector<int> d(1,x%10); //1の位
        while(nx){
            keta++;
            d.push_back(nx%10);
            nx /= 10;
        }
        if(keta != n) continue;
        bool ok = true;
        reverse(d.begin(),d.end());
        rep(i,m){
            if(d[p[i].first-1] != p[i].second) ok = false;
        }
        if(ok){
            cout << x << endl;
            return 0;
        }
    }
    cout << -1 << endl;
    return 0;
}

Measured memory_bytes: 3588
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        int t = sc.nextInt();
        int a = sc.nextInt();
        sc.nextLine();
        int[] h = new int[n];
        double[] cal = new double[n];
        double[] res = new double[n];
        for (int i = 0; i < n; i++){
            h[i] = sc.nextInt();
            cal[i] = t - h[i] * 0.006;
            res[i] = Math.abs(a-cal[i]);
        }
        double min = 999999;
        int ansewer = -1;
        for (int i = 0; i < n; i++){
            if (min > res[i]){
                min = res[i];
                ansewer = i + 1;
            }
        }
        System.out.println(ansewer);
    }
}


Measured memory_bytes: 24020
import java.util.Scanner;
import java.util.Stack;


//4-2 スタック
public class Main {

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

		String[] e = poland.split(" ");
		Stack<Integer> stack = new Stack<>();

		for(int i = 0; i < e.length; i++) {
			if(e[i].equals("+")) {
				//1番目に取り出した数と2番目に取り出した数を足す
				stack.push(stack.pop() + stack.pop());
			}else if(e[i].equals("-")) {
				//2番目に取り出した数から1番目に取り出したを引く
				stack.push(-stack.pop() + stack.pop());
			}else if(e[i].equals("*")) {
				//1番目に取り出した数と2番目に取り出した数を掛ける
				stack.push(stack.pop() * stack.pop());
			}else {
				//数字の場合は、スタックに数字を突っ込む
				stack.push(Integer.parseInt(e[i]));
			}
		}

		System.out.println(stack.pop());

		sc.close();
	}
}


Measured memory_bytes: 26172
N,K=map(int,input().split())
d={}
for i in range(N):
  a,b=map(int,input().split())
  if a not in d:
    d[a]=b
  else:
    d[a]+=b
d=sorted(d.items())
for i in d:
  K-=i[1]
  if K<=0:
    print(i[0])
    break

Measured memory_bytes: 20160
#include<bits/stdc++.h>
using namespace std;
#define fastio() ios::sync_with_stdio(0),cin.tie(0);

int main()
{
	string s;
	cin >> s;
	int ck = 0,flag;
	flag = (s[0] == 'A');
	for (int i = 1; i < s.size(); i++)
	{
		if (i > 1 && i < s.size() - 1 && s[i] == 'C')ck++;
		else if (s[i] < 'a')
		{
			flag = false;
			break;
		}
	}
	puts(flag&&ck==1? "AC" : "WA");
	return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vvi = vector<vi>;
using vvl = vector<vl>;
const int INF = 1 << 28;
const ll MOD = 1000000007;
template <class T>
bool chmax(T &a, const T &b) {
    return (a < b) ? (a = b, 1) : 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    return (b < a) ? (a = b, 1) : 0;
}

template <typename T = int>
void primeFactors(T a, map<T, int> &facs) {
    // double sqrtA = sqrt(a);
    int i = 2;
    // for (int i = 2; i <= sqrtA + 1e-10; ++i) {
    while (a % i == 0) {
        facs[i]++;
        a /= i;
    }
    //}
    // if (a > sqrtA)
    //     facs[a]++;
    return;
}

int main() {
    int n;
    cin >> n;
    vl a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    map<ll, int> twos;
    for (int i = 0; i < n; ++i) {
        primeFactors(a[i], twos);
    }
    cout << twos[2] << "\n";
    return 0;
}

Measured memory_bytes: 384
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10 ** 7)


def solve(N: int, A: "List[int]"):
    m = 1e10
    s = 0
    n_plus, n_minus = 0, 0
    for x in A:
        m = min(m, abs(x))
        if x >= 0:
            n_plus += 1
        else:
            n_minus += 1
        s += abs(x)
    # print(n_plus, n_minus)
    if n_minus % 2 == 0:
        print(s)
    else:
        print(s - m * 2)

    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools
# (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)


if __name__ == '__main__':
    main()


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

using ll = long long;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define pb push_back
#define mp make_pair
#define fill(x, y) memset(x, y, sizeof(x))
#define even(x) x % 2 == 0
#define odd(x) x % 2 != 0
#define all(x) x.begin(), x.end()
#define pcnt __builtin_popcount
#define buli(x) __builtin_popcountll(x)
#define F first
#define S second
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

// these functions return the position of result of Binary Search.
#define LB(s, t, x) (int) (lower_bound(s, t, x) - s)
#define UB(s, t, x) (int) (upper_bound(s, t, x) - s)

ll qp(ll a, ll b, int mo) { ll ans = 1; do { if (b & 1) ans = 1ll * ans * a % mo; a = 1ll * a * a % mo; } while (b >>= 1); return ans; }
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
int dx8[] = { 1, -1, 0, 0, 1, 1, -1, -1 }, dy8[] = { 0, 0, -1, 1, -1, 1, -1, 1 };

template <typename T>
vector<T> make_v(size_t a) { return vector<T>(a); }
template <typename T, typename... Ts>
auto make_v(size_t a, size_t b, Ts... ts) { return vector<decltype(make_v<T>(b, ts...))>(a, make_v<T>(b, ts...)); }
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type
fill_v(T &t, const V &v) { t = v; }
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type
fill_v(T &t, const V &v) { for (auto &e : t) fill_v(e, v); }
// auto dp = make_v<int>(4, h, w);
// fill_v(dp, 0);

const ll INF_LL = (1ll << 60);
const int INF_INT = (int)1e9;
const ll MOD_CONST = (ll)(1e9 + 7);

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

inline tuple<ll, ll> rotate45(tuple<ll, ll> point) { ll x = get<0>(point), y = get<1>(point); return tuple<ll, ll>(x + y, x - y); }
inline bool rangeCheck2D(int nx, int ny, int Width, int Height) { return nx >= 0 and nx < Width and ny >= 0 and ny < Height; }
void out_sequence(vector<ll> &out) { REP(i, out.size()) { cout << out[i]; if (i == out.size() - 1) cout << endl; else cout << " "; } }

int main(void)
{
	cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(20);

	ll N, C; cin >> N >> C;
	vector<pair<ll, ll>> v(N);
	REP(i, N) {
		ll p; ll vv; cin >> p >> vv;
		v[i] = mp(p, vv);
	}
	vector<ll> rs(N+1), ls(N+1);
	vector<ll> rcs(N+1), lcs(N+1);
	vector<ll> rcs2(N+1), lcs2(N+1);
	rs[0] = 0, ls[0] = 0;
	rcs[0] = 0, lcs[0] = 0;
	rcs2[0] = 0, lcs2[0] = 0;
	REP(i, N) {
		rs[i + 1] = rs[i] + v[i].second;
		ls[i + 1] = ls[i] + v[N - 1 - i].second;
		rcs[i + 1] = max(rcs[i], rs[i + 1] - v[i].first);
		lcs[i + 1] = max(lcs[i], ls[i + 1] - (C - v[N - 1 - i].first));
		rcs2[i + 1] = max(rcs2[i], rs[i + 1] - 2 * v[i].first);
		lcs2[i + 1] = max(lcs2[i], ls[i + 1] - 2 * (C - v[N - 1 - i].first));
	}
	ll ans = 0;
	REP(i, N + 1) {
		chmax(ans, rcs[i] + lcs2[N - i]);
		chmax(ans, lcs[i] + rcs2[N - i]);
	}
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 6528
#include<bits/stdc++.h>
using namespace std;
char a[100][100];
int main()
{
    int m,n;
    cin>>m>>n;
    memset(a,48,sizeof(a));
    for(int i=1;i<=m;++i)
      for(int j=1;j<=n;++j)
      {
      	char t;
      	cin>>t;
      	if(t=='#')a[i][j]='#';
      }
    for(int i=1;i<=m;++i)
      for(int j=1;j<=n;++j)
      {
      	if(a[i][j]==48){
      		for(int k=i-1;k<=i+1;++k)
      		  for(int l=j-1;l<=j+1;++l)
      		      if(a[k][l]=='#')a[i][j]++;
        }}
    for(int i=1;i<=m;++i)
    {
        for(int j=1;j<=n;++j)
            cout<<a[i][j];
        cout<<endl;
    } 
}

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

using namespace std;
int main() {
    int n,m;

    cin>>n>>m;
    vector<int> s;
    vector<vector<int>> path(n+1);
    
    for(;m;m--){
        int a,b;
        cin>>a>>b;
        path[a].push_back(b);
        path[b].push_back(a);
    }
    
    queue<int> q;
    q.push(1);
    
    vector<int> ans(n+1);
    set<int> visited;
    visited.insert(1);
    
    while(!q.empty()){
        int e = q.front();
        q.pop();
        
        for(int i:path[e]){
            if(!visited.count(i)){
                ans[i] = e;
                q.push(i);
                visited.insert(i);
            }
        }
    }
    
    if(visited.size()<n)
        cout<<"No"<<endl;
    else{
        cout<<"Yes"<<endl;
        for(int i =2; i<=n; i++)
            cout<<ans[i]<<endl;
    }
    
    
    
    return 0;
}


Measured memory_bytes: 14952
#include <stdio.h>
 
int main(){
    int N;
    scanf("%d", &N);
 
    for(int i=1; i<=9; i++){
        if( N/i < 10 && N%i == 0){
        puts("Yes");
        return 0;
        }
    }
    puts("No");
    return 0;
}

Measured memory_bytes: 3116
#include <bits/stdc++.h>
#include <math.h>
#define _USE_MATH_DEFINES
using namespace std;

typedef long long ll;

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

    int n;
    cin >> n;

    vector<int> count(n);
    for (int i = 2; i <= n; i++) {
        int boss;
        cin >> boss;
        count[boss-1]++;
    }

    for (auto b: count) {
        cout << b << "\n";
    }
}


Measured memory_bytes: 3972
//FIRST THINK THEN CODE.

#include <bits/stdc++.h>
 
 
using namespace std;
 
typedef long long ll;
 
#define rep(i,a,b) for(ll i=a;i<b;++i)
#define rrep(i,a,b) for(ll i=a;i>b;--i)
#define FOR(i,n)  for(ll i=0;i<n;i++)
#define vi vector<int>
#define vl vector<ll>
#define ld long double
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define pii pair<int,int>
#define pll pair<long,long>
#define vpii vector<pii>
#define vpll vector<pll>
#define ff first
#define ss second
#define pb push_back
#define pf push_front
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define bs binary_search
#define d1(x) cout<<(x)<<endl
#define d2(x,y) cout<<(x)<<" "<<(y)<<endl
#define d3(x,y,z) cout<<(x)<<" "<<(y)<<" "<<(z)<<endl
#define d4(a,b,c,d) cout<<(a)<<" "<<(b)<<" "<<(c)<<" "<<(d)<<endl
#define PI 3.1415926535897932384626433832795
#define fix(f,n) fixed<<setprecision(n)<<f
#define all(x) x.begin(),x.end()
#define rev(p) reverse(p.begin(),p.end());
#define endl "\n"
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define popcount(x) __builtin_popcountll(x)
#define sz(x) ((ll)x.size())
const ll M=1000000007;
const ll MM=998244353;
ll begtime = clock();
#define end_routine() cout << "\n\nTime elapsed: " << (clock() - begtime)*1000/CLOCKS_PER_SEC << " ms\n\n";

 
ll newmod(ll a,ll b)
 {
  return ((a%b)+b)%b;
}
 
 
ll powM(ll a,ll b,ll m )
{ 
  if(a<=0)return 0;
  a%=m;
  if(!b)return 1;
  ll ans=1LL;
  while(b)
  {
    if(b&1)ans=ans*a%m;
    a=a*a%m;
    b>>=1;
  }
 
return ans;
}


 
ll poww(ll a,ll b)
{ 
  
  ll ans=1;
  while(b)
  {
    if(b&1)ans=ans*a;
    a=a*a;
    b>>=1;
  }
 
return ans;

}

template<typename T,typename F>
void chmax( T &a,F b){
  if(b>a)a=b;
}

template<typename T,typename F>
void chmin( T &a,F b){
  if(b<a)a=b;
}


const ll N=2e5+5;



int main()
{ 

IOS;

ll n,m;
cin>>n>>m;

vl a(n+1);

FOR(i,n)cin>>a[i+1];

sort(all(a));
rev(a);

vpll b(m+1);

for(int i=1;i<=m;i++){
  cin>>b[i].ss>>b[i].ff;
}

sort(all(b));
rev(b);

vl pra(n+1);



for(int i=1;i<=n;i++){
  pra[i]=pra[i-1]+a[i-1];
}

//FOR(i,n)cout<<pra[i+1]<<" ";

//cout<<endl;

vl v;

for(int i=0;i<m;i++){
  for(int j=1;j<=b[i].ss;j++){
    v.pb(b[i].ff);
    if(sz(v)>=n)break;
  }
}

while(sz(v)<=n)v.pb(0);

vl prb(n+1);

//for(int i=0;i<n;i++)d1(v[i]);

for(int i=1;i<=n;i++)
{
  prb[i]=prb[i-1]+v[i-1];
}

//FOR(i,n)cout<<prb[i+1]<<" ";

//cout<<endl;

ll ans=0;

for(int i=0;i<=n;i++){
ans=max(ans,pra[i]+prb[n-i]);
}

d1(ans);


//end_routine();

return 0;
 
 }


Measured memory_bytes: 8700
#include<bits/stdc++.h>
using namespace std;
int n,x,y,z,md=1e9+7;
long long t,dp[45][1<<17];
long long sol(long long a,long long b)
{
	if(dp[a][b]!=-1)return dp[a][b];
	if((b>>(x+y+z-1))&1&&(b>>(y+z-1))&1&&(b>>(z-1))&1)return 0;
	if(a==n)return 1;
	long long ans=0;
	for(int i=1;i<=10;i++)ans=(ans+sol(a+1,((b<<i)|(1<<(i-1)))&t))%md;
	return dp[a][b]=ans;
}
int main()
{
	scanf("%d%d%d%d",&n,&x,&y,&z);
	t=(1<<(x+y+z))-1;
	long long ans=1;
	for(int i=1;i<=n;i++)ans=ans*10%md;
	memset(dp,-1,sizeof(dp));
	ans-=sol(0,0);
	ans=(ans+md)%md;
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 46336
#include<iostream>
#include<map>
#include<iomanip>
using namespace std;

int main() {
	unsigned int N, M;
	cin >> N >> M;
	int P[M], Y[M];
	map<unsigned int, unsigned int>year;//Key=year,value=number

	for (unsigned int i = 0;i < M;i++) {
		cin >> P[i] >> Y[i];
		year.insert(make_pair(Y[i],i));
	}
	int prefac[N] = {}, id[M];
	
		for (auto it=year.begin();it!=year.end();it++) {
			prefac[P[it->second]-1]++;
			id[it->second] = prefac[P[it->second]-1];
		}
	

	
	for (int i = 0;i < M;i++) {
	cout.fill('0');
	cout.width(6);
	cout << P[i];
	cout.fill('0');
	cout.width(6);
	cout << id[i]<<endl;
	}
}

Measured memory_bytes: 7808
#include <stdio.h>
#include <vector>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<vs> vvs;


typedef vector<pair<int, int>> vpi;
typedef vector<tuple<int, int, int>> vti;


int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int n, i;
    cin >> n;
    vi a(n);
    for (i=0;i<n;i++) cin >> a[i];

    vl ans(n);
    ans[0] = a[0];
    for (i=1;i<n;i++){
        if (i % 2 == 0) ans[0] += a[i];
        else ans[0] -= a[i];
    }
    
    ans[0] /= 2;
    for (i=1;i<n;i++){
        ans[i] = a[i-1] - ans[i-1];
    }
    for (ll a: ans)
        cout << a * 2 << endl;
}


Measured memory_bytes: 2560
def d_islands_war(N, M, Demands):
    demands = sorted(Demands, key=lambda x: x[1])
    ans = 1
    _, previous_cut = demands.pop(0)  # previous_cut番目のすぐ西の橋を切る
    for a, b in demands:
        if a >= previous_cut:
            # これが成り立つとき、まだこの要望を満たしていない
            ans += 1
            previous_cut = b
    return ans

N, M = [int(i) for i in input().split()]
Demands = [[int(i) for i in input().split()] for j in range(M)]
print(d_islands_war(N, M, Demands))

Measured memory_bytes: 24128