akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> l_l;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
#define fi first
#define se second
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
const int INF=1001001000;
const int mINF=-1001001000;
const ll LINF=1010010010010010000;
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;
}
//グリッド：（典型）dp,dfs,bfs,最短経路,その他
int k;
vl a(110000);
ll s,e;
int main(){
    cin>>k;
    rep(i,k) cin>>a[i];
    s=2,e=2;
    int idx=k-1;
    bool ng=false;
    if(s%a[idx]!=0){
        cout<<-1<<endl;
        return 0;
    }
    else{
        while(1){
            e+=a[idx]-1;
            idx--;
            if(idx<0) break;
            ll cnt=e/a[idx]-s/a[idx];
            if(s%a[idx]==0){
                cnt++;
            }
            else s=(s/a[idx]+1)*a[idx];
            if(cnt==0){
                ng=true;
                break;
            }
            e=(e/a[idx])*a[idx];
        }
    }
    if(ng) cout<<-1<<endl;
    else cout<<s<<" "<<e<<endl;
    return 0;
}

Measured memory_bytes: 1152
n, k = map(int, input().split())
s = input()

if s[k-1]=="A":
    print(s[:k-1]+"a"+s[k:])
elif s[k-1]=="B":
    print(s[:k-1]+"b"+s[k:])
else:
    print(s[:k-1]+"c"+s[k:])

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    int div = 1;
    int mul = 1;
    int m = n;
    for (int i = 2; i * i <= n; i++) {
        bool f = true;
        while (n % i == 0) {
            if (f) {
                div *= i;
                mul *= i-1;
                f = false;
            }
            n /= i;
        }
    }
    if (n != 1) div *= n, mul *= n-1;
    cout << (long long) m * mul / div << endl;
}


Measured memory_bytes: 3084
import collections
n=int(input())
a=list(map(int,input().split()))
b=collections.Counter(a)
c=b.most_common()
x=0
for i in range(len(c)):
  if c[i][0]>c[i][1]:x+=c[i][1]
  elif c[i][0]<c[i][1]:x+=c[i][1]-c[i][0]
print(x)

Measured memory_bytes: 21232
s=input()
if s=="AKIHABARA":
    print("YES")
else:
    ans="YES"
    if len(s)>=9 or "KIH" not in s:
        ans="NO"
    else:
        t=s.replace("A","")
        if t!="KIHBR":
            ans="NO"
        else:
            k,b,r=s.find("K"),s.find("B"),s.find("R")
            if k>1 or b-k>4 or r-b>2:
                ans="NO"
    print(ans)

Measured memory_bytes: 3064
import Foundation

extension String {
    func toIntList() -> [Int] {
        return self.split(separator: " ").map{Int($0)!}
    }
    
    func toList() -> [String] {
        return self.map{String($0)}
    }
    
    func count(c:Character) -> Int{
        var result = 0

        for e in self{
            if e == c{
                result += 1
            }
        }
        return result
    }
}

extension Int {
    func toBin() -> String {
        return String(self, radix: 2)
    }
    
    func toD() -> Double {
        return Double(self)
    }
}

extension Double {
    func toI() -> Int {
        return Int(self)
    }
}

extension Array where Element: Equatable{
    func count(n:Self.Element) -> Int{
        var result = 0
        for e in self{
            if e == n{
                result += 1
            }
        }
        return result
    }
}


func disco2020b() -> Int {
    let N = Int(readLine()!)!
    let A = readLine()!.toIntList()
    var left = 0
    var right = N-1
    var diff = 0
    while left <= right {
        if diff < 0 {
            diff += A[left]
            left += 1
        }else{
            diff -= A[right]
            right -= 1
        }
    }
    return abs(diff)
}

print(disco2020b())


Measured memory_bytes: 26380
import java.util.Scanner;

 class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        long[] h=new long[n];
        for(int i=0;i<n;i++)
            h[i]=in.nextLong();
        int[] a = new int[n];
        for(int i=1;i<n;i++){
            if(h[i-1]-h[i] >=0)
                a[i]=0;
            else
                a[i]=-1;
        }
        int ans=0;
        for(int i=1;i<n;){
            boolean inside=false;
            if(a[i]==0){
                inside=true;
                int count=0;
                while(i<n && a[i]==0){
                    count++;
                    i++;
                }
                ans=Math.max(ans, count);
            }
            if(!inside)
                i++;

        }
        System.out.println(ans);

    }
}


Measured memory_bytes: 49896
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	long long x,y,z,m,n;
	scanf("%lld %lld %lld",&x,&y,&z);
	if(1 <= x && 1 <= y && 1 <= z && 1e5 >= x && 1e5 >= y && 1e5 >= z && y + 2*z <= x)
	{
    m = y + z;
    n = (x-z)/m;
    printf("%lld",n);
    }
    else printf("wrong");
    return 0;

}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll mod = 1000000000+7;
const int MaxSize = 100005;
int cycle = 0;
int keep[1000] = {};
bool ok = true;
int white = 1;
int black = 2;
bool prime[MaxSize] = {};
bool findPrime(int n)
{
    bool ok = true;
    for(int i = 2; i <= sqrt(n); i++)
    {
        if(n % i == 0)
        {
            ok = false;
            return false;
        }
    }
    return true;
}
int main()
{
    int n;
    char str[100006] = {};
    cin >> n;
    cin >> str;
    for(int i = 0; i < strlen(str); i++)
    {
        char tem = str[i];
        int vv = tem -'A'+n;
        vv = vv % 26;
        char tt = vv + 'A';
        str[i] = tt;
    }
    cout << str << endl;
    return 0;
}


Measured memory_bytes: 384
#include<iostream>
#include<vector>
#define ll long long int
using namespace std;

int main(){
    int n,m,denger_id;
    vector<int> a;
    vector<ll> step;

    cin>>n>>m;
    a.resize(m+100);
    for(int i=0; i<m; i++){
        cin>>a[i];
    }
    step.resize(n+100);
    for(int i=0; i<n+1; i++){
        step[i]=0;
    }

    step[0]=1;
    denger_id=0;
    if(a[denger_id]==1){
        step[1]=0;
        denger_id++;
    }else{
        step[1]=1;
    }

    for(int i=2; i<n+1; i++){
        if(i==a[denger_id]){
            denger_id++;
        }else{
            step[i]=(step[i-1]+step[i-2])%1000000007;
        }
    }
    cout<<step[n]%1000000007<<endl;
    return 0;
}

Measured memory_bytes: 1408
#include<stdio.h>
int main(void)
{
  int a, b, c;
  scanf("%d", &a);
  scanf("%d", &b);
  scanf("%d", &c);
  if(a==b){
    printf("%d", c);
  }else if(a==c){
    printf("%d", b);
  }else{
    printf("%d", a);
  }
  return 0;
}

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

int main(){
	int n;
	cin >> n;
	int a;
	int min, max;
	long sum;
	cin >> a;
	min = a;
	max = a;
	sum += a;
	for(int i = 1; i < n; i++){
		cin >> a;
		if(min > a){
			min = a;
		}else if(max < a){
			max = a;
		}
		sum += a;
	}
	cout << min << " " << max << " " << sum << endl;
}

Measured memory_bytes: 1168
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> VI;
typedef vector<VI> VVI;
typedef pair<ll, ll> P;
#define FOR(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define rep(i,n)  FOR(i,0,n)
#define PRINT(V) for (auto v : (V)) cout << v << " "
#define ALL(n) begin(n),end(n)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
const ll INF = numeric_limits<ll>::max();
int main()
{
    int n; cin >> n;
    if (n>=30) {cout << "Yes" << endl;}
    else {cout << "No" << endl;}
    
    return 0;
}

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

int G[110][110];
bool seen[110];
int d[110];
int f[110];
int N;
int t = 1;

void dfs( int s ){
    seen[s] = 1;
    d[s] = t++;
    for( int i = 0; i < N; i++ ){
        if( G[s][i] && !seen[i] ){
            dfs( i );
        }
    }
    f[s] = t++;
}

int main(){
    cin >> N;
    for( int i = 0; i < N; i++ ){
        int u, k; cin >> u >> k;
        u--;
        for( int j = 0; j < k; j++ ){
            int v; cin >> v;
            v--;
            G[u][v] = 1;
        }
    }

    for( int i = 0; i < N; i++ ){
        if( !seen[i] ){
            dfs( i );
        }
    }

    for( int i = 0; i < N; i++ ){
        cout << i + 1 << " " << d[i] << " " << f[i] << endl;
    }
}


Measured memory_bytes: 3084
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <numeric>
    
using namespace std;    
typedef long long ll;
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

int type_check(string &s1, string &s2, int n){
    if(s1[n] == s2[n]){
        return 1; //縦置き
    }
    else{
        return 2; //横置き
    }
}

int main(){
    
    int N;
    cin >> N ;
    
    string s1, s2;
    cin >> s1;
    cin >> s2;

    int t_now  = type_check(s1, s2 , 0);
    int t_last = t_now;

    ll ans = 1;
    int start = 0;
    if(t_now == 1){
        start = 1;
        ans = 3;
    } 
    else{
        start = 2; 
        ans = 6;
    }

    for(int i = start; i < N; true){
        t_now = type_check(s1, s2, i);
        if(t_now == 1 && t_last == 1){
            ans *= 2;
            ans %= 1000000007;
        }
        if(t_now == 2 && t_last == 1){
            ans *= 2;
            ans %= 1000000007;
        }
        if(t_now == 1 && t_last == 2){
            ans *= 1;
            ans %= 1000000007;
        }
        if(t_now == 2 && t_last == 2){
            ans *= 3;
            ans %= 1000000007;
        }
        t_last = t_now;
        i += t_now;
    }

    cout << ans << endl;

}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<queue>
#include<set>
using namespace std;
typedef long long LL;
const int N=3010;
int gi() {
	int w=0;bool q=1;char c=getchar();
	while ((c<'0'||c>'9') && c!='-') c=getchar();
	if (c=='-') q=0,c=getchar();
	while (c>='0'&&c <= '9') w=w*10+c-'0',c=getchar();
	return q? w:-w;
}
int s[N],deg[N],q[N];
bool win[N];
int head[N],nxt[N],to[N];
int main()
{
	int n=gi(),i,a,b,tot=0,l=0,r=0,k;
	for (i=1;i<=n;i++) s[i]=gi();
	for (i=1;i<n;i++) {
		a=gi(),b=gi();
		if (s[a]>s[b]) swap(a,b);
		if (s[a]<s[b])
			deg[to[++tot]=b]++,nxt[tot]=head[a],head[a]=tot;
	}
	for (i=1;i<=n;i++) if (!deg[i]) q[++r]=i;
	while (l!=r)
		for (i=head[k=q[++l]];i;i=nxt[i]) {
			win[to[i]]|=!win[k];
			if (!--deg[to[i]]) q[++r]=to[i];
		}
	for (i=1;i<=n;i++) if (win[i]) printf("%d ",i);
	return 0;
}


Measured memory_bytes: 384
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define ll long long
#define mod 998244353
#define inv2 499122177
void Read(int &p)
{
	p = 0;
	char c = getchar();
	for (; c < '0' || c > '9'; c = getchar());
	for (; c >= '0' && c <= '9'; c = getchar())p = p * 10 + c - '0';
}
int Add(int a, int b)
{
	int c = a + b;
	if (c >= mod)
		c -= mod;
	return c;
}
int Sub(int a, int b)
{
	int c = a - b;
	if (c < 0)
		c += mod;
	return c;
}
int Pow(int a, int k)
{
	if (!k)
		return 1;
	int p = Pow(a, k / 2);
	if (k & 1)
		return (ll)p * p % mod * a % mod;
	return (ll)p * p % mod;
}
int A[1 << 18], B[1 << 18], C[1 << 18], D[1 << 18];
int n, s;
void FWT(int *A, int w)
{
	if (w == 1)
		return;
	FWT(A, w >> 1);
	FWT(A + (w >> 1), w >> 1);
	for (int i = 0; i < (w >> 1); i++)
	{
		int x = A[i], y = A[i + (w >> 1)];
		A[i] = Add(x, y);
		A[i + (w >> 1)] = Sub(x, y);
	}
}
void IFWT(int *A, int w)
{
	if (w == 1)
		return;
	IFWT(A, w >> 1);
	IFWT(A + (w >> 1), w >> 1);
	for (int i = 0; i < (w >> 1); i++)
	{
		int x = A[i], y = A[i + (w >> 1)];
		A[i] = (ll)Add(x, y) * inv2 % mod;
		A[i + (w >> 1)] = (ll)Sub(x, y) * inv2 % mod;
	}
}
int main()
{
	Read(n);
	for (int i = 0; i < (1 << n); i++)
		Read(D[i]), s = Add(s, D[i]);
	s = Pow(s, mod - 2);
	for (int i = 0; i < (1 << n); i++)
		A[i] = D[i] = (ll)D[i] * s % mod;
	B[0] = (1 << n) - 1;
	for (int i = 1; i < (1 << n); i++)
		B[i] = mod - 1;
	A[0] = Sub(A[0], 1);
	FWT(A, 1 << n);
	FWT(B, 1 << n);
	int x = 0, p = 0;
	for (int i = 0; i < (1 << n); i++)
	{
		if (A[i] == 0)
			x = i;
		C[i] = (ll)Pow(A[i], mod - 2) * B[i] % mod;
	}
	IFWT(C, 1 << n);
	p = (ll)Sub(0, C[0]) * (1 << n) % mod;
	FWT(C, 1 << n);
	C[x] = p;
	IFWT(C, 1 << n);
	for (int i = 0; i < (1 << n); i++)
		printf("%d\n", C[i]);
}


Measured memory_bytes: 7836
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <cstdlib>
#include <algorithm>
#include <cmath>
using namespace std;


int main(){
int X;
cin >> X;
int n = 0;

for(int i = 1; i <= X; i++){
    int x = i;
    for(int j = 0; j <= X; j++){
         x = x * i;
    if(x <= X)n = max(n,x);
    
    }
}


cout << n << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, l, r) for (int i = l; i<r;i++)
#define vi vector<int>
#define all(v) v.begin(),v.end()
#define pb(c) push_back(c)
#define vii vector<pair<int,int>>
#define ii pair<int,int>
#define max(a, b) (((a)<(b))?b:a)
#define min(a, b) ((a>b)?b:a)
#define mp(i, j) make_pair(i,j)
#define ull unsigned long long int
#define ll  long long int
#define pi 3.141592653589793238
#define inf ((ll)1e18)
#define eps 1e-14
#define maxlg 18
#define left(i) ((i)<<1)
#define right(i) (((i)<<1)+1)
#define maxn 2006
int a,b;
int d[26][26];
int f[105][105];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#ifndef ONLINE_JUDGE
 #endif
    cin>>a>>b;
    rep (i,0,a)
        rep (j,0,b)
            cin>>d[i+1][j+1];

    rep (i,0,100+1)
        rep (j,0,100+1)
            rep (x,1,a+1)
                rep (y,1,b+1) {
                    f[i][j] = max(f[i][j],d[x][y]-i*x-j*y);
                }

    rep (x,1,a+1)
        rep (y,1,b+1) {
            int dist = 1e9;
            rep (i,0,100+1)
                rep (j,0,100+1) {
                    dist= min(dist,f[i][j]+i*x +j*y);
                }
            if (dist!=d[x][y]){
                cout<<"Impossible";
                return 0;
            }
        }
    cout<<"Possible\n";
    vector<string>  ans;
    rep (i,1,101) {
        ans.push_back(to_string(i)+ " "+to_string(i+1)+ " X\n");
        rep (j,102,203) {
            ans.push_back(to_string(i)+ " "+to_string(j)+" "+ to_string(f[i-1][202-j])+"\n");
        }
    }
    rep (i,102,202) {
        ans.push_back(to_string(i)+ " "+to_string(i+1)+ " Y\n");
    }
    cout<<202<<" "<<ans.size()<<endl;
    rep (i,0,ans.size()) {
        cout<<ans[i];
    }
    cout<<1<<" "<<202;
    return 0;


}

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



int main(){
    ll n,m;cin>>n>>m;
    vector<pair<ll,ll> > p(m);
    rep(i,0,m){
        ll a,b;cin>>a>>b;
        p[i]=make_pair(b,a);
    }
    sort(p.begin(),p.end());
    ll last=p[0].first,cn=1;
    rep(i,1,m){
        if(p[i].second>=last){
            cn++;
            last=p[i].first;
        }
    }
    cout<<cn<<endl;
}
    









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

using namespace std;
typedef long long ll;

int N;
vector<int> vec[100010];

int d[100010];
int d2[100010];
const int INF = 1 << 30;

void bfs(){
    queue<int> que;
    for(int i = 0; i < N; ++i){
        d[i] = INF;
    }
    d[0] = 0;
    que.push(0);

    while(!que.empty()){
        int p = que.front();
        que.pop();
        for(int i = 0; i < vec[p].size(); ++i){
            int j = vec[p][i];
            if(d[j] == INF){
                d[j] = d[p] + 1;
                que.push(j);
            }
        }
    }
    queue<int> que2;
    for(int i = 0; i < N; ++i){
        d2[i] = INF;
    }
    d2[N - 1] = 0;
    que2.push(N - 1);

    while(!que2.empty()){
        int p = que2.front();
        que2.pop();
        for(int i = 0; i < vec[p].size(); ++i){
            int j = vec[p][i];
            if(d2[j] == INF){
                d2[j] = d2[p] + 1;
                que2.push(j);
            }
        }
    }
}

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

    cin >> N;
    for(int i = 0; i < N - 1; ++i){
        int a, b;
        cin >> a >> b;
        --a;
        --b;
        vec[a].push_back(b);
        vec[b].push_back(a);
    }

    bfs();

    int sum = 0;
    int sum2 = 0;
    for(int i = 1; i < N - 1; ++i){
        //cout << d[i] << " " << d2[i] << endl;
        if(d[i] <= d2[i]){
            ++sum;
        }
        else {
            ++sum2;
        }
    }
    /*cout << sum << endl;
    cout << sum2 << endl;*/

    if(sum > sum2){
        cout << "Fennec" << endl;
    }
    else {
        cout << "Snuke" << endl;
    }

    return 0;
}

Measured memory_bytes: 6656
import Control.Applicative
import Control.Monad
import Text.Printf

divition :: [Int] -> IO ()
divition [a, b] = let divnum = a `div` b
                      remainder = a `mod` b
                      doublenum = realToFrac a / realToFrac b
                  in printf "%d %d %.5f" divnum remainder (doublenum :: Double)

main = do
    line <- getLine
    divition $ read <$> words line



Measured memory_bytes: 4072
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string>
 
int main()
{
    std::string s;
    std::cin >> s;

    std::cout << s[0] << s.length() - 2 << s[s.length() - 1];
    
    return 0;
}

Measured memory_bytes: 256
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

N, M = map(int, input().split())

if N >= M // 2:
    print(M // 2)
else:
    a = N
    M -= N * 2
    a += M // 4
    print(a)

Measured memory_bytes: 3700
import java.util.Scanner;
  
class Main {
    static int[] aArrays = new int[20];
    static int gIndex = 0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            aArrays[i] = sc.nextInt();
            gIndex++;
        }
        int q = sc.nextInt();
        int[] mArrays = new int[200];
        int index = 0;
        for (int i = 0; i < q; i++) {
            mArrays[i] = sc.nextInt();
            index++;
        }
          
        for (int i = 0; i < index; i++) {
            if (isMatch(0, 0, mArrays[i])) {
                System.out.println("yes");
            } else {
                System.out.println("no");
            }
        }
          
    }
      
    public static boolean isMatch(int j, int S, int T) {
            if (S == T) {
                return true;
            }
            for (int i = j; i < gIndex; i++) {
                if (isMatch(i + 1, S + aArrays[i], T)) {
                    return true;
                }
            }
            return false;
        }
}

Measured memory_bytes: 26732
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;


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

		int n = sc.nextInt();
		boolean[][] a = new boolean[n][n];

		for (int i = 0; i < n; i++) {
			int u = sc.nextInt() - 1;
			int k = sc.nextInt();
			for (int j = 0; j < k; j++) {
				a[u][sc.nextInt() - 1] = true;
			}
		}

		int[] d = new int[n];
		int[] f = new int[n];
		boolean[] used = new boolean[n];
		int clock = 1;
		for (int j = 0; j < n; j++) {
			if (used[j]) {
				continue;
			}

			Deque<Integer> st = new ArrayDeque<Integer>();
			st.push(j);

			while (!st.isEmpty()) {
				int e = st.peek();

				if (!used[e]) {
					used[e] = true;
					d[e] = clock;

					for (int i = n - 1; i >= 0; i--) {
						if (used[i]) {
							continue;
						}
						if (a[e][i]) {
							st.push(i);
						}
					}
				} else {
					st.pop();
					if (f[e] != 0) {
						continue;
					}
					f[e] = clock;
				}

				clock++;
			}
		}

		for (int i = 0; i < n; i++) {
			System.out.printf("%d %d %d\n", i + 1, d[i], f[i]);
		}

		sc.close();
	}
}

Measured memory_bytes: 29504
#include<bits/stdc++.h>
#define ll long long

using namespace std;

const int N=2e5+5;

int n,freq[N];

int main()
{
    cin >> n;
    int x;
    for (int i=0;i<n-1;i++)
    {
        cin >> x;
        freq[x]++;
    }
    for (int i=1;i<=n;i++)
    {
        cout << freq[i] << endl;
    }
    return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;

int main(){
    int r,c;
    cin >> r >> c;
    
    int A[101][101];
    for(int i=0;i<=r;i++) A[i][c]=0;
    for(int i=0;i<=c;i++) A[r][i]=0;
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            cin >> A[i][j];
            A[i][c]+=A[i][j];
            A[r][j]+=A[i][j];
            A[r][c]+=A[i][j];
        }
    }

    for(int i=0;i<=r;i++){
        for(int j=0;j<=c;j++){
            cout << A[i][j] << (j==c?"\n":" ");
        }
    }
    
    return 0;
}


Measured memory_bytes: 3144
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9+7;
using LL = long long;
const LL LINF = 1e18;
const double EPS = 1e-10;
using namespace std;
#define COUT(v) cout<<(v)<<endl
#define CIN(n)  int(n);cin >> (n)
#define LCIN(n) LL(n);cin >> (n)
#define SCIN(n) string(n);cin >> (n)
#define YES(n) cout<<((n)? "YES" : "NO")<<endl
#define Yes(n) cout<<((n)? "Yes" : "No")<<endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) <<endl

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

#define FOREACH(x,a) for(auto& (x) : (a) )

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

#define P pair<int,int>
#define I vector<int>
#define pb(v) push_back(v)
#define V vector
#define rt return
#define rmsame(a) sort(ALL(a)),a.erase(unique(ALL(a)), a.end())
#define ENDL cout<<endl
#define MAXE(a,b) a=max(a,b)
#define MINE(a,b) a=min(a,b)

typedef string::const_iterator State;
class PalseError {};
class Edge{
public:
    int from,to,value;
    Edge(LL a,LL b,LL c){
        from = a;
        to = b;
        value = c;
    }
    Edge(LL a,LL b){
        from = a;
        to = b;
    }
};

int main(){
    CIN(N);
    CIN(A);CIN(B);
    I vec;
    int i = 0,j = 0,k = 0;
    REP(a,N){
        CIN(b);
        if(b <= A){
            i++;
        }
        if(A+1 <= b && b <= B){
            j++;
        }
        if(B + 1 <= b){
            k++;
        }
    }
    cout<<min({i,j,k})<<endl;
    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int h = 10;
        int w = 10;

        for (int i = 1; i < h; i++) {
            for (int j = 1; j < w; j++) {
                System.out.println(i + "x" + j + "=" + (i * j));
            }
        }
    }
}



Measured memory_bytes: 25572
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("avx") 
#include<bits/stdc++.h>
using namespace std;

inline int read()
{
	int ret=0; char c=getchar();
	while(c<48||c>57)c=getchar();
	while(c>=48 && c<=57)ret=ret*10+c-48,c=getchar();
	return ret;
}
#define N 6006
int L[N],vi[N],qn,n,m,p,cnt[N],c[N][3],tot,a[N],u,v,w;
int q[9000009];
long long D[N];

void dfs(int p)
{
	vi[p]=1;
	for(int o=a[p]; o; o=c[o][1]){
		if(vi[c[o][0]])continue;
		dfs(c[o][0]);
	}
}

int main()
{
	n=read(); m=read(); p=read();
	for(int i=1;i<=m;++i){
		u=read(); v=read(); w=read();
		c[++tot][0]=v; c[tot][1]=a[u]; a[u]=tot; c[tot][2]=p-w; 
	} 
	for(int i=1;i<=n;++i)D[i]=1e9; D[1]=0;
	
	for(int i=1;i<=n;++i){
		dfs(i);if(!vi[n])L[i]=1;
		for(int i=1;i<=n;++i)vi[i]=0;
	}
	
	if(L[1]){
		puts("-1");
		return 0;
	}
	
	q[qn=1]=1;
	for(int q1=1;q1<=qn;++q1){
		u=q[q1]; vi[u]=0;
		for(int o=a[u]; o; o=c[o][1]){
			v=c[o][0];
			if(L[v])continue;
			if(D[u]+c[o][2]<D[v]){
				D[v]=D[u]+c[o][2];
				if(!vi[v]){
					++cnt[v];
					if(cnt[v]>n){
						puts("-1");return 0;
					}
					q[++qn]=v;
					vi[v]=1;
				}
			}
		}
	}
	if(!cnt[n]){
		puts("-1");
		return 0;
	}
	printf("%lld\n",max(0ll,-D[n]));
}

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

int main() {
    int n;
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    unordered_map<int, int> mp;
    for (int x: a) {
        mp[x]++;
    }
    sort(a, a + n);
    int ans = 0;
    for (int i = n - 1; i >= 0; i--) {
        int x = a[i];
        if (mp[x] == 0)
            continue;
        mp[x]--;
        int s = 1;
        for (; s <= x; s <<= 1) {}
        int cmpl = s - x;
        if (mp.find(cmpl) != mp.end() && mp[cmpl] > 0) {
            mp[cmpl]--;
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 9288
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <fstream>
#include <cassert>
#include <cstring>
#include <unordered_set>
#include <unordered_map>
#include <numeric>
#include <ctime>
#include <bitset>
#include <complex>

using namespace std;

#define int long long

const int INF = 1e18 + 239;

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

	int n, k;
	cin >> n >> k;
	vector<int> a(n);
	vector<int> f(n);
	for (auto &t : a) {
		cin >> t;
	}
	for (auto &t : f) {
		cin >> t;
	}
	sort(a.begin(), a.end());
	sort(f.rbegin(), f.rend());
	auto can = [&](int x) {
		int c = 0;
		for (int i = 0; i < n; i++) {
			c += max((int)0, a[i] - x / f[i]);
			c = min(c, INF);
		}
		return c <= k;
	};
	int l = -1;
	int r = INF;
	while (r - l > 1) {
		int m = (r + l) / 2;
		if (can(m)) r = m;
		else l = m;
	}
	cout << r << endl;
}


Measured memory_bytes: 3456
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    x=int(input())
    for i in range(-300,300):
        for j in range(-300,300):
            if i**5-j**5==x:
                print(i,j)
                sys.exit()

if __name__=='__main__':
    main()

Measured memory_bytes: 9176
import java.util.*;
 
public class Main {

    private static final long MOD = 998244353;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] num = new int[N+10];
        int maxDist = 0;
        boolean valid = true;
        for (int i = 0; i < N; i++) {
            int dist = sc.nextInt();
            if (i == 0 && dist != 0) {
                valid = false;
            }
            num[dist]++;
            maxDist = Math.max(maxDist, dist);
        }
        if (!valid || num[0] != 1) {
            // invalid
            System.out.println(0);
            return;
        }
        for (int i = 0; i <= maxDist; i++) {
            if (num[i] == 0) {
                // invalid
                System.out.println(0);
                return;
            }
        }

        long ans = 1;
        int i = 0;
        while (num[i] > 0) {
            int a = i > 0 ? num[i-1] : 1;
            int n = num[i];
            // a^n
            while (n-- > 0) {
                ans *= (long)a;
                ans %= MOD;
            }
            i++;
        }
        System.out.println(ans);
    }
}


Measured memory_bytes: 51052
//
//  main.cpp
//  ProgrammingContestChallengeBook2
//
//  Created by Hisaki Kobayashi on 2020/04/04.
//  Copyright © 2020 Hisaki Kobayashi. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <string>
#include <bits/stdc++.h>
#include <stdlib.h>
//#include "mylibrary.hpp"



using namespace std;


int main(){
    long long N,A,B;
    cin >> N >> A >> B;
    
    if(A==0 && B==0){
        cout << 0 << endl;
        return 1;
    }
    
    long long amari = N%(A+B);
    long long div = N/(A+B);
    long long hamideru_A;
    if(amari > A){
        hamideru_A = A;
    }else{
        hamideru_A = amari;
    }
    cout << div*A+hamideru_A << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
  int a,b,c,d,e;
  cin>>a>>b;
  c=a+b;
  d=a-b;
  e=a*b;
  if(c<=d && e<=d)
    cout<<d<<endl;
  else if(d<=c && e<=c)
    cout<<c<<endl;
  else
    cout<<e<<endl;
  
  return(0);
}


Measured memory_bytes: 256
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws IOException{
		Scanner scanner = new Scanner(System.in);
		BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
		int rows = scanner.nextInt();
		int columns = scanner.nextInt();
		int want = scanner.nextInt();
		int black = 0;
		String[] tiles = new String[rows];
		for(int i = 0; i < rows; i++)
			tiles[i] = scanner.next();
		int result = 0;
		for(int i = 0; i < 1 << rows; i++)
			for(int j = 0; j < 1 << columns; j++) { 
				int count = 0;
				for(int k = 0; k < rows; k++) {
					for(int l = 0; l < columns; l++) {
						if((i & (1 << k)) > 0 || (j & (1 << l)) > 0) 
							continue;
						count += tiles[k].charAt(l) == '#'? 1: 0;
					}
				}
				if(count == want)
					result++;
			}	

		log.write("" + result);
		log.flush();
	}	
}


Measured memory_bytes: 27572
#include <bits/stdc++.h>
using namespace std;
using lint     = long long;
const lint inf = 1LL << 60;
const lint mod = 1000000007;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    lint a;
    string b;
    cin >> a >> b;
    lint bb = 0;
    for (int i = 0; i < (int)b.size(); ++i) {
        if (b[i] != '.') {
            bb *= 10;
            bb += b[i] - '0';
        }
    }
    cout << a * bb / 100 << "\n";
    return 0;
};


Measured memory_bytes: 3652
#include<bits/stdc++.h>
using namespace std;
int main()
{
  long long int n,t=0;
  cin>>n;
  int arr[n];
  for(int i=0;i<n;i++)
  cin>>arr[i];
  for(int i=1;i<n;i++)
  {
  	if(arr[i-1]>arr[i])
  	{
  		t=t+(arr[i-1]-arr[i]);
  		arr[i]=arr[i-1];
	  }
  }
  cout<<t;
}

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

int main(){
    int a,b,c,d;
    cin >> a >> b >> c >> d;
    if(abs(a-c)<=d) cout << "Yes";
    else if(abs(a-b)<=d && abs(b-c)<=d) cout << "Yes";
    else cout << "No";
}

Measured memory_bytes: 256
A,B = map(int, input().split())
if A != 0:
    A = A-1

def ruiseki_XOR(N):
    if (N+1) % 2 == 0:
        if ((N+1)//2) % 2 == 0:
            return 0
        else:
            return 1
    else:
        if ((N+1)//2) % 2 == 0:
            return N
        else:
            return 1^N


a = ruiseki_XOR(A)
b = ruiseki_XOR(B)

print(a^b)

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

#define long long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define fill(a,v) memset(a,v,sizeof(a))
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define INF 0x3f3f3f3f//infinity for integers
#define JJ ios_base::sync_with_stdio(0); cin.tie(0)//input fast
#define MOD 1000000007
#define hash(n) unordered_map<int,int>mp(n)

int main()
{
	JJ;
	int n; cin>>n;
	if(n%1000==0){
		cout<<0<<endl;
	}
	else{
		cout<<(1000-(n%1000));
	}
	return 0;
}


Measured memory_bytes: 3648
#include <algorithm>
#include <climits>
#include <cmath>
#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using Graph = std::vector<std::vector<int>>;
using WGraph = std::vector<std::vector<std::pair<int, long long>>>;
using ll = long long;
using namespace std;

int main() {
    int h = 0, w = 0;
    cin >> h >> w;
    int n = 0;
    cin >> n;
    vector<int> a(n);
    for (int i=0; i<n; i++) {
        cin >> a.at(i);
    }
    vector<vector<int>> ans(h, vector<int>(w));
    int cnt = 0;
    int paint = 0;
    bool rev = false;
    int cur_raw = 0;
    auto cur = ans.at(0).begin(), fin = ans.at(0).end();
    while (true) {
        *cur = paint + 1;
        cnt++; 
        if (rev) cur--;
        else cur++;
        if (cnt == a.at(paint)) {
            paint++;
            cnt = 0;
        }
        if (cur == fin) {
            cur_raw++;
            if (cur_raw == h) break;
            if (rev) {
                cur = ans.at(cur_raw).begin(), fin = ans.at(cur_raw).end();
            } else {
                cur = ans.at(cur_raw).end()-1, fin = ans.at(cur_raw).begin()-1;
            }
            rev = !rev;
        }
    }
    for (auto v : ans) {
        for (int x : v) {
            cout << x << " ";
        }
        cout << endl;
    }
    return 0;
}
 

Measured memory_bytes: 384
#include <iostream>

using namespace std;


int main(){
        int N,M,C,A;
        int ans=0;
        cin>>N>>M>>C;
        int B[M];
        for(int i=0;i<M;i++) cin>>B[i];
        for(int j=0;j<N;j++){
                int point=C;
                for(int i=0;i<M;i++){
                        cin>>A;
                        point+=A*B[i];
                }
                if(point>0)ans++;
        }

        cout<<ans<<endl;
        return 0;
}

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

typedef long long ll;

const int INF = 1e9;
const int MOD = 1e9+7;
const ll LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define ANS(n) cout << (n) << endl

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)

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

#define pii pair<int,int>

#define pb(a) push_back(a)
#define mp make_pair

int main()
{
	int n;
	cin >> n;
	int x[n],y[n],h[n];
	FOR(i,0,n){
		cin >> x[i] >> y[i] >> h[i];
	}
	for(int xi = 0; xi <= 100; xi++){
		for(int yi = 0; yi <= 100; yi++){
			int height;
			FOR(i,0,n){
				if(h[i] > 0){
					height = h[i] + abs(xi - x[i]) + abs(yi - y[i]);
					break;
				}
			}
			bool ans = true;
			FOR(i,0,n){
				if(h[i] != max(0, height - abs(xi - x[i]) - abs(yi - y[i]))){
					ans = false;
					break;
				}
			}
			if(ans){
				cout << xi << " " << yi << " " << height << "\n";
				return 0;
			}
		}
	}
	return 0;
}


Measured memory_bytes: 256
s = input()
t = input()
 
s_c = []
t_c = []
for c in 'abcdefghijklmnopqrstuvwxyz':
    s_c.append(s.count(c))
    t_c.append(t.count(c))
s_c.sort()
t_c.sort()
if s_c == t_c:
  print('Yes')
else:
  print('No')

Measured memory_bytes: 3632
require 'pp'
# IS_DEBUG = true
IS_DEBUG = false
def dputs str
	if(IS_DEBUG)
		puts str
	end
end
def dpp str
	if(IS_DEBUG)
		pp str
	end
end

INT_MAX = 100001
N,d=gets.chomp.split(' ').map{|n| n.to_i}
DP=Array.new(N+1)
DP[0]=0
1.upto(N){|i|
	maxsix = 1
	while(maxsix<=i)
		maxsix *= 6
	end
	maxsix /= 6
	min_by_six = DP[i-maxsix]+1
	maxnine = 1
	while(maxnine<=i)
		maxnine *= 9
	end
	maxnine /= 9
	min_by_nine = DP[i-maxnine]+1
	DP[i] = min_by_six < min_by_nine ? min_by_six : min_by_nine
}
puts DP[N]


Measured memory_bytes: 2812
import sys
import math
from collections import defaultdict
from collections import deque

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid_int(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    R, C, K = NMI()
    grid = make_grid_int(R, C, 0)
    for i in range(K):
        r, c, v = NMI()
        grid[r-1][c-1] = v

    dp = [[[0 for _ in range(C+2)] for _ in range(R+2)] for _ in range(4)]

    for r in range(R+1):
        for c in range(C+1):
            for i in range(4):
                now = dp[i][r][c]
                try:
                    val = grid[r][c]
                except:
                    val = 0

                if i == 3:
                    # 取らずに下
                    dp[0][r + 1][c] = max(now, dp[0][r + 1][c])
                    continue

                # 取って下
                dp[0][r+1][c] = max(now + val, dp[0][r+1][c])
                # 取らずに下
                dp[0][r+1][c] = max(now, dp[0][r+1][c])
                # 取って横
                dp[i+1][r][c+1] = max(now + val, dp[i+1][r][c+1])
                # 取らずに横
                dp[i][r][c+1] = max(now, dp[i][r][c+1])

    print(max(dp[0][R][C], dp[1][R][C], dp[2][R][C], dp[3][R][C]))


if __name__ == "__main__":
    main()


Measured memory_bytes: 460620
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    long long int a[n],m[n],sum1=0,sum2=0;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        if(i&1)
        sum1-=a[i];
        else
        sum1+=a[i];
        if(i==0 || i&1)
        sum2+=a[i];
        else
        sum2-=a[i];
    }
    m[0]=sum1;
    m[1]=sum2;
    for(int i=2;i<n;i+=2)
    {
        m[i]=m[i-2]+2*a[i-1]-2*a[i-2];
    }
    for(int i=3;i<n;i+=2)
    {
        m[i]=m[i-2]+2*a[i-1]-2*a[i-2];
    }
    for(int i=0;i<n;i++)
    cout<<m[i]<<" ";
    
}


Measured memory_bytes: 2816
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i <b; i++)
typedef long long ll;
#define int ll
typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int,int> pi;
#define trace(x) cout<<#x<<"="<<x<<"\n";
#define llp 1000000007
#define mod 1000000007

template < typename F, typename S >
ostream& operator << ( ostream& os, const pair< F, S > & p ) {
    return os << "(" << p.first << ", " << p.second << ")";
}

template < typename T >
ostream &operator << ( ostream & os, const vector< T > &v ) {
    os << "{";
    typename vector< T > :: const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "}";   
}

template < typename T >
ostream &operator << ( ostream & os, const set< T > &v ) {
    os << "[";
    typename set< T > :: const_iterator it;
    for ( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << *it;
    }
    return os << "]";
}

template < typename F, typename S >
ostream &operator << ( ostream & os, const map< F, S > &v ) {
    os << "[";
    typename map< F , S >::const_iterator it;
    for( it = v.begin(); it != v.end(); it++ ) {
        if( it != v.begin() ) os << ", ";
        os << it -> first << " = " << it -> second ;
    }
    return os << "]";
}

#define deb(x) cerr << #x << " = " << x << endl;


int32_t main(){
	ios::sync_with_stdio(false);
    int d, g;
    cin>>d>>g;
    vi a;
    REP(i,0,d){
        a.PB(i);
    }
    vi p(d),c(d);
    REP(i,0,d){
        cin>>p[i]>>c[i];
    }
    int ans=1e18;
    do{
        int sum=0;
        int moves=0;
        for(int i=0;i<a.size();i++){
            //for(int j=0 )
            int need=g-sum;
            if(need>0){
                int mul=min((long long)ceil((1.0)*need/((a[i]+1)*100)),p[a[i]]);
                sum+=(mul)*(a[i]+1)*100;
                if(mul==p[a[i]]){
                    sum+=c[a[i]];
                }
                moves+=mul;
                if(sum>=g){
                    break;
                }
            }

        }
        ans=min(moves,ans); 
    }while(next_permutation(a.begin(),a.end()));
    cout<<ans<<"\n";

	return 0;
}

Measured memory_bytes: 256
import java.util.*;

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

        if(X >= 400 && X <= 599){
            System.out.println(8);
        }else if(X >= 600 && X <= 799){
            System.out.println(7);
    }else if(X >= 800 && X <= 999){
        System.out.println(6);
}else if(X >= 1000 && X <= 1199){
    System.out.println(5);
}else if(X >= 1200 && X <= 1399){
    System.out.println(4);
}else if(X >= 1400 && X <= 1599){
    System.out.println(3);
}else if(X >= 1600 && X <= 1799){
    System.out.println(2);
}else if(X >= 1800 && X <= 1999){
    System.out.println(1);
}
}
}

Measured memory_bytes: 35636
#include<stdio.h>
#include<stdlib.h>
int main(){
  int h,w,i,j,k,a,y[200010],x[200010],d[200010]={};
  scanf("%d %d %d",&h,&w,&k);
  for(i=0;i<h;i++){
    for(j=0;j<w;j++){
      scanf("%d",&a);
      y[a]=i;
      x[a]=j;
    }
  }
  for(i=1;i<=h*w;i++)d[i+k]=d[i]+abs(x[i]-x[i+k])+abs(y[i]-y[i+k]);
  scanf("%d",&k);
  while(k--){
    scanf("%d %d",&i,&j);
    printf("%d\n",d[j]-d[i]);
  }
  return 0;
}


Measured memory_bytes: 2560
#include<iostream>

using namespace std;

int main() {
	int N;
	int A[21] = { 0 };
	int B[21] = { 0 };
	int C[20] = { 0 };
	cin >> N;
	for (int i = 0;i < N;i++) {
		cin >> A[i];
	}
	for (int i = 0;i < N;i++) {
		cin >> B[i];
	}
	for (int i = 0;i < N-1;i++) {
		cin >> C[i];
	}
	int ans = 0;
	for (int i = 0;i < N;i++) {
		ans += B[A[i]-1];
		/*cout << A[i] << "!" << A[i - 1] << endl;*/
		if (i>=1&&(A[i] - A[i - 1] == 1)) {
			/*cout << C[A[i]-2] << "$" << endl;*/
			ans += C[A[i]-2];
		}
		/*cout << ans << "#" << i << endl;*/
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
# -*- coding: utf-8 -*-

def input_int_array():
    return map(int, input().split())


def answer(n, x, m):
    a1 = x
    ai = a1
    loop_start = -1
    loop_end = n
    a[1] = a1
    ans = a1
    for i in range(2, n + 1):
        ai = ai*ai % m
        a.append(ai)
        ans += ai
        for j in range(1, i):
            if a[j] == ai:
                loop_start = j
                loop_end = i
                break
        if loop_start >= 0:
            break
    if loop_start < 0 or ai == 0:
        print(ans)
        return
    ans -= ai
    loop_count, remaining = divmod(n - (loop_start - 1), loop_end - loop_start)
    if loop_count > loop_start:
        start_ans = 0
        for i in range(1, loop_start):
            start_ans += a[i]
        loop_sum = ans - start_ans
        ans += loop_sum * (loop_count-1)
        for i in range(loop_start, loop_start + remaining):
            ans += a[i]
    else:
        for i in range(loop_start, loop_end):
            if i < remaining:
                ans += a[i]
            loop_sum += a[i]
        ans += loop_sum * (loop_count-1)
    print(ans)


n, x, m = input_int_array()
a = [0, 0]

answer(n, x, m)


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


using namespace std;

int main() {
	
	string s;
	cin >> s;
	int c=0, ans=0;

	for (int i = 0; i < s.size(); i++) {
		if (i % 2 == 0){
			if (s[i] =='1') c++; 
		}
		else {
			if (s[i] == '0')  c++;
		}



	}
	ans = min(c, (int)s.size() - c);

	cout << ans << endl;


}

Measured memory_bytes: 512
/*  -*- coding: utf-8 -*-
 *
 * 3001.cc: 
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

/* global variables */

double as[MAX_N], bs[MAX_N];

/* subroutines */

/* main */

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) scanf("%lf", as + i), bs[i] = as[i];
  sort(bs, bs + n);

  for (int i = 0; i < n; i++) {
    int k0 = lower_bound(bs, bs + n, as[i]) - bs;
    int k1 = upper_bound(bs, bs + n, as[i]) - bs;
    int sc = k0 * 3 + (k1 - k0) - 1;
    printf("%d\n", sc);
  }
  return 0;
}



Measured memory_bytes: 4652
#include "bits/stdc++.h"
using namespace std;
#define int long long int
#define endl "\n"
const int MOD=1e9+7;
#ifndef HOME
#define cerr if(0)cerr
#endif

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    int suf[n+5] = {0};
    suf[n-1] = 0;
    for(int i=n-2;i>=0;i--){
        if(a[i]<a[i+1]){
            suf[i] = 0;
        }else{
            suf[i] += 1 + suf[i+1];
        }        
    }
    int mx = -1;
    for(int i=0;i<n;i++){
        mx = max(mx,suf[i]);
    }
    cout<<mx<<endl;
    return 0;
    
}



Measured memory_bytes: 1792
#include <iostream>
#include <fstream>
#include <typeinfo>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <iomanip>
#include <cctype>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<25;
const double pi=acos(-1);
const double eps=1e-8;
const vi emp;

struct point{
	double x,y;
	point operator+(point p){
		return point{x+p.x,y+p.y};
	}
	point operator-(point p){
		return point{x-p.x,y-p.y};
	}
	point operator*(double p){
	return point{x*p,y*p};
	}
	point operator/(double p){
		if(!p) return point{0,0}; 
		return point{x/p,y/p};
	}
	bool operator==(point p){
		return fabs(x-p.x)<eps&&fabs(y-p.y)<eps;
	}
};

typedef pair<point,point> pp;
const point O{0,0};

class Geom{
	public:
	double Length(point x,point y){
		point z=y-x;
		return sqrt(z.x*z.x+z.y*z.y);
	}
	double IP(point x,point y){
		return x.x*y.x+x.y*y.y;
	}
	point Projection(pp a,point x){
		point A=x-a.first,B=a.second-a.first;
		double l=Length(O,B);
		return B*IP(A,B)/(l*l)+a.first;
	}
	point Reflection(pp a,point x){
		return Projection(a,x)*2-x;
	}
};

int Q;
double x,y,X1,Y1,X2,Y2;

int main(){
	cout<<syosu(10);
	Geom geo;
	cin>>X1>>Y1>>X2>>Y2>>Q;
	for(int i=0;i<Q;i++){
		cin>>x>>y;
		point ans=geo.Reflection(pp(point{X1,Y1},point{X2,Y2}),point{x,y});
		cout<<ans.x<<' '<<ans.y<<endl;
	}
}

Measured memory_bytes: 1296
#include<iostream>
#include<string>
#include<queue>
#include<utility>

using namespace std;
int main(){

  int n,q;
  pair<string,int> p;
  queue<pair <string,int> > Q;
  int total=0;
  cin>>n>>q;

  for(int i=0;i<n;i++){
    string name;
    int time;
    cin>>name>>time;
    p=make_pair(name,time);
    Q.push(p);
  }
  while(!Q.empty()){
  if(Q.front().second<=q){
    total += Q.front().second;
    cout<<Q.front().first<<" "<<total<<endl;
    Q.pop();
  }else{
    total+=q;
    p=make_pair(Q.front().first,Q.front().second-q);
    Q.pop();
    Q.push(p);
  }
}
}



Measured memory_bytes: 6228
#include<iostream>
#include<cstdlib>
 
using namespace std;
 
struct Node{
  int key;
  Node* next;
  Node* prev;
};
 
struct List{
  Node* nil;
 
  void init(){
    nil=(Node*)malloc(sizeof(Node));
    nil->next=nil;
    nil->prev=nil;
    nil->key=0;
  }
 
  Node* find(int key){
    for(Node* i=nil->next;i!=nil;i=i->next){
      if(i->key==key)return i;
    }
    return nil;
  }
 
  void insert(Node *x){
    x->next=nil->next;
    nil->next->prev=x;
    nil->next=x;
    x->prev=nil;
  }
  
  void add(int x){
    Node* p = (Node*)malloc(sizeof(Node));
    p->key=x;
    insert(p);
  }
   
  void delete_x(int key){
    Node* x=find(key);
    if(x==nil)return;
    x->prev->next=x->next;
    x->next->prev=x->prev;
    free(x);
  }
   
  void deleteFirst(){
    Node* x = nil->next;
    x->prev->next=x->next;
    x->next->prev=x->prev;
    free(x);
  }
   
  void deleteLast(){
    Node* x = nil->prev;
    x->prev->next=x->next;
    x->next->prev=x->prev;
    free(x);   
  }
 
  void output(){
    int flg=0;
    for(Node* i=nil->next;i!=nil;i=i->next){
      if(flg)cout<<' ';
      cout<<i->key;
      flg=1;
    }
    cout<<endl;
  }
};
 
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
 
  List list;
  list.init();
  int n,x;
  string str;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>str;
    if(str=="insert"){
      cin>>x;
      list.add(x);
    }else if(str=="delete"){
      cin>>x;
      list.delete_x(x);
    }else if(str=="deleteFirst"){
      list.deleteFirst();
    }else {
      list.deleteLast();
    }
  }
  //cout<<'-'<<endl;
  list.output();
}



Measured memory_bytes: 34404