akhauriyash/Code-Regression huggingface dataset:
a,b,h,m = map(int,input().split())
import math

minutes = 60*h + m
rada = minutes*360/(60*12)*math.pi/180
radb = m*360/60*math.pi/180


ax,ay = a*math.cos(rada), a*math.sin(rada)
bx,by = b*math.cos(radb), b*math.sin(radb)

c2 = (ax-bx)**2 + (ay-by)**2
print(math.sqrt(c2))

Measured memory_bytes: 9520
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std;

int main(){
    int h, a;
    cin >> h >> a;
    if (h%a != 0){
        cout << h/a+1 << endl;
    }
    else{
        cout << h/a << endl;
    }
}

Measured memory_bytes: 256
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long
#define INF 1000000000000000000
typedef pair<ll, ll> pll;

int main() {
    int N;
    cin >> N;
    vector<int> A(N);
    rep(i, N) { cin >> A[i]; }
    rep(i, N - 1) { cout << A[i] << " "; }
    cout << A[N - 1] << endl;
    for (int i = 1; i <= N - 1; i++) {
        int v = A[i], j = i - 1;
        while (j >= 0 && A[j] > v) {
            A[j + 1] = A[j];
            j--;
        }
        A[j + 1] = v;
        rep(i, N - 1) { cout << A[i] << " "; }
        cout << A[N - 1] << endl;
    }
}


Measured memory_bytes: 3076
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.stream.IntStream;

public class Main {
    private static final int MOD = 1_000_000_007;

    public static void main(final String[] args) {
        final FastScanner scanner = new FastScanner(System.in);
        final int s = scanner.nextInt();
        if (s < 3) {
            System.out.println(0);
            return;
        }

        final CombinationCalculator calculator = new CombinationCalculator(s + 1, MOD);
        IntStream.range(0, s / 3)
            .mapToLong(i -> {
                final int rest = s - (i + 1) * 3;
                return calculator.calc(rest + i, i);
            })
            .reduce((a, b) -> (a + b) % MOD)
            .ifPresent(System.out::println);
    }

    private static class CombinationCalculator {
        private final int size;
        private final int mod;
        private final long[] factorials;
        private final long[] invertedElements;
        private final long[] invertedFactorials;

        CombinationCalculator(final int size, final int mod) {
            this.size = size;
            this.mod = mod;
            this.factorials = new long[size];
            this.invertedElements = new long[size];
            this.invertedFactorials = new long[size];
            init();
        }

        private void init() {
            factorials[0] = 1;
            factorials[1] = 1;
            invertedFactorials[0] = 1;
            invertedFactorials[1] = 1;
            invertedElements[1] = 1;
            for (int i = 2; i < size; i++) {
                factorials[i] = factorials[i - 1] * i % mod;
                invertedElements[i] = mod - invertedElements[mod % i] * (mod / i) % mod;
                invertedFactorials[i] = invertedFactorials[i - 1] * invertedElements[i] % mod;
            }
        }

        /**
         * mod 込みで nCk を計算した結果を返す．
         *
         * @param n 組み合わせの対象となる数
         * @param k 組み合わせる個数
         * @return 組み合わせの結果
         */
        long calc(final int n, final int k) {
            return factorials[n] * (invertedFactorials[k] * invertedFactorials[n - k] % mod) % mod;
        }
    }

    private static class FastScanner {
        private final BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(final InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

        String next() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (final IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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


Measured memory_bytes: 34564
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.PriorityQueue;
 
  
public class Main {
  public static void main(String[] args) {
    InputReader sc = new InputReader(System.in);
  
    int n = sc.nextInt();
    int max = -2000000000;
    int nowmin = sc.nextInt();
    for(int i = 1; i < n; i++){
      int nowvalue = sc.nextInt();
      if(nowvalue - nowmin > max){
        max = nowvalue - nowmin;
      }
      if(nowvalue < nowmin){
        nowmin = nowvalue;
      }
    }
    System.out.println(max);
  }
  
   
// LinkedList<Integer>[] setsu = new LinkedList[n];
// for(int i = 0; i < n; i++){
//   setsu[i] = new LinkedList<Integer>();
// } 
 
 //???????????????????????¬
  static class InputReader {
      private InputStream stream;
      private byte[] buf = new byte[1024];
      private int curChar;
      private int numChars;
      private SpaceCharFilter filter;
 
      public InputReader(InputStream stream) {
          this.stream = stream;
      }
  
      public int next() {
          if (numChars == -1)
              throw new InputMismatchException();
          if (curChar >= numChars) {
              curChar = 0;
              try {
                  numChars = stream.read(buf);
              } catch (IOException e) {
                  throw new InputMismatchException();
              }
              if (numChars <= 0)
                  return -1;
          }
          return buf[curChar++];
      }
  
      public String nextStr() {
        int c = next();
        while(isSpaceChar(c)){c = next();}
        StringBuffer str = new StringBuffer();
        do{
          str.append((char)c);
          c = next();
        }while(!isSpaceChar(c));
        return str.toString();
      }
 
      public int nextInt() {
          int c = next();
          while (isSpaceChar(c))
              c = next();
          int sgn = 1;
          if (c == '-') {
              sgn = -1;
              c = next();
          }
          int res = 0;
          do {
              if (c < '0' || c > '9')
                  throw new InputMismatchException();
              res *= 10;
              res += c - '0';
              c = next();
          } while (!isSpaceChar(c));
          return res * sgn;
      }
  
      public boolean isSpaceChar(int c) {
          if (filter != null)
              return filter.isSpaceChar(c);
          return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
      }
  
      public interface SpaceCharFilter {
          public boolean isSpaceChar(int ch);
      }
  }
}

Measured memory_bytes: 25324
from bisect import bisect_right, bisect_left
N = int(input())
*A, = map(int, input().split())
*B, = map(int, input().split())
*C, = map(int, input().split())
A.sort()
B.sort()
C.sort()
lc = len(C)
ans = 0
for b in B:
    a = bisect_left(A, b)
    c = len(C) - bisect_right(C, b)
    ans += a * c
print(ans)


Measured memory_bytes: 29516
// 20:23 - 20:32
#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int,int> pint;

const int N = 2e5 + 5;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

ll s[N];

// (st, ed]
int BSearch(int L, int R, int st, int ed){
	while(L < R){
		int M = (L+R) / 2;
		ll s1 = s[M] - s[st];
		ll s2 = s[ed] - s[M];
		if(s1 < s2) L = M + 1;
		else R = M;
	}
	return L;
}

ll Cal(int x, int y, int z, int n){
	vector<ll> t;
	t.push_back(s[x]);
	t.push_back(s[y] - s[x]);
	t.push_back(s[z] - s[y]);
	t.push_back(s[n] - s[z]);
	sort(t.begin(), t.end());
	return t.back() - t.front();
}

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

	int n; cin >> n;
	for(int i=1; i<=n; i++){
		cin >> s[i];
		s[i] += s[i-1];
	}

	ll ans = INF_LL;
	for(int i=2; i<=n-2; i++){
		int p1 = BSearch(1, i, 0, i);
		int p2 = BSearch(i+1, n, i, n);
		for(int j=-1; j<=0; j++)
			for(int k=-1; k<=0; k++)
				ans = min(ans, Cal(p1 + j, i, p2 + k, n));
	}
	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 1792
/*
ID: y1197771
PROG: test
LANG: C++
*/
#include<bits/stdc++.h>
#define pb push_back
#define FOR(i, n) for (int i = 0; i < (int)n; ++i)
#define dbg(x) cout << #x << " at line " << __LINE__ << " is: " << x << endl
typedef long long ll;
using namespace std;
typedef pair<int, int> pii;
const int maxn = 1e3 + 10;
void solve() {
    ll x, y;
    while(cin >> x >> y) {
        ll res = 0;
        if(y >= 2 * x) {
            res = x + (y - 2 * x) / 4;
        } else {
            res = y / 2;
        }
        cout << res << endl;
    }
}
int main() {
    //freopen("test.in", "r", stdin);
    //freopen("test.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    solve();
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
#define rep(i,a) for(int i = 0 ; i < a ; i ++)
#define loop(i,a,b) for(int i = a ; i < b ; i ++)
const int M = (1e6);
int p[M];
void isPrime(){
  rep(i,M)p[i] = true;
  p[0] = p[1] = false;
  loop(i,2,M){
    if(p[i])
      for(int j = i + i ; j < M ; j += i)
	p[j] = false;
  }
}
int main(void){
  isPrime();
  int n;
  while(cin>>n){
    int i = n - 1;
    while(!p[i--]);cout<<++i<<" ";
    i = n + 1;
    while(!p[i++]);cout<<--i<<endl;
  }
}

Measured memory_bytes: 5068
#include <stdio.h>

int main() {
	int a1,a2,a3;

	scanf("%d%d%d",&a1,&a2,&a3);

	if(a1>=1&&a1<=13&&a2>=1&&a2<=13&&a3>=1&&a3<=13)

	{if((a1+a2+a3)>=22)
        printf("bust\n");
    else
        printf("win\n");}

	return 0;
}


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

int main(){

    long long a ,b , c , d;
    cin>>a >> b >> c >> d  ;

    long long result1 = (long long)(a * c) ;
    long long result2 = (long long)(a * d) ;
    long long result3 = (long long)(b * c) ;
    long long result4 = (long long)(b * d) ;

    long long answer = max(max(result1 , result2) , max(result3 , result4)) ;

    cout<<answer ;

    return 0 ;

}


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

using namespace std;

const int INF = 1 << 30;

int main() {
  int N, M;
  string S[100], T[100];

  cin >> N;
  for(int i = 0; i < N; i++) cin >> S[i];
  cin >> M;
  for(int i = 0; i < M; i++) cin >> T[i];

  int ret = 0;
  for(int i = 0; i < N; i++) {
    int x = 0;
    for(int j = 0; j < N; j++) {
      if(S[i] == S[j]) ++x;
    }
    for(int j = 0; j < M; j++) {
      if(T[j] == S[i]) --x;
    }
    ret = max(ret, x);
  }
  cout << ret << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(long long int i = 0; i < n; i++)
#define _rep(i,m,n) for(long long int i = m; i < n; i++)
#define print(n) std::cout << n << std::endl
#define _print(n) std::cout << n
using namespace std;
typedef long long ll;
const int N = 1000000;
const ll mod = 1000000007;
using Graph = vector<vector<int>>;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

ll gcd(ll a, ll b){
   if(b == 0) return a;
   return gcd(b, a % b);
}
int main(){
  string a,b; cin >> a >> b;
  if(a.size() > b.size())cout << "GREATER" << endl;
  else if(a.size() < b.size())cout << "LESS" << endl;
  else if(a[0] - '0' > b[0] - '0')cout << "GREATER" << endl;
  else if(a[0] - '0' < b[0] - '0')cout << "LESS" << endl;
  else cout << "EQUAL" << endl;
  return 0;

}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,a,b) for (int i=(a); i<=(b); ++i)
#define per(i,a,b) for (int i=(a); i>=(b); --i)
using namespace std;

double l, r, lmid, rmid, lf, rf, ans = 1;
double p[7], q[7];

double f(double x) {
	double res = 0;
	rep (i, 1, 6) res += max(p[i] * x, q[i] * (1 - x));
	// printf("%.2lf %.2lf\n", x, res);
	return res;
}

int main() {
	rep (i, 1, 6) scanf("%lf", &p[i]), p[i] /= 100;
	rep (i, 1, 6) scanf("%lf", &q[i]), q[i] /= 100;
	l = 0, r = 1;
	rep (T, 1, 100) {
		lmid = (2 * l + r) / 3;
		rmid = (l + 2 * r) / 3;
		ans = min(ans, lf = f(lmid));
		ans = min(ans, rf = f(rmid));
		if (lf < rf) r = rmid;
		else l = lmid;
	}
	printf("%.10lf\n", ans);
	return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<climits>
#include<iostream>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = { 0, 0, 1, -1, 0 };
int dx[] = { 1, -1, 0, 0, 0 };

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

int main() {
	int c[20];   // int[] c = new int[20];
	int w = 0;
	while (true)
	{
		int a;
		cin >>  a;// int a = int.Parse(Console.ReadLine());
		if (a == 0)
			break;
		int sum = 0;
		int b[a];
		for (int j = 0; j < a; j++)
		{
			cin >> b[j];
			sum += b[j];
		}
		int maxi = 0,mini = 1000000000;
		for (int j = 0; j < a; j++) {
			if (maxi < b[j])
				maxi = b[j];
			if (mini > b[j])
				mini = b[j];
		}
		sum -= maxi + mini;
		c[w] = (sum / (a - 2));
		w++;
	}
	for (int j = 0; j<w; j++)
	{
		cout << c[j] << endl;
	}
}

Measured memory_bytes: 3012
import sys
def main():
    input=sys.stdin.readline
    h,w=map(int,input().split())
    a=[list(map(int,input().split())) for _ in range(h)]
    b=[list(map(int,input().split())) for _ in range(h)]
    ofset = 80*164
    mask = (1 << (ofset*2)) - 1
    dp=[[0 for i in range(w)] for j in range(h)]
    dp[0][0] |= 1 << (a[0][0] - b[0][0] + ofset)
    dp[0][0] |= 1 << (b[0][0] - a[0][0] + ofset)
    for i in range(h):
        for j in range(w):
            if i < h - 1:
                A = abs(a[i+1][j] - b[i+1][j])
                dp[i+1][j] |= (dp[i][j] << A) & mask
                dp[i+1][j] |= (dp[i][j] >> A) & mask
            if j < w - 1:
                A = abs(a[i][j+1] - b[i][j+1])
                dp[i][j+1] |= (dp[i][j] << A) & mask
                dp[i][j+1] |= (dp[i][j] >> A) & mask
    ans = 10000000000
    for i in range(ofset, 80*330):
        if (dp[h-1][w-1]>>i)&1: ans = min(ans, i - ofset)
    for i in range(ofset):
        if (dp[h-1][w-1]>>i)&1: ans = min(ans, ofset - i)
    print(ans)
if __name__=='__main__':
  main()


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

int main(){
    string w;
    cin>>w;
    vector<int> s(26,0);
    for(int i=0;i<w.size();i++){
        char c=w.at(i);
        if(c=='a') s.at(0)++;        
        if(c=='b') s.at(1)++;
        if(c=='c') s.at(2)++;
        if(c=='d') s.at(3)++;
        if(c=='e') s.at(4)++;
        if(c=='f') s.at(5)++;
        if(c=='g') s.at(6)++;
        if(c=='h') s.at(7)++;
        if(c=='i') s.at(8)++;
        if(c=='j') s.at(9)++;
        if(c=='k') s.at(10)++;
        if(c=='l') s.at(11)++;
        if(c=='m') s.at(12)++;
        if(c=='n') s.at(13)++;
        if(c=='o') s.at(14)++;
        if(c=='p') s.at(15)++;
        if(c=='q') s.at(16)++;
        if(c=='r') s.at(17)++;
        if(c=='s') s.at(18)++;
        if(c=='t') s.at(19)++;
        if(c=='u') s.at(20)++;
        if(c=='v') s.at(21)++;
        if(c=='w') s.at(22)++;
        if(c=='x') s.at(23)++;
        if(c=='y') s.at(24)++;
        if(c=='z') s.at(25)++;
    }
    int k=0;
    for(int i=0;i<26;i++) if(s.at(i)%2==0) k++;
    if(k==26) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
}

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

const int mod = 1e9+7;

int main(){
	long long n,m,a,ans=0;
	cin >> n >> m;
	
	m/=2;
	if(m>=n){
		ans+=n;
		m-=n;
		ans+=m/2;
	}
	else{
		ans+=m;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define gcd(a,b) __gcd(a,b)
#define pb push_back
#define sevensins ios_base::sync_with_stdio(false);cin.tie(NULL);
#define ll long long int
#define mod  1000000007
#define vect vector<long long int>
#define bins binary_search
#define vecti vector<int>::iterator
#define bitout __builtin_popcount
#define binsrch binary_search
int main() 
{sevensins
ll i,j,k,n;
cin>>n;
ll sum=0;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(k=1;k<=n;k++)
{
    sum+=gcd(i,gcd(j,k));
}

cout<<sum<<"\n";
return 0;
}


Measured memory_bytes: 3652
def main():
    import sys
    input = sys.stdin.buffer.readline
    N, Q = map(int, input().split())
    *C, = map(int, input().split())

    last = [-1]*(N+1)
    P = [[] for _ in [0]*(N+1)]
    for i, c in enumerate(C):
        if last[c] != -1:
            P[last[c]].append(i+1)
        last[c] = i+1

    queries = [[] for _ in [0]*(N+1)]
    indices = [[] for _ in [0]*(N+1)]
    for i in range(Q):
        l, r = map(int, input().split())
        queries[l].append(r)
        indices[l].append(i)

    def add(A, i, x):
        while i <= N:
            A[i] += x
            i += i & -i

    def sum(A, i):
        ret = 0
        while i:
            ret += A[i]
            i ^= i & -i
        return ret

    A = [0]*(N+1)
    ans = [0]*Q
    for l in range(N, 0, -1):
        for cr in P[l]:
            add(A, cr, 1)
        for i, r in zip(indices[l], queries[l]):
            ans[i] = r-l+1-sum(A, r)

    print(*ans, sep='\n')


if __name__ == '__main__':
    main()


Measured memory_bytes: 235772
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n; cin >> n;
    vector<int> a(n);
    int mini = -1, maxi = -1;
    for(int i=0; i<n; i++){
        cin >> a[i];
        if(a[i] > maxi || maxi == -1) maxi = a[i];
        if(a[i] < mini || mini == -1) mini = a[i];
    }
    cout << maxi - mini << endl;
}

Measured memory_bytes: 3576
#pragma region RegionDefs
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include "boost/multi_array.hpp"
#define rep(i,n) for(int i=0,i##_len=(n);i<i##_len;++i)
#define reps(i,j,n) for(int i=(j),i##_len=(n);i<i##_len;++i)
#define repr(i,n) for(int i=(n);i>=0;--i)
#define all(x) begin(x),end(x)
using namespace std;
template<class T=int> using P  = pair<T, T>;
template<class T=int> using V  = vector<T>;
template<class T=int> using PQ = priority_queue<T>;
template<class T=int> using PQG = priority_queue<T, V<T>, greater<T>>;
typedef long long ll;
const int INF = 1e9;
const ll MOD = 1000000007LL;
void in() {}
template<class Head, class... Tail>
void in(Head&& head, Tail&&... tail)
{
    cin >> head;
    in(move(tail)...);
}
#define IN(...) int __VA_ARGS__; in(__VA_ARGS__)
//オーバーフロー対策
#define INL(...) ll __VA_ARGS__; in(__VA_ARGS__)
#define INS(T, ...) T __VA_ARGS__; in(__VA_ARGS__)
#define VIN(T, v, n) V<T> v(n); for(auto& _elem:v)cin>>_elem
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 (a>b) {a=b; return 1;} return 0;}
#define y0 y__0
#define y1 y__1
#define j0 j__0
#define j1 j__1
#pragma endregion RegionDefs

#include <boost/multiprecision/cpp_int.hpp>

namespace mp = boost::multiprecision;

void solve()
{
    mp::cpp_int i = 0;
    INL(n);
    mp::cpp_int s = 0;
    rep(j, n)
    {
        INL(d, c);
        i += d * c;
        s += c;
    }
    cout << s - 1LL + (i - 1LL) / 9LL << endl;
}

int main()
{
    cin.tie(0); cout.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(numeric_limits<double>::max_digits10);

    solve();

    return 0;
}

Measured memory_bytes: 256
N, K = map(int, input().split())
S = input()
cnt1 = 0
cnt2 = 0
if len(S) == 1:
    print(0)
    exit()
p = 0
if 'L' == S[0]:
    for i in range(N):
        if 'L' == S[i]:
            p += 1
        else:
            ind = i
            if i == N-1:
                print(p-1)
                exit()
            break
else:
    for i in range(N):
        if 'R' == S[i]:
            p += 1
        else:
            ind = i
            if i == N-1:
                print(p-1)
                exit()
            break
if p == N:
    ind = N-1
ans = [1]
for i in range(ind, N):
    if S[i] == 'L':
        if cnt2:
            ans.append(2)
            cnt2 = 0
        cnt1 += 1
    else:
        if cnt1:
            ans.append(2)
            cnt1 = 0
        cnt2 += 1
if cnt1:
    ans.append(1)
else:
    ans.append(1)

ans.sort()
ans.reverse()

sums = 0
for i in range(N-1):
    if 'LL' == S[i:i+2]:
        sums += 1
    elif 'RR' == S[i:i+2]:
        sums += 1

if K >= len(ans):
    print(len(S) - 1)
else:
    if len(ans) == 0:
        print(len(S) - 1)
    if sums + sum(ans[:K]) >= len(S):
        print(len(S) - 1)
    else:
        print(sums + sum(ans[:K]))


Measured memory_bytes: 45680
#include <iostream>
#include <string>

bool isPalindromicNumber(std::string number);

int main() {
    int lowerNumber = 0;
    int upperNumber = 0;
    std::cin >> lowerNumber >> upperNumber;
    int numOfPalindromicNumbers = 0;
    for (int i = lowerNumber; i <= upperNumber; i++) {
        if (isPalindromicNumber(std::to_string(i))) {
            numOfPalindromicNumbers++;
        }
    }
    
    std::cout << numOfPalindromicNumbers << std::endl;
}
    
bool isPalindromicNumber(std::string number) {
    bool isPalindromic = true;
    size_t numOfDigit = number.length();
    for (size_t i = 0U; i < numOfDigit / 2U; i++) {
        if (number.at(i) != number.at(numOfDigit - 1 - i)) {
            isPalindromic = false;
            break;
        }
    }
    return isPalindromic;
}

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

P = list(map(int, input().split()))
Q = list(map(int, input().split()))
    
import itertools

per = list(itertools.permutations(list(range(1, N+1))))

p = 0
q = 0
for i in range(len(per)):
    if P == list(per[i]):
        p = i
    if Q == list(per[i]):
        q = i

print(abs(p - q))

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

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

  vector<int> d(K);
  vector<vector<int>> A(K,vector<int>(N));

  int dSum =0;

  for(int i =0;i<K;i++){
    cin >> d.at(i);
    dSum++;
    for(int j =0;j<d.at(i);j++){
      cin >> A.at(i).at(j);
    }
  }
  vector<bool> B(N,false);

  for(int i =0;i<K;i++){
    for(int j =0;j<d.at(i);j++){
      B.at(A.at(i).at(j)-1) =true;
    }
  }
  int cnt =0;
  for(int i =0;i<N;i++){
      if(B.at(i)==true)
      cnt++;
  }

  cout <<N -cnt << endl;
}

Measured memory_bytes: 3604
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define pb push_back
#define v vector
#define mp make_pair
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define rep(i,n) for(ll i=0;i<n;i++)
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int M = 1e9+7;
int ll_max=1e6;
int main() {
	// your code goes here
	IOS;
	int64_t n,x;
	
	ll i,j,m,k;
	int64_t sum=0;
      cin>>n;
      ll a[100001]={0};
      
      rep(i,n){
            cin>>x;
            sum+=x;
            a[x]++;
      }
      ll q;
      cin>>q;
      ll b,c;
      rep(i,q){
            cin>>b>>c;
            sum-=a[b]*b;
            sum+=a[b]*c;
            a[c]+=a[b];
            a[b]=0;
            cout<<sum<<endl;
      }
	return 0;
}

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

# define REP(i,n) for (int i=0;i<(n);++i)
# define rep(i,a,b) for(int i=a;i<(b);++i)
# define p(s) std::cout << s ;
# define pl(s)  std::cout << s << endl;
# define printIf(j,s1,s2) cout << (j ? s1 : s2) << endl;
# define YES(j) cout << (j ? "YES" : "NO") << endl;
# define Yes(j) std::cout << (j ? "Yes" : "No") << endl;
# define yes(j) std::cout << (j ? "yes" : "no") << endl;
# define all(v) v.begin(),v.end()
# define showVector(v) REP(i,v.size()){p(v[i]);p(" ")} pl("")
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;}
typedef long long int ll;
typedef pair<ll,ll> P_ii;
typedef pair<double,double> P_dd;

template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}

template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(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);
}


const int MOD = 1000000007;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;

void addM(long long &a, long long b) {
    a += b;
    if (a >= MOD) a -= MOD;
}

void mulM(long long &a, long long b) {
    a = ((a%MOD)*(b%MOD))%MOD ;
}

// a^b mod M
long myPow(long a,long b,int M) {
    long ret = 1;
    long tmp = a;
    while(b>0) {
        if((b&1)==1) ret = (ret * tmp) % M;
        tmp = (tmp * tmp) % M;
        b = b>>1;
    }
    return ret;
}

 // nCk mod M
int nCk(int n,int k,int M) {
    long ret = 1;
    int mi = min(k, n-k);
    for(int i=1;i<=mi;i++) {
        ret = (ret * myPow(i,M-2,M)) % M;
    }
    for(int i=n-mi+1;i<=n;i++) {
        ret = (ret * i) % M;
    }
    return (int)ret;
};

vector<pair<long long, long long> > prime_factorize(long long n) {
    vector<pair<long long, long long> > res;
    for (long long p = 2; p * p <= n; ++p) {
        if (n % p != 0) continue;
        int num = 0;
        while (n % p == 0) { ++num; n /= p; }
        res.push_back(make_pair(p, num));
    }
    if (n != 1) res.push_back(make_pair(n, 1));
    return res;
}

ll ret = 0;
vector<int> g[10000];
vector<ll> c; 
vector<ll> ans;

ll dfs(int s, int p){
    ll ret = 0;
    if(ans[s] == -1){
        ret += c.back();
        ans[s] = c.back();
        c.pop_back();
    }

    for(auto&& v : g[s]){
        if(p == v) continue;
        ret += dfs(v, s);
    }
    return ret;
}

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

    REP(i, N - 1){
        int a, b;
        cin >> a >> b;
        a--;b--;
        g[a].push_back(b);
        g[b].push_back(a);
    }

    ans.resize(N, -1);
    c.resize(N);
    REP(i, N) cin >> c[i];
    sort(all(c));

    ans[0] = c.back();
    c.pop_back();

    cout << dfs(0, -1) << endl;
    REP(i, N) cout << ans[i] << endl;

    return 0;
}

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

int main(){
    int n,m,k;
    cin>>n>>m>>k;
    ll ans=0;
    vector<ll> a(n+1,0),b(m+1,0);

    REP(i,n){
        ll t;
        cin>>t;
        a[i]=t+a[i-1];
    }
    REP(i,m){
        ll t;
        cin>>t;
        b[i]=t+b[i-1];    
    }

    rep(i,n+1){
        if(a[i]<=k){
            ll c = upper_bound(b.begin(),b.end(),k-a[i])-b.begin()-1;
            ans = max(ans,c+i);
        }
    }

    cout<<ans<<endl;

    return 0;
}

Measured memory_bytes: 6424
#include<bits/stdc++.h>
 #include <ext/pb_ds/tree_policy.hpp>
 #include <ext/pb_ds/assoc_container.hpp>
 #include<chrono>
 using namespace std;
 using namespace std::chrono;      
 using namespace __gnu_pbds; 
 #define fastio     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
 #define fi         first
 #define se         second
 #define int         long long 
 #define pb         push_back
 #define emp        emplace_back
 #define vv(x)      vector<x>
 #define mp(x,y)    map<x,y>
 #define dq(x)      deque<x>
 #define pql(x)     priority_queue<x>
 #define pqs(x)     priority_queue<x,vv(x),greater<x> >
 #define M          1000000007
 #define forf(i,a,b) for(int i=a;i<b;i++)
 #define it(x)      x::iterator
 #define ll         long long 
 #define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
 #define time__(d)  for(long blockTime = 0; (blockTime == 0 ? (blockTime=clock()) != 0 : false); debug("%s time : %.4fs", d, (double)(clock() - blockTime) / CLOCKS_PER_SEC))
 #define vii        vector<int>
 #define big        1e18
 #define sm         -2e9
 #define mkr        make_pair
 #define vpi        vector<pair<int,int> >
 #define pii        pair<int,int>
 #define rng        500005
 #define sz(x)      (int)x.size()
 #define rv(x)      reverse(x.begin(),x.end())
 #define out(x)     cout<<x.fi<<" "<<x.se<<endl;

//#define ordered_set tree<pii, null_type,less<pii>, rb_tree_tag,tree_order_statistics_node_update> 
  
void pr_init()
{
   
    #ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
   
    #endif
}

// friend ostream & operator << (ostream &out, const Complex &c); 
// friend istream & operator >> (istream &in,  Complex &c); 



void solve()
{

 int x,y;
 cin >> x >> y;
 
 int bl=0;
 for(int i=0;i<=x;i++)
 {
    if(i*2+(x-i)*4 == y) bl=1;
 }


 cout<<(bl?"Yes":"No")<<endl;
}


int32_t main()
{
  //pr_init();
  fastio;
  solve();

 }
      
      

Measured memory_bytes: 3648
#include <stdio.h>
void swap();
int data[6]={0};
int main(void)
{
	int i,j,n;
	while(scanf("%d,%d,%d,%d,%d",&data[1],&data[2],&data[3],&data[4],&data[5])!=EOF){
		swap();
		if((data[1]==data[2] && data[2]==data[3] && data[3]==data[4])||(data[2]==data[3] && data[3]==data[4] && data[4]==data[5])) puts("four card");
		else if((data[1]==data[2] && data[2]==data[3] && data[4]==data[5]) || (data[1]==data[2] && data[3]==data[4] && data[4]==data[5])) puts("full house");
		else if((data[1]==1 && data[2]==10 && data[3]==11 && data[4]==12 && data[5]==13 ) || (data[1]+1==data[2] && data[2]+1==data[3] && data[3]+1==data[4] && data[4]+1==data[5])) puts("straight");
		else if((data[1]==data[2] && data[2]==data[3]) || (data[2]==data[3] && data[3]==data[4]) || (data[3]==data[4] && data[4]==data[5])) puts("three card");
		else if((data[1]==data[2] && data[3]==data[4]) || (data[1]==data[2] && data[4]==data[5]) || (data[2]==data[3] && data[4]==data[5])) puts("two pair");
		else if((data[1]==data[2]) || (data[2]==data[3]) || (data[3]==data[4]) || (data[4]==data[5])) puts("one pair");
		else puts("null");
		}
	return 0;
}

void swap(){
	int i,j,temp;
	for(i=1;i<6;i++){
		for(j=1;j<6;j++){
				if(data[i]<data[j]){
					temp=data[i];
					data[i]=data[j];
					data[j]=temp;
				}
		}
	}
	return ;
}

Measured memory_bytes: 580
from collections import Counter
import sys
input=sys.stdin.readline


def solve():
    N, M = map(int, input().split())
    *A, = map(int, input().split())
    *B, = map(int, input().split())
    c = Counter()
    MOD = 10**9+7
    used = set()

    for i in A:
        for j in B:
            if i==j:
                if i in used:
                    print(0)
                    return
                else:
                    used.add(i)
            c[min(i, j)] += 1

    v, ans, bef = 0, 1, 0
    for i, j in sorted(c.items()):
        ans *= 1 if i in used else j
        v += i-bef-1
        for k in range(j-1):
            ans *= v-k
            ans %= MOD
        v -= j-1
        bef = i
    print(ans)


if __name__ == "__main__":
    solve()


Measured memory_bytes: 42096
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,x) for(int i=0;i<x;i++)
#define put(ans)    cout << ans << endl;
#define sort(s) sort(s.begin(),s.end())
#define reverse(s) reverse(s.begin(),s.end())

//最大公約数
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;
}
bool isPrime(int x){
    ll i;
    if(x < 2)return 0;
    else if(x == 2) return 1;
    if(x%2 == 0) return 0;
    for(i = 3; i*i <= x; i += 2) if(x%i == 0) return 0;
    return 1;
}

int main(){
    int a,p,ans(0);
    cin >> a >> p;
    p += a*3;
    ans = p/2;
    put(ans);

    return 0;
}

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

using ll = long long;

using namespace std;

int main() {
    int n, k;
    string s;
    cin >> n >> k >> s;
    char prev = '\0';
    int score = 0;
    for (auto c:s) {
        if (c == prev) score++;
        prev = c;
    }
    cout << score + min(2*k, n - score - 1) << '\n';
    return 0;
}

Measured memory_bytes: 512
fn main() {
    let mut sc = Scanner::new();
    let n = sc.read();
    let a: Vec<u64> = sc.read_vec(n);
    let mut b: Vec<u64> = sc.read_vec(n);

    let mut ans = 0;
    for k in (0..30).rev() {
        ans <<= 1;
        let mask = (1 << (k + 1)) - 1;
        let pop = (mask + 1) >> 1;
        b.sort_by_key(|&b| b & mask);

        let mut cur = 0;
        for &a in &a {
            let a = a & mask;

            let p1 = b.binary_search_by_key(&(1 * pop * 2 - 1), |&b| (a + (b & mask)) * 2)
                .err()
                .unwrap();
            let p2 = b.binary_search_by_key(&(2 * pop * 2 - 1), |&b| (a + (b & mask)) * 2)
                .err()
                .unwrap();
            let p3 = b.binary_search_by_key(&(3 * pop * 2 - 1), |&b| (a + (b & mask)) * 2)
                .err()
                .unwrap();
            let count = p2 - p1 + n - p3;
            cur ^= count & 1;
        }
        ans += cur;
    }
    println!("{}", ans);
}

struct Scanner {
    ptr: usize,
    length: usize,
    buf: Vec<u8>,
    small_cache: Vec<u8>,
}

#[allow(dead_code)]
impl Scanner {
    fn new() -> Scanner {
        Scanner {
            ptr: 0,
            length: 0,
            buf: vec![0; 1024],
            small_cache: vec![0; 1024],
        }
    }

    fn load(&mut self) {
        use std::io::Read;
        let mut s = std::io::stdin();
        self.length = s.read(&mut self.buf).unwrap();
    }

    fn byte(&mut self) -> u8 {
        if self.ptr >= self.length {
            self.ptr = 0;
            self.load();
            if self.length == 0 {
                self.buf[0] = b'\n';
                self.length = 1;
            }
        }

        self.ptr += 1;
        return self.buf[self.ptr - 1];
    }

    fn is_space(b: u8) -> bool {
        b == b'\n' || b == b'\r' || b == b'\t' || b == b' '
    }

    fn read_vec<T>(&mut self, n: usize) -> Vec<T>
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        (0..n).map(|_| self.read()).collect()
    }

    fn usize_read(&mut self) -> usize {
        self.read()
    }

    fn read<T>(&mut self) -> T
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug,
    {
        let mut b = self.byte();
        while Scanner::is_space(b) {
            b = self.byte();
        }

        for pos in 0..self.small_cache.len() {
            self.small_cache[pos] = b;
            b = self.byte();
            if Scanner::is_space(b) {
                return String::from_utf8_lossy(&self.small_cache[0..(pos + 1)])
                    .parse()
                    .unwrap();
            }
        }

        let mut v = self.small_cache.clone();
        while !Scanner::is_space(b) {
            v.push(b);
            b = self.byte();
        }
        return String::from_utf8_lossy(&v).parse().unwrap();
    }
}


Measured memory_bytes: 11640
#include<iostream>
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <iomanip>//setprecision
#include<math.h>
#include <functional>
#include<climits>
using namespace std;
#define rep(i, n)        for(int i=0; i<(int)(n); ++i)
#define ll  long long 
#define mod 1000000007
  vector<ll>to[1000005];
int main(){
  int a,b;cin>>a>>b;
  cout<<max(10-a,0)*100+b;
}


Measured memory_bytes: 23680
def get_divisors(a, b, c):
    #get list of divisors of c
    res = [i for i in range(1, c+1) if c % i == 0]

    #filter between a and b
    res = [i for i in res if a <= i <= b]

    # return sum
    
    return len(res)

if __name__ == "__main__":
    a, b, c = map(int, input().split())
    print(get_divisors(a, b, c))


Measured memory_bytes: 5604
import std.stdio,
       std.string,
       std.conv;

void main() {
  string s = readln.chomp;
  int K = readln.chomp.to!(int);
  // s.to!(char[]);
  char[] ans;
  foreach (char c; s[0..(s.length - 1)]) {
    if (c == 'a') {
      ans ~= c;
    }
    else if ('z' - c + 1 <= K) {
      ans ~= 'a';
      K -= 'z' - c + 1;
    }
    else {
      ans ~= c;
    }
  }
  char c = s[s.length - 1];
  for (int i = 0; i < K % 26; i++) {
    if (c == 'z') {
      c = 'a';
    }
    else {
      c = (c + 1).to!(char);
    }
  }
  writeln(ans ~ c);
}


Measured memory_bytes: 2720
#include<bits/stdc++.h>
#define forr(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = 1; i <= (n); i++)
#define ALL(a) (a.begin()),(a.end())
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll, ll> LP;
const ll LINF = 1LL<<60;
const int INF = 1001001001;
const int MOD = 1000000007;

/* --------------------------------------------------- */

int main() {
    int n;
    cin >> n;
    vector<int> S(n);
    int ans = 0;
    rep(i, n) {
        cin >> S[i];
        ans += S[i];
    }
    sort(ALL(S));
    int value = ans;
    rep(i, n) {
        if(S[i] % 10 != 0) {
            value = S[i];
            break;
        }
    }
    if(ans % 10 == 0) cout << ans - value << endl;
    else cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
 
 
using namespace std;
 
//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
 
//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}
 
//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef int LL;
 
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
 
//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
 
//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
 
//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
 
//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;


void solve(long long N, long long M, std::vector<long long> p, std::vector<std::string> S){
    long long watable[N+10];
    bool actable[N+10];
    // vector <LL> acs;
    REP(i, N+10){
        watable[i]=0;
        actable[i]=false;
    }
    REP(i, M){ 
        if(actable[p[i]]==false&&S[i]=="WA"){
            watable[p[i]]++;
        }
        else if(actable[p[i]]==true&&S[i]=="WA"){
        }
        else if(S[i]=="AC"){
            actable[p[i]]=true;
        }
    }
    long long acs = 0;
    long long was = 0;
    REP(i, N+10){
        if(actable[i]==true)was += watable[i];
        if(actable[i]==true)acs++;
    }
    cout<<acs<<" "<<was<<endl;

}

int main(){
    long long N;
    scanf("%lld",&N);
    long long M;
    scanf("%lld",&M);
    std::vector<long long> p(M);
    std::vector<std::string> S(M);
    for(int i = 0 ; i < M ; i++){
        scanf("%lld",&p[i]);
        std::cin >> S[i];
    }
    solve(N, M, std::move(p), std::move(S));
    return 0;
}


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

int main(){
string a;
  int b;
cin>>a;
  b=0;
  for(int i=0;i<a.size();i++){
  if(a[i]=='2'){
  b++;
  }
  }
  cout<<b<<endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define INF 1000000000000000
#define ll long long
#define ull unsigned long long
#define M (int)(1e9+7)
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=(int)m;i<(int)n;i++)
#define RFOR(i,m,n) for(int i=(int)m;i>=(int)n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
#define IN(a,n) rep(i,n){ cin>>a[i]; }
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265
#define F first
#define S second
#define PB push_back
#define EB emplace_back
void init(){
  cin.tie(0);
  ios::sync_with_stdio(false);
}




template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WG = vector< Edges< T > >;
using UG = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;


UG es;
int n,k;


int dfs(int x,int t,int s){
  int res=0;
  rep(i,es[x].size()){
    if(es[x][i]==t) continue;
    res+=dfs(es[x][i],x,s+1);
  }
  if(s>k/2)
    res++;
  return res;
}

int main(){
  cin>>n>>k;
  es.resize(n);

  rep(i,n-1){
    int a,b;
    cin>>a>>b;
    a--; b--;
    es[a].PB(b);
    es[b].PB(a);
  }

  int ans=inf;
  if(k%2){
    rep(i,n){
      rep(j,es[i].size()){
        ans = min(ans,dfs(i,es[i][j],0)+dfs(es[i][j],i,0));
      }
    }
  }
  else{
    rep(i,n){
      ans = min(ans,dfs(i,-1,0));
    }
  }

  cout<<ans<<endl;
}



Measured memory_bytes: 512
#include <bits/stdc++.h>
//#define int long long

using namespace std;
using LL = long long;
using P = pair<int, int>;

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)

#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e18;
const int MOD = (int)1e9 + 7;

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

    int n, m;
    cin >> n >> m;
    vector<vector<int>> adj(n);
    REP(i, m) {
        int a, b;
        cin >> a >> b;
        a--, b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    deque<int> ans;
    vector<bool> closed(n);
    ans.push_back(0);
    ans.push_back(adj[0][0]);
    closed[0] = true, closed[adj[0][0]] = true;

    while(true){
        bool flag1 = true, flag2 = true;
        for(auto child : adj[ans.front()]){
            if(!closed[child]) flag1 = false;
        }
        for(auto child : adj[ans.back()]){
            if(!closed[child]) flag2 = false;
        }
        if(flag1 && flag2) break;

        if(!flag1){
            for(auto child : adj[ans.front()]) {
                if (!closed[child]) {
                    ans.push_front(child);
                    closed[child] = true;
                    break;
                }
            }
        }
        if(!flag2){
            for(auto child : adj[ans.back()]) {
                if (!closed[child]) {
                    ans.push_back(child);
                    closed[child] = true;
                    break;
                }
            }
        }
    }
    int num = ans.size();
    cout << num << endl;
    REP(i, num){
        cout << ans.front() + 1 << " \n"[i == num-1];
        ans.pop_front();
    }
}

Measured memory_bytes: 6784
#include <cstdio>

const int mo=998244353;
const int maxn=300000;

int n,a,b,ans,fac[maxn+10],ifac[maxn+10];
long long k;

int c(int a,int b)
{
  return 1ll*fac[a]*ifac[b]%mo*ifac[a-b]%mo;
}

int main()
{
  scanf("%d%d%d%lld",&n,&a,&b,&k);
  fac[0]=1;
  for(int i=1; i<=n; ++i)
    {
      fac[i]=1ll*fac[i-1]*i%mo;
    }
  ifac[0]=ifac[1]=1;
  for(int i=2; i<=n; ++i)
    {
      ifac[i]=1ll*(mo-mo/i)*ifac[mo%i]%mo;
    }
  for(int i=2; i<=n; ++i)
    {
      ifac[i]=1ll*ifac[i]*ifac[i-1]%mo;
    }
  for(int i=0; i<=n; ++i)
    {
      if((k-1ll*a*i)%b)
        {
          continue;
        }
      long long cntb=(k-1ll*a*i)/b;
      if((cntb>n)||(cntb<0))
        {
          continue;
        }
      ans=ans+1ll*c(n,i)*c(n,cntb)%mo;
      if(ans>=mo)
        {
          ans-=mo;
        }
    }
  printf("%d\n",ans);
  return 0;
}


Measured memory_bytes: 2560
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

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

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

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------
ll N, K;

int main()
{
    cin >> N >> K;
    ll n2 = N % K;
    ll n3 = abs(n2 - K);
    cout << min(n2, n3) << endl;
}


Measured memory_bytes: 256
#![allow(unused, non_snake_case, dead_code, non_upper_case_globals)]
use {
    proconio::{marker::*, *},
    std::{cmp::*, collections::*, mem::*},
};
fn main() {
    input! {//
        n:usize,q:usize,
        tuv:[(usize,usize,usize);q],
    }
    let mut uf = UnionFind::new(n);
    for &(t, u, v) in tuv.iter() {
        match t {
            1 => {
                if uf.same(u, v) {
                    println!("{}", 1)
                } else {
                    println!("{}", 0);
                };
            }
            _ => uf.unite(u, v),
        }
    }
}
pub struct UnionFind {
    par: Vec<usize>,
    rank: Vec<usize>,
    size: Vec<usize>,
}
impl UnionFind {
    pub fn new(n: usize) -> UnionFind {
        let mut vec = vec![0; n];
        for i in 0..n {
            vec[i] = i;
        }
        UnionFind {
            par: vec,
            rank: vec![0; n],
            size: vec![1; n],
        }
    }
    pub fn find(&mut self, x: usize) -> usize {
        if x == self.par[x] {
            x
        } else {
            let par = self.par[x];
            let res = self.find(par);
            self.par[x] = res;
            res
        }
    }
    pub fn same(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }
    pub fn unite(&mut self, a: usize, b: usize) {
        let apar = self.find(a);
        let bpar = self.find(b);
        if apar == bpar {
            return;
        }
        if self.rank[apar] > self.rank[bpar] {
            self.par[bpar] = apar;
            self.size[apar] += self.size[bpar];
        } else {
            self.par[apar] = bpar;
            self.size[bpar] += self.size[apar];
            if self.rank[apar] == self.rank[bpar] {
                self.rank[bpar] += 1;
            }
        }
    }
    pub fn size(&mut self, a: usize) -> usize {
        let p = self.find(a);
        self.size[p]
    }
}


Measured memory_bytes: 11968
#![allow(dead_code, unused_imports, deprecated)]
use std::cmp::{max, min, Reverse};
use std::collections::*;
use maplit::*;
use num::Integer;
use proconio::input;
use proconio::marker::*;
use proconio::fastout;

const INF : i64 = 1000000007;

#[fastout]
fn main() {
  input! {
    n : usize,
  };
  input! {
    h : [i64; n],
  };

  let mut dp = vec![INF; n];

  for i in 0..n {
    match i {
      0 => { dp[0] = 0; },
      1 => { dp[1] = (h[1] - h[0]).abs(); },
      _ => {
        dp[i] = min(dp[i - 1] + (h[i] - h[i - 1]).abs(), dp[i - 2] + (h[i] - h[i - 2]).abs());
      },
    }
  }
  
  println!("{}", dp[n - 1]);
}

Measured memory_bytes: 3936