akhauriyash/Code-Regression huggingface dataset:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#include<iostream>
using namespace std;
template<class _T>inline void read(_T &_a)
{
	bool f=0; char _c=getchar(); _a=0;
	while(_c<'0'||_c>'9'){ if(_c=='-') f=1; _c=getchar(); }
	while(_c>='0'&&_c<='9'){ _a=(_a<<3)+(_a<<1)-'0'+_c; _c=getchar(); }
	if(f) _a=-_a;
}

int n,k;
long long ans,sum[200001];

int main()
{
	read(n); read(k);
	for (int i=1;i<=n;++i) sum[i]=sum[i-1]+i;
	while(k<=n+1)
	{
		ans+=sum[n]-sum[n-k]-sum[k-1]+1;
		ans%=1000000007;
		++k;
	}
	printf("%lld",ans);
	return 0;
}

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

int main(){
  int N, D;
  cin >> N >> D;
  vector<vector<int>> X(N, vector<int>(D));
  for(int i=0; i<N; i++){
    for(int j=0; j<D; j++){
      cin >> X.at(i).at(j);
    }
  }
  int Ans=0;
  int T=0;
  for(int i=0; i<N; i++){
    for(int j=i+1; j<N; j++){
      T=0;
      for(int k=0; k<D; k++){
        T+=(X.at(i).at(k)-X.at(j).at(k))*(X.at(i).at(k)-X.at(j).at(k));
      }
      int S=sqrt(T);
      if(T==S*S){
        Ans++;
      }
    }
  }
  cout << Ans << endl;
}

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

int main()
{

    int A, B, C;
    int K;
    vector<int> V;
    cin >> A >> B >> C;
    cin >> K;
    V.push_back(A);
    V.push_back(B);
    V.push_back(C);

    sort(V.begin(), V.end());
    for (int i = 0; i < K; i++)
    {
        V[2] = 2 * V[2];
    }
    cout << V[0] + V[1] + V[2];
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <utility>
#include <stack>
using namespace std;
int main() {
  stack<int> s1;
  stack< pair<int,int> > s2;
  char c;
  int d;
  int sum = 0;
  for(int i =0; cin >>c ; i++ ){
    if(c == '\\'){
      s1.push(i);
    }
    else if(c == '/' && s1.size()>0) {
      int j = s1.top();
      d = i-j;
      sum +=d;
      s1.pop();
      while(s2.size()>0 && s2.top().first > j){
          d += s2.top().second;
          s2.pop();
      }
      s2.push(make_pair(j,d));
    }
  }
  int n = s2.size();
  vector <int> pond(n);
  for(int i = 0; i<n; i++){
    pond[n-i-1] = s2.top().second;
    s2.pop();
  }
  cout << sum << endl;
  cout << n ;
  for(int i = 0; i<n; i++){
    cout << ' ' <<pond[i];
  }
  cout << endl;

  return 0;
}

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

int main()
{
  int i,j,n,minij,hold,count=0;
  int array[100];

  scanf("%d",&n);
  if( n < 1 || n > 100 )
    exit(1);

  for( i = 0; i < n; i++ )
    {
      scanf("%d",&array[i]);
      if( array[i] < 0 || array[i] > 100 )
	exit(1);
    }

  for( i= 0; i < n-1; i++ )
    {
      minij = i;
      for( j = i; j < n; j++ )
	{
	  if( array[j] < array[minij] )
	    minij = j;
	}
      hold = array[i];
      array[i] = array[minij];
      array[minij] = hold;
      if( array[i] != array[minij] )
	count++;
    }
  for( i = 0; i < n-1; i++ )
    printf("%d ",array[i]);
  printf("%d\n%d\n",array[n-1],count);

  return 0;
}



Measured memory_bytes: 2144
import sys
sys.setrecursionlimit(10000000)
from functools import lru_cache

@lru_cache(maxsize=None)
def dfs(i,cost):
    if i == N:
        return 0

    res1 = dfs(i+1,cost)
    res2 = dfs(i+1,cost+C[i]) + V[i] - C[i]

    return max(res1,res2)

N = int(input())
V = list(map(int, input().split()))
C = list(map(int, input().split()))

print(dfs(0,0))

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

#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)

using namespace std;

template <class T>
using vec = std::vector<T>;
typedef long long ll;
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; }

const long long INF = 1LL<<60;

int lcm(int a,int b){
    //最小公倍数
    return a/__gcd(a,b) * b;
}



int main()
{
    int n,m;
    cin>>n>>m;
    vec<int> s(m,0);
    vec<int> p(m,0);

    int k,digit;
    rep(i,m){
        cin>>k;
        rep(j,k){
            cin>>digit;
            digit--;
            s[i]|=(1<<digit);
        }
    }
    rep(i,m){
        cin>>p[i];
    }

    bool rem=false;
    int ans=0;
    for(int bit=0;bit<(1<<n);++bit){
        rep(i,m){
            // intからbit flagを数える__builtin_popcount
            rem=(((__builtin_popcount(bit&s[i])) %2)==p[i]);
            if(!rem) break;
        }
        ans+=rem;
    }

    cout<<ans<<endl;
    return 0;

}




Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int i,n,m,count=1;
	cin>>n;
	int a[n];
	for(i=0;i<n;i++)
	cin>>a[i];
	int mini=a[0];
	for(i=1;i<n;i++)
	{
		if(a[i]<mini)
		{count++;
		mini=a[i];}
	}
	
	
	cout<<count;
	 return 0;
}

Measured memory_bytes: 1024
import sys
input=sys.stdin.readline
n=int(input())
l=[list(map(int,input().split())) for i in range(n)]
l.sort(key=lambda x:x[1])
t=0
for i in l:
    a,b=i
    t+=a
    if(t>b):
        print("No")
        break
else:
    print("Yes")

Measured memory_bytes: 53608
//by yjz
#include<bits/stdc++.h>
using namespace std;
#define FF first
#define SS second
#define PB push_back
#define MP make_pair
#define bged(v) (v).begin(),(v).end()
#define foreach(it,s) for(__typeof((s).begin()) it=(s).begin();it!=(s).end();it++)
typedef long long ll;
const int Imx=2147483647;
const ll Lbig=2e18;
const int mod=1e9+7;
//My i/o stream
struct fastio
{
	char s[100000];
	int it,len;
	fastio(){it=len=0;}
	inline char get()
	{
		if(it<len)return s[it++];it=0;
		len=fread(s,1,100000,stdin);
		if(len==0)return EOF;else return s[it++];
	}
	bool notend()
	{
		char c=get();
		while(c==' '||c=='\n')c=get();
		if(it>0)it--;
		return c!=EOF;
	}
}_buff;
#define geti(x) x=getnum()
#define getii(x,y) geti(x),geti(y)
#define getiii(x,y,z) getii(x,y),geti(z)
#define puti(x) putnum(x),putchar(' ')
#define putii(x,y) puti(x),puti(y)
#define putiii(x,y,z) putii(x,y),puti(z)
#define putsi(x) putnum(x),putchar('\n')
#define putsii(x,y) puti(x),putsi(y)
#define putsiii(x,y,z) putii(x,y),putsi(z)
inline ll getnum()
{
	ll r=0;bool ng=0;char c;c=_buff.get();
	while(c!='-'&&(c<'0'||c>'9'))c=_buff.get();
	if(c=='-')ng=1,c=_buff.get();
	while(c>='0'&&c<='9')r=r*10+c-'0',c=_buff.get();
	return ng?-r:r;
}
template<class T> inline void putnum(T x)
{
	if(x<0)putchar('-'),x=-x;
	register short a[20]={},sz=0;
	while(x)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
}
inline char getreal(){char c=_buff.get();while(c==' '||c=='\n')c=_buff.get();return c;}
int A,B,C,D,E,F;
bool fa[6111],fb[6111];
int main()
{
	cin>>A>>B>>C>>D>>E>>F;
	fa[0]=1;
	for(int i=0;i<=3000;i++)fa[i+A*100]|=fa[i];
	for(int i=0;i<=3000;i++)fa[i+B*100]|=fa[i];
	fb[0]=1;
	for(int i=0;i<=3000;i++)fb[i+C]|=fb[i];
	for(int i=0;i<=3000;i++)fb[i+D]|=fb[i];
	int ansb=-1,ansab=-1;
	double mxans=-1;
	for(int a=1;a<=3000;a++)
	{
		if(!fa[a])continue;
		for(int b=0;b<=3000;b++)
		{
			if(!fb[b])continue;
			if(a+b>F)continue;
			if(100*b>E*a)continue;
			if(mxans<1.0*b/(a+b))
			{
				mxans=1.0*b/(a+b);
				ansb=b;
				ansab=a+b;
			}
		}
	}
	cout<<ansab<<" "<<ansb<<endl;
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;

int main(){
	string s;
	cin >> s;
	int c = 0;
	for(int i = 0; i < 3; i++) {
		if (s[i] == '1'){
			c++;
		} 
	}
	cout << c << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int A, B;
    cin >> A >> B;
    vector<vector<int>> d(A+1, vector<int>(B+1));
    for(int i=1; i<=A; i++)
        for(int j=1; j<=B; j++)
            cin >> d[i][j];
    vector<vector<int>> f(101, vector<int>(101, 0));
    for(int a=0; a<=100; a++)
        for(int b=0; b<=100; b++)
            for(int x=1; x<=A; x++)
                for(int y=1; y<=B; y++)
                    f[a][b] = max(f[a][b], d[x][y]-a*x-b*y);

    for(int x=1; x<=A; x++)
        for(int y=1; y<=B; y++){
            int tmp = 1<<30;
            for(int a=0; a<=100; a++)
                for(int b=0; b<=100; b++)
                    tmp = min(tmp, f[a][b]+a*x+b*y);
            if(tmp != d[x][y]){
                cout << "Impossible" << endl;
                return 0;
            }
        }

    cout << "Possible" << endl;
    int N = 202;
    int M = 200 + 101*101;
    vector<tuple<int, int, int>> edges;
    for(int i=0; i<100; i++)
        edges.push_back(make_tuple(i, i+1, -1));
    for(int i=101; i<201; i++)
        edges.push_back(make_tuple(i, i+1, -2));
    for(int i=0; i<=100; i++)
        for(int j=0; j<=100; j++)
            edges.push_back(make_tuple(i, 201-j, f[i][j]));
    cout << N << " " << M << endl;
    for(auto e : edges){
        int u, v, c;
        tie(u, v, c) = e;
        cout << u+1 << " " << v+1 << " ";
        if(c == -1) cout << "X" << endl;
        else if(c == -2) cout << "Y" << endl;
        else cout << c << endl;
    }
    cout << 1 << " " << N << endl;
    return 0;
}

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

#define SIZE 300005
#define MOD 1000000007LL
#define INF INT_MAX / 3
#define LLINF 1LL << 60
#define REP(i,n) for(int i=0;i<n;i++)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define DOWN(i,b,a) for(int i=b;i>=a;i--)
#define SET(a,c) memset(a,c,sizeof a)
#define FORALL(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define FOREACH(i,c) for(auto (i) : (c))
#define BIT(i,j) ((i)>>(j))&1
#define ALL(o) (o).begin(), (o).end()
#define ERASE(o) (o).erase(unique((o).begin(),(o).end()), (o).end())
#define SQ(x) ((x)*(x))
using namespace std;
typedef long long ll;
typedef valarray<int> Array;
typedef pair<ll,ll> Pll;
typedef pair<int, int> Pii;
typedef pair<double, double> Pdd;
template<typename T> inline void priv(vector<T>a){REP(i,a.size()){cout<<a[i];if(i==a.size()-1)cout<<endl;else cout<<" ";}}
int gcd(int a,int b){int c=max(a,b);int d=min(a,b);return c==0||d==0?c:gcd(c%d,d);}
int lcm(int a,int b){return a==0||b==0?0:a*b/gcd(a,b);}

void solve(int a1, int a2, int a3, int a4, int a5)
{
  int a[13]; fill_n(a,13,0);
  a[--a1]++;a[--a2]++;a[--a3]++;a[--a4]++;a[--a5]++;
  int m = 0; bool f = false;
  vector<int> s;
  string ans;
  REP(i,13)
  {
    m = max(a[i],m);
    if(a[i]>0)
    {
      s.push_back(a[i]);
      if(i < 10)
      {
        bool flag = true;
        REP(j,5) if(a[(i+j)%13] == 0)
        {
          flag = false;
          break;
        }
        f = f || flag;
      }
    }
  }
  if(m >= 4) ans = "four card";
  else if(s.size()==2) ans = "full house";
  else if(f) ans = "straight";
  else
  {
    int b[3]; fill_n(b,3,0);
    REP(i,s.size()) b[s[i]-1]++;
    if(b[2] > 0) ans = "three card";
    else if(b[1] > 1) ans = "two pair";
    else if(b[1] > 0) ans = "one pair";
    else ans = "null";
  }
  printf("%s\n",ans.c_str());
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int a,b,c,d,e;
  while(~scanf("%d,%d,%d,%d,%d",&a,&b,&c,&d,&e))
    solve(a,b,c,d,e);
  return 0;
}

Measured memory_bytes: 1236
#include <iostream>
#include <algorithm>
#include <vector>
#define rep(i, n) for(int i=0; i<n; i++)
#define rep1(i, n) for(int i=1; i<n; i++)
using namespace std;

int main() {
    int n;
    cin >> n;
    int tmp;
    vector<int> integers;
    rep(i, n) {
        cin >> tmp;
        integers.push_back(tmp);
    }

    void bubble_sort(vector<int> integers, int n);
    bubble_sort(integers, n);
}

void bubble_sort(vector<int> integers, int n) {
    // bool flag = true;
    int swap_count=0;
    int i = 0;
    while (i<n) { // flag
        // flag = false;
        for (int j=n-1; j>i; j-- ) {
            if (integers[j] < integers[j-1]) {
                swap(integers[j], integers[j-1]);
                swap_count++;
                // flag = true;
            }
        }
        i++;
    }
    rep(i, n-1) cout << integers.at(i) << " ";
    cout << integers.at(n-1) << endl;
    cout << swap_count << endl;
}


Measured memory_bytes: 3148
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

vector<vi> transpose(const vector<vi> &a){
	vector<vi> b(a[0].size(), vi(a.size()));
	rep(i, a.size()) rep(j, a[0].size()) b[j][i] = a[i][j];
	return b;
}
bool check(vector<vi> a){
	while(1){
		vector<vi> x = a;
		rep(it, 2){
			sort(all(a));
			a = transpose(a);
		}
		if(x == a) break;
	}
	//rep(i, a.size()) rep(j, a[0].size()) cerr<<a[i][j]<<(j==a[0].size()-1?"\n":"");
	int h = (a.size() + 1) / 2, w = (a[0].size() + 1) / 2;
	rep(it, 2){
		vector<vi> x = a;
		rep(i, a.size()) rep(j, a[0].size()){
			int v = (i < h) ^ (j < w);
			if(v != a[i][j]) goto FAIL;
		}
		return 1;
		FAIL:;
		rep(i, a.size()) rep(j, a[0].size()) a[a.size() - 1 - i][a[0].size() - 1 - j] = x[i][j];
	}
	return 0;
}

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	int w, h; cin >> w >> h;
	vector<vi> a(h, vi(w));
	//rep(i, h) rep(j, w) a[i][j] = (i + j) & 1;
	
	int cnt = 0;
	rep(i, h) rep(j, w){
		cin >> a[i][j];
		cnt += a[i][j];
	}
	if(2 * cnt > h * w) rep(i, h) rep(j, w) a[i][j] ^= 1;
	
	cout << (check(a) ? "yes" : "no") << endl;
	return 0;
}


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

int main(void) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    --a;
    long long sa = a / c + a / d - a / lcm(c, d);
    long long bb = b / c + b / d - b / lcm(c, d);
    cout << (b - bb) - (a - sa) << endl;
    return 0;
}

Measured memory_bytes: 3568
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define FAST_IO ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);

    string str;
    while(cin>>str){
        cout<<str.substr(0,4)<<" "<<str.substr(4,str.length())<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cstdio>
#include <cmath>
#include <cassert>
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

int n, m;

bool check1(vector<int> a, vector<int> b) {
	int i;
	vector<bool> exist(n + 1, false);
	rep(i, m) {
		if (a[i] <= 0 || a[i] > n) return false;
		if (b[i] <= 0 || b[i] > n) return false;
		if (exist[a[i]]) return false;
		exist[a[i]] = true;
		if (exist[b[i]]) return false;
		exist[b[i]] = true;
	}
	return true;
}

bool check2(vector<int> a, vector<int> b) {
	int i;
	
	vector<bool> used(n + 1, false);
	rep(i, m) {
		int x = abs(a[i] - b[i]);
		x = min(n - x, x);
		if (n % 2 == 0 && x == n / 2) return false;
		if (used[x]) { cout << "a, b = " << a[i] << ", " << b[i] << ", x = " << x << endl; return false; }
		used[x] = true;
	}
	return true;
}

void debug2(vector<int> a, vector<int> b) {
	int i;
	rep(i, m) {
		int x = abs(a[i] - b[i]);
		x = min(n - x, x);
		cout << "a, b = " << a[i] << ", " << b[i] << ", x = " << x << endl;
	}
}

bool naive_checker(vector<int> a, vector<int> b) {
	if (check1(a, b) == false) return false;
	if ((int)a.size() != m || (int)b.size() != m) { return false; }
	
	typedef pair<int, int> P;
	set<P> appear;
	
	int i, j;
	for (i = 0; i < n; i++) {
		cerr << endl;
		for (j = 0; j < m; j++) {
			int A = a[j] - i; if (A <= 0) A += n;
			int B = b[j] - i; if (B <= 0) B += n;
			cerr << A << ", " << B << endl;
			if (appear.find(P(A, B)) != appear.end() || appear.find(P(B, A)) != appear.end()) return false;
			appear.insert(P(A, B));
			appear.insert(P(B, A));
		}
	}
	return true;
}

vector<int> a;
vector<int> b;
void solve() {
	if (n % 2 == 1) {
		for (int i = 0; i < m; i++) {
			a.push_back(i + 1);
			b.push_back(n - i);
		}
	}
	else {
		int x = 1;
		int y = n;
		for (int i = 0; i < min(m, (n + 1) / 4); i++) {
			a.push_back(x);
			b.push_back(y % n);
			x++;
			y--;
		}
		x++;  
		for (int i = 0; i < m - (n + 1) / 4; i++) {
			a.push_back(x);
			b.push_back(y);
			x++;
			y--;
		}
		for (int i = 0; i < m; i++) {
			a[i] = a[i] % n;
			b[i] = b[i] % n;
			if (a[i] == 0) a[i] = n;
			if (b[i] == 0) b[i] = n;
		}
	}
}

void print() {
	int i;
	rep(i, m) cout << a[i] << " " << b[i] << endl;
}

signed main() {
	/*for (int t = 0; t < 1000; t++) {
		cerr << "t = " << t << endl;
		m = rand() % 50 + 1;
		n = 2 * m + 1 + rand() % 10;
		
		a.clear();
		b.clear();
		solve();
		cerr << "solved" << endl;
		if (naive_checker(a, b) == false) {
			cerr << "n = " << n << ", m = " << m << ", NG2" << endl;
			print();
			return 0;
		}
	}
	cerr << "OK" << endl;
	a.clear();
	b.clear();*/
	
	cin >> n >> m;
	if (m < 1 || m * 2 + 1 > n) assert(0);
	
	solve();
	//if (naive_checker(a, b) == false) cerr << "NG" << endl;
	//else cerr << "OK" << endl;
	print();
	return 0;
}

Measured memory_bytes: 4068
S = input()
flag = True
while(flag):
    if(len(S)==0):
        flag = False
    if(S[-5:]=='dream' or S[-5:]=='erase'):
        S=S[:-5]
    elif(S[-6:]=='eraser'):
        S=S[:-6]
    elif(S[-7:]=='dreamer'):
        S=S[:-7]
    else:
        break
if(flag):
    print('NO')
else:
    print('YES')
#反省点

Measured memory_bytes: 133956
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;

int main(){
  ull N;
  cin >> N;
  vector<ull> a(N);
  
  for (ull i = 0; i < N; i++) cin >> a.at(i);
  
  ull b = 1, j = 0;
  bool flag = false;
  while(1) {
    if (a.at(j) == b) {
      j++;
      b++;
      flag = true;
    } else {
      j++;
    }
    if(j == N) break;
  }
  
  if (flag) cout << N - b + 1;
  else cout << -1;
  
  return 0;
}


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

template<int MOD>
struct mod_int {
	static const int Mod = MOD;
	unsigned x;
	mod_int() : x(0) { }
	mod_int(int sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	mod_int(long long sig) { int sigt = sig % MOD; if (sigt < 0) sigt += MOD; x = sigt; }
	int get() const { return (int)x; }

	mod_int &operator+=(mod_int that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
	mod_int &operator-=(mod_int that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	mod_int &operator*=(mod_int that) { x = (unsigned long long)x * that.x % MOD; return *this; }
	mod_int &operator/=(mod_int that) { return *this *= that.inverse(); }

	mod_int operator+(mod_int that) const { return mod_int(*this) += that; }
	mod_int operator-(mod_int that) const { return mod_int(*this) -= that; }
	mod_int operator*(mod_int that) const { return mod_int(*this) *= that; }
	mod_int operator/(mod_int that) const { return mod_int(*this) /= that; }

	mod_int inverse() const {
		long long a = x, b = MOD, u = 1, v = 0;
		while (b) {
			long long t = a / b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		return mod_int(u);
	}
};

template<int MOD>
istream& operator >> (istream& is, mod_int<MOD>& val) {
	long long x;
	is >> x; val = x;
	return is;
}

template<int MOD>
ostream& operator << (ostream& os, const mod_int<MOD>& val) {
	os << val.get();
	return os;
}

using mint = mod_int<1000000007>;

const int MAX = 2e6;

mint fac[MAX + 1];
mint rfac[MAX + 1];

void init() {
	fac[0] = 1;
	for (int i = 1; i <= MAX; i++) {
		fac[i] = fac[i - 1] * i;
	}
	rfac[MAX] = fac[MAX].inverse();
	for (int i = MAX; i >= 1; i--) {
		rfac[i - 1] = rfac[i] * i;
	}
}

mint nPr(int n, int r) {
	return r < 0 || n < r ? 0 : fac[n] * rfac[n - r];
}

mint nCr(int n, int r) {
	return r < 0 || n < r ? 0 : fac[n] * rfac[n - r] * rfac[r];
}

mint nHr(int n, int r) {
	return r == 0 ? 1 : nCr(n + r - 1, r);
}

int main()
{
	init();
	int N;
	cin >> N;
	vector<mint> A(N), sum(N + 1);
	for (int i = 0; i < N; i++) {
		cin >> A[i];
		sum[i + 1] = sum[i] + A[i];
	}
	vector<mint> ran(N + 1);
	mint tmp = 0;
	for (int i = 1; i <= (N + 1) / 2; i++) {
		tmp += sum[N - i + 1] - sum[i - 1];
		ran[N - i + 1] = ran[i] = tmp;
	}
	mint res = 0;
	for (int i = 1; i < N; i++) {
		mint sub = sum[i] + (sum[N] - sum[N - i]);
		mint all = ran[i] - sub;
		res += sub * fac[N - i - 1] * fac[i] * nCr(N, N - i - 1);
		res += all * fac[N - i - 2] * 2 * fac[i] * nCr(N, N - i - 2);
	}
	res += ran[N] * fac[N];
	cout << res << endl;
	return 0;
}


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


void dfs(vector<int> adj[],int sv,bool * visited)
{
	visited[sv]=true;
	for(int i=0;i<adj[sv].size();i++)
	{
		if(visited[adj[sv][i]]==false)
		{
			dfs(adj,adj[sv][i],visited);
		}
	}
}
int cnt(vector<int>adj[],int n)
{
	bool * visited=new bool[n];
	for(int i=0;i<n;i++)
	{
		visited[i]=false;
	}
	
	int ct=0;
	for(int i=0;i<n;i++)
	{
		if(visited[i]==false)
		{
			dfs(adj,i,visited);
			ct++;
		}
		
	}
	return ct;
}
int main()
{
	int n;
	cin>>n;
	int m;
	cin>>m;
	
	vector<int> adj[n];

	
	for(int i=0;i<m;i++)
	{
		int x,y;
		cin>>x>>y;
		adj[x-1].push_back(y-1);
		adj[y-1].push_back(x-1);
	
	}
	
	int ct=cnt(adj,n);
	cout<<ct-1;
}


Measured memory_bytes: 10020
# 最大公約数
def gcd(a, b):
    if a > b:
        return gcd(b, a)
    else:
        if a == 0:
            return b
        else:
            return gcd(b % a, a)


n = int(input())
a = list(map(int, input().split()))  # -> li = [1,2,3,...n]
lg = []
lg.append(0)
rg = []
rg.append(0)
for i in range(n):
    lg.append(gcd(lg[-1], a[i]))
    rg.append(gcd(rg[-1], a[n-i-1]))
# print(lg)
# print(rg)
m = 0
for i in range(n):
    g = gcd(lg[i], rg[n-1-i])
    if g > m:
        m = g
print(m)

Measured memory_bytes: 14436
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		try (Scanner sc = new Scanner(System.in)) {
			// nがあり、nの数だけの整数の入力
			long n = sc.nextLong();
			long m = sc.nextLong();
			List<Long> an = new ArrayList<>();
			for (long i = 0; i < m; i++) {
				an.add(sc.nextLong());
			}
			long cnt = n;
			for (long l : an) {
				cnt = cnt - l;
				if (cnt < -1) {
					cnt = -1;
				}
			}
			System.out.println(cnt);
		}
	}

	/**
	 * 最小公倍数
	 */
	static long lcm(long a, long b) {
		long temp;
		long c = a;
		c *= b;
		while ((temp = a % b) != 0) {
			a = b;
			b = temp;
		}
		return c / b;
	}

	/**
	 * 最大公約数
	 */
	static long gcd(long a, long b) {
		long temp;
		while ((temp = a % b) != 0) {
			a = b;
			b = temp;
		}
		return b;
	}
}

Measured memory_bytes: 46500
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
int main(void)
{
	int a, b;
	char buff[20];
	int i;
	int result = 1;
	scanf("%d%d", &a, &b);
	scanf("%s", buff);

	for (i = 0; i < a + b + 1; ++i){
		if (i == a){
			if (buff[i] != '-'){
				result = 0;
			}
		}
		else{
			if (buff[i] < '0' || '9' < buff[i]){
				result = 0;
			}
		}
	}
	if (result){
		printf("Yes\n");
	}
	else{
		printf("No\n");
	}
	return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
#define N 1005

using namespace std;
int n, m, d[N][N];
long long F[N][N];
char A[N][N];

long long Magic(int x, int y)
{
    if(A[x][y]=='#') return 0;
    if(x>n || y>m) return 0;
    if(x==n&&y==m) return 1;
    if(d[x][y]) return F[x][y]; d[x][y]=1;
    return F[x][y] = (Magic(x+1, y) + Magic(x, y+1))%1000000007;
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=1; i<=n; i++)
        for(int j=1; j<=m; j++) cin>>A[i][j];
    cout<<Magic(1, 1);
    return 0;
}


Measured memory_bytes: 16528
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int INF = 1e9;
int MOD = 1e9+7;
main(){
    ll N,A[100000];
    cin >> N;
    for(int i = 0;i < N;i++)cin >> A[i];
    sort(A,A+N);
    ll a = A[N-1],b = 0,c = 0,d;
    for(int i = N-2;i >= 0;i--){
        if(!b){
            if(a == A[i]){
                b = a;
            }else{
                a = A[i];
            }
        }else{
            if(c == A[i]){
                d = c;
                break;
            }else{
                c = A[i];
            }
        }
    }
    if(b == 0 || c == 0)cout << 0 << endl;
    else cout << b * d << endl;
}

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

int main(){
	double L;	cin >> L;
  	
//  	cout << pow(L/3.0,3) << endl;
 	cout << fixed << setprecision(12) << pow(L/3.0,3) << endl;
 // 	cout << L/3.0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll,ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll,ll>>>
#define PL pair<ll,ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};
 
int main() {
    ll n;
    cin >> n;
    VL a(n), b(n);
    vector<ll> ma(n, 0);
    rep(i, 0, n) cin >> a[i];
    rep(i, 0, n) cin >> b[i];
    rep(i, 0, n){
        ma[a[i]]++;
    }
    VP cp(n);
    rep(i, 0, n) {
        cp[i].first = a[i];
        cp[i].second = i;
    }
    sort(a.begin(), a.end(), [&](ll l, ll r){
        if(ma[l] > ma[r]) return true;
        else if(ma[l] == ma[r] && l < r) return true;
        else return false;
    });
    sort(cp.begin(), cp.end(), [&](PL lp, PL rp){
        ll l = lp.first;
        ll r = rp.first;
        if(ma[l] > ma[r]) return true;
        else if(ma[l] == ma[r] && l < r) return true;
        else return false;
    });
    sort(b.begin(), b.end(), [&](ll l, ll r){
        if(ma[l] > ma[r]) return true;
        else if(ma[l] == ma[r] &&  l < r) return true;
        else return false;
    });
    
    //rep(i, 0, n) cerr << cp[i].first << (i == n-1 ? "\n" : " ");
    //rep(i, 0, n) cerr << a[i] << (i == n-1 ? "\n" : " ");
    //cerr << "a" << endl;
 
    VL ans(n);
 
    ll srt = -1;
    rep(i, 0, n) {
        if(a[0] != b[i]) {
            srt = i;
            break;
        }
    }
    if(srt == -1) {
        cout << "No" << endl;
        return 0;
    }
    rep(i, 0, n) ans[i] =  b[(i + srt) % n];
    rep(i, 0, n) {
        if(a[i] == ans[i]) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    //rep(i, 0, n) cerr << ans[i] << (i == n-1 ? "\n" : " ");
    VL true_ans(n);
    rep(i, 0, n) true_ans[cp[i].second] = ans[i];
    rep(i, 0, n) cout << true_ans[i] << (i == n-1 ? "\n" : " ");
    return 0;
}

Measured memory_bytes: 14340
// E. Magical Permutation
// time limit per test1 second
// memory limit per test256 megabytes
// inputstandard input
// outputstandard output
// Kuro has just learned about permutations and he is really excited to create a
// new permutation type. He has chosen 𝑛 distinct positive integers and put all
// of them in a set 𝑆. Now he defines a magical permutation to be:

// A permutation of integers from 0 to 2𝑥−1, where 𝑥 is a non-negative integer.
// The bitwise xor of any two consecutive elements in the permutation is an
// element in 𝑆. Since Kuro is really excited about magical permutations, he
// wants to create the longest magical permutation possible. In other words, he
// wants to find the largest non-negative integer 𝑥 such that there is a magical
// permutation of integers from 0 to 2𝑥−1. Since he is a newbie in the subject,
// he wants you to help him find this value of 𝑥 and also the magical
// permutation for that 𝑥.

// Input
// The first line contains the integer 𝑛 (1≤𝑛≤2⋅105) — the number of elements in
// the set 𝑆.

// The next line contains 𝑛 distinct integers 𝑆1,𝑆2,…,𝑆𝑛 (1≤𝑆𝑖≤2⋅105) — the
// elements in the set 𝑆.

// Output
// In the first line print the largest non-negative integer 𝑥, such that there
// is a magical permutation of integers from 0 to 2𝑥−1.

// Then print 2𝑥 integers describing a magical permutation of integers from 0 to
// 2𝑥−1. If there are multiple such magical permutations, print any of them.

// Examples
// inputCopy
// 3
// 1 2 3
// outputCopy
// 2
// 0 1 3 2
// inputCopy
// 2
// 2 3
// outputCopy
// 2
// 0 2 1 3
// inputCopy
// 4
// 1 2 3 4
// outputCopy
// 3
// 0 1 3 2 6 7 5 4
// inputCopy
// 2
// 2 4
// outputCopy
// 0
// 0
// inputCopy
// 1
// 20
// outputCopy
// 0
// 0
// inputCopy
// 1
// 1
// outputCopy
// 1
// 0 1
// Note
// In the first example, 0,1,3,2 is a magical permutation since:

// 0⊕1=1∈𝑆
// 1⊕3=2∈𝑆
// 3⊕2=1∈𝑆
// Where ⊕ denotes bitwise xor operation.

#include <bits/stdc++.h>

#define inf 0x3f3f3f3f

using namespace std;

int n, m, d;
int i, j, k;

void solve() {
  cin >> n;
  vector<int> data(n);
  for (i = 0; i < n; ++i) cin >> data[i];
  auto largest = max_element(data.begin(), data.end());
  int max_value = *largest;
  *largest = -1;
  auto secondLargest = max_element(data.begin(), data.end());
  for (i = 0; i < n; ++i) {
    if (i == largest - data.begin()) {
      cout << *secondLargest << endl;
    } else {
      cout << max_value << endl;
    }
  }
  return;
}

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


Measured memory_bytes: 2432
n = int(input())
books = [input().split() for _ in range(n)]
q = int(input())
for i in range(q):
  qt, qa, qdf, qdt = input().split()
  for title, auther, date in books:
    if (qt == "*" or qt in title) and (qa == "*" or qa in auther) and (qdf == "*" or qdf <= date) and (qdt == "*" or date <= qdt):
      print(title)
  if i != q - 1:
    print() 


Measured memory_bytes: 5632
using System;
using System.Collections.Generic;

namespace aizu
{
    public class OnlineJudge
    {
        static void Main()
        {
            int val = int.Parse(Console.ReadLine());
            Dictionary<string, Action<int>> calc = new Dictionary<string, Action<int>> {
                { "+", p => val += p }, { "-", p => val -= p }, { "*", p => val *= p }, { "/", p => val /= p }
            };
            string operate;
            while((operate = Console.ReadLine()) != "=") {
                calc[operate](int.Parse(Console.ReadLine()));
            }
            Console.WriteLine(val);
        }
    }
}

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

#define pb push_back
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define REP(i,n) for(int _n=n, i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define trav(a, x) for (auto& a : x)
using ull = uint64_t;
using ll = int64_t;
using PII = pair<int, int>;
using VI = vector<int>;
#define INF (1ll<<60)

string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string) s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B> string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A> string to_string(A v) {
bool first = true; string res = "{";
for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); }
res += "}"; return res; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

const ll MOD = 1000000007;
map<string, ll> dp[105];

vector<string> S = {"AA", "AC", "AG", "AT", "CA", "CC", "CG", "CT", "GA", "GC", "GG", "GT", "TA", "TC", "TG", "TT"};

int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    int N;
    cin >> N;
    vector<string> SS;
    for (char c : {'A', 'C', 'G', 'T'}) {
        for (string s : S) {
            SS.pb(s + c);
        }
    }
    for (string s : SS) {
        if (s != "AGC" && s != "ACG" && s != "GAC") {
            dp[N - 1][s] = 1LL;
        }
    }
    for (int i = N - 2; i > 1; i--) {
        for (string s : SS) {
            for (string t : SS) {
                if (s[1] == t[0] && s[2] == t[1]) {
                    string ss = "";
                    ss = ss + s[0] + t[0] + t[1] + t[2];
                    if (ss.substr(0, 3) == "AGC" || ss.substr(0, 3) == "ACG" || ss.substr(0, 3) == "GAC") {
                        continue;
                    }
                    if (ss.substr(1, 3) == "AGC" || ss.substr(1, 3) == "ACG" || ss.substr(1, 3) == "GAC") {
                        continue;
                    }
                    if (ss[0] == 'A' && ss[2] == 'G' && ss[3] == 'C') continue;
                    if (ss[0] == 'A' && ss[1] == 'G' && ss[3] == 'C') continue;
                    dp[i][s] = (dp[i + 1][t] + dp[i][s]) % MOD;
                }
            }
        }
    }
    ll ans = 0;
    for (auto s : SS) {
        ans = (ans + dp[2][s]) % MOD;
    }
    cout << ans << endl;
}

Measured memory_bytes: 896
n=int(input())

s=list(input())

black = [0]*n

white = [0]*n

for i in range(n):
    if s[i] == '.':
        white[i] = white[i-1]+1
        black[i] = black[i-1]
    else:
        white[i] = white[i-1]
        black[i] = black[i-1]+1
        
ans = 100000000

for i in range(n-1):
    if s[i] != s[i+1]:
        ans = min(ans,black[i]+(white[-1]-white[i+1]))
        
ans = min(ans,max(black),max(white))
print(ans)
        

Measured memory_bytes: 64752
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AtCoder
{
	class Program
	{
		static void Main(string[] args)
		{
			var n = long.Parse(Console.ReadLine());
			long sum = 0;

			for(int i = 1; i <= n; i++)
			{
				if(i % 3 == 0 || i % 5 == 0)
				{
					continue;
				}
				sum += i;
			}

			Console.WriteLine(sum);
		}
	}
}


Measured memory_bytes: 17608
S = input()
N = len(S)
A = [0]*N


f = 0
n = 0
t = 0
s = 0
for k in range(N):
    if f == 0 and S[k] == "R":
        t += 1
    elif f == 0 and S[k] == "L":
        f = 1
        n = k
        s = 1
    elif f == 1 and S[k] == "L":
        s += 1
    elif f == 1 and S[k] == "R":
        if (s+t)%2 == 0:
            A[n-1] = (s+t)//2
            A[n] = (s+t)//2
        else:
            if t%2 == 0:
                A[n-1] = (t+s)//2
                A[n] = (t+s)//2 + 1
            else:
                A[n-1] = (t+s)//2 + 1
                A[n] = (t+s)//2
        f = 0
        t = 1
        s = 0
if (s+t)%2 == 0:
    A[n-1] = (s+t)//2
    A[n] = (s+t)//2
else:
    if t%2 == 0:
        A[n-1] = (t+s)//2
        A[n] = (t+s)//2 + 1
    else:
        A[n-1] = (t+s)//2 + 1
        A[n] = (t+s)//2
print(*A)


Measured memory_bytes: 6528
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define all(a) (a).begin(),(a).end()
#define fs front().first
using namespace std;
typedef pair<int,int> pii;

template<class Comp>
void add(deque<pii> &q, pii nxt, Comp comp){
  while(!q.empty() && comp(q.back(),nxt))q.pop_back();
  q.push_back(nxt);
}

void del(deque<pii> &q, int p){
  if(q.front().second == p)q.pop_front();
}

int main(){
  int n,d;
  scanf("%d%d",&n,&d);
  
  vector<pii> x(n),y(n);
  rep(i,n){
    int X,Y,B;
    scanf("%d%d%d",&X,&Y,&B);
    x[i] = pii(B,X); y[i] = pii(B,Y);
  }
  sort(all(x)); sort(all(y));

  long long res = 0;
  deque<pii> maxx, maxy, minx, miny;
  int r = 0;
  rep(l,n){
    while(r<n && x[r].first-x[l].first<=d){
      add(maxx,pii(x[r].second,r),less<pii>());
      add(minx,pii(x[r].second,-r),greater<pii>());
      add(maxy,pii(y[r].second,r),less<pii>());
      add(miny,pii(y[r].second,-r),greater<pii>());
      r++;
    }

    res = max( res, (long long)(maxx.fs - minx.fs) * (maxy.fs - miny.fs) );

    del(maxx,l); del(minx,-l);
    del(maxy,l); del(miny,-l);
  }
  printf("%lld\n",res);
}

Measured memory_bytes: 4472
#include<cstdio>
#define ll long long
#define mod 1000000007
#define N 110000
inline char gc(){
	static char now[1<<16],*S,*T;
	if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
	return *S++;
}
inline int read(){
	int x=0;char ch=gc();
	while (ch<'0'||ch>'9') ch=gc();
	while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
	return x;
}
int a[N],in[N],color[N],mark[N],cnt[N],n,foot[N];bool circle[N];
ll dp[N];
int main(){
//	freopen("agc.in","r",stdin);
	n=read();ll ans=1;
	for (int i=1;i<=n;++i) a[i]=read(),++in[a[i]];
	for (int i=1;i<=n;++i){
		if (color[i]) continue;int x=i;
		for(;!color[x];x=a[x]) color[x]=i;
		if (color[x]!=i) continue;
		for (;!circle[x];x=a[x]) circle[x]=1; 
	}
	for (int i=1;i<=n;++i) if ((circle[i]&&in[i]>2)||(!circle[i]&&in[i]>1)) {printf("0");return 0;}
	//处理痞老板(基环内向树） 
	for (int i=1;i<=n;++i){
		if (in[i]) continue;int x=i,tmp=0;
		while(!circle[x]) x=a[x],tmp++;foot[x]=tmp;
	} 
	for (int i=1;i<=n;++i){
		if (!circle[i]) continue;int x=i,st=0,first=0,id=0,len=0;
		while (circle[x]){
			++id;circle[x]=0;
			if (foot[x]){
				if (!first){
					st=first=id;len=foot[x];x=a[x];continue;
				}else{
					int numm=(foot[x]<(id-st))+(foot[x]<=(id-st));
					(ans*=numm)%=mod;if (!ans) {printf("0");return 0;}st=id;x=a[x];continue;
				}
			}x=a[x];
		}if (first){
			int numm=(len<(id+first-st))+(len<=(id+first-st));
			(ans*=numm)%=mod;if (!ans) {printf("0");return 0;}
		}else cnt[id]++;
	}
	//for (int i=1;i<=n;++i) if (cnt[i]) printf("%d %d\n",i,cnt[i]);
	for (int i=1;i<=n;++i){
		dp[0]=1;if (!cnt[i]) continue;
		if (i>1&&(i%2)){
			for (int j=1;j<=cnt[i];++j) {
				dp[j]=dp[j-1]*2%mod;if (j>1) (dp[j]+=dp[j-2]*(j-1)*i%mod)%=mod;
			}
		}else{
			for (int j=1;j<=cnt[i];++j){
				dp[j]=dp[j-1];if (j>1) (dp[j]+=dp[j-2]*(j-1)*i%mod)%=mod;
			}	
		}
		(ans*=dp[cnt[i]])%=mod;if (!ans){printf("0");return 0;}
	}printf("%lld",ans);
	return 0;
}

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

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

    string answer = "Yes";
    if (n % 2 == 1)
    {
        answer = "No";
    }
    else if (n >= 2)
    {
        for (int i = 0; i < n / 2; i++)
        {
            if (s[i] != s[i + n / 2])
            {
                answer = "No";
                break;
            }
        }
    }
    cout << answer << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define call(var) cout<<#var<<"="<<var<<endl;
#define dup(x,y) (((x)+(y)-1)/(y))
using ll=long long;
using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using Graph = vector<vi>;
using P = pair<int,int>;
const int MOD=(int)1e9+7;
const int INF=(int)1e9;
const ll LINF=(ll)1e18;
template<class t,class u> inline bool chmax(t&a,u b){if(a<b){a=b;return true;}return false;}
template<class t,class u> inline bool chmin(t&a,u b){if(b<a){a=b;return true;}return false;}

int n,m,q;
vi nums;
vi a,b,c,d;

int calc2(){
    int ret = 0;
    rep(i,q){
        if(nums[b[i]-1] - nums[a[i]-1] == c[i]) ret += d[i];
    }
    return ret;
}

int ans = INT_MIN;
void calc(int depth, int mix){
    if(depth == n-1){
        for(int i = mix; i<=m;++i){
            nums[depth] = i;
            chmax(ans, calc2());
        }
        return;
    }else{
        for(int i=mix;i<=m;++i){
            nums[depth] = i;
            calc(depth+1, i);
        }
    }
    return;
}

int main(){
    cin >> n >> m >> q;
    nums.resize(n);
    a.resize(q);
    b.resize(q);
    c.resize(q);
    d.resize(q);
    rep(i,q){
        cin >> a[i] >> b[i] >> c[i] >> d[i];
    }
    for(int i=1;i<=m;++i){
        calc(0, i);
    }
    cout << ans << endl;
}

Measured memory_bytes: 3644
from numpy import*
from scipy.sparse import*
def main():
  f=csgraph.floyd_warshall
  t=int32(open(0).read().split())
  n,m,l=t[:3]
  t=t[3:]
  m*=3
  print('\n'.join(map(str,map(int,f(f(csr_matrix((t[2:m:3],(t[:m:3],t[1:m:3])),[n+1]*2),0)<=l)[t[m+1::2],t[m+2::2]].clip(0,n)%n-1))))
main()

Measured memory_bytes: 41980
#include<stdio.h>
#define N 44


int F[N];

int fibonacci(int n){

  if( n == 0 || n ==1){
    return F[n] = 1;
  }

  if(F[n] != -1){
    return F[n];
  }

  return F[n] = fibonacci(n-2) + fibonacci(n-1);
}

int main(){

  int j;
  
  for(j=0;j<=N;j++){
    F[j] = -1;
  }
  
      int i=0,n;

  scanf("%d",&n);

  i = fibonacci(n);

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

  return 0;
}
  

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

using namespace std;

int main()
{
  int n, q;
  cin >> n >> q;
  
  pair<string, int> p[100000];
  queue<pair<string, int> > que;
  for (int i = 0; i < n; i++) {
    cin >> p[i].first >> p[i].second;
    que.push(p[i]);
  }
  
  int t = 0;
  while (que.size()) {
    if (que.front().second <= q) {
      t += que.front().second;
      cout << que.front().first << " " << t << endl;
    } else {
      t += q;
      que.front().second -= q;
      que.push(que.front());
    }
    que.pop();
  }
}


Measured memory_bytes: 7756
//????????????2???????????§??????????????????????????????????????????????¬??????¨NG. ???????????¨??¢?´¢????????????
//??????????????????????????????????????????????????£?????????????????????????????????O(2^n)????????????????????°??§?????????
//?????????????????????????????????????????£?????????)???????????°????????¶?????????????????????????????§???????????????????§???????????¢??????????????????§?????¨?????????????????????????????????????????§????????????
#include <iostream>
#include <algorithm>
using namespace std;

int n;
int x[24], y[24], r[24], c[24];
short int dp[1 << 24];

//rem???i??????????????????i????????£???????????°1, ??????????????????????????°0
//????????????????????????????????????????????°????????§???
int dfs(int rem) {
	bool isOut[24];	//isOut[i] = ???i??????????????????????????°true
	int i, j;
	
	if (rem == 0) return 0;
	if (dp[rem] >= 0) return dp[rem];
	
	for (i = 0; i < n; i++) {
		for (j = 0; j < i; j++) {
			if ((rem >> j) % 2 == 0) continue;	//??¢????????????????????????
			int dd = (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
			int rr = (r[i] + r[j]) * (r[i] + r[j]);
			if (dd < rr) break;
		}
		isOut[i] = (j == i);
	}
	
	int ret = 0;
	for (i = 0; i < n; i++) {
		if ((rem >> i) % 2 == 0 || !isOut[i]) continue;
		for (j = i + 1; j < n; j++) {
			if ((rem >> j) % 2 == 0 || !isOut[j]) continue;
			if (c[i] != c[j]) continue;
			//???i, j????????????????????????
			ret = max(ret, dfs(rem - (1 << i) - (1 << j)) + 2);
		}
	}
	return (dp[rem] = ret);
}

int main() {
	while (cin >> n) {
		if (!n) { break; }
		for (int i = 0; i < n; i++) {
			cin >> x[i] >> y[i] >> r[i] >> c[i];
		}
		for (int i = 0; i < (1<<n); i++) {
			dp[i] = -1;
		}
		cout << dfs((1 << n) - 1) << endl;
	}
}

Measured memory_bytes: 33928
#include<cstdio>
#include<cstring>
#define inlien inline
#define retrun return
#define cpnst const
typedef long long ll;
int n,m;
#include<algorithm>
ll A[5555],B[222][5555];
ll D[5555][5555];
void adv(int x1,int y1,int x2,int y2,ll v)
{D[x1][y1]+=v,D[x2+1][y2+1]+=v,D[x1][y2+1]-=v,D[x2+1][y1]-=v;}
int l[5555],r[5555];
struct pii
{int x,v;}p[5555];
bool operator<(cpnst pii x,cpnst pii y)
{return x.v<y.v;}
void ddd(int x)
{
	int L=x,R=x;
	if(l[x-1])L=l[x-1];
	if(r[x+1])R=r[x+1];
	r[L]=R,l[R]=L,l[x]=L,r[x]=R;
}
void ins(ll*f)
{
	register int i,ii;
	for(i=1;i<=n;i++)l[i]=r[i]=0,p[i].x=i,p[i].v=f[i];
	std::sort(p+1,p+n+1);
	for(i=1;i<=n;i++)
		ii=p[i].x,ddd(ii),adv(l[ii],ii,ii,r[ii],f[ii]);
}
void calc()
{
	register int i,ii;
	for(i=1;i<=n;i++)
		for(ii=1;ii<=n;ii++)
			D[i][ii]+=D[i][ii-1];
	for(i=1;i<=n;i++)
		for(ii=1;ii<=n;ii++)
			D[i][ii]+=D[i-1][ii];
	ll ans=0;
	for(i=1;i<=n;i++)
		for(ii=i;ii<=n;ii++)
			if(D[i][ii]+A[i]-A[ii]>ans)ans=D[i][ii]+A[i]-A[ii];
	printf("%lld\n",ans);
}
int main()
{
	scanf("%d%d",&n,&m);
	register int i,ii;
	for(i=2;i<=n;i++)
		scanf("%lld",A+i),A[i]+=A[i-1];
	for(i=1;i<=n;i++)
		for(ii=1;ii<=m;ii++)
			scanf("%lld",B[ii]+i);
	for(i=1;i<=m;i++)
		ins(B[i]);
	calc();
}
/*
Giving the world a better yourself is giving yourself a better world. 
*/

Measured memory_bytes: 229504
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, start, n) for (ll i = (ll)(start); i < (ll)(n); ++i)
static const ll INFTY = 1L << 62L;
ll dfs(vector<vector<ll>> &g, ll *v, ll i)
{
    v[i] = true;
    stack<ll> s;
    ll number = 0;
    s.push(i);
    while (!s.empty())
    {
        ++number;
        ll p = s.top();
        s.pop();
        rep(i, 0, g[p].size())
        {
            ll tmp = g[p][i];
            if (v[tmp] == false)
            {
                v[tmp] = true;
                s.push(tmp);
            }
        }
    }
    return number;
}
ll solver(vector<vector<ll>> &g, ll n)
{
    ll ans = 0;
    ll v[n + 1] = {};
    rep(i, 1, n + 1)
    {
        if (v[i] == false)
        {
            ll tmp = dfs(g, v, i);
            if (tmp > ans)
                ans = tmp;
        }
    }
    return ans;
}
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    ll a, b;
    vector<vector<ll>> g(n + 1, vector<ll>(0));
    rep(i, 0, m)
    {
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    cout << solver(g, n) << endl;
}

Measured memory_bytes: 17948
#include<bits/stdc++.h>
using namespace std;
int n;
vector<string> s(0);
bool fa,fb;

int main(){
    cin>>n;
    
   
    for(int i=0;i<n;i++)
    {
        string tmp;
        cin>>tmp;
        s.push_back(tmp);

        if(s.size() == 1) continue;
        for(int j=0;j<s.size() - 1;j++)
        {
            if(tmp == s[j])fa = true;
        }
    }  
    if(fa)  
    {
        puts("No\n");
        return 0;
    }

    for(int i=0;i<s.size() - 1;i++)
    {
        if(s[i].back() != s[i+1].front()) fb = true;
    }
    if(fb)
        puts("No\n");
    else puts("Yes\n");
}



Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int k = Integer.parseInt(sc.next());
		int x = Integer.parseInt(sc.next());
		int min = x - k + 1;
		int max = x + k - 1;
		for (int i = min; i <= max; i++) {
			System.out.println(i);
		}
		sc.close();
	}
}

Measured memory_bytes: 23124
#include <cstdio>
#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
#include <cmath>
#include <iomanip>
#include <regex>
#include <bits/stdc++.h>
#include <string>
using namespace std;

int main(){
    int N,M;
    cin>>N>>M;
    vector<vector<int>> v(N,vector<int>());
    for(int i=0;i<M;i++){
        int a,b;
        cin>>a>>b;
        a--;b--;
        v[a].push_back(b);
        v[b].push_back(a);
    }
    bool flag = false;
    for(int val:v[0]){
        for(int end:v[val]){
            if(end==N-1)flag=true;
        }
    }
    cout<<(flag?"POSSIBLE":"IMPOSSIBLE")<<endl;
}

Measured memory_bytes: 12016