akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include <numeric>
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define INF 9999999

using namespace std;


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


ll m = 1000000007;

ll gcd(ll x, ll y) {
	ll m = max(x, y), n = min(x, y);
	if (m%n == 0)return n;
	else return gcd(m%n, n);
}
ll lcm(ll x, ll y) {
	return x / gcd(x, y)*y;
}

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

int main() {
	int N, i;
	cin >> N >> i;
	cout << N - i + 1 << endl;
}


Measured memory_bytes: 256
# https://atcoder.jp/contests/abc136/tasks/abc136_c



N = gets.to_i
Hs = gets.split.map(&:to_i)

min_from_last = {}
idx = N - 1
min = Hs[idx]
Hs.reverse_each do |height|
  min = height if height < min
  min_from_last[idx] = min
  idx -= 1
end

Hs.each_with_index do |height, index|
  if min_from_last[index + 1] && height - min_from_last[index + 1] >= 2
    puts 'No'
    exit
  end
end

puts 'Yes'


Measured memory_bytes: 17028
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <random>
#include <chrono>
 
using namespace std;
using namespace __gnu_pbds;
 
#define endl '\n'
 
typedef long long ll;
typedef pair<ll, ll> pii;
typedef tree<int, null_type,less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
template<typename T>
static T randint(T lo, T hi){
	return uniform_int_distribution<T>(lo, hi)(rng);
}


int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
 
	// freopen("settling.in", "r", stdin);
	// freopen("settling.out", "w", stdout);
	
	
	int n, m, k;
	cin >> n >> m >> k;

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

	int ans = 0;

	for(int i = 0; i < (1 << n); i++){
		for(int j = 0; j < (1 << m); j++){
			vector<vector<bool>> f(n, vector<bool>(m));

			for(int k = 0; k < n; k++)
				if(i & (1 << k))
					for(int l = 0; l < m; l++)
						f[k][l] = true;

			for(int k = 0; k < m; k++)
				if(j & (1 << k))
					for(int l = 0; l < n; l++)
						f[l][k] = true;

			int cnt = 0;
			for(int k = 0; k < n; k++)
				for(int l = 0; l < m; l++)
					if(!f[k][l] && a[k][l] == '#')
						cnt++;

			ans += cnt == k;
		}
	}

	cout << ans << endl;

	return 0;
}


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

int main(){
    int n;
    int t,a;
    int H[1001];
    double min;
    int ans;
    cin >> n;
    cin >> t >> a;
    for(int i=0;i<n;i++){
        cin >> H[i];
    }
    min = abs((double)(t-(double)(H[0]*0.006)-a));
    ans = 1;
    for(int i=1;i<n;i++){
        if(min > abs((double)(t-(double)(H[i]*0.006)-a))){
            min = abs((double)(t-(double)(H[i]*0.006)-a));
            ans = i+1;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0, max_i = (n); i < max_i; i++)
#define repi(i, a, b) for (long long i = (a), max_i = (b); i < max_i; i++)
using namespace std;
typedef long long ll;

int main() {
  string S, T; cin >> S >> T;
  bool b = false;
  if(T.substr(0,S.size()) == S && S.size() + 1 == T.size()) b = true;
  if(b)
    puts("Yes");
  else
    puts("No");
  return 0;
}


Measured memory_bytes: 3596
import collections
N, M = map(int, input().split())
A = list(map(int, input().split()))

# 普通の累積和
cumsum = [0]
for i in range(N):
    cumsum.append(cumsum[-1] + A[i])

# 上で作った累積和のmodMを計算
cumsum_mod = []
for i in range(N + 1):
    cumsum_mod.append(cumsum[i] % M)

c = collections.Counter(cumsum_mod)

ans = 0
for v in c.values():
    ans += v * (v - 1) // 2

print(ans)


Measured memory_bytes: 106284
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define ll long long
#define ull unsigned long long
#define ld long double
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define rbf(a, x) for (auto& a : x)
#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 drep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define pll pair<long long,long long>
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define vvc vector<vector<char>>
#define vc vector<char>
#define vvb vector<vector<bool>>
#define vb vector<bool>
#define Maxe(x) *max_element(all(x))
#define Mine(x) *min_element(all(x))
#define Size(x) ((ll)(x).size())
#define umap unordered_map
const long long INF = 1LL << 60;
const long double pi = 3.1415926535897932;
#define Fi first
#define Se second
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

    template<typename Num>
        constexpr Num mypow(Num a, long long b) {
            if(b==0)return 1;
            if (a==0)return 0;
            Num x = 1;
              while (b > 0) {
                if(b & 1)x*=a;
                a*=a;
                b >>= 1;
              }
              return x;
        }

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

//lis,modpow,uf,modncr,soinsubunkai,yakusurekkyo
//mint,dijkstra,gyakugen

    long long modpow(long long a, long long n, long long mod) {
        long long res = 1;
        while (n > 0) {
            if (n & 1) res = res * a % mod;
            a = a * a % mod;
            n >>= 1;
        }
        return res;
    }

    ll modnCr(ll n, ll r, ll mod) {
        ll res = 1;
        rep (i, r) {
            res = res * (n - i) % mod * modpow(i + 1, mod - 2, mod) % mod;
        }
        return res;
    }

    signed main(){
        ios::sync_with_stdio(false);
        cin.tie(nullptr);
        ll x,y;
        cin>>x>>y;
        const ll MOD=1e9+7;
        if((x+y)%3!=0){
            cout<<0<<endl;
            return 0;
        }
        ll a=(2*x-y)/3;
        ll b=x-2*a;
        if(a<0||b<0){
            cout<<0<<endl;
            return 0;
        }
        ll res=modnCr(a+b,a,MOD);
        cout<<res<<endl;
    }

Measured memory_bytes: 3604
#include<bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define sz(a) int(a.size())
#define clr(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,int> pli;
const int inf=1e9;
const ll Inf=1e18;
const int N=1510;
const int mod=0;
int gi() {
    int x=0,o=1;char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if(ch=='-') o=-1,ch=getchar();
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*o;
}
template<typename T> bool chkmax(T &a,T b) { return a<b?a=b,1:0; };
template<typename T> bool chkmin(T &a,T b) { return a>b?a=b,1:0; };
int add(int a,int b) { return a+b>=mod?a+b-mod:a+b; }
int sub(int a,int b) { return a-b<0?a-b+mod:a-b; }
void inc(int &a,int b) { a=(a+b>=mod?a+b-mod:a+b); }
void dec(int &a,int b) { a=(a-b<0?a-b+mod:a-b); }
int n,l[N][N],r[N][N];
char s[N][N];
ll ans=0,f[N][N];
int main() {
    n=gi();
    for(int i=1;i<=n;i++) scanf("%s",s[i]+1);
    for(int i=n;i;i--)
        for(int j=n;j;j--)
            if(isdigit(s[i][j])) {
                bool f0=isdigit(s[i+1][j]),f1=isdigit(s[i][j+1]);
                if(!f0&&!f1) {
                    l[j][i]=r[j][i]=j;
                    for(int k=i+1;k<=n;k++) l[j][k]=n+1,r[j][k]=0;
                }
                else if(!f0) {
                    f[i][j]=f[i][j+1];
                    l[j][i]=j,r[j][i]=r[j+1][i];
                    for(int k=i+1;k<=n;k++) l[j][k]=l[j+1][k],r[j][k]=r[j+1][k];
                }
                else if(!f1) {
                    f[i][j]=f[i+1][j];
                    l[j][i]=r[j][i]=j;
                }
                else {
                    int lst=0;
                    f[i][j]=f[i+1][j]+f[i][j+1];
                    l[j][i]=j,r[j][i]=r[j+1][i];
                    for(int k=i+1;k<=n;k++) {
                        if(l[j+1][k]<=r[j][k]&&l[j+1][k]>lst) f[i][j]-=f[k][l[j+1][k]];
                        lst=r[j][k];
                        chkmin(l[j][k],l[j+1][k]);
                        chkmax(r[j][k],r[j+1][k]);
                    }
                }
                ans+=f[i][j]*(s[i][j]-'0'),f[i][j]+=s[i][j]-'0';
            }
            else {
                for(int k=i;k<=n;k++) l[j][k]=n+1,r[j][k]=0;
            }
    printf("%lld\n",ans);
    return 0;
}


Measured memory_bytes: 38144
from collections import Counter
S = input()[::-1]
cur = 0
mods = Counter()
mods[0] += 1
MOD = 2019
for i,d in enumerate(S):
  cur += int(d) * pow(10, i, MOD)
  mods[cur%MOD] += 1

res = 0
for k,v in mods.items():
  res += (v * (v-1) // 2)

print(res)

Measured memory_bytes: 69408
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, b) for(int i = (a); i<(b); ++i)
#define rep(i, n) for (int i=0; i<(n); ++i)
#define out(x) cout << x << endl

int main() {
    string S;
    cin >> S;
    int n = S.size();
    for (int i=0; i<n; i++) {
        if (i%2) {
            if (S.at(i)=='R') {
                out("No");
                return 0;
            }
        } else {
            if (S.at(i)=='L') {
                out("No");
                return 0;
            }
        }
    }
    out("Yes");
}

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

int main() {
    string N;
    cin >> N;
    
    int len = N.length();

    int sum = 0;
    for (int i=0; i < len; i++) {
        sum += int(N[i] - '0');
    }

    if (stoi(N) % sum == 0) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for(int i=0; i<n; i++)
#define rep2(i, s, e) for(int i=s; i<e; i++)
#define print(i) cout << i << endl;

int main(){

    int n; cin >> n;
    int h[n];
    rep(i, n){
        cin >> h[i];
    }

    int ans = 0;
    int temp = 0;
    rep2(i, 1, n){
        if(h[i] <= h[i-1]){
            temp++;
            ans = max(ans, temp);
        }
        else{
            temp = 0;
        }
    }

    cout << ans;

    return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
using ull = unsigned long long;
const ull MOD = 1e9 + 7;

ull n, k;
ull a[2001];
ull bk_sm[2001], sm[2001];

int main() {
    #ifdef DEBUG
        freopen("in.txt", "r", stdin);
    #endif
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

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

    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i == j) continue;
            if (a[j] < a[i]) sm[i]++;
            if (a[j] < a[i] && j > i) bk_sm[i]++;
        }
    }

    /*
    for (int i = 0; i < n; i++) {
        cout << bk_sm[i] << ' ';
    } cout << '\n';
    for (int i = 0; i < n; i++) {
        cout << sm[i] << ' ';
    } cout << '\n'; */

    ull ans = 0;
    for (int i = 0; i < n; i++) {
        ans += bk_sm[i] * k;
        ans %= MOD;
    }
    for (int i = 0; i < n; i++) {
        ull tmp = k * (k - 1ULL) / 2ULL;
        tmp %= MOD;
        ans += sm[i] * tmp;
        ans %= MOD;
    }

    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <bitset>
#include <tuple>
#include <stack>
#include <utility>

using namespace std;
typedef long long ll;

int main(){
    ll K, A, B;
    cin >> K >> A >> B;
    if(A>=B-1){
        cout << K+1 << endl;
        return 0;
    }
    ll N = 1, G = 0;
    for(int k=0; k<K-1; k++){
        if(G > 0){
            G -= 1;
            N += B;
        }
        else if(N >= A){
            G += 1;
            N -= A;
        }
        else{
            N += 1;
        }
    }
    if(G > 0) N += B;
    else N += 1;
    cout << N << endl;
    return 0;
}

Measured memory_bytes: 384
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int x = sc.nextInt();
    int y = sc.nextInt();
    int r; //最大公約数
    while(true) {
        r=x%y;
        x=y;
        y=r;
        if(r==0) break;
    }
    System.out.println(x);
    }
}



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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i,n) for(int i=0;i<(int)n;++i)

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){
  os << "(" << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator << (ostream& os, const vector<T> v){
  for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator << (ostream& os, const vector<vector<T>> v){
  for(int i = 0; i < (int)v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}

using Graph = vector<vector<ll>>;
pll dfs(ll now, ll par, Graph &G){
	pll res = {0, now};
	for(auto next: G[now]){
		if(next == par) continue;
		auto cost = dfs(next, now, G);
		cost.first++;
		res = max(res, cost);
	}
	return res;
}

ll tree_diameter(Graph &G){
	auto p = dfs(0, -1, G);
	auto q = dfs(p.second, -1, G);
	// (p.second, q.second) is the diameter
	return q.first;
}


// https://atcoder.jp/contests/agc033/tasks/agc033_c
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N;
    cin >> N;
    Graph G(N);
    REP(i, N-1){
    	ll x, y;
    	cin >> x >> y;
    	x--, y--;
    	G[x].push_back(y);
    	G[y].push_back(x);
    }
    ll d = tree_diameter(G);

    cout << (d%3==1 ? "Second" : "First") << endl;
    return 0;
}

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

inline int num (const std::string& S, int idx){ return S.at(idx) - '0'; }
inline unsigned int bitMask(int i){ return (1 << i); }
inline bool isTrue(unsigned int bit, int i){ return bit & bitMask(i); }

int main(){
	int i;
	cin >> i;
	cout << i/2 + i%2 << endl;
	return 0;
}


Measured memory_bytes: 256
//----------BHAVIK DIWANI(PICT_COMP)---------------
#include<bits/stdc++.h>
#define test ll t; cin>>t; while(t--)
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define mod 1000000007
#define ll long long
#define int long long
#define ull unsigned long long
#define MAX 1000005
#define pb push_back
#define mkp make_pair
#define vi vector<int>
#define pii pair<int,int>
#define endl '\n'
#define vs vector<string>
#define mii map<int,int>
#define msi map<string,int>
#define vpii vector< pair<int, int > >
#define vpsi vector< pair< string ,int > >
#define forci(p,q) for(int i=p;i<q;i++)
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);}
ull power(ull a, ull b) {a %= mod;  ull res = 1;  while (b > 0) { if (b & 1) res = res * a % mod;  a = a * a % mod;  b >>= 1; }  return res%mod; }
ll modmul(ll x, ll y){return (x*y)%mod;}
ll modadd(ll x, ll y){return (x+y)%mod;}
ll modsub(ll x, ll y){return (x-y+mod)%mod;}
ll fact[1000007]={0};
void facto() {fact[0]=1;fact[1]=1;for(int i=2;i<1000007;i++)fact[i]=(fact[i-1]*i)%mod;}
ll ncr(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(power(fact[r],mod-2)))%mod; res=(res*(power(fact[n-r],mod-2)))%mod; return res;}
ll npr(ll n,ll r) {ll res=1; res=fact[n]; res=(res*(power(fact[n-r],mod-2)))%mod; return res;}
inline long long toint(const std::string &s) {std::stringstream ss; ss << s; long long x; ss >> x; return x;}
inline std::string tostring(long long number) {std::stringstream ss; ss << number; return ss.str();}
inline std::string tobin(long long x) {return std::bitset<63>(x).to_string();}
/*bool prime[MAX];
ull sieve(){memset(prime,true,sizeof(prime));for(ull p=2;p*p<MAX;p++){if(prime[p]==true){for(ull i=2*p;i<MAX;i+=p){prime[i]=false;}}}prime[0]=0;prime[1]=0;
}*/
/*int inv[1000001]={0};
void findinverse(){inv[1] = 1;for(int i = 2; i <=1e6 ; ++i)	inv[i] = (mod - (mod/i) * inv[mod%i] % mod) % mod;}
*/
using namespace std;
int solve()
{
	int n;
	cin>>n;
	int a[n];
	forci(0,n){
		cin>>a[i];
	}
	int cnt=0;
	a[0]--;
	for(int i=1;i<n ;i++){
		if(a[i]>a[i-1])
			a[i]--;
		else if(a[i]<a[i-1]){
			cout<<"No";
			return 0;
		}
	}
	cout<<"Yes";
	return 0;
}
signed main()
{
	fastio;
//	test
		solve();
}







Measured memory_bytes: 4404
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>
#include <deque>
#include <cmath>
#include <map>
using ll = long long;
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> l(n);
  for (int i = 0; i < n; i++) {
    int m;
    cin >> m;
    l.push_back(m);
  }
  sort(l.begin(), l.end(), greater<int>());

  int ans = 0;
  for (int i = 0; i < k; i++) {
    ans += l[i];
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class Main {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int n = in.nextInt();
		Set<Integer> d = new HashSet<>();
		for (int i = 0; i < n; i++) {
			d.add(in.nextInt());
		}
		System.out.println(d.size());

		in.close();
	}
}

Measured memory_bytes: 23764
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;

long long Abs(int a,int b){
    int res=a-b;
    if(res<0)res*=-1;
    return res;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin>>n;
    vector<int> a(n);
    for(int i=0;i<n;i++)cin>>a[i];
    sort(a.begin(),a.end());
    long long ans=0;
    int r=a.size()-1,l=1;
    int num1=a[0],num2=a[0];
    for(int i=0;i<n-1;i++){
        if(i%4==0){
            ans+=Abs(num1,a[r]);
            num1=a[r];
            r--;
        }
        else if(i%4==1){
            ans+=Abs(num2,a[r]);
            num2=a[r];
            r--;
        }
        else if(i%4==2){
            ans+=Abs(num1,a[l]);
            num1=a[l];
            l++;
        }
        else{
            ans+=Abs(num2,a[l]);
            num2=a[l];
            l++;
        }
    }
    long long tmp=ans;
    reverse(a.begin(),a.end());
    ans=0;
    r=a.size()-1;
    l=1;
    num1=a[0];
    num2=a[0];
    for(int i=0;i<n-1;i++){
        if(i%4==0){
            ans+=Abs(num1,a[r]);
            num1=a[r];
            r--;
        }
        else if(i%4==1){
            ans+=Abs(num2,a[r]);
            num2=a[r];
            r--;
        }
        else if(i%4==2){
            ans+=Abs(num1,a[l]);
            num1=a[l];
            l++;
        }
        else{
            ans+=Abs(num2,a[l]);
            num2=a[l];
            l++;
        }
    }
    cout<<max(ans,tmp)<<endl;

}

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

using namespace std;

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

    int N;
    cin >> N;
    int num_odd = (N + 1) / 2;
    cout.precision(17);
    cout << fixed << (double)num_odd / N << "\n";
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ld=long double;
using st=string;
using ch=char;
typedef pair<ll,ll> P;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<P> vP;
typedef vector<ch> vc;
typedef vector<vc> vvc;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,n) FOR(i,0,n)
#define ROF(i,a,b) for(ll i=a;i>=b;i--)
#define per(i,a) ROF(i,a,0)
const ll MOD=1000000007;
const ll MOD2=998244353;
const ld PI=acos(-1);
const ll INF=1e18;
st abc="abcdefghijklmnopqrstuvwxyz";
st ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
struct edge{ll to,cost;};

const ll MXN=100005;
vector<edge> G[MXN];
vector<bool> B(MXN,false);
vl ans(MXN);

void bfs(ll x,ll d){
  B[x]=true;
  ans[x]=d;
  ll k=G[x].size();
  rep(i,k){
    if(B[G[x][i].to]==false){
      bfs(G[x][i].to,G[x][i].cost+d);
    }
  }
}

  
int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  #define endl '\n'
  ll N;
  cin >> N;
  rep(i,N-1){
    ll a,b,c;
    cin >> a >> b >> c;
    a--;b--;
    G[a].push_back({b,c});
    G[b].push_back({a,c});
  }
  ll Q,K;
  cin >> Q >> K;
  K--;
  bfs(K,0);
  rep(i,Q){
    ll x,y;
    cin >> x >> y;
    x--;y--;
    cout << ans[x]+ans[y] << endl;
  }
}


Measured memory_bytes: 14336
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,T;
char s[100001];
signed main()
{
	cin>>s;
	vector<char>v[2];
	int n=strlen(s);
	for(int i=0;i<n;i++)
		v[1].push_back(s[i]);
	cin>>T;
	int cur=0;
	while(T--)
	{
		int op;
		cin>>op;
		if(op==1)
			cur^=1;
		else
		{
			int f;
			char c;
			cin>>f>>c;
			if(f==1)
				v[cur].push_back(c);
			else
				v[cur^1].push_back(c);
		}
	}
	for(int i=(int)v[cur].size()-1;i>=0;i--)
		cout<<v[cur][i];
	for(int i=0;i<v[cur^1].size();i++)
		cout<<v[cur^1][i];
	return 0;
}
/*
abc
4
2 1 p
2 1 c
2 2 c
1
*/

Measured memory_bytes: 1020
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)

int N;
int main(){
  cin >> N;
  cout << (1 << (int)log2(N)) << endl;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define N 300010
struct TreeArray {
    int f[N];
    #define lowbit(x) ((x)&(-x))
    inline void add(int k,int dat) {
        while (k<N) f[k]+=dat,k+=lowbit(k);
    }
    inline int sum(int k) {
        int ans=0;
        while (k) ans+=f[k],k-=lowbit(k);
        return ans;
    }
    inline void add(int l,int r,int dat) {
        add(l,dat),add(r+1,-dat);
    }
}T;
inline int read() {
    int x=0;
    char ch=getchar();
    while (!isdigit(ch)) ch=getchar();
    while (isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x;
}
struct node {
    int l,r;
}a[N];
inline int cmp(node a,node b) {
    return a.r-a.l<b.r-b.l;
}
int main() {
    int n=read(),m=read(),p=1;
    for (int i=1;i<=n;i++) a[i].l=read(),a[i].r=read();
    sort(a+1,a+n+1,cmp);
    for (int i=1;i<=m;i++) {
        while (p<=n && a[p].r-a[p].l+1<=i) T.add(a[p].l,a[p].r,1),++p;
        // puts("qwq");
        int ans=n-p+1;
        for (int j=0;j<=m;j+=i) ans+=T.sum(j);
        printf("%d\n",ans);
    }
    return 0;
}

Measured memory_bytes: 3712
#include <cstdio>
#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <cmath>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#define rep(i,n) for (int i=0;i<n;i++)
using ll = long long;
using namespace std;


void solve(){

}
int main (){
    ios::sync_with_stdio(0);
    cin.tie(0);
    char s;
    cin >> s;
    if(s=='A') cout << 'T';
    else if(s=='C') cout << 'G';
    else if(s=='G') cout << 'C';
    else if(s=='T') cout << 'A';

    return 0;
}

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

int main(){
    int n;
    cin >> n;
    int a[n],b[n];
    for(int i=0; i<n; i++){
        cin >> a[i];
    }
    for(int i=0; i<n; i++){
        cin >> b[i];
    }
    long long ans = 0;
    for(int k=28; k>=0; k--){
        int cnt = 0;
        int t = pow(2,k);
        for(int i=0; i<n; i++){
            a[i] %= 2*t;
            b[i] %= 2*t;
        }
        sort(b,b+n);
        for(int i=0; i<n; i++){
            cnt += lower_bound(b,b+n,2*t-a[i]) - lower_bound(b,b+n,t-a[i]);
            cnt += lower_bound(b,b+n,4*t-a[i]) - lower_bound(b,b+n,3*t-a[i]);
        }
        if(cnt%2) ans += pow(2,k);
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1792
import java.io.*;
import java.util.*;

public class Main {
  private static String solve(String[] ss) {
    String v = "" + ss[0].charAt(0) + ss[1].charAt(0) + ss[2].charAt(0);
    return v.toUpperCase();
  }
  
  private static void execute(ContestReader reader, PrintWriter out) {
    String[] ss = reader.next(3);
    out.println(solve(ss));
  }
  
  public static void main(String[] args) {
    ContestReader reader = new ContestReader(System.in);
    PrintWriter out = new PrintWriter(System.out);
    execute(reader, out);
    out.flush();
  }
}

class ContestReader {
  private BufferedReader reader;
  private StringTokenizer tokenizer;
  
  ContestReader(InputStream in) {
    reader = new BufferedReader(new InputStreamReader(in));
  }
  
  public String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }
  
  public int nextInt() {
    return Integer.parseInt(next());
  }
  
  public long nextLong() {
    return Long.parseLong(next());
  }
  
  public double nextDouble() {
    return Double.parseDouble(next());
  }
  
  public String[] next(int n) {
    String[] array = new String[n];
    for (int i = 0; i < n; i++) {
      array[i] = next();
    }
    return array;
  }
  
  public int[] nextInt(int n) {
    int[] array = new int[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextInt();
    }
    return array;
  }
  
  public long[] nextLong(int n) {
    long[] array = new long[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextLong();
    }
    return array;
  }
  
  public double[] nextDouble(int n) {
    double[] array = new double[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextDouble();
    }
    return array;
  }
  
  public char[] nextCharArray() {
    return next().toCharArray();
  }
  
  public int[][] nextInt(int n, int m) {
    int[][] matrix = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextInt();
      }
    }
    return matrix;
  }
  
  public long[][] nextLong(int n, int m) {
    long[][] matrix = new long[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextLong();
      }
    }
    return matrix;
  }
  
  public double[][] nextDouble(int n, int m) {
    double[][] matrix = new double[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextDouble();
      }
    }
    return matrix;
  }
  
  public char[][] nextCharArray(int n) {
    char[][] matrix = new char[n][];
    for (int i = 0; i < n; i++) {
      matrix[i] = next().toCharArray();
    }
    return matrix;
  }
}


Measured memory_bytes: 23124
c1 = list(map(int, input().split()))
c2 = list(map(int, input().split()))
c3 = list(map(int, input().split()))
b = [0]*3
b[0] = c1[0]
b[1] = c1[1]
b[2] = c1[2]
C = c1+c2+c3

a = [0]*3

a[0] = 0
a[1] = C[3] - b[0]
a[2] = C[6] - b[0]



z = 0
for x in a:
    for y in b:
        if C[z] != x + y:
            print("No")
            exit()
        z += 1

print("Yes")

Measured memory_bytes: 3064
def dfs(i, sum, count, nokori):
    global ans
    if i == d:
        # G 点に満たなければ nokori のうち一番大きいものを解く
        if sum < g:
            use = max(nokori)
            # 解く問題が問題数を超えないように注意
            n = min(pc[use - 1][0], -(-(g - sum) // (use * 100))) #ムダに-演算子を重ねているのは切り上げしたいから
            count += n
            sum += n * use * 100
        if sum >= g:
            ans = min(ans, count)
    else:
        # 総合スコア、解いた問題数、まだ解いてない問題を更新
        dfs(i + 1, sum, count, nokori)
        dfs(i + 1, sum + pc[i][0] * (i + 1) * 100 + pc[i][1], count + pc[i][0], nokori - {i + 1})
d, g = map(int, input().split())
pc = [list(map(int, input().split())) for i in range(d)]
ans = float("inf")
dfs(0, 0, 0, set(range(1, d + 1)))
print(ans)

Measured memory_bytes: 9144
//#include <algorithm>
//#include <cstdio>
//#include <functional>
//#include <bits/stdc++.h>
#include <iostream>
//#include <cfloat>
//#include <climits>
//#include <cstdlib>
#include <cstring>
//#include <cmath>
//#include <map>
//#include <queue>
//#include <set>
//#include <sstream>
//#include <stack>
//#include <string>
//#include <time.h>
//#include <vector>
//#include <random>
//#include <unordered_set>
//#include <complex>
#define rt0 return 0
using namespace std;
 
int main()
{
	string s;
	char c;
	int i;
	
	cin>>s;
	
	int len = s.size();
	if(len<26) {
		for(c='a';c<='z';c++) {
			for(i=0;i<len;i++) if(c==s[i]) break;
			if(i>=len) break;
		}
		s.push_back(c);//c一定存在，不用判断 
		cout<<s; 
		rt0;
	} 
	
	//len=26
	s.pop_back();
	while(s.size()>0) {
		int t=s.size()-1;
		for(c=s[t]+1;c<='z';c++) {
			for(i=0;i<s.size()-1;i++) if(c==s[i]) break;
			if(i>=(s.size()-1)) break;
		}
		s.pop_back();		
		if(c<='z') {//找到 
			s.push_back(c);
			break;
		}
	}
	if(s.size()==0) cout<<"-1";
	else cout<<s;
    rt0;
}


Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<numeric>
#include<map>
#include<iostream>
using namespace std;
#define rep(i,n) for(int i = 0;i<((int)(n));i++)
#define reg(i,a,b) for(int i = ((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i = ((int)(n)-1);i>=0;i--)
#define ireg(i,a,b) for(int i = ((int)(b));i>=((int)(a));i--)
typedef long long ll;
typedef pair<ll, ll> mp;

/*
実装には要注意
*/


//再帰タイプ
template<typename T_n>//使うときはsegment_tree<double> S(left,right,_default);のように型を指定する
struct segment_tree{
	typedef struct node_{
		T_n value;
		int left,right; //[left,right)
		struct node_ *left_child,*right_child;
	}* node;
	node parent_node = new node_;
	T_n default_value;//範囲外の場合の評価値
	T_n eval(T_n a, T_n b){//segment_treeの評価関数
		return min(a,b);
	}
	T_n back_propagation(T_n before, T_n after){//伝播する値を計算
		return min(before,after);
	}
	segment_tree(int left,int right,T_n _default){//どんな範囲でも作れる
		default_value = _default;
		make_segtree_(parent_node,left,right);
	};
	void make_segtree_(node p,int left,int right){
		p->left=left;
		p->right=right;
		p->value=default_value;
		if(left+1!=right){
			node q=new node_, r=new node_;
			p->left_child=q;
			p->right_child=r;
			make_segtree_(q,left,(left+right)/2);
			make_segtree_(r,(left+right)/2,right);
		}else{//要素数が1個
			p->left_child=NULL;
			p->right_child=NULL;
		}
	}
	T_n query(int left,int right){
		return query_(parent_node,left,right);
	}
	T_n query_(node p,int left,int right){
		if(p->right<=left || right<=p->left)return default_value;
		if(left<=p->left && p->right<=right)return p->value;
		return eval( query_(p->left_child,left,right), query_(p->right_child,left,right) );
	}
	void change(int pos,T_n value){
		change_(parent_node,pos,value);
	}
	T_n change_(node p,int pos,T_n value){
		//上に差分を伝播
		if(p->left<=pos && pos<p->right){//入っているなら
			//全て更新する
			if(p->left==pos && pos+1==p->right){//justなら
				p->value = value;
			}else{
				p->value = eval(change_(p->left_child,pos,value), change_(p->right_child,pos,value));
			}
		}
		return p->value;//差分が上に伝播する
	}
};

ll N,Q,c[100010],x[100010],y[100010];

int main(void){
	cin>>N>>Q;
	segment_tree<ll> f(0,N,(1ull<<31)-1);
	rep(i,Q){
		cin>>c[i]>>x[i]>>y[i];
	}
	rep(i,Q){
		if(c[i]==0){
			f.change(x[i],y[i]);
		}else{
			cout<<f.query(x[i],y[i]+1)<<endl;
		}
	}
	// rep(i,n){
	// 	cout<<f.query(i,i+2)<<endl;
	// }
	return 0;
}


Measured memory_bytes: 14840
#include <bits/stdc++.h>
#define rep(i, m, n) for(int i = m; i < (n); i++)
#define print(x) cout << (x) << endl;
#define printv(x) for(int i = 0; i < x.size(); i++){ cout << (x[i]) << " "; } cout << endl; 
#define printa(x,n) for(int i = 0; i < n; i++){ cout << (x[i]) << " ";} cout << endl;
#define printa2(x,m,n) for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ cout << x[i][j] << " ";} cout << endl;}
#define INF (1e9)
typedef long long ll;
typedef struct{
    int x;
    int y;
} P;

using namespace std;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    string W[110];
    cin >> N;
    rep(i,0,N){
        cin >> W[i];
    }
    bool flg = true;
    vector<string> ww;
    ww.push_back(W[0]);
    
    rep(i,0,N-1){
        if(W[i][W[i].size()-1] == W[i+1][0]){
            bool flg_tmp = true;
            rep(j,0,ww.size()){
                if(W[i+1] == ww[j]){
                    flg_tmp = false;
                }
            }
            if(!flg_tmp){
                flg = false;
                print("No");
                return 0;
            }else{
                ww.push_back(W[i+1]);
            }

        }else{
            flg = false;
            print("No");
            return 0;
        }
        

    }
    print("Yes");

}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdlib>
#include<set>

using namespace std;

set<set<int> > ans;

void dfs(const set<int> &c,const vector<int> &r){
  if(r.empty()){
    ans.insert(c);
  }else{
    for(int i=0;i<2;i++){
      int x=i?r.back():*c.rbegin()-r.back();
      vector<int> n=r;
      for(auto e:c){
	int d=abs(e-x);
	auto it=lower_bound(begin(n),end(n),d);
	if(it==end(n)||*it!=d)goto next;
	n.erase(it);
      }
      {      
	auto nc=c;
	nc.insert(x);
	dfs(nc,n);
      }
    next:
      ;
    }
  }
}

int main(){
  for(int N;cin>>N,N;){
    vector<int> d(N*(N-1)/2);
    for(int i=0;i<d.size();i++){
      cin>>d[i];
    }
    sort(begin(d),end(d));
    set<int> is{0,d.back()};
    d.pop_back();
    ans.clear();
    dfs(is,d);
    for(auto e:ans){
      vector<int> v(e.begin(),e.end());
      for(int i=0;i+1<v.size();i++){
	cout<<v[i+1]-v[i]<<" \n"[i+2==v.size()];
      }
    }
    cout<<"-----"<<endl;
  }
}

Measured memory_bytes: 1248
/*
_/_/_/_/    _/_/_/_/_/  _/_/_/
_/      _/      _/    _/      _/
_/      _/      _/    _/      _/
_/      _/      _/    _/      _/
_/      _/      _/    _/  _/  _/
_/      _/  _/  _/    _/    _/_/
_/_/_/_/      _/_/     _/_/_/_/_/

_/_/_/_/    _/    _/  _/      _/
_/      _/   _/  _/   _/_/  _/_/
_/      _/    _/_/    _/ _/_/ _/
_/      _/     _/     _/  _/  _/
_/      _/    _/_/    _/      _/
_/      _/   _/  _/   _/      _/
_/_/_/_/    _/    _/  _/      _/

_/_/_/_/_/ _/_/_/_/_/ _/_/_/_/_/
    _/         _/     _/
    _/         _/     _/
    _/         _/     _/_/_/_/
    _/         _/     _/
    _/         _/     _/
    _/     _/_/_/_/_/ _/_/_/_/_/

_/_/_/_/_/ _/_/_/_/_/ _/_/_/_/_/
    _/         _/     _/
    _/         _/     _/
    _/         _/     _/_/_/_/
    _/         _/     _/
    _/         _/     _/
    _/     _/_/_/_/_/ _/_/_/_/_/
*/
#include <atcoder/all>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cctype>

#include <algorithm>
#include <random>
#include <bitset>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <vector>
#include <chrono>
#include <iostream>
#include <limits>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

template <class T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& x : v)
    is >> x;
  return is;
}

template <class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  if (!v.empty()) {
    os << v.front();
    for (int i = 1; i < v.size(); ++i)
      os << ' ' << v[i];
  }
  return os;
}

using mint = atcoder::modint998244353;

int main() {
#ifdef ELEGIA
  freopen("test.in", "r", stdin);
  int nol_cl = clock();
#endif
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, k;
  cin >> n >> k;
  mint p = mint(k - 1) / k;

  atcoder::fenwick_tree<mint> fw(n), fw2(n);
  mint ans = 0;
  for (int i = 1; i < k; ++i) {
    ans += mint(i - 1) / 2;
    int pos; cin >> pos;
    fw.add(pos - 1, 1);
    fw2.add(pos - 1, 1);
  }
  for (int i = k; i <= n; ++i) {
    ans += mint(k - 1) / 2;
    int pos; cin >> pos;
    mint cp = p.pow(i - k);
    mint cnt = fw.sum(pos, n);
    mint psum = fw2.sum(pos, n) * cp;
    ans += cnt - psum;
    fw.add(pos - 1, 1);
    fw2.add(pos - 1, 1 / cp);
  }
  cout << ans.val() << '\n';

#ifdef ELEGIA
  LOG("Time: %dms\n", int ((clock()
          -nol_cl) / (double)CLOCKS_PER_SEC * 1000));
#endif
  return 0;
}


Measured memory_bytes: 4488
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

const long MOD = 1000000007;
typedef pair<int, int> P;
typedef long long ll;

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

    int r[R];
    for(int i=0; i<R; i++){
        cin >> r[i];
        r[i]--;
    }

    int dp[N][N];
    const int INF = 1000000005;
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){
            dp[i][j] = INF;
        }
        dp[i][i] = 0;
    }

    for(int i=0; i<M; i++){
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        dp[a][b] = c;
        dp[b][a] = c;
    }

    for(int k=0; k<N; k++){
        for(int i=0; i<N; i++){
            for(int j=0; j<N; j++){
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]);
            }
        }
    }

    sort(r, r+R);
    int ans = 1000000005;
    do{
        int t = 0;
        for(int i=1; i<R; i++){
            t += dp[r[i-1]][r[i]];
        }
        ans = min(ans, t);
    }while(next_permutation(r, r+R));

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 512
#include<stdio.h>
#include<stdlib.h>
int main(){
int i = 0;
int j = 0;
int k = 0;
int N;
int *A,v;
scanf("%d",&N);

A = (int *)malloc(sizeof(int) * N);
for(i=0;i<N;i++){
  scanf("%d",&A[i]);
}
printf("%d",A[0]);
for(k= 1;k<N;k++){
  printf(" %d",A[k]);
}
printf("\n");
// N個の要素を含む0-オリジンの配列
    for (i = 1; i < N;i++){

  v = A[i];
    j = i - 1;
    while (j >= 0 && A[j] > v){
      A[j+1] = A[j];
     j--;
   A[j+1] = v;
}

printf("%d",A[0]);
for(k= 1;k<N;k++){
  printf(" %d",A[k]);
}

printf("\n");
    }

return 0;
}



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

int main(){
    int N;
    cin >> N;
    vector<string> S(N);
    vector<ll> C(N);
    for (int i = 0; i < N; i++) {
        cin >> S[i] >> C[i];
    }
    
    vector<vector<pair<ll, ll>>> edge(N*40+1);
    for (int i = 0; i < N; i++) {
        int K = S[i].size();
        for (int j = 0; j < K; j++) {
            string ss = S[i].substr(K-j-1, j+1);
            ll K1 = j+1;
            for (int k = 0; k < N; k++) {
                ll K2 = S[k].size();
                string R = S[k];
                reverse(R.begin(), R.end());
                if (K1 < K2) {
                    if (R.substr(0, K1) == ss) {
                        edge[i*20+j].push_back({(k+N)*20+K2-K1-1, C[k]});
                    }
                }
                if (K1 > K2) {
                    if (ss.substr(0, K2) == R) {
                        edge[i*20+j].push_back({i*20+j-K2, C[k]});
                    }
                }
                string T = ss+S[k], U = T;
                reverse(U.begin(), U.end());
                if (T == U) {
                    edge[i*20+j].push_back({N*40, C[k]});
                }
            }
        }
    }
    
    for (int i = 0; i < N; i++) {
        int K = S[i].size();
        for (int j = 0; j < K; j++) {
            string ss = S[i].substr(0, j+1);
            ll K2 = j+1;
            for (int k = 0; k < N; k++) {
                ll K1 = S[k].size();
                string R = S[k];
                reverse(R.begin(), R.end());
                if (K1 < K2) {
                    if (R == ss.substr(K2-K1, K1)) {
                        edge[(i+N)*20+j].push_back({(i+N)*20+j-K1, C[k]});
                    }
                }
                if (K1 > K2) {
                    if (R.substr(K1-K2, K2) == ss) {
                        edge[(i+N)*20+j].push_back({k*20+K1-K2-1, C[k]});
                    }
                }
                string T = S[k]+ss, U = T;
                reverse(U.begin(), U.end());
                if (T == U) {
                    edge[(i+N)*20+j].push_back({N*40, C[k]});
                }
            }
        }
    }
    
    ll inf = 1e18;
    vector<ll> dist(N*40+1, inf);
    priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
    for (int i = 0; i < N; i++) {
        int K = S[i].size();
        dist[i*20+K-1] = C[i];
        pq.push({C[i], i*20+K-1});
        string T = S[i];
        reverse(T.begin(), T.end());
        if (S[i] == T) dist[N*40] = min(dist[N*40], C[i]);
    }
    while (!pq.empty()) {
        int now = pq.top().second;
        ll d = pq.top().first;
        pq.pop();
        if (d > dist[now]) continue;
        for (auto p : edge[now]) {
            int nxt = p.first;
            ll nd = d+p.second;
            if (dist[nxt] <= nd) continue;
            dist[nxt] = nd;
            pq.push({nd, nxt});
        }
    }
    
    if (dist[N*40] == inf) cout << -1 << endl;
    else cout << dist[N*40] << endl;
	return 0;
}


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

int main() {
  int h,w;
  cin>>h>>w;
  vector<vector<char>>s(h,vector<char>(w));
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++) cin>>s[i][j];
  }
  int ans=0;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      vector<vector<int>>distance(h,vector<int>(w));
      vector<vector<bool>>a(h,vector<bool>(w));
      a[i][j]=true;
      queue<int>q;
      q.push(i);
      q.push(j);
      if(s[i][j]=='.'){
        while(!q.empty()){
          int f=q.front();
          q.pop();
          int g=q.front();
          q.pop();
          if(f-1>=0){
            if(!a[f-1][g]&& s[f-1][g]=='.'){
            int c=distance[f][g]+1;
            ans=max(ans,c);
            distance[f-1][g]=c;
            a[f-1][g]=true;
            q.push(f-1);
            q.push(g);
            }
          }
          if(f+1<=h-1){
            if(!a[f+1][g] && s[f+1][g]=='.'){
            int d=distance[f][g]+1;
            ans=max(ans,d);
            distance[f+1][g]=d;
            a[f+1][g]=true;
            q.push(f+1);
            q.push(g);
            }
          }
          if(g-1>=0){
            if(s[f][g-1]=='.' && !a[f][g-1]){
            int e=distance[f][g]+1;
            ans=max(ans,e);
            distance[f][g-1]=e;
            a[f][g-1]=true;
            q.push(f);
            q.push(g-1);
            }
          }
          if(g+1<=w-1){
            if(!a[f][g+1] && s[f][g+1]=='.'){
            int k=distance[f][g]+1;
            ans=max(ans,k);
            distance[f][g+1]=k;
            a[f][g+1]=true;
            q.push(f);
            q.push(g+1);
            }
          }
        }
      }
    }
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 3632
#include <iostream>
#include <stdio.h>
#include <string>
#include <list>
#include <vector>
#include <algorithm>

#define rep(i,n) for(ll i = 0;i < n;i++)
#define REP(i,n,k) for(ll i = n;i < k;i++)
#define P(p) cout << (p) << endl;//print
typedef long long ll;
using namespace std;

int main(){
    cin.tie(0);
   	ios::sync_with_stdio(false);
    
    int a;
    cin >> a;
    cout << 48 - a << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main() {
  int64_t n,i,i1,i2,j1,j2,ans=0;
  cin>>n;
  vector<int64_t> p(n+1);
  map<int64_t,int64_t> m;
  set<int64_t> s={0,n+1};
  m[0]=0;
  m[n+1]=n+1;
  for(i=1;i<=n;i++){
    cin>>p.at(i);
    m[p.at(i)]=i;
    s.insert(i);
  }
  for(i=1;i<=n;i++){
    auto lb=s.lower_bound(m.at(i));
    i1=*prev(lb,1);
    if(i1==0) i2=0;
    else i2=*prev(lb,2);
    j1=*next(lb,1);
    if(j1==n+1) j2=n+1;
    else j2=*next(lb,2);
    s.erase(m.at(i));
    ans+=i*((i1-i2)*(j1-m.at(i))+(m.at(i)-i1)*(j2-j1));
  }
  cout<<ans<<endl;
}

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

int main() {
  int N, M, A=0, B;
  cin >> N >> M;
  vector<int> C(N, -1);
  rep(i, M){
    cin >> B;
    if(C.at(B-1)==-1) cin >> C.at(B-1);
    else{
      int D;
      cin >> D;
      if(C.at(B-1)!=D){
        A=-1;
        break;
      }
    }
    if(C.at(0)==0 && N!=1){
      A=-1;
      break;
    }
  }
  if(A==-1) cout << -1 << endl;
  else{
    rep(i, N){
      if(C.at(i)!=-1) cout << C.at(i);
      else if(i==0 && N!=1) cout << 1;
      else cout << 0;
    }
    cout << endl;
  }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, C=1;
  cin >> N;
  vector<long> A(N);
  for(int i=0; i<N; i++)
    cin >> A.at(i);
  
  sort(A.begin(), A.end());
  
  for(int i=0; i<N-1; i++){
    if( A.at(i) == A.at(i+1) )
        C=0;
  }
  if(C==1)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

Measured memory_bytes: 1792
#include<cstring>
#include<cstdio>
#include<cmath>
#include<memory.h>
#include<algorithm>
#include<map>
#define ll long long
#define ld long double
#define maxn 310
#define For(i,j,k)  for(ll i=j;i<=k;++i)
#define FOr(i,j,k)  for(ll i=j;i>=k;--i)
const ll inf=1e9;
const ld eps=1e-7;
using namespace std;
inline ll read(){   ll x=0,f=1;char ch=getchar();   while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}  while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}  return x*f; }
inline void wrote(ll x){    if (x<0) putchar('-'),x=-x; if (x>=10) wrote(x/10);   putchar(x%10+'0');  }
void writeln(ll x){ wrote(x);   puts("");   }
const ll N=100100,mod=1e9+7;
ll ans=1,sum[N],n;
int main(){
	n=read();
	For(i,1,n)	sum[read()]++;
	if (!(n&1)){
		for(ll i=1;i<n;i+=2)	if (sum[i]!=2)	ans=0;
	}else{
		For(i,1,n){
			if (!(sum[abs(i-1-(n-i))]--))	ans=0;
		}
	}
	For(i,1,n/2)	ans=ans*2%mod;
	writeln(ans);
}

Measured memory_bytes: 896
console.log((args=>{
  const [[Q,H,S,D],[N]] = args.trim().split`\n`.map(r=>r.split` `.map(v=>v|0));
  let p = 0n, n = BigInt(N);
  if ( Q*8 > D && H*4 > D && S*2 > D ) {
    p += BigInt(D) * BigInt(N>>1);
    n = BigInt(N&1);
    if ( !n ) return `${p}`;
  }
  if ( Q*4 > S && H*2 > S ) p += n*BigInt(S);
  else if ( Q*2 > H ) p+=n*2n*BigInt(H);
  else p+=n*4n*BigInt(Q);
  return `${p}`;
})(require('fs').readFileSync('/dev/stdin', 'utf8')));


Measured memory_bytes: 29552
#include <stdio.h>
int main()
{
	int a=0,b,c=0;
	for(int i=1;i<=3;i++)
	{
		scanf("%d",&b);
		if(b==5)
		{
			a++;
		}
		else if(b==7)
		{
			c++;
		}
	}
	if(a==2 && c==1)
	{
		printf("YES\n");
	}
	else
	{
		printf("NO\n");
	}
	return 0;
}

Measured memory_bytes: 1596
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define DEBUG true
#ifdef ONLINE_JUDGE
#undef DEBUG
#define DEBUG false
#endif 

using namespace std;

#define MAXN ((int)2e5+5)
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9 + 9)
#define ll long long
#define _ << " " <<
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define TRACE(x) if(DEBUG) cerr << #x << " = " << x << endl;
#define TRACE2(x,y) if(DEBUG) cerr << #x << " = " << x << " | " << #y << " = " << y << endl;
#define pb push_back
#define all(x) x.begin(), x.end()
#define endl "\n"
#define pii pair<int,int>
#define mid ((l+r)/2)

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	string s;
	int ct = 0,n;
	cin >> s;	
	n = s.size();

	for (int i = 0; i < n; ++i)
		ct += (s[i] != s[n - 1 - i]);

	cout << ct/2 << endl;
}

Measured memory_bytes: 256
N,K=map(int,input().split())
A=sorted(map(int,input().split()))
r=0
MOD=10**9+7
max_n=10**5
fac=[1]*(max_n+1)
inv=[1]*(max_n+1)
ifac=[1]*(max_n+1)
for n in range(2,max_n+1):
    fac[n]=(fac[n-1]*n)%MOD
    inv[n]=MOD-inv[MOD%n]*(MOD//n)%MOD
    ifac[n]=(ifac[n-1]*inv[n])%MOD
def comb(n,k):
    if n<k:
        return 0
    if n<0 or k<0:
        return 0
    return (fac[n]*ifac[k]*ifac[n-k])%MOD
for i,a in enumerate(A):
    r=(r+(comb(i,K-1)-comb(N-1-i,K-1))*a)%MOD
print(r)

Measured memory_bytes: 19152