akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define mod 998244353
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fll
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
const int maxn=1e5+10;
int a[maxn],b[maxn];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	for(int i=0;i<n;i++)
		scanf("%d",&b[i]);
	sort(b,b+n);
	ll ans=1;
	int prea=0,preb=0;
	int ca=0,cb=0;
	while(ca<n&&cb<n)
	{
		if(a[ca]<b[cb])
		{
			if(preb!=0)
			{
				ans=ans*1ll*preb%MOD;
				preb--;
			}
			else
			{
				prea++;
			}
			ca++;
		}
		else
		{
			if(prea!=0)
			{
				ans=ans*1ll*prea%MOD;
				prea--;
			}
			else
			{
				preb++;
			}
			cb++;
		}
	}
	while(ca<n)
	{
		ans=ans*1ll*preb%MOD;
		preb--;
		ca++;
	}
	while(cb<n)
	{
		ans=ans*1ll*prea%MOD;
		prea--;
		cb++;
	}
	printf("%lld\n",ans);
	return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
 
int checksumdigit(int n){
	int sum = 0;
  while(n > 0){
  	sum += n % 10;
    n /= 10;
  }
  return sum;
}

int main(){
  	int N,A,B;
	cin >> N >> A >> B;
  	int total = 0;
  	for(int i = 0; i <= N; i++){
    	int sum = checksumdigit(i);
      	if(sum >= A && sum <= B){
        	total += i;
        }
    }
  cout << total << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int N,M;
int A[100010];
pair<ll,int> op[100010];;
int main(){
    cin >> N >> M;
    for(int i=1;i<=N;i++) cin >> A[i];
    ll b; int c;
    for(int i=1;i<=M;i++){
        cin >> b >> c;
        op[i] = {c,b};
    }
    sort(op+1,op+M+1,greater<pair<ll,int>>());
    sort(A+1,A+N+1);
    int id = 1;
    for(int i=1;i<=M;i++){
        int len = op[i].second;
        ll ne = op[i].first;
        for(int j=1;j<=len;j++){
            if(A[id]<ne){
                A[id] = ne;
                id++;
                if(id>N) break;
            }else break;
        }
        if(id>N) break;
    }
    ll ans = 0;
    for(int i=1;i<=N;i++) ans += A[i];
    cout << ans << endl;
}

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

#define rep(i, n) for (int i = 0; i < n;i++)

int main(){

    int H, N,total=0;
    cin >> H >> N;
    vector<int> A(N);
    rep(i, N){
        cin >> A.at(i);
        total += A.at(i);
    }

    if(total>=H)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
    
}



Measured memory_bytes: 640
#include<iostream>
#include<string>
using namespace std;
int ctoi(char c) {
	int i; char ch = '0';
	for (i = 0; i < 10; i++){
		if (c == ch)return i;
		else ch++;
	}
	return -1;
}
int main() {
	string s; cin >> s;
	int ans1 = 0, ans2, anss = 0;
	for (int i = 0; i < s.size(); i += 2) {
		if (i == 0) { anss = ctoi(s[i]); ans2 = ctoi(s[i]); }
		else if (i % 2 == 0) {
			if (s[i - 1] == '+') {
				ans1 += anss;
				anss = ctoi(s[i]);
				ans2 += ctoi(s[i]);
			}
			else {
				anss *= ctoi(s[i]);
				ans2 *= ctoi(s[i]);
			}
		}
	}
	ans1 += anss;
	int v; cin >> v;
	if (ans1 == v) {
		if (ans2 == v)
			cout << "U\n";
		else
			cout << "M\n";
	}
	else {
		if (ans2 == v)
			cout << "L\n";
		else
			cout << "I\n";
	}
}

Measured memory_bytes: 3220
// lcmとか__builtin_popcountとかはg++ -std=c++17 default.cppみたいなかんじで

#include <bits/stdc++.h>
#define mod 1000000007
#define INF LLONG_MAX
#define ll long long
#define ln cout<<endl
#define Yes cout<<"Yes"<<endl
#define NO cout<<"NO"<<endl
#define YES cout<<"YES"<<endl
#define No cout<<"No"<<endl
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
using namespace std;
ll dx[4]={1,0,-1,0};
ll dy[4]={0,1,0,-1};

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

    ll  a,b,c,d,e,n,k,maxi=0,f=0,mini=INF,sum=0,q;
	string str,g,ko;
    ll x,y;
    char hoge;
    cin>>a>>b;
    if(a==b){
        cout<<"Draw"<<endl;
        return 0;
    }
    if(a>b){
        if(b!=1){
            cout<<"Alice"<<endl;
            return 0;
        }
    }
    if(a==1)            { cout<<"Alice"<<endl; return 0;}

    cout<<"Bob"<<endl;
    return 0;
}


Measured memory_bytes: 3620
#include<bits/stdc++.h>
using namespace std;
#define sd(x) scanf("%lld",&x)
#define slld(x) scanf("%lld",&x)
#define ss(x) scanf("%s",x)
#define mod 1000000007
#define bitcount __builtin_popcountll
#define ll long long
#define pb push_back
#define pi pair<ll,ll>
#define pii pair<pi,ll>
#define mp make_pair
vector<pii>edges;
vector<ll>v[100005],v2[100005];
ll dist1[100005],dist2[100005],ways1[100005],ways2[100005];
struct compare
{
	bool operator()(pii x, pii y)
	{
		return x.first.first>y.first.first;
	}
};
int main()
{
	ll i,j,k;
	ll n,m,s,t;
	sd(n);
	sd(m);
	sd(s);
	sd(t);
	while(m--)
	{
		sd(i);
		sd(j);
		sd(k);
		v[i].pb(j);
		v[j].pb(i);
		v2[i].pb(k);
		v2[j].pb(k);
		edges.pb(mp(mp(i,j),k));
	}
	for(i=1;i<=n;i++)
	{
		dist1[i]=dist2[i]=1e18;
	}
	priority_queue<pii,vector<pii>,compare>pq;
	pq.push(mp(mp(0,s),0));
	ways1[0]=ways2[0]=1;
	while(!pq.empty())
	{
		pii x=pq.top();
		pq.pop();
		if(dist1[x.first.second]==x.first.first)
		{
			ways1[x.first.second]+=ways1[x.second];
			if(ways1[x.first.second]>=mod)
				ways1[x.first.second]-=mod;
			continue;
		}
		if(dist1[x.first.second]!=1e18)
			continue;
		dist1[x.first.second]=x.first.first;
		ways1[x.first.second]+=ways1[x.second];
		if(ways1[x.first.second]>=mod)
			ways1[x.first.second]-=mod;
		for(i=0;i<v[x.first.second].size();i++)
		{
			if(dist1[v[x.first.second][i]]==1e18)
			{
				pq.push(mp(mp(dist1[x.first.second]+v2[x.first.second][i],v[x.first.second][i]),x.first.second));
			}
		}
	}
	pq.push(mp(mp(0,t),0));
	while(!pq.empty())
	{
		pii x=pq.top();
		pq.pop();
		if(dist2[x.first.second]==x.first.first)
		{
			ways2[x.first.second]+=ways2[x.second];
			if(ways2[x.first.second]>=mod)
				ways2[x.first.second]-=mod;
			continue;
		}
		if(dist2[x.first.second]!=1e18)
			continue;
		dist2[x.first.second]=x.first.first;
		ways2[x.first.second]+=ways2[x.second];
		if(ways2[x.first.second]>=mod)
			ways2[x.first.second]-=mod;
		for(i=0;i<v[x.first.second].size();i++)
		{
			if(dist2[v[x.first.second][i]]==1e18)
			{
				pq.push(mp(mp(dist2[x.first.second]+v2[x.first.second][i],v[x.first.second][i]),x.first.second));
			}
		}
	}
	ll cnt=0;
	for(i=1;i<=n;i++)
	{
		if(dist1[i]==dist2[i]&&dist1[t]==2*dist1[i])
		{
			int temp=(1ll*ways1[i]*ways2[i])%mod;
			cnt+=(1ll*temp*temp)%mod;
			if(cnt>=mod)
				cnt-=mod;
		}
	}
	// printf("%lld\n",cnt );
	for(i=0;i<edges.size();i++)
	{
		ll x=edges[i].first.first;
		ll y=edges[i].first.second;
		// printf("here2\n");
		if(dist1[x]<dist1[y]&&dist1[x]+dist2[y]+edges[i].second==dist1[t]&&2*dist1[x]<dist1[t]&&2*dist2[y]<dist2[s])
		{
			int temp=(1ll*ways1[x]*ways2[y])%mod;
			cnt+=(1ll*temp*temp)%mod;
			if(cnt>=mod)
				cnt-=mod;
		}
		else if(dist1[y]<dist1[x]&&dist1[y]+dist2[x]+edges[i].second==dist1[t]&&2*dist1[y]<dist1[t]&&2*dist2[x]<dist2[s])
		{
			int temp=(1ll*ways1[y]*ways2[x])%mod;
			cnt+=(1ll*temp*temp)%mod;
			if(cnt>=mod)
				cnt-=mod;
		}
	}
	ll temp=(ways1[t]*ways1[t])%mod;
	temp-=cnt;
	temp+=mod;
	if(temp>=mod)
		temp-=mod;
	printf("%lld\n",temp );
	return 0;
}

Measured memory_bytes: 31788
#include <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
typedef long long int ll;
using namespace std;

int main() {
	int n, m;
	cin >> n >> m;
	n--; m--;
	int ans = n* m;
	printf("%d\n", ans);
	return 0;
}


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

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline    cerr << __LINE__ << endl;

using ll = long long;
using ull = unsigned long long;
template<class T, class U> using P = pair<T,U>;
template<class T> using Heap = priority_queue<T>;
template<class T> using heaP = priority_queue<T,vector<T>,greater<T>>;
template<class T,class U> using umap = unordered_map<T,U>;
template<class T> using uset = unordered_set<T>;

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

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

template<class T, size_t N, class U>
void Fill(T (&a)[N], const U&v){
    fill((U*)a,(U*)(a+N),v);
}

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

int main(){
  int n; cin >> n;
  vector<int> v;
  v.push_back(0);
  int sum = 0;
  int np = 0;
  for(int i = 0; i < n; ++i){
    int x; cin >> x;
    v.push_back(x);
    sum += abs(x - np);
    np = x;
  }
  sum += abs(v.back());
  debug(sum);
  v.push_back(0);
  for(int i = 1; i <= n; ++i){
    if((v[i-1] - v[i] > 0) == (v[i] - v[i+1] > 0)){
      cout << sum << endl;
    } else {
      cout << sum - 2*min(abs(v[i-1]-v[i]),abs(v[i]-v[i+1])) << endl;
    }
  }
  
}


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

const int N = 100005;

using namespace std;
typedef long long ll;

#define forn(i, n) for (int (i) = 0; (i) != (n); (i)++)
#define all(x) (x).begin(), (x).end()


int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    int x = 0;
    for (int i = 1; ; i++)
    {
        x += i;
        if (x >= n) cout << i, exit(0);
    }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long i,x,n,m,s=1;
    cin>>n>>m;
    x=__gcd(n,m);
    for(i=2;i*i<=x;i++)
    {
        if(x%i==0)
        {
            s++;
            while(x%i==0)
            {
                x/=i;
            }
        }
    }
    if(x!=1)
    {
        s++;
    }
    cout<<s<<"\n";
    return 0;
}

Measured memory_bytes: 3640
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <random>
#include <climits>
#include <set>
#include <deque>
#include <queue>
#include <bitset>
#include <stack>
#include <regex>

#define mod 1000000007
#define INF2 9999999999
#define INF (1<<30)
#define rep(i,n) for(int (i)=0; (i)<(n); (i)++)
#define P pair<int, int>

using namespace std;
using ll = int64_t;

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

ll digit(ll n){
    ll res = 0;
    while(n > 9){
        res += n%10;
        n /= 10;
    }
    return res+n;
}

void solve(){
    ll n;
    cin >> n;
    if(n%digit(n) == 0){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
}

int main(){
    solve();
    return 0;
}

Measured memory_bytes: 256
N = int(input())
A = [int(x) for x in input().split()]

SUM = [0] * N
SUM[0] = A[0]
for i in range(1, N):
    SUM[i] = SUM[i - 1] + A[i]

left_min = right_min = ans = float('INF')
left_max = right_max = 0
p = 0
q = 1
r = N // 2
s = r + 1
for r in range(2, N - 1):
    # 左
    P1, Q1 = P2, Q2 = -1, -1
    low, high = 1, r - 1
    while low + 1 < high:
        mid = (low + high) // 2
        P = SUM[mid - 1]
        Q = SUM[r - 1] - SUM[mid - 1]
        if P < Q:
            low = mid
        else:
            high = mid
    P1, Q1 = SUM[low - 1], SUM[r - 1] - SUM[low - 1]
    P2, Q2 = SUM[high - 1], SUM[r - 1] - SUM[high - 1]
    left_min, left_max = min(P1, Q1), max(P1, Q1)
    if abs(Q1 - P1) > abs(Q2 - P2):
        left_min, left_max = min(P2, Q2), max(P2, Q2)

    # 右
    R1, S1 = R2, S2 = -1, -1
    low, high = r + 1, N - 1
    while low + 1 < high:
        mid = (low + high) // 2
        R = SUM[mid - 1] - SUM[r - 1]
        S = SUM[N - 1] - SUM[mid - 1]
        if R < S:
            low = mid
        else:
            high = mid
    R1, S1 = SUM[low - 1] - SUM[r - 1], SUM[N - 1] - SUM[low - 1]
    R2, S2 = SUM[high - 1] - SUM[r - 1], SUM[N - 1] - SUM[high - 1]
    right_min, right_max = min(R1, S1), max(R1, S1)
    if abs(R1 - S1) > abs(R2 - S2):
        right_min, right_max = min(R2, S2), max(R2, S2)

    # print(P1, Q1, R1, S1, sum([P1, Q1, R1, S1]), sum(A))
    # print(P2, Q2, R2, S2, sum([P2, Q2, R2, S2]), sum(A))
    # print("--------")
    ans = min(max(left_max, right_max) - min(left_min, right_min), ans)

print(ans)


Measured memory_bytes: 84228
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
//cout << setprecision(20) << 

int main() {
    ll N; cin >> N;
    map<ll, ll> m;
    ll sum = 0;

    // input
    for (int i = 0; i < N; ++i) {
        ll a; cin >> a;
        if (!m.count(a)) {
            m[a] = 0;
        }
        m[a]++;
        sum += a;
    }

    // query
    ll Q; cin >> Q;
    for (int i = 0; i < Q; ++i) {
        ll b, c; cin >> b >> c;
        if (m.count(b)) {
            ll appear_times = m[b];
            sum -= appear_times * b;
            m.erase(b);
            if (!m.count(c)) m[c] = 0;
            m[c] += appear_times;
            sum += appear_times * c;
            cout << sum << endl;
        }
        else {
            cout << sum << endl;
        }
    }
}

Measured memory_bytes: 7496
#include<stdio.h>
#include<math.h>

int isprime(int x);

int main(void){

int n;
	int a[10000];
	int i,num,j,sosu;
	scanf("%d", &n);
	for(i =0;i<n;i++){
		scanf("%d", &a[i]);
	}
	sosu =0;
	for(i =0 ;i<n;i++){
		if(isprime(a[i]) ==1){
			sosu++;}
	}
	
	printf("%d\n", sosu);
	return 0;
}

int isprime(int x){
	int i=0;
	if(x ==2){
		return 1;}
	if(x<2 || x%2==0){
		return 0;
	}
	i = 3;
	while(i <=sqrt(x)){
		if(x%i ==0){
			return 0;}
		i+=2;
	}
	return 1;
}


Measured memory_bytes: 2100
import sys
sys.setrecursionlimit(10**7)

n, q = map(int, input().split())
ans = [0 for _ in range(n)]
trees = [[] for _ in range(n)]

for _ in range(n-1):
    a, b = map(int, input().split())
    trees[a-1].append(b-1)
    trees[b-1].append(a-1)

for _ in range(q):
    p, x = map(int, input().split())
    ans[p-1] += x

visited = [False]*n
def dfs(n):
    visited[n] = True
    for i in trees[n]:
        if visited[i]:
            continue
        ans[i] += ans[n]
        dfs(i)
dfs(0)
for i in ans:
    print(i)



Measured memory_bytes: 221852
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge { int u, v; ll w; };
 
ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;
 
int main() {
	int N; cin >> N;
	vector<int> a(N);
	for (int i = 0; i < N; i++)
		cin >> a[i];
	int s; cin >> s;
	vector<int> dp(90001);
	dp[0] = 1;
	for (int i = 0; i < N; i++)
		for (int x = 90000; x >= 0; x--)
			if (dp[x])
				dp[x + a[i]] = (dp[x + a[i]] - dp[x]) % MOD;
	int sum = 0;
	for (int x = 0; x <= s; x++) {
		ll z = 1;
		for (int i = 0; i < N; i++)
			z = z * (s - x) % MOD;
		sum = (sum + dp[x] * z) % MOD;
	}
	cout << (sum + MOD) % MOD << endl;
}

Measured memory_bytes: 3100
import sys

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

if n == 1:
    s = "0"
    flag = True
    for j in l:
        if s[j[0]-1] != str(j[1]):
            flag = False
    if flag:
        print(0)
        sys.exit()

for i in range(10**(n-1), 10**n):
    s = str(i)
    flag = True
    for j in l:
        if s[j[0]-1] != str(j[1]):
            flag = False
    if flag:
        print(i)
        sys.exit()

print(-1)

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

//////全探索したいときに開けよ////////
vector<vector<int>> fspattern;
vector<int> fspattern_draft;
int fspcnt = 0;

void setfspattern_core(int floor,int number,int degree){
  if(floor==degree){
    fspattern.at(fspcnt) = fspattern_draft;
    fspcnt++;
  }else{
    rep(i,number){
      fspattern_draft.at(floor) = i;
      setfspattern_core(floor+1,number,degree);
    }
  }
}

void setfspattern(int number,int degree){
  int64_t array_num = 1;
  rep(i,degree){
    array_num *= number;
  }
  fspattern = vector<vector<int>>(array_num,vector<int>(degree));
  fspattern_draft = vector<int>(degree);
  setfspattern_core(0,number,degree);
}

//////nCrのmodを求めたいときに開けよ/////////
//int64_t univMod = 1000000007;
//int64_t factnum = 100000
//vector<int64_t> modfact(factnum);
//vector<int64_t> modinvfact(factnum);

/*void setmodfact(){
  for(int i=0;i<factnum;i++){
    if(i == 0){
      modfact.at(i) = 1;
    }else{
      modfact.at(i) = (modfact.at(i-1)*i)%univMod;
    }
  }
}

int64_t calcmodpower(int64_t a,int64_t n){
  int64_t res = 1;
  while(n != 0){
    if(n & 1){
      res = (res * a) % univMod;
    }
    a = (a*a) % univMod;
    n = n >> 1;
  }
  return res;
}

int64_t calcinverse(int64_t n){
	return calcmodpower(n,univMod-2);
}

void setmodinvfact(){
  for(int i=0;i<factnum;i++){
    if(i==0){
      modinvfact.at(i) = 1;
    }else{
      modinvfact.at(i) = calcmodpower(modfact.at(i),univMod-2);
    }
  }
}

int64_t calcmodcomb(int64_t n,int64_t r){
  return (((modfact.at(n)*modinvfact.at(r))%univMod)*modinvfact.at(n-r))%univMod;
}

int64_t calcmodpositive(int64_t n){
  return (n + ((abs(n)+univMod-1)/univMod)*univMod)%univMod;
}
  
    */



int64_t calcGCD(int64_t a,int64_t b){
	int64_t p = a;
	int64_t q = b;
	int64_t c = a%b;
	while(c != 0){
		p = q;
		q = c;
		c = p%q;
	}
	return q;
}


int main(){
  int a,b,h;
  cin >> a >> b >> h;
  cout <<((a+b)*h)/2 <<  endl;
  
  
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  int N, T;
  cin >> N;
  string S;
  cin >> S;
  T = 0;
  for (int s=0;s<1000;s++){
    int tmp = s;
    string t = "";
    for (int i=0;i<3;i++){
      int j = tmp % 10;
      t = t + to_string(j);
      tmp /= 10;
    }
    int l = 0;
    for (int m=0;m<N;m++){
      if (S.at(m) == t.at(l)){
        l += 1;
      }
      if (l == 3){
        break;
      }
    }
    if (l >= 3){
      T += 1;
    }
  }
  cout << T << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fastIO (cin.tie(0), cout.tie(0), ios::sync_with_stdio(false))
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define precise(i) fixed << setprecision(i)
using li = long long int;
using uli = unsigned long long int;
using namespace std;

int main() {
    fastIO;
    int n, m;
    cin >> n >> m;
    while (m--) {
        int day;
        cin >> day;
        n -= day;
    }
    if (n < 0)
        cout << -1;
    else
        cout << n;
}

Measured memory_bytes: 3624
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(500000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc=[list(map(int,input().split())) for i in range(q)]

lst=[0]*(10**5+1)
ans=0
for i in range(n):
    lst[a[i]]+=1
    ans+=a[i]
# print(ans)

for i in range(q):
    b,c=bc[i]
    bcnt=lst[b]
    ans+=(c-b)*bcnt
    lst[c]+=bcnt
    lst[b]=0
    print(ans)

Measured memory_bytes: 91236
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int (i)=0;i<(n);i++)
#define REP(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define INF INT_MAX
#define MOD 1000000007
#define fcout cout << fixed << setprecision(15)
#define int long long
#define yorn(f) puts((f)?"Yes":"No")
#define YORN(f) puts((f)?"YES":"NO")
#define sec second
#define fir first
typedef long long ll;
typedef pair<int, int> P;
typedef priority_queue<int> pque;
typedef vector<int> Vec;
int gcd(int a,int b){return b?gcd(b,a%b):a;};
int lcm(int a,int b){return a/gcd(a,b)*b;};
int mod(int a,int b){return (a+b-1)/b;};
template<typename A, size_t N, typename T>
void Fill(A(&array)[N],const T &val){std::fill((T*)array,(T*)(array+N),val);}
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;};
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;};



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

	int n, a, b;
	cin >> n >> a >> b;
	int ans = 0;
	ans += (n / (a + b)) * a;
	ans += min(a, n % (a + b));
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
from collections import deque

K=int(input())
q=deque()
for i in range(1,10):
    q.append(str(i))

while K>0:
    n=q.popleft()
    t=int(n[-1])
    K-=1

    for i in range(max(0,t-1),min(9,t+1)+1):
        q.append(n+str(i))

print(n)

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

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> ii;
typedef tuple<ll, ll, ll> iii;
typedef vector<ll> vi;
typedef vector<ii> vii;
typedef vector<iii> viii;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
#define REP(i,n) for (ll i = 0; i < n; ++i)
#define REPR(i,n) for (ll i = n-1; i >= 0; --i)
#define FOR(i,m,n) for (ll i = m; i < n; ++i)
#define FORR(i,m,n) for (ll i = n-1; i >= m; --i)
#define FORE(x,xs) for (const auto& x : xs)
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define ALL(v) v.begin(), v.end()

const int MAX = 1e5+10;

int N;
int H[MAX];

string solve() {
    REPR (i, N-1) {
        if (H[i] > H[i+1]) H[i]--;
        if (H[i] <= H[i+1]) continue;
        return "No";
    }
    return "Yes";
}

int main() {
    cin >> N;
    REP (i, N) cin >> H[i];
    cout << solve() << endl;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
static const int INF = 1e9+7;
#define REP(i,n) for (int i = 0; i <(n); ++i)
//debug 
#ifdef _DEBUG
#define debug(var)  do{cout << #var << " :";view(var);}while(0)
#else
#define debug(...)
#endif
template<typename T> void view(T e){cout << e << endl;}
template<typename T> void view(const vector<T>& v){for(const auto& e : v){ cout << e << " "; } cout << endl;}
template<typename T> void view(const vector<vector<T> >& vv){ for(const auto& v : vv){ view(v); } }


int main(){
  string s;
  cin >> s;
  int n = s.size();
  n -= 2;

  cout << *s.begin() << n << *s.rbegin() << endl;
  return 0;

}

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

int main() {
  int n , a;
  cin >> n >> a;
  
  int b = n%500;
  
  if ( b <= a ) {
    cout << "Yes" <<endl;
  }
  
  else{
    cout << "No" <<endl;
  }
}

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

#define testc int t;cin>>t;while(t--)
#define endl "\n"
#define rep(i,a,b) for(int i=a;i<b;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define repi(i,n) for(int i=0;i<n;++i)
#define REPI(i,n) for(int i=0;i<=n;++i)

# define C continue;
# define R return

# define D double
# define I insert
# define ll long long
# define ld long double
# define ull unsigned long long

# define pb push_back
# define vi vector < int >
# define vc vector < char >
# define vs vector < string >
# define vb vector < bool >
# define vd vector < D >
# define vll vector < ll >
# define vld vector < ld >
# define vull vector < ull >

# define vvi vector < vector < int > >
# define vvb vector < vector < bool > >
# define vvc vector < vector < char > >
# define vvll vector < vector < ll > >
# define vvd vector < vector < D > >

# define all(v) (v).begin() , (v).end()
# define allrev(v) (v).rbegin() , (v).rend()

# define pii pair < int , int >
# define vpii vector < pii >
# define vvpii vector < vector < pii > >
# define F first
# define S second
# define mp make_pair

# define dist(a,b,p,q) sqrt((p-a)*(p-a) + (q-b)*(q-b));
# define mod 1000000007


//Declare needed variables
int n,k;

void scan_var(){
    cin>>n>>k;
}

int INF = 1e9 + 5;

void solve(){
    vi v(n);
    repi(i,n){
        cin>>v[i];
    }
    vi cost(n,INF);
    cost[0] = 0;
    rep(i,1,n){
        for(int j=i-1;j>=i-k;--j){
            //cout<<"* ";
            if(j>=0){
                cost[i] = min(cost[i],cost[j] + abs(v[j]-v[i]));
            }
        }
    }
    cout<<cost[n-1]<<endl;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    scan_var();
    solve();
    return 0;
}

Measured memory_bytes: 1024
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		A(sc);
		sc.close();
	}
	
	public static void A(Scanner sc) {
		int a = sc.nextInt();
		int b = sc.nextInt();
		System.out.println((a+b+1)/2);
	}
	
	public static void B(Scanner sc) {
		
	}
}


Measured memory_bytes: 21716
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
ll f(ll n){
  int ret = 0;
  while(n){
    ret += n % 10;
    n /= 10;
  }
  return ret;
}
double g(ll n){
  return (double)n / f(n);
}
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll K;
  cin>>K;
  // 候補を全列挙
  vector<ll> v;
  ll ten = 1;
  FOR(i,0,15){
    FOR(j,1,150){
      v.push_back(j*ten-1);
    }
    ten *= 10;
  }
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  FOR(i,1,v.size()){
    bool ok = true;
    FOR(j,i+1,v.size()){
      if(g(v[i]) > g(v[j])){
        ok = false;
        break;
      }
    }
    if(ok) {
      cout << v[i] << endl;
      K--;
      if(K==0) return 0;
    }
  }
}

Measured memory_bytes: 384
#![allow(unused_imports)]
// use itertools::Itertools;
use proconio::{input, marker::*};

fn main() {
    input! {
        a: usize,
        b: usize
    };
    let num = format!("{}{}", a, b).parse::<usize>().unwrap();
    for i in 1..=num {
        if i * i == num {
            println!("Yes");
            return;
        }
    }
    println!("No");
}


Measured memory_bytes: 2140
import sys
input = sys.stdin.readline
sys.setrecursionlimit(int(1E+7))

N, Q = map(int, input().split())
link = [[] for _ in range(N+1)]
for n in range(N-1):
    a, b = map(int, input().split())
    link[a].append(b)
    link[b].append(a)
V = [0] * (N+1)
ans = [0] * (N+1)
for q in range(Q):
    p, x = map(int, input().split())
    V[p] += x

def dfs(i, parent, acc):
    V[i] += acc
    for j in link[i]:
        if j != parent:
            dfs(j,i,V[i])
cur = 1
parent = 0
acc = 0
dfs(cur, parent , acc)
V = [str(v) for v in V]
print (" ".join(V[1:]))




Measured memory_bytes: 228912
#include <bits/stdc++.h>
#define ll long long 
using namespace std;
const ll mod = 1e9+7;
const ll nax = 1e5+7;
const ll INF = 1e18;

ll dp[nax]={0},n,m,vis[nax]={0};
vector<ll> edge[nax];

void DFS(ll s){
	vis[s]=true;
	
	
	for (auto it: edge[s]){
		if (!vis[it])	DFS(it);

		dp[s]=max(dp[s],dp[it]+1);
	}
}

int main(){
//	freopen("input.inp","r",stdin);
//	freopen("output.out","w",stdout);
	
	ios_base::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	
	cin>>n>>m;
	
	for (ll i=0;i<m;++i){
		ll a,b;
		cin>>a>>b;
		
		edge[a].push_back(b);
	}
	
	for (ll i=1;i<=n;++i) if (!vis[i]) DFS(i);
	
//	for (ll i=1;i<=n;++i) cout<<dp[i]<<" ";
//	cout<<"\n";
	
	cout<<*max_element(dp+1,dp+n+1);
}

Measured memory_bytes: 13320
#include <bits/stdc++.h>
#include <math.h>
#define ALL(a)    a.begin(), a.end() 
#define rep(i, K, N)  for(long long int i = K; i < N; i++)
#define ll long long int
using namespace std;

int main(){
ll N, Ans = 1000000000, K;cin >> N >> K;
bool C = 0;
vector<ll> n;
vector<ll> p{0};

rep(i, 0, N){

    ll a;cin >> a;

    if(a == 0)continue;

    if(a > 0)C = 1;

    if(C)p.push_back(a);

    else n.push_back(a);

}

n.push_back(0);
reverse(ALL(n));
  
if(p == n && p.size() == 1){cout << 0;exit(0);}
  
//for(auto q : n)cout << q << endl;
  
int A = K - n.size() + 1, B = K, D = p.size() - 1;
  

for(ll i = max(A, 0); i <= min(B, D); i++){

    Ans = min(Ans, min(2*p[i] + abs(n[K - i]), 2*abs(n[K - i]) + p[i]));


}

cout << Ans;

}

Measured memory_bytes: 4504
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define uli unsigned long long int
#define INF 999999999999999999
#define rep(i,m,n) for(lli i = m;i < n;i++)
#define rrep(i,m,n) for(lli i=m-1;i>=n;i--)
#define pb(n) push_back(n)
#define UE(N) N.erase(unique(N.begin(),N.end()),N.end());
#define Sort(n) sort(n.begin(), n.end())
#define Rev(n) reverse(n.begin(),n.end())
#define Out(S) cout << S << endl
#define NeOut(S) cout << S
#define HpOut(S) cout << setprecision(25) << S << endl
#define Vec(K,L,N,S) vector<L> K(N,S)
#define DV(K,L,N,M,S) vector<vector<L>> K(N,vector<L>(M,S))
#define TV(K,L,N,M,R,S) vector<vector<vector<L>>> K(N,vector<vector<L>>(M,vector<L>(R,S)))
#define pint pair<lli,lli>
#define paf(L,R) pair<L,R>
#define mod 1000000007
#define MAX 5100000
#define ALL(a)  a.begin(),a.end()
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  lli A,B,C,D,E,F,N,M,K,L,X,Y,Z,H,W,sum=0,num=0,flag=0;string S,T;
  cin >> N;
  Vec(P,bool,N,1);
  sum=N*(N+1)/2;
  rrep(i,N,0)if(sum>N&&sum-i-1>=N){
    sum-=(i+1);
    P[i]=0;
  }
  rep(i,0,N)if(P[i])Out(i+1);
}
  

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

typedef long long ll;
typedef tuple<ll,ll,ll> tlll;

int main() {
    int T; cin >> T;
    while (T--) {
        int N; cin >> N;
        ll ans = 0;
        vector<vector<int>> vl(N+1);
        vector<vector<int>> vr(N+1);
        for (int i = 0; i < N; ++i) {
            ll k, l, r; cin >> k >> l >> r;
            if (l > r) {
                ans += r;
                vl[k].push_back(l-r);
            } else if (l < r) {
                ans += l;
                vr[k].push_back(r-l);
            } else {
                ans += l;
            }
        }
        priority_queue<int, vector<int>, greater<int>> pl;
        for (int i = 1; i <= N; ++i) {
            for (int x : vl[i]) {
                pl.push(x);
            }
            while (pl.size() > i) pl.pop();
        }
        while (!pl.empty()) {
            ans += pl.top();
            pl.pop();
        }
        priority_queue<int, vector<int>, greater<int>> pr;
        for (int i = N; 1 <= i; --i) {
            for (int x : vr[i]) {
                pr.push(x);
            }
            while (pr.size() > N-i) pr.pop();
        }
        while (!pr.empty()) {
            ans += pr.top();
            pr.pop();
        }
        cout << ans << endl;
    }
}

Measured memory_bytes: 18144
#include<iostream>
#include<algorithm>
#include<vector>
#define LL long long
using namespace std;
int main() {
	int N;
	cin >> N;
	vector<int> line(N * 3);
	for (int i = 0; i < N * 3; ++i) {
		cin >> line[i];
	}
	sort(line.begin(), line.end());
	LL ans = 0;
	for (int i = N; i < N * 3; i += 2) {
		ans += line[i];
	}
	cout << ans << endl;
	return 0;
}

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

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define FOR(i, n) for(int (i)=0; (i)<(n); (i)++)
#define FORI(i, n) for(int (i)=n-1; (i)>=0; (i)--)
#define FOR1(i, n) for(int (i)=1; (i)<=(n); (i)++)
//#define TEST

int main(int argc, char** argv) {
    //ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout << setprecision(15);
    if (argc == 2 && atoi(argv[1]) == 123456789) {
        freopen("d:\\code\\cpp\\stdin", "r", stdin);
        //freopen("d:\\code\\cpp\\stdout", "w", stdout);
    }

    int N, M;
    cin >> N >> M;
    vector<vector<int>> ss(M);
    vector<int> p(M);
    FOR(i, M){
        int ki;
        cin >> ki;
        ss[i].resize(ki);
        FOR(j, ki)
            cin >> ss[i][j];
    }
    FOR(i, M)
        cin >> p[i];

    int sol = 0;
    int n = 1<<N;
    FOR(mask, n){
        vector<int> lights(N, 0);
        FOR(j, N)
            if (mask & 1<<j)
                lights[j] = 1;
        bool allon = true;
        FOR(j, M){
            int sum = 0;
            for(int s : ss[j]){
                sum += lights[s-1];
            }
            allon &= (sum%2 == p[j]);
        }
        if (allon)
            sol++;
    }
    cout << sol << '\n';

    if (argc == 2 && atoi(argv[1]) == 123456789)
        cerr << clock()*1.0/CLOCKS_PER_SEC << " sec\n";
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef long double ld;
 
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
 
#define pb push_back
#define fi first
#define se second
 
const ll MOD = 1000000007;

int main() {
   int n; scanf("%d", &n);
   vector<int> v(n);
   for (int& i : v)
      scanf("%d", &i);
   sort(v.begin(), v.end());
   double ans = (v[0] + v[1]) / 2.;
   for (int i = 2; i < n; ++i)
      ans = (ans + v[i]) / 2;
   printf("%f", ans);
   return 0;
}

Measured memory_bytes: 256
N = int(input())
P = list(map(int,input().split()))

ans = 0

# 各部分列の最小値
minval = max(P)
# for i in range(N):
for x in P:
    # arr = P[0],...,P[i]の最小値
    minval = min(minval,x)

    # P[i]が最小値以下なら
    if x <= minval:
        ans += 1
    
print(ans)

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

struct  info{
    int x;
    int y;
    int point;

    info(){
        x = y = point = 0;
    }
};

int main() {

    int h,w;

    cin >> h >> w;

    bool s[h][w];
    bool moved[h][w];

    int white_num = 0;

    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            char c;
            scanf(" %c",&c);
            s[i][j] = false;
            if(c == '.'){
                s[i][j] = true;
                white_num++;
            }
            moved[i][j] = false;
        }
    }

    info now_info;

    queue<info> que;

    que.push(now_info);

    info ans;



    while(!que.empty()){

        now_info = que.front();
        que.pop();

        //cout << now_info.x << " " << now_info.y << " " << now_info.point << endl;

        int posx = now_info.x, posy = now_info.y, point = now_info.point;

        //moved[posy][posx] = true;

        point++;


        if(posx+1 != w && s[posy][posx + 1] && !moved[posy][posx + 1]){//right
            now_info.x = posx + 1;
            now_info.y = posy;
            now_info.point = point;

            moved[now_info.y][now_info.x] = true;

            if(posy == h-1 && posx + 1 == w-1){
                ans = now_info;
                break;
            }
            que.push(now_info);
        }

        if(posx-1 != -1 && s[posy][posx - 1] && !moved[posy][posx-1] ){//left

            now_info.x = posx - 1;
            now_info.y = posy;
            now_info.point = point;

            moved[now_info.y][now_info.x] = true;

            /*if(posy == h-1 && posx - 1 == w-1){
                ans = now_info;
                break;
            }*/
            que.push(now_info);
        }

        if(posy + 1 != h && s[posy + 1][posx] && !moved[posy + 1][posx]){//down
            now_info.x = posx;
            now_info.y = posy + 1;
            now_info.point = point;

            moved[now_info.y][now_info.x] = true;

            if(posy + 1 == h-1 && posx == w-1){
                ans = now_info;
                break;
            }
            que.push(now_info);
        }

       if(posy - 1 != -1 && s[posy - 1][posx] && !moved[posy - 1][posx]){//up

            now_info.x = posx;
            now_info.y = posy - 1;
            now_info.point = point;

           moved[now_info.y][now_info.x] = true;

            /*if(posy - 1 == h-1 && posx == w-1){
                ans = now_info;
                break;
            }*/

            que.push(now_info);
        }

    }

    ans.point++;

    if(ans.point == 1){
        white_num = 0;
    }

    cout << white_num - ans.point << endl;

    return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

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

                String N = scan.next();

                int sum = 0;

                for (int i = 0; i < N.length(); i++) {
                	sum += Integer.parseInt(N.substring(i,i+1));
                }

                if (sum%9 == 0) {
                	System.out.println("Yes");
                } else {
                	System.out.println("No");
                }

                scan.close();

        }

}

Measured memory_bytes: 49700
#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<queue>
#include<stack>
#include<cmath>
#include<list>
#include<set>
#include<map>

using namespace std;
template<class T>
T gcd(T x, T y) {
	return y ? gcd(y, x % y) : x;
}

template<class T>
vector<T> enum_div(T n) {
	vector<T> ret;
	for (T i = 1; i * i < n; ++i) {

		if (n % i == 0) {
			ret.push_back(i);

			if (i != 1 && i * i != n) {
				ret.push_back(n / i);
			}
		}
	}
	return ret;
}

template<class T>
map<T, int> prime_factorization(T n) {
	map<T, int> ret;

	for (long long i = 2; i * i <= n; ++i) {
		while (n % i == 0) {
			ret[i]++;
			n /= i;
		}
	}

	if (n != 1)ret[n] = 1;
	return ret;
}

long long MOD = 1000000007LL;
long long dp[100005][13];

int main() {
	string s;
	cin >> s;
	int n = s.length();
    dp[0][0] = 1;
	for (int i = 0; i < n; ++i) {
		int digit;
		if (s[i] == '?')digit = -1;
		else digit = s[i] - '0';
		for (int j = 0; j < 10; ++j) {
			if (digit != -1 && digit != j)continue;
			for (int ki = 0; ki < 13; ++ki) {
				dp[i + 1][(ki * 10 + j) % 13] += dp[i][ki];
			}
		}
		for (int j = 0; j < 13; ++j)dp[i + 1][j] %= MOD;
	}

	cout << dp[n][5] << endl;
	return 0;
}

Measured memory_bytes: 10624
//agc018_a.cpp
//Sun Sep 30 10:24:06 2018

#include <iostream>
#include <string>
#include <queue>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <math.h>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll=long long;
typedef pair<int,int> P;

ll gcd (ll a, ll b){ //最大公約数
	if (b == 0){
		return a;
	}
	return gcd(b, a%b);
}

int main(){
	ll n,k;
	cin >> n >> k;
	ll a[n];
	ll amax = 0;
	ll gcdnum = 0;
	for (int i=0;i<n;i++){
		cin >> a[i];
		if (amax < a[i]){
			amax = a[i];
		}
		if (i==0){
			gcdnum = a[0];
		}else if (i>=1){
			gcdnum = gcd(gcdnum,a[i]);
		}
	}
//	cout << "gcdnum is "<< gcdnum << endl; 
	if (amax >= k && k%gcdnum==0){
		cout << "POSSIBLE" << endl;
	}else {
		cout << "IMPOSSIBLE" << endl;
	}
}

Measured memory_bytes: 1024
//include
//------------------------------------------
#include <bits/stdc++.h>
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> P;
typedef long long 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);
const long long INF = 1000000007;
//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;
// chmax chmin
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main(void){
    string s; cin >> s;
    int a=0,z=s.size()-1;
    while(s[a]!='A') a++; while(s[z]!='Z') z--;
    cout << z-a+1 << endl;
    return 0;
}

Measured memory_bytes: 640
/*
javac Main.java && java Main
*/

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
	public static void main(String[] args) { new Main(); }
	FS in = new FS();
	PrintWriter out = new PrintWriter(System.out);

	int n, p;
	char[] s;
	long x;

	Main() {
		n = in.nextInt();
		p = in.nextInt();
		s = in.next().toCharArray();

		x = 0;
		if (p == 2 || p == 5) {
			for (int i = 0; i < n; i++)
				if ((s[i] - '0') % p == 0)
					x += i + 1;
		}
		else {
			int t = 1, y = 0, z = 0;
			HashMap<Integer, Integer> mp = new HashMap<>();
			mp.put(0, 1);
			for (int i = n - 1; i >= 0; i--) {
				z = s[i] - '0';
				y = (y + t * z) % p;

				if (mp.containsKey(y)) x += mp.get(y);
				mp.putIfAbsent(y, 0);
				mp.put(y, 1 + mp.get(y));

				t = (t * 10) % p;
			}
		}
		out.println(x);
		out.close();
	}

	
	
	int abs(int x) { if (x < 0) return -x; return x; }
	long abs(long x) { if (x < 0) return -x; return x; }
	int max(int x, int y) { if (x < y) return y; return x; }
	int min(int x, int y) { if (x > y) return y; return x; }
	long max(long x, long y) { if (x < y) return y; return x; }
	long min(long x, long y) { if (x > y) return y; return x; }
	int gcd(int x, int y) { while (y > 0) { x = y^(x^(y = x)); y %= x; } return x; }
	long gcd(long x, long y) { while (y > 0) { x = y^(x^(y = x)); y %= x; } return x; }
	long lcm(int x, int y) { return ((long) x) * (y / gcd(x, y)); }
	long lcm(long x, long y) { return x * (y / gcd(x, y)); }

	class FS {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens()) {
				try { st = new StringTokenizer(br.readLine()); }
				catch (Exception e) {}
			} return st.nextToken();
		}
		int nextInt() { return Integer.parseInt(next()); }
		long nextLong() { return Long.parseLong(next()); }
		double nextDouble() { return Double.parseDouble(next()); }
	}
}


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

int main(){

int a,b;
char op;

for(;;){

cin>>a>>op>>b;

if(op == '?') break;

int ans;
switch(op) {
case '+': ans = a + b; break;
case '-':ans = a - b; break;
case '*':ans = a * b; break;
case '/':ans = a / b; break;
}
cout<<ans<<endl;
}
return 0;

}


Measured memory_bytes: 3080
#include <iostream>
#include <cstdio>

using namespace std;



//?´???°????????¢??°

int isPrime(int n)
{
	unsigned int i;
  
	if (n < 2)			// ??\??????2??????????????°???????????¨??§false
		return 0;
	else if (n == 2)	// ??\??????2????????°???????????¨??§True
		return 1;
  
	if (n % 2 == 0)		//??\??????2??§???????????????????????°True
		return 0;
  
	for (i = 3; i <= n / i; i += 2)	//????????\???????´???°??????
		if (n % i == 0)
			return 0;
	return 1;

}
int main()
{
	unsigned int a;			//?´???°??????????????°???????????°
	unsigned int b;			//?´???°??????????????°???
	int pcnt = 0;			//?´???°?????°
	
	cin >> a;				// ?´???°?????????????????°???????????°?????\???
	for(int i=0; i < a; i++)// ??°????????? ?????¶ ??\?????° < ??°???????????°
	{
		cin >> b;			// ?´???°??????????????°???(b)?????\?????????
		if(isPrime(b) == 1) // ?´???°??????(True or False)
		{
			pcnt++;			// ?´???°????????§?????????????????????
		}
	}
	
	cout << pcnt << endl;	// ?´???°?????°???????????????

}

Measured memory_bytes: 3088
#include<stdio.h>

int gcd(int a,int b){
    int r,tmp;

  /* 自然数 a > b を確認・入替 */
  if(a<b){
    tmp = a;
    a = b;
    b = tmp;
  }
 
  /* ユークリッドの互除法 */
  r = a % b;
  while(r!=0){
    a = b;
    b = r;
    r = a % b;
  }
    return b;
  }

int main(void){

int K,i,j,k,sum=0;
scanf("%d",&K);


for(i=1;i<=K;i++){
  for(j=1;j<=K;j++){
    for(k=1;k<=K;k++){
      int x=gcd(i,j);
      int y=gcd(x,k);
      sum+=y;
    }
  }
}
printf("%d",sum);

  return 0;
}

Measured memory_bytes: 1732
/********************************************
 *Author*        :ZZZZone
 *Created Time*  : 一  3/26 10:54:23 2018
 * Ended  Time*  : 一  3/26 11:03:05 2018
*********************************************/

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;

inline void OPEN(string s){
    freopen((s + ".in").c_str(), "r", stdin);
    freopen((s + ".out").c_str(), "w", stdout);
}

int main()
{
    //OPEN("arc92-d");
    int n;
    scanf("%d", &n);
    vector<int> a(n), b(n);
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    for(int i = 0; i < n; i++) scanf("%d", &b[i]);
    vector<int> c, d;
    int ans = 0;
    int pow_2 = 1;
    for(int i = 0; i <= 28; i++){
        c = a, d = b;
        for(int j = 0; j < n; j++){
            c[j] %= 2 * pow_2;
            d[j] %= 2 * pow_2;
        }
        sort(c.begin(), c.end());
        sort(d.begin(), d.end());
        int l, r, sum = 0;
        for(int j = 0; j < n; j++){
            l = lower_bound(d.begin(), d.end(), pow_2 - c[j]) - d.begin();
            r = lower_bound(d.begin(), d.end(), 2*pow_2 - c[j]) - d.begin() - 1;
            sum += (r-l+1);
            l = lower_bound(d.begin(), d.end(), 3*pow_2 - c[j]) - d.begin();
            r = lower_bound(d.begin(), d.end(), 4*pow_2 - c[j]) - d.begin() - 1;
            sum += (r-l+1);
        }
        if(sum & 1) ans += pow_2;
        pow_2 *= 2;
    }
    printf("%d\n", ans);
    return 0;
}


Measured memory_bytes: 3328
//半径 r の円の面積と円周の長さを求めるプログラム
#include <stdio.h>
#include <math.h>

int main(void)
{
    //変数定義
    double dRadius;
    double dVolume;
    double dCircleLength;

    //変数の初期化
    dRadius = 0;
    dVolume = 0;
    dCircleLength = 0;

    scanf("%lf",&dRadius);
    dVolume = dRadius * dRadius * M_PI;
    dCircleLength = 2 * M_PI * dRadius;
    printf("%.6f %.6f\n",dVolume,dCircleLength); 

    return 0;
}


Measured memory_bytes: 2044