akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

#define MEM(a, b) memset(a, (b), sizeof(a))
#define PI 3.1415926535897932384626433832795
#define endl '\n'
#define exists(s, e) (s.find(e)!=s.end())
#define WHILE(n)
#define pb push_back
#define f first
#define s second
#define UNI(vec) vec.erase(std::unique(vec.begin(), vec.end()),vec.end());
#define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
char _;
typedef long long ll;
typedef long double ld;
typedef std::pair<int, int> pii;
typedef std::pair<int, pii> piii;

const int INF = 0x3f3f3f3f;
#define MAXN 100001
using namespace std;
int n,m,ans;
int dp[MAXN];
vector<int> a[MAXN];

int dfs(int cur){
    if(dp[cur]) return dp[cur];
    int an = 0;
    for(int i : a[cur]){
        dfs(i);
        an=max(an, dp[i]+1);
    }
    return dp[cur] = an;
}

int main(){
    cin.sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for (int i =0,x,y; i < m; i++){
        cin>>x>>y;
        a[x].pb(y);
    }
    for(int i = 1; i <= n; i++){
        ans = max(ans, dfs(i));
    }
    cout<<ans;
}


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

using ll = long long;
#define debug(x) cerr << #x << ": " << x << endl;
#define line() cerr << "---------------" << endl;

int main() {
   int n, k;
   cin >> n >> k;
   map<int, int> m;
   for (int i = 0; i < n; i++) {
      int x;
      cin >> x;
      m[x]++;
   }

   vector<int> v;
   set<int> s;
   for (auto x : m) {
      v.push_back(x.second);
      s.insert(x.first);
   }

   if (s.size() <= k)  {
      cout << 0 << endl;
      return 0;
   }

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

   int ans = 0;
   for (int i = 0; i < s.size()-k; i++) {
      ans += v[i];
   }

   cout << ans << endl;

   return 0;
}

Measured memory_bytes: 19828
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int f(int x,int y,int a,int b){
	if( x >= 5 && y >= 2 ) return ( a * x + b * y ) * 8 / 10;
	return ( a * x + b * y );
}

int main(){
	int N;
	while(cin >> N&&N){
		string s;
		cin >> s;
		string te[100];
		string ba;
		for(int i = 0 ; i < s.size() ; i++){
			int u = i % N;
			te[u] += s[i];
			if( s[i]=='S' ){
				ba += te[u];
				te[u]="";
			}
			if( s[i]=='L' ){
				te[u]+= ba;
				ba = "";
			}
		}
		vector<int> a;
		for(int i = 0 ; i < N ; i++){
			a.push_back(te[i].size());
		}
		sort(a.begin(),a.end());
		for(int i = 0 ; i < N ; i++){
			cout << a[i] << " ";
		}
		cout << ba.size() << endl;
		
	}
	
}

Measured memory_bytes: 1240
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,K,Q,A,B,C,L,R;
string S,T;
const ll MOD = (1e+9) + 7;
const ll INF = 1LL << 60;
typedef pair<ll,ll> P;

typedef vector<P> vp;
typedef vector<vp> matP;


int main(){
    cin>>A>>B>>S;
    bool yes = true;
    rep(i,A+B+1){
        if((i == A) && (S[i] != '-')) yes = false;
        if((i != A) && (S[i] == '-')) yes = false;
    }
    cout<<(yes ? "Yes" : "No")<<endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<ctime> //printf("\n%lldms",clock());
#include<cmath>
#include<queue>
//#include<bits/stdc++.h>
//#define lijyya 300030
#define oo1 0x3f3f3f3f
#define oo2 0x7fffffff
#define f1(i,a,b) for (register int i=a;i<=b;++i)
#define f2(i,a,b) for (register int i=a;i<b;++i)
#define f3(i,a,b) for (register int i=a;i>=b;--i)
#define f4(i,a,b) for (register int i=a;i>b;--i)
using namespace std;
typedef long long ll;
template<typename T> inline void checkmax(T &x,T &y){
    x=max(x,y);
}
template<typename T> inline void checkmin(T &x,T &y){
    x=min(x,y);
}
inline void read(long long &x){
	char ch=getchar(); x=0;int f=1;
	while(ch<'0'||ch>'9'){
		if(ch=='-') f=-1;ch=getchar();
	} 
	while(ch>='0'&&ch<='9'){
		x=x*10+ch-'0',ch=getchar();
	}
	x=x*f;
}

long long n,a[90];
int main(){
	read(n);
	a[0]=2;
	a[1]=1;
	f1(i,2,88){
		a[i]=a[i-1]+a[i-2];
	}
	return printf("%lld\n",a[n]),0;
}












Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
    string S;
    cin>>S;
    int x=0;

        for(int i=0 ; i<S.size() ; i++){
        if(S[i]=='+'){
           x++;
           }
           else{
            x--;
           }
    }
    cout << x << endl;
return 0;
}


Measured memory_bytes: 3576
import java.util.Scanner;

public class Main {
    static final int MODULUS = 1_000_000_007;

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

        int n = sc.nextInt();
        int m = sc.nextInt();
        int[] x = readArray(sc, n);
        int[] y = readArray(sc, m);

        System.out.println(solve(x, y));

        sc.close();
    }

    static int[] readArray(Scanner sc, int size) {
        int[] result = new int[size];
        for (int i = 0; i < result.length; ++i) {
            result[i] = sc.nextInt();
        }

        return result;
    }

    static int solve(int[] x, int[] y) {
        return multiplyMod(computeSideSum(x), computeSideSum(y));
    }

    static int computeSideSum(int[] values) {
        int result = 0;
        int prefixSum = values[0];
        for (int i = 1; i < values.length; ++i) {
            result = addMod(result, subtractMod(multiplyMod(values[i], i), prefixSum));

            prefixSum = addMod(prefixSum, values[i]);
        }

        return result;
    }

    static int addMod(int a, int b) {
        return (a + b) % MODULUS;
    }

    static int subtractMod(int a, int b) {
        return ((a - b) % MODULUS + MODULUS) % MODULUS;
    }

    static int multiplyMod(int a, int b) {
        return (int) ((long) a * b % MODULUS);
    }
}

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

typedef long long LL;

const int N=500;

int n;

void into(){
  scanf("%d",&n);
}

int ans[N+9][N+9];

void Divide_ans(int l,int r,int d){
  if (l==r) return;
  int mid=l+r>>1;
  for (int i=l;i<=mid;++i)
	for (int j=mid+1;j<=r;++j) ans[i][j]=d;
  Divide_ans(l,mid,d+1);
  Divide_ans(mid+1,r,d+1);
}

void work(){
  Divide_ans(1,n,1);
}

void outo(){
  for (int i=1;i<n;++i){
	for (int j=i+1;j<=n;++j) printf("%d ",ans[i][j]);
	puts("");
  }
}

int main(){
  into();
  work();
  outo();
  return 0;
}

Measured memory_bytes: 4640
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
 
typedef pair<int,int> P;
struct edge{int to, id;};
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mod 1000000007

using graph = vector<vector<ll>>;


int main(){
    int x;
  cin >> x;
    int ans = 10 - x / 200;
    cout << ans << endl;
}

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

int main()
{
	int k;
	string s;
	cin>>k;
	cin>>s;
	if(s.size()>k)
	{
		for(int i=0;i<k;i++)
		   cout<<s[i];
		cout<<"...";
	}
	else
	{
		for(int i=0;i<s.size();i++)
		cout<<s[i];
	}
	return 0;
}


Measured memory_bytes: 3624
n,k=map(int,input().split())
s=input()
l=[]
cnt=1
for i in range(n-1):
  if s[i]!=s[i+1]:
    l.append(i+1)
l=[0]+l+[n]
ans=0
for i in range(len(l)-1):
  if s[l[i]]=='0':
    ans=max(ans,l[min(i+2*k,len(l)-1)]-l[i])
  else:
    ans=max(ans,l[min(i+2*k+1,len(l)-1)]-l[i])
print(ans)

Measured memory_bytes: 8692
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int) x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1000000007;
template<class T> void print(const T& t){ cout << t << endl; }
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; }

int main(){
  ll ans = 0;
  ll n;
  cin >> n;
  vi p(n);
  rep(i,n){
    cin >> p[i];
  }
  ll min = MOD;
  rep(i,n){
    if(min>=p[i]){
      ans++;
    }
    chmin(min, p[i]);
  }
  print(ans);
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const int SIZE = 10;

int n, m;
bool g[SIZE][SIZE];

int main() {
    cin >> n >> m;
    for(int i = 1; i <= m; i++) {
        int a, b;
        cin >> a >> b;
        g[a][b] = g[b][a] = true;
    }

    vector<int> v(n);
    iota(v.begin(), v.end(), 1);
    ll ans = 0;
    
    do {
        bool ok = true;
        for(int i = 0; i < v.size()-1; i++)
            if(!g[v[i]][v[i+1]])  ok = false;
        if(ok)  ans++;
    } while(next_permutation(v.begin()+1, v.end()));

    cout << ans << endl;
}

Measured memory_bytes: 256
/*#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
*/
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define Fast ios_base::sync_with_stdio(false);cin.tie(NULL);
typedef pair<ll , pair<ll, ll> > pi;
int pow(int x,int y){
    int res=1;
    while(y){
        if(y&1) res*=x;
        y>>=1;
        x*=x;
    }
    return res;
}   
struct Compare {
    constexpr bool operator()(pi const & a,
                              pi const & b) const noexcept
    { return a.first < b.first || (a.first == b.first && a.second.first > b.second.first); }
 
};
void prefix_function( string s,ll arr[] )
{
     long long border=0; arr[0]=0;
    for(long long i=1;i<s.size();i++)
    {
      while(border>0 && s[i]!=s[border])
        border=arr[border-1];
     
     if(s[i]==s[border])
         border++;
      else
       border=0;
      
      arr[i]=border;
    }
}//send mod-2 for a^-1 if mod is a prime number

ll mod=1000000007;
ll add( ll a , ll b)
{
    return (((a%mod)+(b%mod))%mod);
}
ll mul(ll a,ll b)
{
  return (((a%mod)*(b%mod))%mod);
}
ll binpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) res = mul(res, a);
        a = mul(a, a);
        b >>= 1;
    }
    return res;
}
ll subs(ll a,ll b)
{
    return (((a%mod)-(b%mod)+mod)%mod);
}
ll dv(ll a,ll b)
{
    ll inv=binpow(b,mod-2);
    return mul(a,inv);
}

ll dsu_arr[100000];
ll dsu_sz[100000];
void dsu(ll n)
{
    for(ll i=0;i<=n;i++)
    {
        dsu_arr[i]=i;
        dsu_sz[i]=1;
    }
}
ll find(ll x)
{
     ll root=x;
    while (root!=dsu_arr[root])
    {
        root=dsu_arr[root];
    }
    while(x!=dsu_arr[x])
    {
        dsu_arr[x]=root;
        x=dsu_arr[x];
    }
    return root;
}
ll merge(ll x,ll y)
{
    ll root1=find(x);
    ll root2=find(y);
    
    if(root1==root2)
    return 0ll;
 
    if(dsu_sz[x]>dsu_sz[y]){
    dsu_arr[root2]=root1;
    dsu_sz[root1]+=dsu_sz[root2];
    }
    else
    {
         dsu_sz[root2]+=dsu_sz[root1];
        dsu_arr[root1]=root2;
    }
    return 1ll;
}
/*
vector<ll>adj[100005];
bool vis[100005];
ll dist[100005];
void bfs(ll c)
{
  vis[c]=true;
  dist[c]=0;
  q.push(c);
while(!q.empty())
{
    ll x=q.front();
    q.pop();
   
    for(ll i=0;i<adj[x].size();i++)
    {
        ll y=adj[x][i];
        if(!vis[y])
        {
            vis[y]=true;
            dist[y]=dist[x]+1;
            
            q.push(y);
        }
    }
 
}
}
*/

int main()
{
    Fast
ll n;
cin>>n;
ll dp[n+5];

for(ll i=0;i<=n;i++)
{
    dp[i]=0;
}
for(ll i=3;i<=n;i++)
{
   
    for(ll j=3;j<=i;j++)
    {
        if(j==i)
        {
            if(j>=3)
            dp[i]=add(dp[i],1ll);
        }
        else
      dp[i]=add(dp[i],dp[i-j]);
      
    }
   
}
cout<<dp[n]<<endl;


}



   
      
    
        



Measured memory_bytes: 3640
import sys
input = sys.stdin.readline

N = int(input())
A = [list(map(int,input().split())) for i in range(N)]

for i in range(1,N+1):
    for j in range(N-1):
        a,b = sorted([i,A[i-1][j]])

        A[i-1][j] = a*(N-1)+b-N-1

E=[[] for i in range(N*N)]
EREV=[[] for i in range(N*N)]

EDGEIN=[0]*(N*N)

for i in range(N):
    for j in range(1,N-1):
        E[A[i][j-1]].append(A[i][j])
        EREV[A[i][j]].append(A[i][j-1])
        EDGEIN[A[i][j]]+=1

from collections import deque
Q=deque()

for i in range((N-1)**2):
    if EDGEIN[i]==0 and len(E[i])>0:
        Q.append(i)

TOP=[]
while Q:
    x=Q.pop()
    TOP.append(x)
    for to in E[x]:
        EDGEIN[to]-=1
        if EDGEIN[to]==0:
            Q.append(to)


H=[1]*(N**2)
for x in TOP[::-1]:

    for fr in EREV[x]:
            
        if H[fr]<H[x]+1:
            H[fr]=H[x]+1

if min(EDGEIN)<0 or max(EDGEIN)>0:
    print(-1)
else:
    print(max(H))


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

using namespace std;

const int N = 123456;

int n, m, a[N], p[N], go[N];
bool visit[N];
long long k, answer[N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    p[i] = i;
  }
  for (int i = n; i; --i) {
    a[i] -= a[i - 1];
  }
  cin >> m >> k;
  for (int i = 1, x; i <= m; ++i) {
    cin >> x;
    swap(p[x], p[x + 1]);
  }
  for (int i = 1; i <= n; ++i) {
    go[p[i]] = i;
  }
  for (int i = 1; i <= n; ++i) {
    if (!visit[i]) {
      vector<int> cycle(1, i);
      for (int x = go[i]; x != i; x = go[x]) {
        cycle.push_back(x);
      }
      for (auto x : cycle) {
        visit[x] = true;
      }
      int times = k % cycle.size(), y = cycle[0];
      while (times--) {
        y = go[y];
      }
      for (auto x : cycle) {
        answer[y] = a[x];
        y = go[y];
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    answer[i] += answer[i - 1];
    cout << answer[i] << '\n';
  }
  return 0;
}

Measured memory_bytes: 3440
#include <stdio.h>

int saidai ( int a , int b ) {
	int tmp , c;
	if ( a > b ) {
		tmp = a;
		a = b;
		b = tmp;
	}
	while ( b > 0 ) {
		c = a % b;
		a = b;
		b = c;
	}
	return a;
}

int main (void) {
	int a , b , min , max;
	
	while ( scanf("%d %d",&a,&b) != EOF ) {
		max = saidai(a,b);
		min = a / max * b;
		
		printf("%d %d\n",max,min);
	}
	return 0;
}

Measured memory_bytes: 0
S = input()
count = 0

if S[0] == '0':
	for i in range(1, len(S)):
		if i%2 == 0:
			if S[i] == '1':
				count += 1
		else:
			if S[i] == '0':
				count += 1
else:
	for i in range(1, len(S)):
		if i%2 == 0:
			if S[i] == '0':
				count += 1
		else:
			if S[i] == '1':
				count += 1

print(count)

Measured memory_bytes: 3188
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
using namespace std;

template<typename T>
struct BIT{//1-indexed
private:
  vector<T> data;
  const int n;
public:
  BIT(int sz) : data(sz+1,0), n(sz) {}
  BIT(vector<T> t) : data(t), n((int)t.size()) {}
  T sum(int i){
    T s = 0;
    while(i > 0){
      s += data[i];
      i -= i&(-i);
    }
    return s;
  }

  T sum(int i, size_t j){ return sum(j) - sum(i-1); }//sum[i,j)

  void add(int i, T x){
    while(i <= n){
      data[i] += x;
      i += i&(-i);
    }
  }
};

int main(){
  long long N, K;
  cin >> N >> K;
  vector<long long> A(N);
  map<long long,int> M;
  for(int i = 0; i < N; ++i){
    cin >> A[i];
    A[i] -= K;
    if(i > 0) A[i] += A[i-1];
    ++M[A[i]];
  }
  ++M[0];
  int t = 0;
  for(auto &e : M) e.second = t++;
  BIT<long long> bit(t);
  long long ans = 0;
  bit.add(M[0]+1,1);
  for(int i = 0; i < N; ++i){
    ans += bit.sum(M[A[i]]+1);
    bit.add(M[A[i]]+1,1);
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 15872
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <map>
#include <vector>
#include <deque>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <memory>

using namespace std;

#if 1
#define PRINT(x) { cout << #x << ": " << (x) << endl; }
#define PRINT_CONTAINER(x)  { cout << #x << ": ";                       \
        for (size_t ix = 0; ix < (x).size(); ++ix) cout << (x)[ix] << ", "; \
        cout << endl; }
#else
#define PRINT(x)
#define PRINT_CONTAINER(x)
#endif

typedef pair<int, int> p;

int n, m;
map<string, int> s;

int main(int argc, char *argv[]) {
    char buf[100];
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%s", buf);
        auto it = s.find(buf);
        if (it == s.end()) {
            s[buf] = 1;
        } else {
            it->second += 1;
        }
    }
    scanf("%d", &m);
    for (int i = 0; i < m; ++i) {
        scanf("%s", buf);
        auto it = s.find(buf);
        if (it == s.end()) {
            s[buf] = -1;
        } else {
            it->second -= 1;
        }
    }
    int x = 0;
    for (auto it = s.begin(); it != s.end(); ++it) {
        x = max(it->second, x);
    }
    printf("%d\n", x);
}


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

int main(){
	cin.tie(0),ios::sync_with_stdio(false);
	int n,Q; string s; cin>>n>>s>>Q;
	vector<set<int>>st(26);
	for(int i=0;i<n;++i){
		st.at(s.at(i)-'a').insert(i);
	}
	for(int i=0;i<Q;++i){
		int q; cin>>q;
		if(q==1){
			int j; char c; cin>>j>>c; --j;
			st.at(s.at(j)-'a').erase(j);
			s.at(j)=c;
			st.at(s.at(j)-'a').insert(j);
		}
		else{
			int l,r; cin>>l>>r; --l;
			int ans=0;
			for(int j=0;j<26;++j){
				if(!st.at(j).size())continue;
				auto itr=st.at(j).lower_bound(l);
				if(itr!=st.at(j).end()&&*itr<r)++ans;
			}
			cout<<ans<<"\n"s;
		}
	}
}


Measured memory_bytes: 27216
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		TreeMap<Long, Integer> map = primeFactorize(n);
		int ans = 0;
		for (Entry<Long, Integer> entry : map.entrySet()) {
			int value = entry.getValue();
			int index = 1;
			while (value >= index) {
				ans++;
				value -= index;
				index++;
			}
		}
		System.out.println(ans);
		sc.close();
	}

	static TreeMap<Long, Integer> primeFactorize(long n) {
		TreeMap<Long, Integer> map = new TreeMap<>();
		if (n < 2) {
			return map;
		}

		for (long i = 2; i * i <= n; i++) {
			if (n % i != 0) {
				continue;
			}
			int times = 0;
			while (n % i == 0) {
				times++;
				n /= i;
			}
			map.put(i, times);
		}
		if (n != 1) {
			map.put(n, 1);
		}
		return map;
	}
}

Measured memory_bytes: 35888
#include <stdio.h>

int main(void)
{
    int a, b, i, j, k;

    while(1){

    scanf("%d %d", &a, &b);

    if(a == 0 && b == 0)
    break;

    for(i = 1; i <= a; i++)
    {
        if(i == 1||i == a)
        {
            for(k = 1; k <= b; k++)
            {
                printf("#");
            }
        }
        else if(i >= 2 || i <= a - 1)
        {
            printf("#");

            for(j = 1; j < b-1; j++)
            {
                printf(".");
            }

            printf("#");
        }
        

        printf("\n");
    }

    printf("\n");

    }

    return 0;
}


Measured memory_bytes: 2484
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ll  long long
#define INF (ll)1e18 + 50
#define MAXN (int)1e5 + 50
#define fi first
#define se second
#define MOD 1000000007
#define all(v)((v).begin()), ((v).end())
#define A 911382323
#define B 972663749
#define int long long


int mmc(int a, int b){
	return a * b/ __gcd(a, b);
}

int32_t main() {
    int a, b; cin >> a >> b;
    
    cout << mmc(a, b);
    return 0;
}

Measured memory_bytes: 256
n=int(input())
ac=0
wa=0
tle=0
re=0
for i in range(n):
    s=input()
    if s == "AC":
        ac+=1
    elif s == "WA":
        wa+=1
    elif s == "TLE":
        tle+=1
    else:
        re+=1
print("AC x " + str(ac))
print("WA x " + str(wa))
print("TLE x " + str(tle))
print("RE x " + str(re))


Measured memory_bytes: 9200
#include <bits/stdc++.h>
#define int long long
using namespace std;
int MOD = 1000000007;
int pw(int a, int b){
    if (b == 0) return 1;
    if (b == 1) return a;
    if (b%2) return ((a * pw(a, b-1)) % MOD);
    int rs = pw(a, b/2);
    return (rs*rs) % MOD;
}
pair<int, int> solve(vector<int> &v){
    int n = v.size();
    int mn = 1e12;
    for (int i=0; i < n; i++) mn = min(mn, v[i]);
    int holes = 0;
    vector<pair<int, int> > ress;
    vector<int> a, b;
    for (int i=0; i < n; i++){
        if (v[i] > mn) a.push_back(v[i] - mn);
        else{
            if (a.size() > 0){
                ress.push_back(solve(a));
                b.push_back(a.size());
            }
            a.clear();
            holes++;
        }
    }
    if (a.size() > 0){
        ress.push_back(solve(a));
        b.push_back(a.size());
    }
    int N = 2;
    for (int i=0; i < ress.size(); i++) {
        //cout << ress[i].first << " " << ress[i].second << endl;
        N *= ress[i].second;
        N %= MOD;
    }
    N %= MOD;
    int on = N;
    N *= pw(2, mn-1);
    N %= MOD;
    int R = pw(2, holes);
    for (int i=0; i < ress.size(); i++){
        int H = (ress[i].first + 2 * ress[i].second) % MOD;
        R *= H;
        R %= MOD;
    }
    R -= on;
    R %= MOD;
    if (R < 0) R += MOD;
    return {R, N};
}
signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i=0; i < n; i++){
        cin >> v[i];
    }
    pair<int, int> p = solve(v);
    //cout << p.first << " " << p.second << endl;
    cout << (p.first + p.second) % MOD << endl;
    return 0;
}


Measured memory_bytes: 384
//Created by Kira

#include<bits/stdc++.h>
using namespace std;

#define IOS cin.sync_with_stdio(0);cin.tie(0); cout.tie(0);
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define pii pair<int,int> 
#define pb push_back
#define vi vector<int> 
using ll = long long;

const int mod = 1e9 + 7;

ll pwr(ll a, ll b);
    
//dp[i] - True if first player has i stones and wins the game


int main(){
   IOS


   int n, k; cin>>n>> k;

   vi a(n);
   for(int & x : a)cin>>x;

   vector<bool> dp(k+1, false);

    for(int stones = 0; stones <= k; stones++){
        for(int  i : a){
            if(stones - i >= 0 && !dp[stones-i])
                dp[stones] = true;
        }
    }



   cout<<(dp[k] ? "First" : "Second")<<"\n";















}

ll pwr(ll a, ll b){
   a %= mod;	//Remove mod if not required
   ll res = 1;
   while(b > 0){
    if(b&1)
        res = res * a % mod;
        a = a * a % mod;
    b >>= 1;
    }

    return res;
}



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

score = 0
for i in range(n):
    r = b[i] - a[i]
    if r < 0:
        score += b[i]
    else:
        score += a[i]
        score += min(a[i + 1], r)
        a[i + 1] = max(0, a[i + 1] - r)

print(score)

Measured memory_bytes: 18624
#include <stdio.h>
#include <stdlib.h>
#define N1 100
#define N2 99

void push(int);
int pop(void);
int isEmpty(void);
int isFull(void);

int top,S[N1];

int main(){
  int n, n1, n2;
  char c[N1];

  while(scanf("%s", c) != EOF){
    if(c[0] != '+' && c[0] != '-' && c[0] != '*'){
      n = atoi(c);
      push(n);
    }
    else if(c[0] == '+' || c[0] == '-' || c[0] == '*'){
      n2 = pop();
      n1 = pop();
      if(c[0] == '+') push(n1 + n2);
      if(c[0] == '-') push(n1 - n2);
      if(c[0] == '*') push(n1 * n2);
    }
      if(getchar() == '\n') break;
    
  }

  printf("%d\n", pop());

  return 0;
}

void push(int x){
  if(isFull() == -1){
    printf("Stack overflow\n");
    exit(1);
  }
  else{
    S[top] = x;
    top++;
  }
}

int pop(){
  if(isEmpty() == -1){
    printf("Stack underflow\n");
    exit(1);
  }
  else{
    top--;
    return S[top];
  }
}

int isEmpty(){
  if(top == 0) return -1;
  else return 0;
}

int isFull(){
  if(top == N1) return -1;
  else return 0;
}

Measured memory_bytes: 548
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n = int(input())

def divisors(n):
    ret = set()
    m = 1
    while m*m <= n:
        if n%m==0:
            # ret.add(m)
            if n//m > m+1:
                ret.add(n//m)
        m += 1
    return ret
ndivs = list(divisors(n))

ans = sum(ndivs) - len(ndivs)*1

print(ans)

Measured memory_bytes: 3188
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
const int inf = 1001001001;



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

  int a = 0;
  for(int i = 1; i < k+10; i++) {
    a = a*10+7;
    a %= k;
    if(a == 0) {
      cout << i << endl;
      return 0;
    }
  }
  
  cout << -1 << endl;
  return 0;
}


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

public class Main {
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		PrintWriter out = new PrintWriter(System.out);
		int n = sc.nextInt();
		int k = sc.nextInt();
		int pos = 1;
		int ans = 0;
		for(int i = 0; i < n; i++) {
			int num = sc.nextInt();
		}	
		while(pos < n) {
			pos += k - 1;
			ans++;
		}
		out.println(ans);
		
		out.close();
	}

	//2点間の距離を返す
	static double distance(int x1, int y1, int x2, int y2) {
		double w = Math.pow(x1- x2, 2);
		double h = Math.pow(y1- y2, 2);
		return Math.sqrt(w + h);
	}

static class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;
    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }
    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}
    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}
    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}
    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }
    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }
    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }
    public double nextDouble() { return Double.parseDouble(next());}
}
}


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

class node{
    int key;
    node *left;
    node *right;

    public:
    node();

    node(int);
    node* Insert(node *,int);
    void inorderprint(node *);
    void preorderprint(node *);
    bool find(node *,int);
};

vector<int> print;
bool flag = false;

node::node() : key(0),left(NULL),right(NULL){}

node::node(int v){
    key = v;
    left = right = NULL;
}

node* node::Insert(node *root,int value){
    if(!root){
        return new node(value);
    }
    if(value>root->key){
        root->right = Insert(root->right,value);
    }else{
        root->left = Insert(root->left,value);
    }

    return root;
}

void node::inorderprint(node *root){
    if(!root) return;
    inorderprint(root->left);
    print.push_back(root->key);
    inorderprint(root->right);
}
void node::preorderprint(node *root){
    if(!root) return;
    print.push_back(root->key);
    preorderprint(root->left);
    preorderprint(root->right);
}
bool node::find(node *root,int value){
    if(!root) return false;
    if(root->key==value) return true;
    if(root->key>value) find(root->left,value);
    else find(root->right,value);
}

int main(){
    int n;cin >> n;
    node t,*root = NULL;
    for(int i=0;i<n;i++){
        string s;cin >> s;
        if(i==0){
            int value;cin >> value;
            root = t.Insert(root,value);
            continue;
        }
        if(s[0]=='p'){
            print.clear();
            t.inorderprint(root);
            for(int pr=0;pr<print.size();pr++){
                cout << " ";
                cout << print[pr];
                if(pr==print.size()-1) cout << endl;
            }
            print.clear();
            t.preorderprint(root);
            for(int pr=0;pr<print.size();pr++){
                cout << " ";
                cout << print[pr];
                if(pr==print.size()-1) cout << endl;
            }
        }else{
            int value;cin >> value;
            if(s[0]=='i') t.Insert(root,value);
            else{
                flag=false;
                flag = t.find(root,value);
                cout << (flag?"yes":"no") << endl;
            }
        }
    }
    
    return 0;
}


Measured memory_bytes: 17384
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner scan = new Scanner(System.in);

		int A = scan.nextInt();
		int B = scan.nextInt();
		int C = scan.nextInt();
		int K = scan.nextInt();

		if (A >= K) System.out.println(K);
		else if (A + B >= K) System.out.println(A);
		else System.out.println(A - (K - A - B));
	}
}

Measured memory_bytes: 35796
ss = input()
l = len(ss)
aa = ss[:(l-1)//2]
bb = ss[(l+3)//2-1:]
def palindrome(string): return 1 if string==string[::-1] else 0

a = ''.join(map(str,aa))
b=''.join(map(str,bb))
s = ''.join(map(str,ss))

if palindrome(a)==1 and palindrome(b)==1 and palindrome(s)==1:
    print('Yes')
else:
    print('No')

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#include <vector>
#define rep(i,n) for(int i = 0; i < n; i++)
using ll = long long;
using namespace std;
int main(){
  string s,t;
  cin >> s >> t;
  int tlen = t.length();
  int slen = s.length();
  for(int i = slen - tlen; i >= 0; i--){
    int c = 0;
    rep(m, tlen){
      if(t[m] == s[i + m] || s[i + m] == '?') c++;
    }
    if(c == tlen){
      rep(j, slen){
        if(j < i || j >= i + tlen){
          if(s[j] == '?'){
            cout << 'a';
          }else{
            cout << s[j];
          }
        }else{
          cout << t[j - i];
        }
      }
      cout << endl;
      return 0;
    }
  }
  cout << "UNRESTORABLE" << endl;
  return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;
public class Main { //これは累積和だよーん

	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		int n = stdIn.nextInt();
		
		int k = stdIn.nextInt();
		
		int [] p = new int[n];
		
		for(int i = 0; i < n; i++) {
			p[i] = stdIn.nextInt();
		}
		
		
		int[]s = new int[n+1];
		
		for(int i = 0; i < n; i++) {
			s[i+1] = s[i] + p[i];
		}
		
		int max = Integer.MIN_VALUE;
		int index = 0;
		
		for(int i = 0; i < n+1-k; i++) {
			int sum = s[i+k]-s[i];
			if(max < sum) {
				max = sum;
				index = i;
			}
			
		}
		
		double ans = 0;
		
		for(int i = 0; i < k; i++) {
			ans += p[index++]/2.0 + 0.5;
		}
		System.out.println(ans);
	}

}


Measured memory_bytes: 72316
#include <iostream>
#include <vector>
#include <queue>
#include <set>
using namespace std;

struct edge{
    int s,t;
};

int main(){
    int N,M;
    cin >> N >> M;
    vector<edge> edges;
    vector<vector<int>> v(N);
    for(int i=0;i<M;i++){
        int a,b;
        cin >> a >> b;
        a--; b--;
        v[a].push_back(b);
        edges.push_back({a,b});
    }
    vector<int> ans;
    for(auto& x:edges){
        vector<int> dist(N,-1);
        vector<int> pre(N,-1);
        dist[x.t] = 0;
        queue<int> Q;
        Q.push(x.t);
        while(!Q.empty()){
            int now = Q.front(); Q.pop();
            for(auto y:v[now]){
                if(now==x.s && y==x.t) continue;
                if(dist[y]==-1){
                    dist[y] = dist[now]+1;
                    pre[y] = now;
                    Q.push(y);
                }
            }
        }
        if(dist[x.s]==-1) continue;
        int now = x.s;
        set<int> s;
        while(now!=-1){
            s.insert(now);
            now = pre[now];
        }
        int cnt = 0;
        for(auto& y:edges){
            if(s.count(y.s) && s.count(y.t)) cnt++;
        }
        if(cnt==(int)s.size()){
            for(auto y:s) ans.push_back(y);
            break;
        }
    }
    if(ans.empty()){
        cout << -1 << endl;
        return 0;
    }
    cout << (int) ans.size() << endl;
    for(auto x:ans) cout << x+1 << endl;
}

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

// {U}{INT,LONG,LLONG}_{MAX,MIN}
#define INF         INT_MAX/3
#define LLINF       LLONG_MAX/3
#define MOD         (1000000007LL)
#define MODA(a, b)  a=((a)+(b))%MOD
#define MODP(a, b)  a=((a)*(b))%MOD
#define inc(i, l, r)   for(int i=(l);i<(r);i++)
#define dec(i, l, r)   for(int i=(r)-1;i>=(l);i--)
#define pb          push_back
#define se          second
#define fi          first
#define mset(a, b)  memset(a, b, sizeof(a))

using LL  = long long;
using G   = vector<vector<int>>;

int di[] = {0, -1, 0, 1};
int dj[] = {1, 0, -1, 0};
// }}}

int main() {
    cin.tie(0);ios::sync_with_stdio(false);
    int n, a, b;cin >> n >> a >> b;
    cout << min(a*n, b) << endl;
    return 0;
}


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

int main() {

  int a,b,c,d,e;

  cin >>a>>b>>c>>d>>e;
  

 if(a==0){
   cout<<1<<endl;
 }
 else if(b==0){
      cout<<2<<endl;
 }
   else if(c==0){
      cout<<3<<endl;
 } else if(d==0){
      cout<<4<<endl;
 }
   else if(e==0){
      cout<<5<<endl;
 }

 return 0;
}

Measured memory_bytes: 3636
n, c, k = map(int, input().split())
l = [int(input()) for _ in range(n)]
l = sorted(l)
val, ans = l[0], 1
count = 1
for i in l[1:]:
    count += 1
    if i-val > k or count > c:
        ans += 1
        count, val = 1, i
print(ans)

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

int maxhappiness(vector<vector<int>> &h, int n, int prev_activity)
{
    int ans;
    if (n == 0)
        return ans = 0;
    ans = 0;
    for (int i = 0; i < 3; i++)
    {
        if (prev_activity == i)
            continue;
        ans = max(ans, h[n - 1][i] + maxhappiness(h, n - 1, i));
    }
    return ans;
}

int maxhappiness(vector<vector<int>> &h, int n)
{
    vector<vector<int>> dp(n + 1, vector<int>(3, 0));
    dp[0][0] = 0;
    //no vacation no activity
    for (int v = 1; v <= n; v++) //vacations
    {
        for (int activity = 0; activity < 3; activity++)
        {
            if (v == 1)
                dp[v][activity] = h[v - 1][activity];
            else
            {
                for (int j = 0; j < 3; j++)
                    if (activity != j)
                        dp[v][activity] = max(dp[v][activity], h[v - 1][activity] + dp[v - 1][j]);
            }
        }
    }

    int ans = 0;
    for (int i = 0; i < 3; i++)
        ans = max(ans, dp[n][i]);
    return ans;
}

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

    vector<vector<int>> hps(n, vector<int>(3, 0)); //happiness points
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < 3; j++)
            cin >> hps[i][j];
    }
    //objective is to gain maximum happiness points here
    //each activity can't be done for two or more consecutive days
    cout << maxhappiness(hps, n) << "\n";
    return 0;
}

Measured memory_bytes: 11136
from collections import Counter
 
S = raw_input()
 
if len(S) == 2:
    if S[0] == S[1]:
        print 1, 2
    else:
        print -1, -1
else:
    for i in range(len(S)):
        T = S[i:i+3]
        if max(Counter(T).values()) >= 2:
            print i+1, i+3
            break
    else:
        print -1, -1

Measured memory_bytes: 6008
#include <iostream>

using namespace std;

int main()
{
    bool check[100001];
    //vector<int> primes;
    for (int i=2; i<=100000; i++) {
        if (!check[i]) {
            //primes.push_back(i);
            for (int j=i+i; j<=100000; j+=i) {
                check[j] = true;
            }
        }
    }
    int x;
    int ans = 100003;
    cin >> x;
    for (int i=x; i<=100000; i++) {
        if (!check[i]) {
            ans = i;
            break;
        }
    }
   cout << ans << '\n';
   return 0;
}

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

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

template<typename H> bool chmin(H& v1, const H v2) { if (v1 > v2) { v1 = v2; return true; } return false; }
template<typename H> bool chmax(H& v1, const H v2) { if (v1 < v2) { v1 = v2; return true; } return false; }
template<typename H> void read(H& head) { cin >> head; }
template<typename H, typename ...T> void read(H& head, T& ...tail) { cin >> head; read(tail...); }
template<typename H> void write(H head) { cout << head << '\n'; }
template<typename H, typename ...T> void write(H head, T ...tail) { cout << head << " "; write(tail...); }
template<typename ...T> void die(T ...tok) { write(tok...); exit(0); }

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);
	
	int n; read(n);
	vector<ll> a(n);
	rep(i, 0, n) read(a[i]);
	rep(i, 0, n) if (!a[i]) die(0);
	double dp = 1.0;
	ll rp = 1;
	rep(i, 0, n) {
		dp *= a[i];
		rp *= a[i];
		if (dp > 1.1e18) die(-1);
	}
	if (rp > 1'000'000'000'000'000'000ll) die(-1);
	die(rp);
}


Measured memory_bytes: 4444
package main
import "fmt"

func min(a int,b int) int{
	if a>b{
		return b
	} else {
		return a
	}
}
func zet(a int) int{
	if a<0 {
		return a*(-1)
	} else{
		return a
	}
}

func main() {
	var n,tmp int
	fmt.Scan(&n)
	a := []int{}
	tot := 0
	for i:=0;i<n;i+=1{
		fmt.Scan(&tmp)
		tot += tmp
		a = append(a,tmp)
	}
	ans := 1000000007
	tot_a :=0
	for i:=0;i<n;i+=1 {
		tot_a += a[i]
		tot -= a[i]
		ans = min(ans,zet(tot-tot_a))
	}
	fmt.Println(ans)
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
bool num[300000];
int main() {
	for(int i=2;i<548;i++)if(!num[i])for(int j=i*2;j<300000;j+=i)num[j]=1;
	int n;
	while(cin>>n,n){
		int ans=0;
		for(int i=n+1;i<=2*n;i++)if(!num[i])ans++;
		cout<<ans<<endl;
	}
}

Measured memory_bytes: 1452
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sstream>
#include <unordered_map>
#include <vector>


#define INF 4000000000000000000LL
#define MOD 1000000007
#define ALL(x) std::begin(x), std::end(x)


int main(int argc, char** argv)
{
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(0);

  std::cout << std::fixed << std::setprecision(6);
  std::cerr << std::fixed << std::setprecision(6);

  int N, a;

  std::cin >> N;

  for (int i = 0; i < N; i ++) {
    std::cin >> a;

    if (a % 2) {
      std::cout << "first" << std::endl;

      return 0;
    }
  }

  std::cout << "second" << std::endl;
  
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(i,n)   for(int i=0;i<n;i++)
#define tsolve() int t; cin>>t; while(t--) solve();
#define all(s)  s.begin(),s.end()
#define ff first
#define ss second
#define pi pair<int,int>
#define vi vector<int>
#define vpi vector<pi>  
int main(){
    int n;
    cin>>n;
    vi a(2*n);
    f(i,2*n)  cin>>a[i];
    sort(all(a));
    ll c=0;
    for(int i=0;i<2*n;i+=2)  c+=min(a[i],a[i+1]);
    cout<<c;
    return 0;
}

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

using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define repa(i,a,n) for(int i=(a);i<(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrepa(i,a,n) for(int i=(n)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()
#define MOD 1000000007

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

int main(){
  
    int n,m;cin>>n>>m;
    vvi to(n);
    int a,b;
    rep(i,m){
        cin>>a>>b;
        to[a-1].push_back(b-1);

    }

    vi ans,checked(n,0),tmp;
    queue<pair<int,vi>> q;
    int v,br=0;
    vi::iterator itr;

    rep(i,n){
        if(checked[i]==1)continue;
        tmp.clear();
        tmp.push_back(i);
        q.emplace(i,tmp);
        while(!q.empty()){
            v = q.front().first;
            tmp = q.front().second;
            q.pop();
            checked[v]=1;
            rep(j,to[v].size()){
                itr = find(all(tmp),to[v][j]);
                if(tmp.end()==itr){
                    checked[to[v][j]]=1;
                    tmp.push_back(to[v][j]);
                    q.emplace(to[v][j],tmp);
                    tmp.pop_back();
                }else{
                    while(itr!=tmp.end()){
                        ans.push_back(*itr);
                        itr++;
                    }
                    br=1;
                    break;
                }
            }
            if(br==1)break;
        }
        if(br==1)break;
    }

    if(ans.size()==0){
        cout << -1 << endl;
        return 0;
    }

    // cout<<"\n";rep(i,ans.size())cout << ans[i] << endl;cout<<"\n";

    vi::iterator itr2,ne;
    int update=1,tmpi;
    
    itr=ans.begin();
    while(itr!=ans.end()){
        tmpi=to[*itr].size();
        update=0;
        rep(i,tmpi){
            itr2=find(all(ans),to[*itr][i]);
            // cout<<"itr,i,itr2  "<<*itr<<","<<i<<","<<*itr2<<"\n";
            if(itr+1==ans.end())ne=ans.begin();
            else ne = itr+1;
            if(to[*itr][i]!=*ne && itr2!=ans.end()){
                update=1;
                if(itr2 > itr){
                    // for(auto itt=itr+1;itt!=itr2;itt++)cout<<"erase "<<*itt;
                    itr = ans.erase(itr+1,itr2);
                }else{
                    // for(auto itt=itr+1;itt!=ans.end();itt++)cout<<"erase "<<*itt<<"\n";
                    ans.erase(itr+1,ans.end());
                    // for(auto itt=ans.begin();itt!=itr2;itt++)cout<<"erase "<<*itt<<"\n";
                    itr = ans.erase(ans.begin(),itr2);
                }
                break;
            }
        }
        if(update==0)itr++;
    }

    cout << ans.size() << endl;
    rep(i,ans.size())cout << ans[i]+1 << endl;

    return 0;
}

Measured memory_bytes: 384
#include <iostream>
#include <string>
#include <utility>
#include <stack>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <climits>
#include <set>
#include <cmath>
#include <numeric>
using namespace std;

vector <int> edges[50];
int a[50];
int b[50];

bool visited[50];

void dfs_without(int a, int b){
    stack <int> st;
    st.push(0);
    while(!st.empty()){
        auto cur = st.top();
        st.pop();
        visited[cur] = true;
        for(int i = 0; i < edges[cur].size(); i++){
            int next = edges[cur][i];
            if(visited[next] || (cur == a && next == b) || (cur == b && next == a)){
                continue;
            }
            st.push(next);
        }
    }
}

bool is_connected_without(int a, int b, int N){
    for(int i = 0; i < N; i++){
        visited[i] = false;
    }

    dfs_without(a, b);

    for(int i = 0; i < N; i++){
        if(!visited[i]){
            return false;
        }
    }
    return true;
}

int main(){
    int N;
    int M;
    cin >> N >> M;
    for(int i = 0; i < M; i++){
        cin >> a[i] >> b[i];
        a[i] --;
        b[i] --;
        edges[a[i]].push_back(b[i]);
        edges[b[i]].push_back(a[i]);
    }

    int ans = 0;
    for(int i = 0; i < M; i++){
        if(!is_connected_without(a[i], b[i], N)){
            ans ++;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
H,W = map(int,input().split())
N = int(input())
Data = list(map(int,input().split()))
Ans = [[0]*W for _ in range(H)]

w = 0
h = 0
direction = 1
num = 0
for d in Data:
    num += 1
    for _ in range(d):
        Ans[h][w] = num

        if direction == 1:
            if w == W-1:
                direction = 2
                h += 1
            else:
                w += 1
        else:
            if w == 0:
                direction = 1
                h += 1
            else:
                w -= 1

for h in range(H):
    for w in range(W):
        print( Ans[h][w], end = ' ')
    print()


Measured memory_bytes: 3920
#include <iostream>
#include <vector>
#include <cmath>
#include <map>
 
using namespace std;
int N;
string S;

int mas[100][100];
int num[100][100];
int H,W;

int count(int, int);

int main()
{
  int i,j;
  int n;
  int ret=0;

  cin >> H >> W;
  for (i=0; i<H; i++)
    for (j=0; j<W; j++)
      num[i][j] = H+W;

  for (i=0; i<H; i++) {
    cin >> S;
    for (j=0; j<W; j++) {
      switch(S[j]) {
      case '#':
        mas[i][j] = '#';
        break;
      case '.':
        mas[i][j] = '.';
        break;
      }
    }
  }

  for (i=0; i<H; i++) {
    for (j=0; j<W; j++) {
      num[i][j] = count(i, j);
    }
  }

  printf("%d\n", num[H-1][W-1]);

  return 0;
}
int count(int ph, int pw) {
  int c1, c2;
  if (mas[ph][pw] == '.') {
    if (ph>0) {
      if (pw>0) {
        return min(num[ph-1][pw], num[ph][pw-1]);  //��܂��͍��̃}�X�̏��������Ɠ���
      } else {  //���[�̃}�X�̏ꍇ
        return num[ph-1][pw];  //��̃}�X�Ɠ���
      }
    } else {  // ��[�̃}�X�̏ꍇ
      if (pw>0) {
        return num[ph][pw-1];  //���̃}�X�Ɠ���
      } else {
        return 0;
      }
    }
  } else {
    if (ph>0) {
      if (pw>0) {
        if (mas[ph-1][pw] == '#') c1 = num[ph-1][pw];
        else c1 = num[ph-1][pw]+1;
        if (mas[ph][pw-1] == '#') c2 = num[ph][pw-1];
        else c2 = num[ph][pw-1]+1;
        return min(c1, c2);
      } else {
        if (mas[ph-1][pw] == '#') return num[ph-1][pw];
        else return num[ph-1][pw]+1;
      }
    } else {
      if (pw>0) {
        if (mas[ph][pw-1] == '#') return num[ph][pw-1];
        else return num[ph][pw-1]+1;
      } else {
        return 1;
      }
    }
  }
  return -1;
}


Measured memory_bytes: 384
#include <iostream>
#include <string>
#include <climits>


using namespace std;


using ll = long long;


int main()
{
    string S;
    cin >> S;

    ll ans = 0;
    for (ll i = 0; i < S.length(); i++) {
        if ((i % 2 == 0 && S[i] == '1') || (i % 2 == 1 && S[i] == '0'))
            ans++;
    }
    ll tmp = 0;
    for (ll i = 0; i < S.length(); i++) {
        if ((i % 2 == 0 && S[i] == '0') || (i % 2 == 1 && S[i] == '1'))
            tmp++;
    }

    cout << min(ans, tmp) << endl;
}


Measured memory_bytes: 512
# 5
# 1 2 3 4 5
# 3
# 3 4 1

import copy

Sn = int(input())
S = list(map(int, input().split()))

Tn = int(input())
T = list(map(int, input().split()))

def liner_search(nums, n, v):
    S_copy = copy.deepcopy(nums)
    S_copy.append(v)
    i = 0
    while S_copy[i] != v:
        i += 1 
    
    return i != n


if __name__ == "__main__":
    count = 0 
    for t in T:
        if liner_search(S, Sn, t):
            count += 1

    print(count)
        

    




Measured memory_bytes: 7096