akhauriyash/Code-Regression huggingface dataset:
n,ma,mb=map(int,input().split())
INF=10**17
dp=[[[INF]*(10*n+1) for i in range(10*n+1)] for j in range(n+1)]
dp[0][0][0]=0
ans=10**17
for i in range(n):
  a,b,c=map(int,input().split())
  for j in range(10*n+1):
    for k in range(10*n+1):
      if dp[i][j][k]!=INF:
        dp[i+1][j+a][k+b]=min(dp[i+1][j+a][k+b],dp[i][j][k]+c)
      dp[i+1][j][k]=min(dp[i+1][j][k],dp[i][j][k])
for i in range(1,10*n+1):
  for j in range(1,10*n+1):
    if i*mb==j*ma:
      ans=min(ans,dp[n][i][j])
if ans==INF:
  print(-1)
else:
  print(ans)

Measured memory_bytes: 96732
def main():
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]
    
    n_num = len(list(set(A) ) )
#    print(n_num)
#    nums = [0] * n_num
    nums = [0]* max(A)
#    print(nums)
    for a in A:
        nums[a - 1] += 1
    nums.sort()
#    print(nums)
    
    count = 0
    sub = n_num - K
    for num in nums:
        if(sub <= 0):
            break
        
        if(num > 0):
            count += num
            sub -= 1
    
    print(count)
if __name__=="__main__":
    main()

Measured memory_bytes: 25760
#include <iostream>
#include <vector>
#include <map>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#define rep(i,n) for (int i=0;i<n;i++)
using namespace std;
typedef long long ll;

int main(){
  ll N,K;
  ll sum = 0;
  vector<ll>::iterator itr;
  vector<ll> v;
  cin >> N >> K;
  rep(i,N){
    ll H;
    cin >> H;
    v.push_back(H);
  }
  sort(v.begin(),v.end());
  rep(i,N-K){
    sum += v[i];
  }

  cout << sum << endl;
  
  return 0;
}


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

#define pb         push_back

typedef long long ll;
const ll INF = 1000000000ll;
const ll MOD = 1000000007ll;
const double EPS = 1e-8;

int main(void) {
	//ios_base::sync_with_stdio(false);
	//cin.tie(0);
	
	int n;
	vector<int> l;

	cin >> n;
	for(int i=0; i<2*n; i++){
		int t;
		cin >> t;
		l.pb(t);
	}

	sort(l.begin(), l.end());

	long long int sum = 0;
	for(int i=0; i<2*n; i+=2){
		sum += l[i];
	}

	cout << sum << endl;

	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<math.h>
using namespace std;
typedef long long ll;
#define int long long
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template<class T>bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
#define fore(i,a) for(auto &i:a)
#define REP(i,n) for(int i=0;i<n;i++)
#define eREP(i,n) for(int i=0;i<=n;i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define eFOR(i,a,b) for(int i=(a);i<=(b);++i)
#define SORT(c) sort((c).begin(),(c).end())
#define rSORT(c) sort((c).rbegin(),(c).rend())
#define LB(x,a) lower_bound((x).begin(),(x).end(),(a))
#define UB(x,a) upper_bound((x).begin(),(x).end(),(a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
//priority_queue<int,vector<int>, greater<int> > q2;



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

	int N, K; cin >> N >> K;
	int bi[50] = { 0 };
	REP(i, N) {
		int a; cin >> a;
		int j = 0;
		while (a) {
			bi[j++] += a % 2;
			a /= 2;
		}
	}
	int dp[50][2] = { 0 };
	REP(i, 50)dp[i][1] = -1;
	for (int i = 48; i >= 0; i--) {
		if ((1LL << i) > K) {
			chmax(dp[i][0], dp[i + 1][0] + (1LL << i)*bi[i]);
		}
		else if ((1LL << i)&K) {
			chmax(dp[i][0], dp[i + 1][0] + (1LL << i)*(N - bi[i]));
			chmax(dp[i][1], dp[i + 1][0] + (1LL << i)*bi[i]);
			if (dp[i + 1][1] != -1)chmax(dp[i][1], dp[i + 1][1] + (1LL << i)*(N - bi[i]));
			chmax(dp[i][1], dp[i + 1][1] + (1LL << i)*bi[i]);
		}
		else {
			chmax(dp[i][0], dp[i + 1][0] + (1LL << i)*bi[i]);
			chmax(dp[i][1], dp[i + 1][1] + (1LL << i)*(N - bi[i]));
			chmax(dp[i][1], dp[i + 1][1] + (1LL << i)*bi[i]);
		}
	}






	cout << max(dp[0][0], dp[0][1]) << endl;

	return 0;
}



Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main()
{
    char a;
    cin >> a;

    if (a >= 'A' && a <= 'Z') cout << 'A' << endl;
    else cout << 'a' << endl;
    return 0;
}

Measured memory_bytes: 3632
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char const* argv[]){
    int a,b;
    char c[1];
    scanf("%d", &a);
    scanf("%s", c);
    scanf("%d", &b);

    if (c[0] == '+') {
        printf("%d\n", a+b);
    }else{
        printf("%d\n", a-b);
    }

    return 0;
}


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



Measured memory_bytes: 2140
import java.util.*;
public class Main {
  public static void main(String[] args){

    Scanner sc = new Scanner(System.in);
    int A = sc.nextInt();
    int B = sc.nextInt();

    if (A < B) {
      if (A < (B - 1)) {
        System.out.println(B + (B - 1));
      } else {
        System.out.println(A + B);
      }
    } else {
      if (B < (A - 1)) {
        System.out.println(A + (A - 1));
      } else {
        System.out.println(A + B);
      }
    }

  }
}

Measured memory_bytes: 25172
n = int(input())
a = list(map(int, input().split()))

if n%2==0:
    odd = list(range(1, n, 2))
    eve = list(reversed(list(range(2, n+1, 2))))
    ix = eve+odd
    for i in ix:
        print(a[i-1], end=' ')

else:
    odd = list(reversed(list(range(1, n+1, 2))))
    eve = list(range(2, n, 2))
    ix = odd+eve
    for i in ix:
        print(a[i-1], end=' ')

print()

Measured memory_bytes: 26180
n,m=map(int,input().split())
a=[]
mod=1e9+7
oks=[True]*(n+1)
for i in range(m):
    a.append(int(input()))
    oks[a[i]]=False
dp=[0]*(n+1)
dp[0]=1
for i in range(n):
    for j in range(i+1, min(n,i+2)+1):
        if oks[j]:
            dp[j]+=dp[i]
            dp[j]%=mod

print(int(dp[n]))

Measured memory_bytes: 8616
#include <stdio.h>
int max(int n,int m){
    if(n>m) return n;
    else return m;
}
int main(void){
    int n;
    long ans=0,cnt=0,i;
    scanf("%d",&n);
    long a[n];
    for(i=0;i<n;i++) scanf("%ld",&a[i]);
    for(i=1;i<n;i++){
        if(a[i-1]>=a[i]) cnt++;
        else{
            ans = max(ans,cnt);
            cnt=0;
        }
    }
    if(a[i-1]>a[i]) cnt++;
    ans = max(ans,cnt);
    printf("%ld",ans);
}


Measured memory_bytes: 896
/**
*    author:  Dooloper
*    created: 28.06.2020 21:35:09
**/

#include <bits/stdc++.h>

#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
using P = pair<int,int>;
using ll = long long;

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n,m,k;
  cin >> n >> m >> k;
  vector<int> a(n);
  vector<int> b(m);
  rep(i,n) cin >> a[i];
  rep(i,m) cin >> b[i];
  ll t = 0;
  rep(i,m) t += b[i];
  int j = m;
  int ans = 0;
  rep(i,n+1){
    while ( j > 0 && t > k){
      --j;
      t -= b[j];
    }
    if( t > k ) break;
    ans = max(ans,i+j);
    if(i == n) break;
    t += a[i];
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 4792
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        exec();
    }

    static void exec() {
        PrintWriter out = new PrintWriter(System.out);
        Scanner sc = new Scanner(System.in);
        int a = Integer.parseInt(sc.next());
        int b = Integer.parseInt(sc.next());

        if((a%2==0 && b%2==0) || (a%2!=0 && b%2!=0)) {
            out.println((a+b)/2);
        }else {
            out.println("IMPOSSIBLE");
        }


        out.flush();

    }

}


Measured memory_bytes: 21844
_MULTITEST = False
##
def solve():
    n = int(input())
    a = list(map(int, input().split()))

    a.sort()
    print(sum(
        a[n - (k//2) - 1] for k in range(1, n)
    ))

##
if __name__ == "__main__":
    t = (int(input()) if _MULTITEST else 1)
    for tt in range(t):
        solve()

Measured memory_bytes: 31436
x=int(input())

dic={}



a=set([])
for i in range(1000):
    a.add(i**5)
    dic[i**5]=i
    a.add(-1*i**5)
    dic[-1*i**5]=i*(-1)

#print(dic)

for item1 in a:
    for item2 in a:
        if item1 - item2==x:
            print(dic[item1],dic[item2])
            exit()



Measured memory_bytes: 66056
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;
const int INF = 1 << 30;

int main()
{
	long double T[128][128], dp[128][128];
	int n, m;

	while(cin >> n >> m)
	{
		if(n == 0 && m == 0)
		{
			break;
		}

		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < n; j++)
			{
				cin >> T[i][j];
			}
		}
		
		memset(dp, -1, sizeof(dp));
	
		for(int i = 0; i < n; i++)
		{
			dp[0][i] = 1.0;
		}

		for(int i = 1; i < m; i++)
		{
			for(int j = 0; j < n; j++)
			{
				dp[i][j] = -1.0;
				for(int k = 0; k < n; k++)
				{
					//dp[i][j] : i回目で肥料kを使った時の最大の苗木の大きさ
					dp[i][j] = max(dp[i][j], dp[i-1][k]*T[k][j]);
				}
			}
		}

		long double res = -1; 
		for(int i = 0; i < n; i++)
		{
			res = max(res, dp[m-1][i]);
		}
	
		printf("%.2Lf\n",res);
	}
}

Measured memory_bytes: 1712
n = int(input())
prev = -1
res = -1
a = [int(input()) for _ in range(n)]
for i in a:

    if i - prev > 1:
        print(-1)
        exit()
    elif i == prev + 1:
        res += 1
    else:
        res += i
    prev = i

print(res)


Measured memory_bytes: 11048
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)n; ++i)
#define all(obj) (obj).begin(), (obj).end()
static const int dx[4] = {0, 1, 0, -1};
static const int dy[4] = {1, 0, -1, 0};
static const char dir[4] = {'u', 'r', 'd', 'l'};
static const ll INF = 1ll << 60;
static const ll MOD = 1e9 + 7;

signed main() {
  ll K;
  ll sum = 0;
  cin >> K;
  for (auto a = 1; a <= K; ++a) {
    for (auto b = 1; b <= K; ++b) {
      for (auto c = 1; c <= K; ++c) {
        sum += __gcd(a, __gcd(b, c));
      }
    }
  }
  cout << sum << endl;
  return 0;
}

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

using i64 = long long int;

class UnionFindTree {
  private:
    vector<int> par;   // 親
    vector<int> depth; // 木の深さ

  public:
    // n要素で初期化
    UnionFindTree(int n) {
        for (int i = 0; i <= n; i++) {
            par.push_back(i);
            depth.push_back(0);
        }
    }

    // 木の根を求める
    int find(int x) {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }

    // xとyの属する集合を併合
    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y)
            return;

        if (depth[x] < depth[y]) {
            par[x] = y;
        } else {
            par[y] = x;
            if (depth[x] == depth[y])
                depth[x]++;
        }
    }

    // xとyが同じ集合に属するか否か
    bool same(int x, int y) { return find(x) == find(y); }
};

int main(){
    int n;
    cin >> n;
    map<string, int> mp;
    vector<int> v(n+1);
    for(int i=1;i<=n;++i){
        string a;
        int x;
        cin >> a >> x;
        mp[a] = i;
        v[i] = x;
    }
    int m;
    cin >> m;
    UnionFindTree uft = UnionFindTree(n);
    for(int i=0;i<m;++i){
        string s, t;
        cin >> s >> t;
        uft.unite(mp[s], mp[t]);
    }

    for(int i=1;i<=n;++i){
        int par = uft.find(i);
        v[par] = min(v[par], v[i]);
    }

    int ans = 0;
    for(int i=1;i<=n;++i){
        int par = uft.find(i);
        ans += v[par];
    }

    cout << ans << endl;

    return 0;
}



Measured memory_bytes: 3816
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func getNextLine(scanner *bufio.Reader) string {
	var buffer string
	for {
		line, isPrefix, _ := scanner.ReadLine()
		buffer += string(line)
		if isPrefix == false {
			break
		}
	}
	return buffer
}

func getIntList(scanner *bufio.Reader) []int {
	list := strings.Split(getNextLine(scanner), " ")
	l := len(list)
	result := make([]int, l)
	for i := 0; i < l; i++ {
		result[i], _ = strconv.Atoi(list[i])
	}
	return result
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := bufio.NewReader(fp)

	var k int64

	s := getNextLine(scanner)
	fmt.Sscan(getNextLine(scanner), &k)

	for i := 0; i < len(s); i++ {
		if s[i] != '1' {
			fmt.Println(fmt.Sprintf("%c", s[i]))
			return
		}
		if int64(i) == k-1 {
			fmt.Println("1")
			return
		}
	}
	fmt.Println("1")
}


Measured memory_bytes: 640
def gcd(a,b):
  while b:a,b=b,a%b
  return a
n,x=map(int,input().split())
a=[abs(i-x)for i in list(map(int,input().split()))]
x=a[-1]
for i in a[:-1]:x=gcd(x,i)
print(x)


Measured memory_bytes: 14252
#include<bits/stdc++.h>
                    #include <set>
					#include <ext/pb_ds/assoc_container.hpp> 
					#include <ext/pb_ds/tree_policy.hpp> 
					using namespace __gnu_pbds; 
					#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
					using namespace std;
                    #define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
                    #define pb push_back
                    #define ff first
                    #define ss second
                    #define sz size()
                    #define mp make_pair
                    #define int long long int
                    #define double long double
                    #define bp __builtin_popcountll
                    #define FOR(i,a,b) for(int i=a;i<b;i++)
                    #define ROR(i,a,b) for(int i=a;i>=b;i--)
                    const int M=1e9+7;
                    const int N=5e3+5;
                    const int inf=1e18;
                    int dp[405][405];
                    int n;
                    int a[405],pre[405];
                    int solve(int l,int r)
                    {
                    	if(l>r) return inf;
                    	if(l==r) return 0;
                    	int &ans=dp[l][r];
                    	if(ans!=-1) return ans;
                    	ans=inf;
                    	for(int i=l;i<r;i++)
                    	{
                    		ans=min(ans,solve(l,i)+solve(i+1,r)+pre[r]-pre[l-1]);
                    	}
                    	return ans;
                    }
                    int32_t main()
                    {
                		FAST
						cin>>n;
						for(int i=1;i<=n;i++)
						cin>>a[i];
						for(int i=1;i<=n;i++)
						pre[i]=pre[i-1]+a[i];
						memset(dp,-1,sizeof(dp));
						cout<<solve(1,n);
						
                    }

Measured memory_bytes: 1536
import java.io.*;
import java.util.Arrays;

public class Main {
  public static final int MAX_W = 100000 * 10000;

  public static void main(String args[]) {
    try {
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String[] inputs = in.readLine().split(" ");
      int n = Integer.parseInt(inputs[0]);
      int k = Integer.parseInt(inputs[1]);
      int[] loads = new int[n];
      for(int i=0; i<n; i++) {
        loads[i] = Integer.parseInt(in.readLine());
      }
      int left = 0;
      int right = MAX_W;
      int middle = 0;
      while(right > left + 1){
        middle = (left + right) / 2;
        if(getMaxLoadCount(middle, k, loads) >= n) {
          right = middle;
        } else {
          left = middle;
        }
      }
      System.out.println(right);
    } catch(IOException e) {
    }
  }

  public static int getMaxLoadCount(int maxLoadSize, int truckCount, int[] loads) {
    int j = 0;
    int[] trucks = new int[truckCount];
    Arrays.fill(trucks, maxLoadSize);
    for(int k=0; k<truckCount; k++) {
      while(trucks[k] - loads[j] >= 0) {
        trucks[k] = trucks[k] - loads[j];
        j++;
        if(j == loads.length) {
          return j;
        }
      }
    }
    return j;
  }
}

Measured memory_bytes: 45732
import sys
N=int(input())
alist=list(map(int,input().split()))

a2list=[alist[0]]
for i in range(1,N):
  if a2list[-1]!=alist[i]:
    a2list.append(alist[i])
#print(a2list)

N2=len(a2list)
if N2==1:
  print(1000)
  sys.exit(0)

yama_list=[False]*N2
tani_list=[False]*N2
if a2list[0]>a2list[1]:
  yama_list[0]=True
if a2list[0]<a2list[1]:
  tani_list[0]=True

for i in range(1,N2-1):
  if a2list[i-1]<a2list[i]>a2list[i+1]:
    yama_list[i]=True
  if a2list[i-1]>a2list[i]<a2list[i+1]:
    tani_list[i]=True
    
if a2list[N2-2]<a2list[N2-1]:
  yama_list[N2-1]=True
if a2list[N2-2]>a2list[N2-1]:
  tani_list[N2-1]=True
    
#print(yama_list)
#print(tani_list)

money=1000
kabu=0
for i in range(N2):
  if yama_list[i]:
    #sell all
    money+=a2list[i]*kabu
    kabu=0
  if tani_list[i] and i<N2-1:
    #buy as possible
    k=money//a2list[i]
    kabu+=k
    money-=k*a2list[i]
    
print(money)

Measured memory_bytes: 62116
#include <iostream>
#include <algorithm>
using namespace std;
int main(){
    int H,W,N;
    cin >>H>>W>>N;
    int a[H][W];
    int color,k=0;
    for(int i=1;i<N+1;i++){
        cin>>color;
        for(int j=k;j<color+k;j++){
            a[j/W][abs((j/W)%2*(W-1)-(j%W))]=i;
        }
        k+=color;
    }
    for(int i = 0; i < H; i++){
        for(int j = 0; j < W; j++){
            cout << a[i][j] << ' ';
        }
        cout << '\n';
    }
    return 0;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>
#include<vector>
#include<string>
#include<algorithm>
#include<cmath>
 
#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define int long long int
#define mod 1000000007
 
using namespace std;
 
signed main()
{
    fast;
    int n;
    cin>>n;

    string s1;
    cin>>s1;
    string s2;
    cin>>s2;

    string str = "";

    for ( int i = 0 ; i < s1.length() ; i++ )
    {
        str += s1[i];
        str += s2[i];
    }

    cout<<str<<endl;
    return 0;
}

Measured memory_bytes: 256
/**********************************************************/
/**            Bismillahir-rahmanir-rahim                **/
/**  In the name of Allah, the Beneficent, the Merciful. **/
/**********************************************************/
/**@author: Sayed Sohan*/
/**status : */
/**url: */
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
typedef long long  ll;
typedef unsigned long long ull;
#define FOR(i,x,n) for(ll i=(x);i<(n);i++)
#define IFOR(i,n,x) for(ll i=(n-1);i>=(x);i--)
#define REP(i,n) for(ll i=0;i<n;i++)
#define IREP(i,n) for(ll i = n-1;i>=0;i--)
#define cas(c,x)  cout<<"Case "<<c++<<": "<<x<<NL;
#define clr(arr,value) memset(arr,value,sizeof(arr))
#define MOD 1000000007
#define PI acos(-1.0)
#define rtod 180/PI
#define DIF if(0)
#define PB push_back
#define MP make_pair
#define SP " "
#define NL "\n"
#define DE cout<<"HERE"<<endl;
#define F first
#define S second
#define INF 1e18
#define Pause system("pause")
#define __FAST_IO ios_base::sync_with_stdio(0);
#define clr2V(x,sz) for(int iii=0;iii<=sz;iii++) x[iii].clear();
#define NUM1(x) __builtin_popcount(x)
#define MAX 200010/** can increase another 0 */

//** your code goes from here



int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    __FAST_IO;
    ll i,j,k,arr[MAX],l,m,n,x,y,coun,sum,ans,T,check;
    vector<ll> v;
    string s;

    cin>>n;

    ll mat[10][10];
    clr(mat,0);

    arr[0] = 0;
    ll z = 10;
    for(i=1;i<=n;i++){
        if(i%10==0){
            arr[i] = arr[i-1];
            if(i%z==0) z*=10;
            continue;
        }
        if(i<=9){
            arr[i] = arr[i-1]+1;
            mat[i][i]++;
        }
        else{
            y = i%10;
            x = (i/double(z)) * 10;
            ans = (mat[y][x]*2)+1;
            //cout<<x<<SP<<y<<NL;
            if(x!=y) ans--;
            if(ans == 1) ans = 0;
            mat[x][y]++;
            arr[i] = arr[i-1] + ans;
        }
    }
    cout<<arr[n]<<NL;
    return 0;
}



Measured memory_bytes: 1792
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repI(i, d, n) for (int i = (d); i < (n); ++i)
#define reps1(i, n) for (int i = 1; i < (n); ++i)
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using M = map<int, int>;
using Ml = map<ll, ll>;
ll const INF = 1ll<<61;
double pi = 3.141592653589793238;

int main() {
  ll k;
  cin >> k;

  ll num = 7;
  if (num % k == 0) {
    cout << 1 << endl;
    return 0;
  }
  ll cnt = 2;

  while(cnt <= 1e6) {
    num = ((num*10) % k + 7 % k) % k;
    if (num == 0) {
      cout << cnt << endl;
      return 0;
    }

    cnt++;
  }

  cout << -1 << endl;

  return 0;
}


Measured memory_bytes: 3632
#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define mod 1000000007
#define endl "\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define MAXX 1000000000000000000

int32_t main()
{
    ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int x,n,i,j;
	cin >> x;
	n = 100;
	i = 0;
	while(1)
	{
		i++;
		n += n/100;
		if(n >= x)
		{
			cout << i ;
			return 0;
		}
	}
	return 0;
}

Measured memory_bytes: 3656
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;

int dp[3007][3007],len1,len2;
stack<char>sa;
struct madoka{
	int x;
	int y;
}pre[3007][3007];

char s[3007],t[3007];


int main(){
	cin>>s+1;
	cin>>t+1;
	dp[0][0]=0;
	for(int i=1;i<=strlen(s+1);i++){
		for(int j=1;j<=strlen(t+1);j++){
			if(s[i]==t[j]){
				dp[i][j]=dp[i-1][j-1]+1;
				pre[i][j]={i-1,j-1};
			}
			else{
				if(dp[i][j-1]>=dp[i-1][j]){
					dp[i][j]=dp[i][j-1];
					pre[i][j]={i,j-1};
				}
				else{
					dp[i][j]=dp[i-1][j];
					pre[i][j]={i-1,j};
				}
			}
		}
	}
	int x=strlen(s+1),y=strlen(t+1),linx,liny;
	while(x!=0){
		linx=pre[x][y].x;
		liny=pre[x][y].y;
		if(x-linx==1&&y-liny==1){
			sa.push(s[x]);
		}
		x=linx;
		y=liny;
	}
	while(!sa.empty()){
		printf("%c",sa.top());
		sa.pop();
	}
}

Measured memory_bytes: 109344
#define FNAME ""

#include <bits/stdc++.h>

#define hash padjf9srpi
#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define forn(i, n) for (int i = 0; i < (n); i++)
#define fornr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define forab(i, a, b) for (int i = (a); i < (b); i++)
#define gcd __gcd
#define all(a) (a).begin(), (a).end()
 
#ifdef _WIN32
    #define I64 "%I64d"
#else
    #define I64 "%lld"
#endif

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;                                                                                                                                                                                      
typedef vector <int> vi;

template <class T> T sqr(const T &a) {return a * a;}

const int MAXN = 1e5 + 100;

int n, m, used[MAXN];
vi a[MAXN];


int dfs(int v, int k = 1) {
    used[v] = k;
    int res = 1;
    for (int to: a[v]) {
        if (!used[to]) res &= dfs(to, 3 - k);
        else if (used[to] == k) res = 0; 
    }
    return res;
}

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

#ifdef LOCAL
    freopen(FNAME".in", "r", stdin);
    freopen(FNAME".out", "w", stdout);
#endif

    scanf("%d%d", &n, &m);
    forn(i, m) {
        int x, y;
        scanf("%d%d", &x, &y);
        x--, y--;
        a[x].pb(y), a[y].pb(x);
    }
    int k = 0;
    int l = 0;
    int cnt = 0;
    forn(i, n) {
        if (!a[i].size()) l++, cnt++;
        else if (!used[i]) {
            k += dfs(i);
            cnt++;
        }
    }
    cerr << k << " " << l << " " << cnt << endl;
    printf("%lld\n", 2ll * l * n - 1ll * l * l + 2ll * k * (cnt - k - l) + sqr((LL) cnt - k - l) + 2ll * k * k);
}




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

int main(){
    int a, b, c, cnt = 0, ans = 0;
    string S;
    vector<int> v(10);
    
    cin >> a;
    cout << a * a << endl;
}

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

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int N;
int A[202020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	FOR(i,N) {
		cin>>A[i];
		if(i==0 && A[i]) return _P("-1\n");
		if(i && A[i]>A[i-1]+1) return _P("-1\n");
	}
	set<int> S;
	int dif=0;
	ll ret=0;
	for(i=N-1;i>=0;i--) {
		if(S.size() && *S.begin()+dif==0) S.erase(S.begin());
		if(A[i]) S.insert(A[i]-dif);
		ret+=S.size();
		dif--;
	}
	cout<<ret<<endl;
	
		
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


Measured memory_bytes: 5760
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int main()
{

string s;
vector<string>v;
long long int i,n;
cin>>n;
for(i=0;i<n;i++)
{
  cin>>s;
  sort(s.begin(),s.end());

  v.push_back(s);

}

sort(v.begin(),v.end());
vector<long long int>ans;
long long int temp=0;
for(i=0;i<v.size()-1;i++)
{
  if(v[i]!=v[i+1])
    {
     ans.push_back(temp+1);
     temp=0;
    }

   else
   {
     temp++;


   }



}
ans.push_back(temp+1);


long long int sum=0;
for(i=0;i<ans.size();i++)
sum=sum+((ans[i]*(ans[i]-1))/2);



cout<<sum<<endl;









return 0;
}



Measured memory_bytes: 7284
//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define pb push_back
#define F first
#define S second
#define ins insert
#define mp make_pair
#define bit __builtin_popcount
#define md (l + ((r - l) / 2))
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define ub upper_bound
#define lb lower_bound
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;


using ll = long long;



const int N = 2e5+33, mod = 1e9 + 7, mod2 = 998244353;
const int MAX = 1e5 + 11 , T = 2000;
const int INF1 = 2e9 + 11;
const ll INF2 = 2e18 + 11;
const double INF3 = 1e8 + 11;
const int base = 500;
const int P = 31;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double EPS = 1e-4;
const double PI = acos(-1.0);


template<typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }


typedef pair <int, int> pii;        // #include <ext/pb_ds/assoc_container.hpp>
typedef pair <ll, ll> pll;         // #include <ext/pb_ds/tree_policy.hpp>
typedef vector <int> vi;          // #include <ext/rope>
                // #include <ext/rope>
                                 // #include <ext/rope>
bool prime(int n) {             // template<typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
    for(int i=2;i<=sqrt(n);i++){
        if(n%i==0){return 0;}
    }return 1;}

int a[N];
unordered_map<int , ll> p;

void solve() {
    int n;
    cin >> n;
    for(int i = 1; i <= n; i ++) {
        cin >> a[i];
    }
    ll cnt = 0;
    for(int i = 1; i <= n; i ++) {
        cnt += p[a[i]-i];
        p[-a[i]-i] ++;
    }
    cout << cnt;
}
/*

*/

main() {
    ios;
    int tt = 1;
//    cin >> tt;
    while(tt --) {
        solve();
//        brute();
    }
    return 0;
}
/*

*/


Measured memory_bytes: 19288
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <ctype.h>
#include <stdint.h>
#include <string.h>
#include <wchar.h>
#include <math.h>

#define N_MAX (100)
#define P_MAX (100)
#define DP_ARRAY_SIZE (N_MAX * P_MAX / 32 + 1)

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(a) ((a) < 0 ? -(a) : (a))
#define ABSS(a, b) ((a) > (b) ? (a) - (b) : (b) - (a))

int compare_sz_desc(const void* a, const void* b) {
    return *((size_t*)a) > * ((size_t*)b) ? -1 : 1;
}

int compare_f(const void* a, const void* b) {
    return *((int64_t*)a) < *((int64_t*)b) ? -1 : 1;
}

int compare_f2(const void* a, const void* b) {
    return *((uint64_t*)a) > * ((uint64_t*)b) ? -1 : 1;
}

static size_t comb(const size_t n, const size_t r) {
    size_t result = 1;

    for (size_t i = 0; i < r; i++) {
        result *= n - i;
        result /= i + 1;
    }

    return result;
}

static uint64_t combU64(const uint64_t n, const uint64_t r) {
    uint64_t result = 1;

    for (uint64_t i = 0; i < r; i++) {
        result *= n - i;
        result /= i + 1;
    }

    return result;
}

static uint64_t gcdU64(uint64_t m, uint64_t n)
{
    uint64_t temp;

    while (m % n != 0) {
        temp = n;
        n = m % n;
        m = temp;
    }

    return n;
}

static size_t gcdZu(size_t m, size_t n) {
    size_t temp;

    while (m % n != 0) {
        temp = n;
        n = m % n;
        m = temp;
    }

    return n;
}

int main(void) {
    char S[16];

    scanf("%s", S);

    const size_t len = strlen(S);
    size_t cnt = 0;

    for (size_t i = 0; i < len; i++) {
        if (S[i] == 'x') {
            cnt++;
        }
    }

    puts(cnt <= 7 ? "YES" : "NO");

    return 0;
}


Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve()
{
    int n; cin >> n;
    int ans = 0;
    for(int i = 1; i <= n; i++)
    {
        int te; cin >> te;
        if(te % 2 == 1 && i%2 == 1)
        {
            ans++;
        }
    }
    cout << ans << endl;
}
int main()
{
    solve();
    return 0;
}


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

int main() {
int n;
cin>>n;
if(n==1){
cout<<0<<endl;
return 0;
}
vector<int> vec(n);
for(int i=0;i<n;i++){
cin>>vec.at(i);
}
int a=0;
for(int i=0;i<n-1;i++){
if(vec.at(i+1)>vec.at(i)){
a++;
}
}
cout<<a<<endl;
}



Measured memory_bytes: 3108
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <numeric>
#include <tuple>
#include <stack>

#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define print(s) cout << s << endl
typedef long long ll;
using namespace std;

int ctoi(char c) {
	if (c >= '0' && c <= '9') {
		return c - '0';
	}
	return 0;
}

int main() {
	int n;cin >> n;
	vector<int> v(n);
	rep(i,n) cin >> v[i];
	sort(all(v));
	cout << v[n-1] - v[0] << endl;
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main()
{
    int a,b;
    cin>>a>>b;
    if(a+b==15)
    {
        cout<<"+"<<endl;
    }
    else if(a*b==15)
    {
        cout<<"*"<<endl;
    }
    else
    {
        cout<<"x"<<endl;
    }
    return 0;
}

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

//FILE *fin = freopen("c.in", "r", stdin);

/* ------------------------------------------------ */
int x, y, z, n;
struct Pers
{
    int a, b, c, ty;
    bool operator < (const Pers &ot) const
    {
        if (ty == 1)
            return a - c > ot.a - ot.c;
        return b - c > ot.b - ot.c;
    }
} v[maxN];
/* ------------------------------------------------ */
long long sum[2][maxN];
priority_queue < Pers > H;
/* ------------------------------------------------ */
long long ans;

int cmp(const Pers &x, const Pers &y)
{
    if (x.a - x.b == y.a - y.b)
        return x.a - x.c > y.a - y.c;
    return x.a - x.b > y.a - y.b;
}

void prec(int t, int l, int r, int sz)
{
    bool s = (t == 1) ? 0 : 1;
    while (!H.empty())
        H.pop();
    for (int i = l; i != r; i += t)
    {
        v[i].ty = t;
        H.push(v[i]);
        sum[s][i] = sum[s][i - t] + (t == 1) * v[i].a + (t == -1) * v[i].b;
        if (abs(i - l) + 1 > sz)
        {
            Pers p = H.top();
            H.pop();
            sum[s][i] -= (t == 1) * (p.a - p.c) + (t == -1) * (p.b - p.c);
        }
    }
}

int main()
{
    scanf("%d %d %d", &x, &y, &z);
    n = x + y + z;
    for (int i = 1; i <= n; ++ i)
        scanf("%d %d %d", &v[i].a, &v[i].b, &v[i].c);
    sort(v + 1, v + n + 1, cmp);

    prec(1, 1, n - y + 1, x);
    prec(-1, n, x, y);
    for (int i = x; i <= n - y; ++ i)
        ans = max(ans, sum[0][i] + sum[1][i + 1]);
    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 4724
import java.util.*;
class Main{
    public static void main(String[] args){

        Scanner sc = new Scanner(System.in);
        int a,b,c = 0;
        a = sc.nextInt();
        b = sc.nextInt();
        c = sc.nextInt();
        if(a==b&&a!=c){
            System.out.println("Yes");
        } else if(b==c&&b!=a){
            System.out.println("Yes");
        } else if(c==a&&c!=b){
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

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

signed main(void)
{
	ll x, k, d, cnt, ans1, ans2;
	cin >> x >> k >> d;
	x = abs(x);
	if (x / d < k)
	{
		cnt = x / d;
		ans1 = x - cnt * d;
		ans2 = abs(ans1 - d);
		k -= cnt;
		if (k % 2 == 0)
			cout << ans1 << endl;
		else
			cout << ans2 << endl;
	}
	else
		cout << x - k * d << endl;
	return 0;
}


Measured memory_bytes: 3672
"use strict";
function gcd(a) {
    var f = (a, b) => b ? f(b, a % b) : a;
    var ans = a[0];
    for (var i = 1; i < a.length; i++) {
        ans = f(ans, a[i]); 
    }
    return ans;
}
function Main(input) {
    let lines = input.split("\n");
    let N = lines[0];
    let An = lines[1].split(/\s/).map((v) => v - 0);
    console.log(gcd(An));
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));


Measured memory_bytes: 29512
#!/usr/bin/env python3
import sys
input = sys.stdin.readline

n, p = map(int, input().split())
s = input().rstrip()
n = len(s)

if p == 2 or p == 5:
    ans = 0
    for i, item in enumerate(s):
        if int(item) % p == 0:
            ans += (i + 1)
else:
    mods = [0]
    s = s[::-1]
    mod = 0
    for i, ch in enumerate(s):
        mod = (mod + int(ch) * pow(10, i, p))  % p
        mods.append(mod)
    
    cnt = [0] * p
    for item in mods:
        cnt[item] += 1
    ans = 0
    for item in cnt:
        ans += item * (item - 1) // 2
print(ans)
    

Measured memory_bytes: 47404
#include <iostream>
#include <vector>
#include <array>
#include <set>
#include <cmath>
#include <string>
#include <sstream>
#include <queue> 
#include <list>
#include <stack>
#include <unordered_map>
#include <algorithm> 
#include <numeric>  
#include <functional>
#include <cassert>
#include <bitset>
#include <cctype>  
#include <iomanip> 
#include <limits>  
#include <regex>
// std::numeric_limits<int>::max(), min()
// std::setprecision(X)
// std::islower(), std::isupper(), std::tolower, std::toupper
// std::accumulate(m.begin(), m.end(), 0LL);
// std::sort(begin(),end()) // 0, 1, 2, 3 ,4 (default, less);
// std::priority_queue<int, vector<int>, less<int>> que; // 4, 3, 2, 1 (defualt, less) : 
// if ( std::regex_match(S, std::regex("(dream|dreamer|erase|eraser)*")) ) std::cout << "YES" << std::endl;
 
#define rep(i, n) for(int i = 0; i < (int)(n);    i++)
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vvi;

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


int main (void) {
  std::cin.tie(0);  std::ios::sync_with_stdio(false);
  ll A, B; std::cin >> A >> B;
  std::cout << lcm(A,B) << std::endl;
  return 0;
}


Measured memory_bytes: 256
//                             In The Name Of Allah
#include <bits/stdc++.h>
#define	ss second
#define ff first
#define use_fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ret(n) return cout << n, 0
#define se(n) cout << setprecision(n) << fixed
#define pb push_back
#define int long long
#define ld long double
using namespace std; 
 
const int N = 2e5 + 1000, OO = 1e9, M = 998244353, P = 6151, SQ = 1000, lg = 12;
typedef pair <int, int> pii;

int32_t main() {
    use_fast;
    int a, b, k;
    cin >> a >> b >> k;
    if(a >= k)
    	cout << a - k << " " << b << endl;
	else
		cout << 0 << " " << max((int)0, b - (k - a)) << endl; 
	return 0; 
}
/*
be carefull :
1- if not solve after 20 min, read again twice
2- after submit read the code again
3- fun with contest
4- ... 
*/	

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

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

        int A = Integer.parseInt(sc.nextLine());
        int B = Integer.parseInt(sc.nextLine());
        int C = Integer.parseInt(sc.nextLine());
        int X = Integer.parseInt(sc.nextLine());

        int ans = 0;
        for (int i = 0; i <= A; i++) {
            for (int j = 0; j <= B; j++) {
                for (int j2 = 0; j2 <= C; j2++) {
                    int sum = i * 500 + j * 100 + j2 * 50;
                    if (sum == X) {
                        ans++;
                    }
                }
            }
        }
        System.out.println(ans);
    }
}


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

ll pow(ll k, ll n) {
  ll p = 1;
  for (ll i = 0; i < n; i++) {p*=k;}
  return p;
}

int main() {
  ll N; cin >> N;
  vector<ll> v = {3,5,7};
  ll res = 0;
  
  for (ll D = 1; D < 10; D++) {
    for (ll bits = 0; bits < pow(3,D); bits++) {
      ll d[D];
      ll n = bits;
      set<ll> s;
      for (ll j = D-1; j >= 0; j--) {
        d[j] = n/pow(3, j);
        n = n -d[j]*pow(3, j);
        s.insert(d[j]);
      }
      ll num = 0;
      for (ll j = 0; j < D; j++) {
        num += pow(10,j)*v[d[j]];
      }
      if (num <= N && s.count(0) && s.count(1) && s.count(2)) {res++;}
    }
  }
  cout << res << endl;
}

Measured memory_bytes: 256
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
 
int main() {
    ll N, X, M; scanf("%lld%lld%lld", &N, &X, &M);

    vector<ll> mod(M);
    ll x = X, ans = 0;
    for(ll i=0; i<N; i++) {
        // すでに訪れたことがある
        if(mod[x]) {
            vector<ll> rec(1, 0);
            ll y = x;
            while(true) {
                ll s = (rec.back());
                rec.emplace_back(s + y);
                y = y*y % M;
                if(y == x) break;
            }
            ll p = rec.size() - 1;
            ll m = (N-i) % p, d = (N-i) / p;
            ans += d*rec.back() + rec[m];
            break;
        }
        else {
            mod[x] = true;
            ans += x;
            x = x*x % M;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 4600
def main():
    H, N = map(int, input().split())
    A = list(map(int, input().split()))

    damage = 0

    for i in range(N):
        damage += A[i]
        if damage >= H:
            print("Yes")
            exit(0)

    print("No")


if __name__ == "__main__":
    main()


Measured memory_bytes: 13964
#include <bits/stdc++.h>
#define watch(x) cout <<(#x)<<" is "<<(x)<<endl
#define debug cout <<"hi"<<endl
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
int main(){
  int k; cin >>k;
  queue<ll>q; ll ans;
  for(int i=1; i<10; i++) q.push(i);
  while(k--){
    ll x=q.front();
    if(x%10) q.push(10*x+(x%10)-1);
    q.push(10*x+(x%10));
    if(9-x%10) q.push(10*x+(x%10)+1);
    if(!k) ans=x;
    q.pop();
  }
  cout <<ans;
return 0;}

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


#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define lmax1 9223372036854775807
#define max1 2147483647
#define lmin1 -9223372036854775808
#define pi 3.141592653589793116
#define mod 1000000007

int a[26];
long long int dp[26][2];
long long int go(int i,int j)
{
	if(i>=26)
		return 1;
	if(dp[i][j]!=-1)
		return dp[i][j];
	else
	{	int ans;
			ans=(go(i+1,0)+(a[i]*1ll*go(i+1,1))%mod)%mod;
		dp[i][j]=ans;
		return ans;
	}
}
int main() {
	off;
	
	int n;
	string s;
	cin>>n>>s;
	memset(dp,-1,sizeof(dp));
	memset(a,0,sizeof(a));
	for(int i=0;i<n;i++)
	{
		++a[s[i]-'a'];
	}
	cout<<(go(1,0)+(a[0]*1ll*go(1,1))%mod)%mod-1;
  	return 0;
}

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

using ll = long long;
#define REP(i,n) for(int i=0; i < (n); ++i)
#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)

#define INF 1e9


int main(){
  string S;
  cin >> S;
  int ans = 0;
  int cnt = 0;
  REP(i, S.size()){
    if(S[i] == 'A' || S[i] == 'C' || S[i] == 'G' || S[i] == 'T'){
      cnt++;
    }
    else{
      cnt =0;
    }
    ans = max(ans, cnt);
  }
  cout << ans << endl;
  return 0;
}


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

// ショートハンド
// #define ll intに変えればメモリ節約
#define ll long long

/* 
 * ループ系のマクロ
 * `REP(i, 10) { cout << i; }`のように、変数名,回数で動く
 */
#define REPS(i, a, n) for (ll i = (a); i < (ll)(n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (ll i = (a); i >= (ll)(n); --i)
#define DEP(i, n) DEPS(i, n, 0)
#define EACH(i, n) for (auto&& i : n)

/*
 * 正規表現系
 */
#define MATCH(_str, _ptn) regex_match(_str, regex(_ptn))
#define REPLACE(_str, _ptn, _replacement) regex_replace(_str, regex(_ptn), _replacement)

const int MOD = 1e9 + 7;

ll a,b,n,x;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> a >> b >> n;
    x = min(b - 1, n);
    cout << fixed << a * x / b - x / b;
}

Measured memory_bytes: 3632
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
 
int main() {
  int N;
  long x, a[101] = {0};
  cin >> N >> x;
  for (int i = 0; i < N; i++) cin >> a[i];
  sort(a, a + N);
  int res = 0;
  for (res = 0; res < N; res++) {
    x -= a[res];
    if (x < 0) break;
  }
  if (x > 0) res--;
  cout << res << endl;
  return 0;
}

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

using namespace std;

#define INF 1000000007
#define LINF (1LL << 60)
#define PI 3.14159265358979

typedef long long i64;
typedef pair<i64,i64> P;

inline i64 mod(i64 a, i64 m) { return (a % m + m) % m; }

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



string s;
void solve(){
	cin >> s;
	int n = s.size();
	
	for(int i = 2; i < n; i+=2){
		bool ok = 1;
		for(int j = 0; j < n/2-i/2; j++){
			if(s[j] != s[n/2-i/2+j]) ok = 0;
		}
		if(ok){
			cout << n-i << endl;
			return;
		}
	}
}

int main(){
	std::cin.tie(0);
	std::ios::sync_with_stdio(false);
	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
	
	return 0;
}


Measured memory_bytes: 256
#include<stdio.h>

int main(void){
    int n,i,ans=1,j,lev;
    scanf("%d",&n);
    int p[n];
    for(i=0;i<n;i++){
        scanf("%d",&p[i]);
    }
    lev = p[0];
    for(i=1;i<n;i++){
        if(lev>=p[i]){
            ans++;
            lev = p[i];
        }
    }
    printf("%d\n",ans);
    return 0;
}



Measured memory_bytes: 896
#include <bits/stdc++.h>
#define rep(i,l,n) for(int i=l;i<n;i++)
#define rer(i,l,n) for(int i=l;i<=n;i++)
#define all(a) a.begin(),a.end()
#define o(a) cout<<a<<endl
#define pb(a) push_back(a)
#define mk(a,b) make_pair(a,b)
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main(){
	while(1){
		int a,ans=0,m[6]={500,100,50,10,5,1};
		cin>>a;
		if(a==0) break;
		a=1000-a;
		while(1){
			if(a==0) break;
			rep(i,0,6){
				if(a>=m[i]){
					ans++; a-=m[i]; break;
				}
			}
		}
		o(ans);
	}
}

Measured memory_bytes: 1160
from heapq import *

Q=[]
teast=[]
X,Y,Z,K=list(map(int,input().split(" ")))

for i in range(3):
    teast.append(list(map(int,input().split(" "))))

A=sorted(teast[0],reverse=True)
B=sorted(teast[1],reverse=True)
C=sorted(teast[2],reverse=True)

arg_hash={}
heappush(Q,(-A[0]-B[0]-C[0],0,0,0))
arg_hash[(0,0,0)]=1

for k in range(K):
    ans=heappop(Q)
    a,b,c=ans[1:]
    print(-ans[0])
    argA=(a+1,b,c)
    if a <X-1 and( not argA in arg_hash):
        arg_hash[argA]=1
        heappush(Q,(-A[a+1]-B[b]-C[c],)+argA)
    argB=(a,b+1,c)
    if b <Y-1 and( not argB in arg_hash):
        arg_hash[argB]=1
        heappush(Q,(-A[a]-B[b+1]-C[c],)+argB)
    argC=(a,b,c+1)
    if c <Z-1 and( not argC in arg_hash):
        arg_hash[argC]=1
        heappush(Q,(-A[a]-B[b]-C[c+1],)+argC)

Measured memory_bytes: 4864