akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

int a[100005] , b[100005] , c[100005];
int dp[100005][4];


main()
{
	int n;
	cin >> n;
	
	for(int i = 1; i <= n; i++)
	{
		scanf("%d%d%d" , &a[i] , &b[i] , &c[i]);
	}
	
	dp[1][1] = a[1];
	dp[1][2] = b[1];
	dp[1][3] = c[1];
	
	for(int i = 2; i <= n; i++)
	{
		dp[i][1] = max( dp[i-1][2] , dp[i-1][3] ) + a[i];
		dp[i][2] = max( dp[i-1][1] , dp[i-1][3] ) + b[i];
		dp[i][3] = max( dp[i-1][1] , dp[i-1][2] ) + c[i];
	}
	
	
	
	
	
	
	
	cout << max(dp[n][1] , max(dp[n][2] , dp[n][3])) << endl;
	
	
}


Measured memory_bytes: 2944
#include <iostream>
#include <map>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
#pragma warning (disable: 4996)

string S;

int main() {
	cin >> S; int dep = 0, mindep = 0;
	for (int i = 0; i < S.size(); i++) {
		if (S[i] == 'S') dep++;
		else dep--;
		mindep = min(mindep, dep);
	}
	cout << -mindep + (dep - mindep) << endl;
	return 0;
}

Measured memory_bytes: 3676
#include<bits/stdc++.h>
#define INF 9223372036854775807LL
#define inf 1000000007
#define SYOU(x) setprecision(x+1)
#define abs(x,y) (max(x,y)-min(x,y))
#define lol long long
#define mp make_pair
#define fi first
#define se second
using namespace std;
lol a,b,c,d;
int i[100][100];
bool vi[100][100];
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s[100],w;
  cin >>a>>b;
  for(int n=0;n<a;n++) cin >>s[n];
  lol ans=0;
  queue<pair<int,int> > q;
  for(int n=0;n<a;n++){
    for(int m=0;m<b;m++){
      memset(vi,false,sizeof(vi));
      memset(i,inf,sizeof(i));
      if(s[n][m]!='#') q.push(mp(n,m)),i[n][m]=0;
      while(!q.empty()){
	int x=q.front().fi;
	int y=q.front().se;
	vi[x][y]=true;
	q.pop();
	if(ans<i[x][y]) ans=i[x][y];
	if(x+1<a&&s[x+1][y]!='#'&&!vi[x+1][y]&&i[x+1][y]>i[x][y]+1) q.push(mp(x+1,y)),i[x+1][y]=i[x][y]+1;
	if(y+1<b&&s[x][y+1]!='#'&&!vi[x][y+1]&&i[x][y+1]>i[x][y]+1) q.push(mp(x,y+1)),i[x][y+1]=i[x][y]+1;
	if(x-1>=0&&s[x-1][y]!='#'&&!vi[x-1][y]&&i[x-1][y]>i[x][y]+1) q.push(mp(x-1,y)),i[x-1][y]=i[x][y]+1;
	if(y-1>=0&&s[x][y-1]!='#'&&!vi[x][y-1]&&i[x][y-1]>i[x][y]+1) q.push(mp(x,y-1)),i[x][y-1]=i[x][y]+1;
      }
    }
  }
  cout <<ans<<'\n';
  return (0);
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
using namespace std;
int main(){
    int n;
    cin>>n;
    int ans;
    if(n==1)ans=1;
    else if(n<4)ans=2;
    else if(n<8)ans=4;
    else if(n<16)ans=8;
    else if(n<32)ans=16;
    else if(n<64)ans=32;
    else ans=64;
    cout<<ans<<endl;
}

Measured memory_bytes: 256
a = int(input())
s = []
ret = ['z', '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',
       ]
while a > 0:
    a -= 1
    shang = int(a / 26)
    yushu = int(a % 26)
    # print(shang, yushu)
    s.append(yushu+1)
    a = shang

s.reverse()
# print(s)
v = ''
for i in range(len(s)):
    v += ret[s[i]]
print(v)


Measured memory_bytes: 9200
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
using namespace std;
typedef long long ll;
ll N,M,L;
ll S[111111];
ll K[111111];
ll rw[111111];
ll dp[2222][2222];
ll solve(int r,int l){
  ll ret;
  int no=max(r,l);
  if(dp[r][l]!=-1) return dp[r][l];
  if(r==N-1){
    return dp[r][l]=(rw[K[r]]-rw[K[l]-1])/L;
  } else if(l==N-1){
    return dp[r][l]=(rw[K[l]]-rw[K[r]-1])/L;
  }
  ret=min(solve(r,no+1)+(rw[K[no+1]]-rw[K[l]-1])/L,
	  solve(no+1,l)+(rw[K[no+1]]-rw[K[r]-1])/L);
  //printf("%lld\n",ret);
  return dp[r][l]=ret;
}
main(){
  scanf("%lld %lld %lld",&N,&M,&L);
  for(int i=0;i<N;i++){
    scanf("%lld",&K[i]);
  }
  for(int i=0;i<M;i++){
    scanf("%lld",&S[i]);
  }
  memset(dp,-1,sizeof(dp));
  for(int i=0;i<M;i++)
    rw[i+1]=rw[i]+S[i];
  sort(K,K+N);
  printf("%lld\n",solve(0,0));
}

Measured memory_bytes: 42424
#include <iostream>

int main()
{
	int ipt = 0;
	while(1)
	{
		std::cin >> ipt;
		if (ipt == 0)
			return 0;
		
		int cnt = 0;
		for (int i = 1; i < ipt; i++)
		{
			int buf = i;
			for (int j = i + 1; j < ipt; j++)
			{
				buf += j;
				if (buf == ipt)
				{
					cnt++;
				}
				if (buf >= ipt)
				{
					break;
				}
			}
		}
		std::cout << cnt << '\n';
	}

	return 0;
}


Measured memory_bytes: 3020
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define rush() int T;scanf("%d",&T);while(T--)

typedef long long ll;
const int maxn = 505;
const ll mod = 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;

int pre[maxn];
int a[maxn],b[maxn];

int find(int x)
{
    int t,r=x;
    while(pre[x]!=x)
    {
        x=pre[x];
    }
    while(r!=x)
    {
        t=pre[r];
        pre[r]=x;
        r=t;
    }
    return x;
}

void join(int a,int b)
{
    int A=find(a);
    int B=find(b);
    if(A!=B)
    {
        pre[A]=B;
    }
}

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=0;i<m;i++)
    {
        scanf("%d%d",&a[i],&b[i]);
    }
    int ans=0;
    for(int i=0;i<m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            pre[j]=j;
        }
        for(int j=0;j<m;j++)
        {
            if(j!=i) join(a[j],b[j]);
        }
        int flag=0;
        for(int j=2;j<=n;j++)
        {
            if(find(j)!=find(j-1))
            {
                flag=1;
                break;
            }
        }
        if(flag) ans++;
    }
    printf("%d\n",ans);
}


Measured memory_bytes: 128
#include<iostream>
#include<cstdio>
#include<vector>
#include <algorithm>
#include <functional>
#include<cmath>

using namespace std;

int main(){
	int n;
	cin>>n;
	for( int i=0; i<n; i++)
	{
		int a[3];
		cin>>a[0]>>a[1]>>a[2];
		sort(a,a+3);
		
		if( pow(a[0],2)+pow(a[1],2) == pow(a[2],2) ) puts("YES");
		else puts("NO");
	}

	return 0;
}

Measured memory_bytes: 1128
/*
 KUNAL RAUT (ZUKONIT14)  :- "DO WHAT YOU LIKE!"
 -PICT,PUNE! :)
 */
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define mod 1000000007
#define mod9 1000000009
#define pi 3.1415926535
#define MAXN 1000001
#define N 1000001
#define MAX2N 1*1000 + 10
#define all(v) v.begin(), v.end()
#define ms(s, n) memset(s, n, sizeof(s))
#define prec(n) fixed<<setprecision(n)
#define forci(p,n) for(ll i=p;i<(ll)n;i++)
#define forcj(p,n) for(ll j=p;j<(ll)n;j++)
#define bolt ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define bits(a) __builtin_popcount(a)
#define test ll t;cin>>t;for(ll testi=1;testi<=t;testi++)
ll zero=0;
ll one=1;
ll gcd(ll a,ll b){    if(b==0) return a;    return gcd(b,a%b);}
ll lcm(ll a,ll b) {    return (a/gcd(a,b)*b);}
ll expo(ll x, ll y) {ll res=1;x=x%mod;while(y>0){if(y&1)res=(1ll*res*x)%mod;
    y=y>>1;x=(1ll*x*x)%mod;} return res;}
ll ncr(ll n,ll r){ ll res=1; if(r>n-r)r=n-r; for(ll i=0;i<r;i++) {  res*=n-i;  res/=i+1; } return res;}
ll max(ll a,ll b){return (a>b)?a:b;}
bool prime(ll n){ll i;for(i=2;i<=sqrt(n);i++){if(n%i==0)return false;}return true;}
bool sortbysec(const pair<ll,ll> &a,const pair<ll,ll> &b)
{   if(a.second == b.second)
    return a.first < b.first;
    return (a.second < b.second);}
//https://www.youtube.com/watch?v=WTJSt4wP2ME

ll rr[]={0,1,1,1,0,-1,-1,-1,};
ll cc[]={1,1,0,-1,-1,-1,0,1};
#define MOD 998244353

signed main()
{
    bolt;
    string s,t;cin>>s>>t;
    cout<<t+s;
}


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

using namespace std;
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {

    ll n; cin >> n;
    string ans = "";

    while (n > 0) {
        n--;
        ans += 'a' + n % 26;
        n /= 26;
    }

    reverse(ans.begin(), ans.end());

    cout << ans << endl;
}

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

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef pair<int, int> pii;

#define abs(a) ((a) < (0)? -(a):(a))
#define rep(i, n) REP(i, 0, (n))
#define REP(i, a, n) for(int i=(a); i<(n); i++)
#define pb push_back

int main() {
    string g;
    int y, m, d;
    
    while(cin>>g>>y>>m>>d, g != "#") {
        if(y > 31 || (y == 31 && m >=5)) {
            g = "?";
            y -= 30;
        }
        cout << g << " " << y << " " << m << " " << d << endl;
    }
}


Measured memory_bytes: 3180
#include<stdio.h>
int main(){
	int n,m,i,a,b,ans;
	scanf("%d %d",&n,&m);
	int h[n];
	int count[n];
	for(i=1;i<=n;i++){
	scanf("%d",&h[i]);
	count[i]=1;
	}
	for(i=1;i<=m;i++){
		scanf("%d %d",&a,&b);
		if(h[a]>=h[b]){
			count[b]=0;
		}
		if(h[a]<=h[b])
		count[a]=0;
	}
	for(i=1,ans=0;i<=n;i++){
		if(count[i]==1)
		ans++;
	}
	printf("%d",ans);
}


Measured memory_bytes: 2484
#include<stdio.h>
#define N 100005

typedef struct
{
  int parent;
  int left;
  int right;
}rooted;

int n,dep[N];
rooted A[N];

void Print(int *);
void depth();

int main()
{
 int j,i,num,lim=0,c,left,right,k[N];
 scanf("%d",&n);            //節点の個数
 for(i=0;i<n;i++)     //初期化　
 {
  dep[i]=0;
  A[i].parent = A[i].left = A[i].right = -1;
 }

 for(j=0;j<n;j++)
 {
  scanf("%d",&num);        //node番号
  scanf("%d",&k[num]);          //子の数
  for(i=0;i<k[num];i++)
  {
   scanf("%d",&c);              //子の番号
   if(i == 0)A[num].left = c;
   else A[left].right=c;
   left = c;
   A[c].parent = num;
  }

 }
 depth();
 Print(k);

 return 0;
}


void Print(int *k)
{
  int j,i,c,s=0;
  for(j=0;j<n;j++)             //print
  {
    if(A[j].parent == -1)printf("node %d: parent = %d, depth = %d, root, [",j,A[j].parent,dep[j]);
    else if(k[j] != 0) printf("node %d: parent = %d, depth = %d, internal node, [",j,A[j].parent,dep[j]);
    else printf("node %d: parent = %d, depth = %d, leaf, [",j,A[j].parent,dep[j]);

    c = A[j].left;
    s=0;
    while(c!=-1)
    {
      if(s == 0)printf("%d",c);
      else printf(", %d",c);
      c = A[c].right;
      s++;
    }

    printf("]\n");
  }
}


void depth()
{
  int i,j;
  for(i=0;i<n;i++)
  {
    j=i;
    while(A[j].parent != -1)
    {
      dep[i]++;
      j = A[j].parent;
    }
  }
}



Measured memory_bytes: 3752
from collections import deque
H,W = map(int,input().split())
S = list()
for i in range(H):
    S.append(list(input()))
mx = 0
for i in range(H):
    for j in range(W):
        if S[i][j] == "#":
            continue
        q = deque([(i,j,0)])
        T = [[S[k][l] for l in range(W)] for k in range(H)]
        T[i][j] = "#"
        while q:
            x,y,c = q.popleft()
            for dx, dy in [(0,1),(0,-1),(1,0),(-1,0)]:
                nx,ny = x+dx,y+dy
                if 0 <= nx < H and 0 <= ny < W and T[nx][ny] == ".":
                    T[nx][ny] = "#"
                    q.append((nx,ny,c+1))
        mx = max(mx,c)
print(mx)

Measured memory_bytes: 3316
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

# 改行またはスペース区切りの入力をすべて読み込んでイテレータを返します。
def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

n,m = get_nums_l()
lines = list(map(lambda s: s.strip(), sys.stdin.readlines()))
cons = list(map(lambda s: list(map(int, s.split())), lines))

for i in range(1000):
    s = str(i)
    keta = len(s)

    if keta != n:
        continue

    for con in cons:
        if con[0] - 1 > len(s):
            break
        if s[con[0] - 1] != str(con[1]):
            break
    else:
        print(s)
        exit()
print(-1)

Measured memory_bytes: 38384
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
#include <random>
#include <cassert>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MEM(a,b) memset((a),(b),sizeof(a))
const LL INF = 1e9 + 7;
const int N = 2e3 + 10;

int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int n, l;
	cin >> n >> l;
	int tot = n * (n - 1) / 2 + n * l;
	pair<int, int> ans = { int(INF),0 };
	for (int i = 1; i <= n; i++)
	{
		ans = min(ans, make_pair(abs(tot - (tot - (l + i - 1))), tot - (l + i - 1)));
	}
	cout << ans.second << endl;
	return 0;
}

Measured memory_bytes: 256
N = int(input())
ab = []
for i in range(N):
    a, b = map(int,input().split())
    ab.append([a,b])
ab.sort(key=lambda x:x[0])
ab.sort(key=lambda x:x[1])
cur = 0
ans = True
for i in range(N):
    cur += ab[i][0]
    if cur > ab[i][1]:
        ans = False
        break
print("Yes" if ans else "No")
    

Measured memory_bytes: 38072
// AtCoder template
// sabaより胡蝶しのぶさんの方が可愛いのではないか？
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i = 0; i < n; ++i)

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

    int n; cin >> n;
    ll z,w, a[n]; cin >> z >> w;
    rep(i,n) cin >> a[i];
    if(n == 1) cout << abs(a[0]-w) << endl;
    else cout << max(abs(a[n-1]-w), abs(a[n-2]-a[n-1])) << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define int ll
#define pb push_back
#define setbits(x)          __builtin_popcountll(x)
#define endl "\n"
typedef long long ll;
int decimalsum(int x){
int sum=0;
while(x){
    sum+=x%10;
    x=x/10;
}
return sum;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//                int php[4000] , cdc[8000], cdm[8000];
//                for(int j=0;j<5;j++){
//for(int i=0;i<2000;i++)php[i/10]=1;
//for(int i=0;i<2000;i++)php[i/10]=1;
//for(int i=0;i<2000;i++)php[i/10]=1;
//for(int i=0;i<2000;i++)php[i/10]=1;
//for(int i=0;i<2000;i++)php[i/10]=1;
//for(int i=0;i<2000;i++)php[i/10]=1;
//
//int t;
//cin>>t;
//while(t--)
{
   //solve();
  int n , k;
  cin>>n;
  vector<int>a(n+1);
  for(int i=1;i<=n;i++)cin>>a[i];
  vector<int>dp(n+1 , 0);
  dp[2]=abs(a[1]-a[2]);
  for(int i=3;i<=n;i++){
    dp[i]= min(abs(a[i]-a[i-1]) +dp[i-1], abs(a[i]-a[i-2])+dp[i-2]);
  }
  cout<<dp[n]<<endl;

}
    return 0;
}


Measured memory_bytes: 4744
def check(a, b, c, x):
    test = 500 * a + 100 * b + 50 * c
    if test > x:
        return 0
    elif test == x:
        return 1
    else:
        return 0

def resolve():
    a = int(input())
    b = int(input())
    c = int(input())
    x = int(input())
    
    result = 0
    #これは総当たり…
    for i in range(a, -1, -1):
        if 500 * i > x:
            continue
        for j in range(b, -1, -1):
            if 500 * i + 100 * j > x:
                continue
            for k in range(c, -1, -1):
                if 500 * i + 100 * j + 50 * k > x:
                    continue
                result += check(i, j, k, x)
    print(result)
resolve()

Measured memory_bytes: 3064
import sys
X,N=map(int,input().split())
if N==0:
  print(X)
  sys.exit(0)

pset=set(map(int,input().split()))
for i in range(1000):
  if not X-i in pset:
    print(X-i)
    sys.exit(0)
  if not X+i in pset:
    print(X+i)
    sys.exit(0)


Measured memory_bytes: 9176
// Problem : B - 1%
// Contest : AtCoder - AtCoder Beginner Contest 165
// URL : https://atcoder.jp/contests/abc165/tasks/abc165_b
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/coder3101/cp-editor)

// Mere bas ka nahi hai
// still i'm trying
// apna time aayega
//
//        .--------------.  
//        | Try First One|
//        '--------------'
//                |     .--------------.
//                |     |              |
//                V     V              |
//              .--------------.       |
//              |      AC.     |<---.  |  
//              '--------------'    |  |
//              (True)|  |(False)   |  |
//           .--------'  |          |  |
//           |           V          |  |
//           |  .--------------.    |  |
//           |  |   Try Again  |----'  |
//           |  '--------------'       |
//           |                         |
//           |  .--------------.       |
//           '->| Try Next One |-------'
//              '--------------'                          		  
//
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll power(ll x,ll y,ll m){if(y==0)return 1;ll p=power(x,y/2,m)%m;p=(p*p)%m;return (y%2==0)?p:(x*p)%m;}
ll nCr(ll n,ll r,ll m){if(r>n)return 0;ll a=1,b=1,i;for(i=0;i<r;i++){a=(a*n)%m;--n;}while(r){b=(b*r)%m;--r;}return (a*power(b,m-2,m))%m;}
#define Test int t;cin>>t;while(t--)
#define endl '\n'
void solve()
{
	ll x;
	cin >> x;
	ll n=100;
	int ans=0;
	while(n<x)
	{
		ll add=n/100;
		n+=add;
		ans++;
	}
	cout << ans << endl;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	//Test
	solve();
	return 0;
}


Measured memory_bytes: 3652
#include <iostream>
#include <bitset>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <functional>
#include <cctype>
#include <list>
#include <limits>
//#include <boost/multiprecision/cpp_int.hpp>

const double EPS = (1e-10);


using namespace std;
using Int = long long;
//using namespace boost::multiprecision;

const Int MOD = 1000000007;

const int MAXN = 1000000;
Int fact[MAXN], inv[MAXN];

Int mod_pow(Int x, Int n) {
    Int res = 1;
    while(n > 0) {
        if(n & 1) res = (res * x) % MOD; //ビット演算(最下位ビットが1のとき)
        x = (x * x) % MOD;
        n >>= 1; //右シフト(n = n >> 1)
    }
    return res;
}

void init_fact() {
    fact[0] = 1;
    for(int i=1; i<MAXN; i++) {
        fact[i] = (fact[i-1] * i) % MOD;
    }

    inv[MAXN - 1] = mod_pow(fact[MAXN - 1], MOD-2);
    for(int i=MAXN - 2; i>=0; i--) {
        inv[i] = (inv[i+1] * (i+1)) % MOD;
    }
}

Int comb(int n, int r) {
    if(r < 0 || n < r) return 0;
    return fact[n] * inv[n-r] % MOD * inv[r] % MOD;
}

template<typename T>
T gcd(T a, T b) {
    return b != 0 ? gcd(b, a % b) : a;
}

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

Int A[25][25];

int main(){
    cin.tie(0);

    Int N, M, C; cin >> N >> M >> C;

    for (int i = 0; i < N+1; i++){
        for (int j = 0; j < M; j++){
            cin >> A[i][j];
        } 
    }
    Int ans = 0;
    for (int i = 1; i < N+1; i++){
        Int tmp = 0;
        for (int j = 0; j < M; j++){
            tmp += A[0][j] * A[i][j];
        }
        if (tmp + C > 0) ans++;
    }
    cout << ans << endl;
}



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

using namespace std;
using ll  = long long;
using pii = pair<int, int>;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int H, W, D;
  cin >> H >> W >> D;
  vector<pii> A(H * W + 1);
  for (int i = 1; i <= H; i++) {
    for (int j = 1; j <= W; j++) {
      int a;
      cin >> a;
      A[a] = make_pair(i, j);
    }
  }

  vector<int> CS(H * W + 1);
  CS[1] = A[1].first + A[1].second;
  for (int i = 2; i < H * W + 1; i++) {
    if (i - D < 1) continue;
    CS[i] = CS[i - D] + abs(A[i].first - A[i - D].first) +
            abs(A[i].second - A[i - D].second);
  }

  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    cout << abs(CS[r] - CS[l]) << '\n';
  }
  return 0;
}


Measured memory_bytes: 2176
// https://ynucpc.github.io/blog/2018/04/15/cp_vscode/
// http://www.atmarkit.co.jp/ait/articles/1509/08/news019.html

#include <bits/stdc++.h>
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define rep1(i,n) for(ll i=1;i<=(ll)(n);i++)
#define rep3(i,n,m) for(ll i=(n);i<(ll)(m);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << (x) << endl
// #define max(a, b) ((a) > (b) ? (a) : (b))
// #define min(a, b) ((a) < (b) ? (a) : (b)) 
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
int _stoi(string s, int tmp=10){
	istringstream stm(s);
	int res;
	stm >> res;
	return res;
}
double _stod(string s, int tmp=10){
	istringstream stm(s);
	double res;
	stm >> res;
	return res;
}
template<typename T>string to_string(const T& n){
	std::ostringstream stm;
	stm << n;
	return stm.str();
}

// 降順ソート: sort(A, A+N, greater<>());
// min: *min_element(A, A+N)
// argmin: min_element(A, A+N)-A

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


int main(){	
	// ll A; cin >> A;
	// cout << "hello" << _stoi("14") << max({1,3}) << endl;
	ll N;
	cin >> N;
	ll H[N];
	rep(i, N)cin >> H[i];
	ll ans=0, ma=0;
	rep(i, N){
		if (ma <= H[i]){
			ans++;
			ma = H[i];
		}
	}
	cout << ans << endl;
}








Measured memory_bytes: 256
import sys
import math
from collections import defaultdict, deque, Counter
from copy import deepcopy
from bisect import bisect, bisect_right, bisect_left
from heapq import heapify, heappop, heappush
    
input = sys.stdin.readline
def RD(): return input().rstrip()
def F(): return float(input().rstrip())
def I(): return int(input().rstrip())
def MI(): return map(int, input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int, input().split()))
def TI(): return tuple(map(int, input().split()))
def LF(): return list(map(float,input().split()))
def Init(H, W, num): return [[num for i in range(W)] for j in range(H)]
    
    
def main():
    N = I()
    #dpの初期化
    dp = [0]*(N+1)
    dp[0] = 1
    
    mod=10 ** 9 + 7
    
    for i in range(3, N+1):
        dp[i] = (dp[i-1]+dp[i-3]) % mod
        
    print(dp[N])

if __name__ == "__main__":
    main()

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

#define crap ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define db1(x) cout<<#x<<"="<<x<<'\n'
#define db2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define db3(x,y,z) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define rep(i,n) for(int i=0;i<(n);++i)
#define repA(i,a,n) for(int i=a;i<=(n);++i)
#define repD(i,a,n) for(int i=a;i>=(n);--i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define CN cout<<"NO\n";
#define CY cout<<"YES\n";

using namespace std;
using ll = long long;
vector<vector<int>>row,col;
void printArray(vector<int> arr, int n,int f) 
{ 
    if(!f)
    row.pb(arr);
else col.pb(arr);
} 
void ps(vector<int> arr, int index,  
                       vector<int> subarr,int f) 
{ 
    if (index == arr.size()) 
    { 
        int l = subarr.size(); 
        if (l != 0) 
            printArray(subarr, l,f); 
    } 
    else
    { 
        ps(arr, index + 1, subarr,f); 
        subarr.push_back(arr[index]); 
        ps(arr, index + 1, subarr,f);
    } 
    return; 
} 
void solve()
{
    ll n,m,k; cin>>n>>m>>k;
    ll ct=0;
    int arr[n][m];
    rep(i,n)
    {
        rep(j,m)
        {
            char ch; cin>>ch;
            if(ch=='.') arr[i][j]=0;
            else {arr[i][j]=1;ct++;}
        }
    }
    if(k>ct){cout<<"0\n";return;}
    vector<int>v(n),u(m);
    rep(i,n) v[i]=i;
    rep(i,m) u[i]=i;
    vector<int>b;
    ps(v, 0, b,0);
    ps(u,0,b,1); 
    ll ans=0;
    if(ct==k) {ans=1;}
    for(auto r:row)
    {
        for(auto c:col)
        {
            ll te=0;
            for(auto rw:r)
            {
                rep(i,m)
                {
                    if(binary_search(c.begin(), c.end(),i)==false)
                    {
                        if(arr[rw][i]==1) te++;
                    }
                }
            }
            for(auto cl:c)
            {
                rep(i,n)
                {
                    if(arr[i][cl]==1) te++;
                }
            }
            if(ct-te==k) ans++;
        }
    }
    for(auto c:col)
    {
        ll te=0;
        for(auto cl:c)
            {
                rep(i,n)
                {
                    if(arr[i][cl]==1) te++;
                }
            }
            if(ct-te==k) ans++;
    }
    for(auto r:row)
    {
        ll te=0;
        for(auto rw:r)
            {
                rep(i,m)
                {
                    if(arr[rw][i]==1) te++;
                }
            }
            if(ct-te==k) ans++;
    }
    cout<<ans;
}
int main()
{
    crap;
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r", stdin) ;
        freopen("output.txt","w", stdout) ;
        freopen("error.txt","w", stderr) ;
    #endif
    int t;t=1;
    //cin>>t;
    while(t--)
        {solve();}
    return 0;
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
    // A, B, C をD(3)で管理、Aを0とする、クエリを先読みして次のを見て選択
    int N;
    cin >> N;
    vector<int> D(3);
    rep(i, 3) cin >> D[i];
    string ans;
    auto add = [&] (int a, int b) {
        D[a]++; D[b]--;
        ans += 'A' + a;
    };
    vector<P> Q;
    rep(i, N) {
        string S;
        cin >> S;
        int a = 0, b = 1;
        if (S == "AB") a = 0, b = 1;
        if (S == "AC") a = 0, b = 2;
        if (S == "BC") a = 1, b = 2;
        Q.emplace_back(a, b);
    }
    Q.emplace_back(0, 1);
    rep(i, N) {
        int a = Q[i].first;
        int b = Q[i].second;
        if (!D[a] && !D[b]) {
            puts("No");
            return 0;
        }
        if (!D[a]) {
            add(a, b);
        } else if (!D[b]) {
            add(b, a);
        } else {
            int na = Q[i+1].first;
            int nb = Q[i+1].second;
            if (a == na || a == nb) {
                add(a, b);
            } else {
                add(b, a);
            }
        }
    }
    puts("Yes");
    rep(i, N) {
        cout << ans[i] << '\n';
    }
    return 0;
}

Measured memory_bytes: 4324
n,m = map(int, input().split())
ls = []
rs = []
for _ in range(m):
  l,r = map(int, input().split())
  ls.append(l)
  rs.append(r)
  
ans = min(rs) - max(ls) + 1

if ans > 0: print(ans)
else: print(0)

Measured memory_bytes: 16772
#include <bits/stdc++.h>
#include <vector>
#include <iostream>
#include<algorithm>
#include<string>
#include <map>
#include <queue>
#include <stack>
#include<set>

#define DIV 1000000007
#define TE 2e5
using namespace std;
using ll = long long;
using ldb = long double;

int main() {
	int N; cin >> N;
	string c; cin >> c;
	int cnt = 0, L = 0, R = N - 1;
	while (L < R) {
		if (c[L] != 'W')L++;
		if (c[R] != 'R')R--;
		if (c[L] == 'W' && c[R] == 'R'&&L<R) {
			cnt++;
			swap(c[L], c[R]);
		}
	}


	cout << cnt << endl;
}


Measured memory_bytes: 3760
#include<bits/stdc++.h>
using namespace std;
const int maxn=100001;
int n,a[maxn],b[maxn];
long long res;
void calc(){
  int mid=n/2,l=0,r=n-1,flg_i=0,flg_o=0,count_o=0,L,R;
  b[mid]=a[l++];L=mid;R=mid;
  while(l<=r){
    if(flg_o==0) flg_i==0 ? b[++R]=a[r--] : b[++R]=a[l++] , flg_i^=1;
    else flg_i==0 ? b[--L]=a[r--] : b[--L]=a[l++] , flg_i^=1;
    if(++count_o==2) flg_o^=1 , count_o=0;
  }
  long long tmp=0;
  for(int i=L;i<R;i++)tmp+=abs(b[i]-b[i+1]);
  res=max(res,tmp);
}
int main(){
  scanf("%d",&n);
  for(int i=0;i<n;i++)scanf("%d",&a[i]);
  sort(a,a+n);
  if(n==2) {printf("%d",a[1]-a[0]); return 0;}
  calc();reverse(a,a+n);calc();
  printf("%lld",res);
  return 0;
}

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

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

   int K,Q; cin>>K>>Q;
   vector<int> d(K);
   for(int i=0;i<K;i++) cin>>d[i];

   while(Q--){
      int N,X,M; cin>>N>>X>>M;
      vector<int> D=d;
      for(int i=0;i<K;i++) D[i]=(D[i]%M==0? M:D[i]%M);
      lint fst=X,lst=X;
      lst+=accumulate(D.begin(),D.end(),0LL)*((N-1)/K);
      lst+=accumulate(D.begin(),D.begin()+(N-1)%K,0LL);
      cout<<(N-1)-(lst/M-fst/M)<<endl;
   }
   return 0;
}


Measured memory_bytes: 384
import java.util.Scanner;

public class Main {
	static Scanner scan = new Scanner (System.in);
	public static void main(String args[]){
		run();
	}
	static void run(){
		int n, minJ, tmp, count;
		boolean judge;
		
		n = scan.nextInt();
		
		int[] a = new int[n];
		
		count = 0;
		
		judge = false;
		
		for(int i = 0; i < n; i++){
			a[i] = scan.nextInt();
		}
		
		for(int i = 0; i < n; i++){
			minJ = i;
			for(int j = i; j < n; j++){
				if(a[j] < a[minJ]){
					minJ = j;
					judge = true;
				}
			}
			
			if(judge){
				tmp = a[i];
				a[i] = a[minJ];
				a[minJ] = tmp;
				count++;
				judge = false;
			}
		}
		for(int i = 0; i < n; i++){
			if(i == n - 1){
				System.out.println(a[i]);
			}else{
				System.out.print(a[i]+" ");
			}
		}
		System.out.println(count);
	}
}

Measured memory_bytes: 26132
#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>

using namespace std;

#define REP(i, n) for(ll i = 0;i < n;i++)
#define REPR(i, n) for(ll i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(ll i = m;i >= n;i--)
#define REPO(i, n) for(ll i = 1;i <= n;i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(),n.end()
#define MOD (ll) 1000000007
#define P pair<ll, ll>



string s;
ll dp[110000][2];
int main() {
	cin >> s;
	dp[0][0] = 1;
	REP(i, s.size()) {
		dp[i + 1][1] += dp[i][1] * 3;
		dp[i + 1][0] += dp[i][0];
		if (s[i] == '1') {
			dp[i + 1][1] += dp[i][0];
			dp[i + 1][0] += dp[i][0];
		}
		dp[i + 1][1] %= MOD;
		dp[i + 1][0] %= MOD;
	}
	cout << (dp[s.size()][0] + dp[s.size()][1]) % MOD << endl;
}


Measured memory_bytes: 2048
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define mo 1000000007
int i,j,k,m,n,x,y,z,S,Ans,ans,dp[41][1<<17];
inline ll Pow(ll x,ll d) {
    ll ans = 1, y = x;
    for (; d; y = y * y % mo, d >>= 1)d & 1 ? ans = ans * y % mo : 0;
    return ans;
}
int main() {
    cin >> n >> x >> y >> z;
    S = (1 << x + y + z) - 1;
    ans = (1 << x - 1) | (1 << x + y - 1) | (1 << x + y + z - 1);
    dp[0][0] = 1;
    for (i = 1; i <= n; ++i)
        for (j = 0; j <= S; ++j)
            if (dp[i - 1][j])
                for (k = 1; k <= 10; ++k) {
                    int o = ((j << k) | (1 << k - 1)) & S;
                    if ((o & ans) == ans)(Ans += 1ll * dp[i - 1][j] * Pow(10, n - i) % mo) %= mo;
                    else (dp[i][o] += (dp[i - 1][j]) % mo) %= mo;
                }
    printf("%d\n", Ans);
}

Measured memory_bytes: 19328
#pragma GCC optimize("Ofast")
    #include<bits/stdc++.h>
    //#include <ext/pb_ds/assoc_container.hpp> 
    #define vi vector<long long int>
    #define ll long long 
    #define mp make_pair
    #define p push
    #define pb push_back
    #define np next_permutation
    #define fin(i,a,b) for(int i=a;i<b;i++)
    #define max3(a,b,c) max(max(a,b),c)
    #define min3(a,b,c) min(min(a,b),c)
    //typedef tree<int,null_type,less<int>,rb_tree_tag, tree_order_statistics_node_update> indexed_set;
    const ll INF =1e18;
    const ll mod =1e9+7;
    using namespace std;
    const int N = 50000;
    int checkprime(int n){
        for(int i=2;i<min(N,n);i++){
            if(n%i==0)return 0;
        }
        return 1;
    }
    //using namespace __gnu_pbds;

    int main(){

        #ifdef _DEBUG
        freopen("input.txt", "r", stdin);
    //	freopen("output.txt", "w", stdout);
    #endif
        ios::sync_with_stdio(0); 
        cin.tie(0);
        cout.tie(0);
        //code from here>>
        string s;
        cin>>s;
        int z=s.size();
        if(s[z-1]=='s'){
            cout<<s<<"es"<<endl;
        }else{
            cout<<s<<"s"<<endl;
        }
    
            
        
        
        
        return 0;
    }


Measured memory_bytes: 3660
import sys
N=int(input())

H=[int(x) for x in input().rstrip().split()]
flag=0
mi=-1
for i in range(N-1):

  if H[i]<=H[i+1] and mi<=H[i]:
    mi=max(mi,H[i]-1)
    continue
  elif H[i+1]<H[i]:
    H[i]=H[i]-1
    mi=max(mi,H[i])
    if H[i+1]<H[i] or H[i]<mi:
      flag=1
      print("No")
      sys.exit()

if flag==0:
  print("Yes")



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

int main(){

    int N;
    cin >> N;

    int A[N];
    for(int i = 0; i < N; i++){
        scanf("%d",&A[i]);
    }

    double inv = 0;
    for(int i = 0; i < N; i++){
        inv += 1.0/A[i];
    }

    double ans = 1.0/inv;

    cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio> 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
using namespace std;
/*---------------------------------------------------*/

int n,u,k,v;
int g[105][105];

int main(){
  scanf("%d",&n);

  rep(i,n){
    scanf("%d%d",&u,&k);
    rep(i,k){
      scanf("%d",&v);
      g[u][v]=1;
    }
  }
  
  REP(i,1,n+1){
    printf("%d",g[i][1]);
    REP(j,2,n+1){
      printf(" %d",g[i][j]);
    }
    printf("\n");
  }
  return 0;
}

Measured memory_bytes: 3196
import java.util.Scanner;

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

        int a = sc.nextInt();
        int b = sc.nextInt();

        int n = Integer.parseInt(String.valueOf(a)+String.valueOf(b));

        for (int i = 1; i < 330; i++) {
            if (i * i == n) {
                System.out.println("Yes");
                return;
            }
        }
        System.out.println("No");

    }
}


Measured memory_bytes: 23764
import java.util.*;

public class Main{
public static void main(String[] args)
	{	Scanner sc = new Scanner(System.in);
     	int n = sc.nextInt();
     	int d = sc.nextInt();
     
     	int ans = 0;
     	for(int i=0; i<n; i++)
        {
          int a = sc.nextInt();
          int b = sc.nextInt();
     double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
          if(c<=d)
            ans++;
        }
     	System.out.println(ans);
  
		}
}

Measured memory_bytes: 59124
#include<stdio.h>

//main
int main(){
  int i,j,n,c,t,s,e=0;
  int r[10][10]; //set

  //loop
  while(1){
    scanf("%d",&n); //input
    if(n==0){  //error
      break;
    }
    e++;

    //init
    for(i=0;i<10;i++){
      for(j=0;j<10;j++){
	r[i][j]=0;
      }
    }
    c=1;

    if(n==1){
      printf("Case %d:\n",e);
      printf("%3d\n",1);
    }
    
    
    //main algorhythm start
    
    else{  //n>=2
      i=-1;
      j=0;
      while(1){  //left up loop
	if(i==n-1){
	  break;
	}
	i++;
	r[i][j]=c++;
	s=i;
	for(t=0;t<s;t++){
	  j++;
	  i--;
	  r[i][j]=c++;
	}
	if(j==n-1){
	  break;
	}
	j++;
	r[i][j]=c++;
	s=j;
	for(t=0;t<s;t++){
	  j--;
	  i++;
	  r[i][j]=c++;
	}
      }
      if(i==0){  //right down prepare
	i++;
	r[i][j]=c++;
	s=n-i-1;
	for(t=0;t<s;t++){
	  i++;
	  j--;
	  r[i][j]=c++;
	}
      }
      while(1){  //right down loop
	j++;
	r[i][j]=c++;
	if(c-1==n*n){
	  break;
	}
	s=n-j-1;
	for(t=0;t<s;t++){
	  i--;
	  j++;
	  r[i][j]=c++;
	}
	i++;
	r[i][j]=c++;
	if(c-1==n*n){
	  break;
	}
	s=n-i-1;
	for(t=0;t<s;t++){
	  i++;
	  j--;
	  r[i][j]=c++;
	}
      }
      
      //main alogorhythm end
      
      
      //output
      printf("Case %d:\n",e);
      for(i=0;i<n;i++){
	for(j=0;j<n;j++){
	  printf("%3d",r[i][j]);
	}
	printf("\n");
      }
    }
  }
  return 0;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
struct Edge {
  ll to;
  ll cost;
};
ll gcd(ll a, ll b) {
  if (a < b) {
    swap(a, b);
  }
  if (a % b == 0) {
    return (b);
  } else {
    return (gcd(b, a % b));
  }
}

ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
int main() {
  ll N, M;
  cin >> N >> M;
  string S, T;
  cin >> S >> T;
  ll L = lcm(N, M);
  ll n = N / gcd(N, M), m = M / gcd(N, M);
  REP(i, gcd(N, M)) {
    if (S[i * n] != T[i * m]) {
      L = -1;
    }
  }
  cout << L << endl;
}

Measured memory_bytes: 3764
N = int(input())

S = [int(input()) for _ in range(N)]

ans = sum(S)
if ans % 10 == 0: # 10の倍数の時
    min_s = float('inf')
    for s in S: # sの中から, 10の倍数でない、かつ最小のものを探してくる
        if s % 10 != 0:
            min_s = min(min_s, s)
    if min_s == float('inf'): # sの中に10の倍数でないものがなかったとき
        ans = 0
    else:
        ans -= min_s

print(ans)


Measured memory_bytes: 38384
n,m = map(int,input().split())
coins = list(map(int,input().split()))

inf = float("inf")
dp = [inf]*(n+2)

dp[0]= 0

for i in range(1,n+1):
  for coin in coins:
    if i-coin>=0:
      dp[i]=  min(dp[i-coin]+1,dp[i])
    
print(dp[n])


Measured memory_bytes: 7264
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll, vl/*, greater<ll>*/> pql; //降順(/*昇順*/)
typedef set<ll> sl;
#define rep(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep2(i, n) for(ll i = 1; i <= ll(n); ++i)
//#define rep(i, k, n) for(ll i = k-1; i < ll(n); ++i)
//#define rep2(i, k, n) for(ll i = k; i <= ll(n); ++i)
#define all(v) (v).begin(), (v).end()
bool chmin(ll &a, ll b) {if(b < a) {a = b; return 1;} return 0;}
bool chmax(ll &a, ll b) {if(b > a) {a = b; return 1;} return 0;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 1e9;
const char newl = '\n';
ll num=1, ans=0, cnt=0, sum=0;

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

  ll A, B, C, D;
  cin >> A >> B >> C >> D;
  
  if((A-1)/D >= (C-1)/B) cout << "Yes" << newl;
  else cout << "No" << newl;
  return 0;
}

Measured memory_bytes: 3612
import sys,collections
input = sys.stdin.readline

def main():
    N,M,K = list(map(int,input().split()))
    fr = [[] for i in range(N+1)]
    bl = [[] for i in range(N+1)]

          
    for _ in range(M):
        a,b = map(int,input().split())
        fr[a].append(b)
        fr[b].append(a)
    for _ in range(K):
        c,d = map(int,input().split())
        bl[c].append(d)
        bl[d].append(c)

    fl = [-1 for i in range(N+1)]
    fl[0] = 0
    fl[1] = 1
    nxt = 1
    dic = {}
    s = set(range(1,N+1))
    while nxt != 0:
        q = collections.deque([nxt])
        
        fl[nxt]=nxt
        cnt = 0
        while q:
            now = q.popleft()
            s.discard(now)
            for f in fr[now]:
                if fl[f] == -1:
                    fl[f] = nxt
                    cnt +=1
                    q.append(f)
        dic[nxt] = cnt
        #try:
            #nxt = fl.index(-1)
        #except:
            #nxt = 0
        if len(s) == 0:
            nxt = 0
        else:
            nxt = s.pop()


    mbf = collections.deque()
    for i in range(1,N+1):
        ff = 0
        for j in bl[i]:
            if fl[j] == fl[i]:
                ff -= 1
        
        ff += dic[fl[i]]-len(fr[i])
        mbf.append(ff)
    print(*mbf)

if __name__ == '__main__':
    main()


Measured memory_bytes: 118708
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double PI = acos(-1);
/* global variables */
/* function */
/* main */
int main(){
    long double A, B, H, W;
    cin >> A >> B >> H >> W;

    long double theta = abs(((H / 12.0 + W / 60 / 12)- W / 60.0) * 360.0);
    theta = min(360 - theta, theta);
    long double rad = theta*PI/180.0;
    long double C2 = A*A + B*B - 2.0 * A * B * cos(rad);
    cout << setprecision(10) << sqrt(C2) << '\n';
}


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

int main(void) {
    int a, b, x;
    cin >> a >> b >> x;

    string ans;

    if (x < a) {
        ans = "NO";
    } else {
        if ((a+b) < x) {
            ans = "NO";
        } else {
            if (b < (x-a)) {
                ans = "NO";
            } else {
                ans = "YES";
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3612
N, M, K = map(int, input().split())
is_k = False
for k in range(N+1):
    for l in range(M+1):
        c = k*(M-l)+l*(N-k)
        if c==K:
            is_k = True

if is_k:
    print("Yes")
else:
    print("No")


Measured memory_bytes: 2940
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#define next next2
using namespace std;
const int MAXN=100010;
const int INF=1000000000;
int n,a[1010],f[1010];
int head[MAXN],to[MAXN],next[MAXN],cnt=1;
inline void adde(int f,int t)
{
    cnt++,to[cnt]=t,next[cnt]=head[f],head[f]=cnt;
    cnt++,to[cnt]=f,next[cnt]=head[t],head[t]=cnt;
}
int g[1010][5010];
void DFS(int x,int fa)
{
    vector<int> v;
    for(int i=head[x];i;i=next[i])
        if(to[i]!=fa) DFS(to[i],x),v.push_back(to[i]);
    int ch=v.size();
    if(!ch) {f[x]=0;return;}
    for(int i=0;i<=ch;i++)
        for(int j=0;j<=a[x];j++)
            g[i][j]=INF;
    f[x]=INF;
    g[0][0]=0;
    for(int i=1;i<=ch;i++)
    {
        int o=v[i-1];
        for(int j=a[o];j<=a[x];j++)
            g[i][j]=min(g[i][j],g[i-1][j-a[o]]+f[o]);
        for(int j=1;j<=a[x];j++)
            g[i-1][j]=min(g[i-1][j-1],g[i-1][j]);
        for(int j=f[o];j<=a[x];j++)
            g[i][j]=min(g[i][j],g[i-1][j-f[o]]+a[o]);
    }
    for(int j=1;j<=a[x];j++)
        g[ch][j]=min(g[ch][j-1],g[ch][j]);
    f[x]=min(f[x],g[ch][a[x]]);
}
int main()
{
    scanf("%d",&n);
    for(int i=2,x;i<=n;i++) scanf("%d",&x),adde(i,x);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    DFS(1,-1);
    if(f[1]==INF) puts("IMPOSSIBLE");
    else puts("POSSIBLE");
    return 0;
}


Measured memory_bytes: 19072
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#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 all(v) v.begin(), v.end()
#define sz(v) v.size()
#define INF 100000000000000 //10^14
template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
int main()
{
    ll x;
    cin >> x;
    if(x == 0){
        cout << 1 << endl;
    }
    else{
        cout << 0 << endl;
    }
}

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

int main() {
  string s; { cin >> s; }

  ll m1{}, m2{}, c{};
  deque<ll> L1, L2; {
    for (ll i{}, now{}, sum{}; i < s.length(); ++i) {
      if (s[i] == '\\') {
        ++now;
      } else if (s[i] == '/') {
        --now;
        if (now == m1) {
          L1.push_back(sum);
          sum = 0;
          c = i;
        }
        m1 = min(m1, now);
      }
      sum += now - m1;
    }
    for (ll i{ static_cast<ll>(s.length() - 1) }, now{}, sum{}; i >= c; --i) {
      if (s[i] == '/') {
        ++now;
      } else if (s[i] == '\\') {
        --now;
        if (now == m2) {
          L2.push_front(sum);
          sum = 0;
        }
        m2 = min(m2, now);
      }
      sum += now - m2;
    }
  }
  vector<int> L; {
    for (auto&& x : L1) L.push_back(x);
    for (auto&& x : L2) L.push_back(x);
    ll A; {
      for (auto&& x : L) A += x;
      cout << A << endl;
    }
    int k = L.size(); {
      cout << k;
      for (int i = 0; i < k; ++i) cout << ' ' << L[i];
    }
  }
  cout << endl;
  return 0;
}



Measured memory_bytes: 3248
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define REPE(i, a, b) for (int i = (a); i <= int(b); ++i)
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(), x.end()
#define PB push_back
#define EB emplace_back
using LL = long long;
using PII = pair<int, int>;
#define F first
#define S second

int n, a[100010];

int main() {
  scanf("%d", &n);
  REP(i, n) scanf("%d", &a[i]);
  int ans = 0;
  for (int i = 0; i < n;) {
    ans++;
    int cur = 0;
    int j = i + 1;
    while (j < n) {
      int v = 0;
      if (a[j] != a[j - 1]) {
        v = a[j] > a[j - 1] ? 1 : -1;
      }
      if (cur == v || cur == 0 || v == 0) {
        if (cur == 0) cur = v;
        j++;
      }
      else break;
    }
    i = j;
  }
  printf("%d\n", ans);
  return 0;
}



Measured memory_bytes: 640
import random
class Dice:
    def __init__(self, ls):
        self.top,   self.bottom = ls[0], ls[5]
        self.right, self.left   = ls[2], ls[3]
        self.front, self.back   = ls[1], ls[4]

    def rot(self, d):
        if   d == "N":
            (self.top, self.back, self.bottom, self.front) = (
             self.front, self.top, self.back, self.bottom)
        elif d == "S":
            (self.top, self.back, self.bottom, self.front) = (
             self.back, self.bottom, self.front, self.top)
        elif d == "E":
            (self.top, self.right, self.bottom, self.left) = (
             self.left, self.top, self.right, self.bottom)
        elif d == "W":
            (self.top, self.right, self.bottom, self.left) = (
             self.right, self.bottom, self.left, self.top)

    def show(self):
        return (self.top, self.bottom, self.right,
                self.left, self.front, self.back)

S = set()
N = int(raw_input())
judge = True
for i in xrange(N):
    dice = Dice(map(int, raw_input().split()))
    s = set()
    for loop in xrange(1000):
        nums = dice.show()
        if nums in S:
            judge = False
            break
        s.add(nums)
        dice.rot("NSEW"[random.randint(0, 3)])
    if not judge:
        break
    S |= s
print "Yes" if judge else "No"

Measured memory_bytes: 5764
fn main() {
    for (mut min, mut max) in input() {
        if min > max {
            std::mem::swap(&mut min, &mut max);
        }

        println!("{} {}", min, max);
    }
}

fn input() -> Vec<(i32, i32)> {
    (1..)
        .map(|_| {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            let mut i = s.trim().split_whitespace().map(|q| q.parse::<i32>().unwrap());
            (i.next().unwrap(), i.next().unwrap())
        }).take_while(|q| *q != (0, 0)).collect()
}



Measured memory_bytes: 5028
#include <bits/stdc++.h>
using namespace std;
void Main();

using i8  = int8_t;   /* -128 ~ 127 */
using u8  = uint8_t;  /* 0 ~ 255 */
using i16 = int16_t;  /* -32,768 ~ 32,767 */
using u16 = uint16_t; /* 0 ~ 65,535 */
using i32 = int32_t;  /* -2,147,483,648 ~ 2,147,483,647 */
using u32 = uint32_t; /* 0 ~ 4,294,967,295 */
using i64 = int64_t;  /* -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 */
using u64 = uint64_t; /* 0 ~ 18,446,744,073,709,551,615 */
using f32 = float;    /* (-3.4 * 10^38) ~ (3.4 * 10^38) */
using f64 = double;   /* (-1.7 * 10^308) ~ (1.7 * 10^308) */
using f80 = __float80;

template <class T> using Vec = vector<T>;

/* 定数 */
constexpr i64 INF = 1010000000000000017;
constexpr i64 MOD = 1e9 + 7;
constexpr f64 EPS = 1e-12;
constexpr f64 PI = 3.14159265358979323846;

#define ALL(v) v.begin(), v.end()

#define GCD(a, b) __gcd(a, b) /* 最大公約数 */
#define LCM(a, b) a / __gcd(a, b) * b /* 最小公倍数 */

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}

void Main() {
    u32 A, B;
    cin >> A >> B;
    cout << (A - 1) * (B - 1) << endl;
}


Measured memory_bytes: 256
import math
N,M= list(map(int,input().split(" ")))
nums = list(map(int,input().split(" ")))
s = sum(nums)
boder = s / (4 * M ) #これ以上なら選べる
cot = 0



for num in nums:
    if num >= boder:
        cot += 1

if cot >= M :
    print("Yes")
else:
    print("No")



Measured memory_bytes: 38384