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

#define ll long long
#define fornum(A,B,C) for(A=B;A<C;A++)
#define mp make_pair
#define pii pair<int,int>
#define pll pair<ll,ll>

using namespace std;

/////////////////////////////////////////////////////
#define MOD


ll N,M,L,Q,S[101010],T[101010];
vector<pll> abcv[333];
vector<ll> exv[333];
ll mk[333];

ll i,j,k;
void bfs(ll s){
    //printf("\n%lld:\n",s);
    ll i,j=0;
    fornum(i,0,N){
        mk[i+1]=-1;
    }
    mk[s]=L;
    
    while(true){
        
        ll a=-1,aa=-1;
        fornum(i,0,N){
            if(aa<mk[i+1]){
                a=i+1;
                aa=mk[i+1];
            }
        }
        if(a==-1)
            return;
        //printf("%lld ",a);
        exv[s].push_back(a);
        fornum(i,0,abcv[a].size()){
            pll b=abcv[a][i];
            if(mk[b.first]==-2)
                continue;
            mk[b.first]=max(mk[b.first],aa-b.second);            
        }
        mk[a]=-2;
    }
}
void solve(ll s,ll t){
    //printf("aa");
    ll i,j;
    queue<pll> q;
    fornum(i,0,N){
        mk[i+1]=0;
    }
    q.push({s,0});
    mk[s]=1;
    while(q.size()){
        auto qa=q.front();
        //printf("a%llda",qa.first);
        q.pop();
        //printf("%lld ",qa.first);
        
        fornum(i,0,exv[qa.first].size()){
            ll b=exv[qa.first][i];
            if(!mk[b]){
                if(b==t){
                    printf("%lld\n",qa.second);
                    return;
                }
                mk[b]=1;
                q.push({b,qa.second+1});
            }
        }
    }
    printf("-1\n");
}

int main(){
    scanf("%lld%lld%lld", &N,&M,&L);
    fornum(i,0,M){
        ll a,b,c;
        scanf("%lld%lld%lld",&a,&b,&c);
        if(c>L)
            continue;
        abcv[a].push_back({b,c});
        abcv[b].push_back({a,c});
    }
    fornum(i,0,N){
        bfs(i+1);
    }
    scanf("%lld",&Q);
    fornum(i,0,Q){
        scanf("%lld%lld",&S[i],&T[i]);
    }
    fornum(i,0,Q){
        solve(S[i],T[i]);
    }
    
    return 0;
}

Measured memory_bytes: 5504
#include <bits/stdc++.h>
using namespace std;
typedef long long ll ; 
const int mod = 1e9+7 ; 
int n ;
ll dp[100001][2] ; 
vector<vector<int> >v ; 
ll calc(int u ,int prt ,  bool yes) {
	if (v[u].size() == 1 && u!= 0 ) {
		if (yes)
		return 1 ;
		else
		return 2 ; 
		
	}
	ll &ret = dp[u][yes] ; 
	if (ret != -1)return ret ;
	ll one = 1 , zero = 1 ; 
	for (int i =0 ; i<v[u].size()  ; ++i) {
		int x =v[u][i] ; 
		if (x == prt)continue ; 
		if (yes == 0){
			zero = (zero%mod * 1ll *calc(x , u , 0)%mod)%mod;// + calc(x , u , 1) ; 
	//		cout<<u<<" "<<ret<<endl;
		}else {
			zero = (zero%mod *1ll *calc(x , u , 0)%mod)%mod ; 
		}
		
	}
	zero%=mod ; 
		for (int i =0 ; i<v[u].size()  ; ++i) {
		int x =v[u][i] ; 
		if (x == prt)continue ; 
		if (yes == 0){
			one = (one%mod * 1ll *calc(x , u , 1)%mod)%mod;// + calc(x , u , 1) ; 
	//		cout<<u<<" "<<ret<<endl;
		}else {
			one = (one%mod *1ll *calc(x , u , 0)%mod)%mod ; 
		}
		
	}
	one%=mod ; 
	if (yes == 0)
	ret = (one%mod +zero%mod)%mod ;
	else 
	ret = one%mod ; 
	return ret%mod ; 
}
int main() {
	scanf("%d" ,&n); 
	if (n == 1)return puts("2") , 0 ; 
	memset(dp , -1 , sizeof dp) ; 
	v.resize(n); 
	for (int i =0 ; i<n-1 ; ++i) {
		int a , b ; 
		scanf("%d %d" , &a , &b) ; 
		a-- ; 
		b--; 
		v[a].push_back(b); 
		v[b].push_back(a) ; 
	}
	ll ans = calc(0 , -1 , 0)%mod ; 
	printf("%lld\n" , ans%mod) ; 
	return 0;
}

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

using namespace std;
struct seg
{
    int x, y;
    bool operator<(seg b) const
    {
        return x<b.x;
    }
};
long long n,v1,v2,nv1,nv2,ans,i;
seg a[200005];
int main()
{
    cin>>n;
    v1=1e18;
    v2=1e18;
    ans=0;

    for(i=1;i<=n;i++)
    {
        cin>>a[i].x>>a[i].y;
    }
    sort(a+1, a+n+1);

    for(i=1;i<=n;i++)
    {
        nv1=a[i].x+a[i].y;
        nv2=a[i].x-a[i].y;

        ans=max(ans, nv1-v1);
        ans=max(ans, nv2-v2);

        v1=min(v1, nv1);
        v2=min(v2, nv2);
    }

    cout<<ans;
    return 0;
}


Measured memory_bytes: 5144
#include "bits/stdc++.h"
#define rep(i,b) for(ll i=0;i<b;i++)
#define ll long long
using namespace std;
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvvl vector<vector<vector<ll>>>
#define debug(x) cerr<<#x<<" "<<x<<'\n';
ll mod =1e9;
int main() {
    int n;
    cin >> n;

    map<string, int> m;
    rep(i, n) {
        string s;
        cin >> s;
        sort(s.begin(),s.end());
        m[s]++;
    }

    ll ans = 0;
    for(auto i : m) {
        ll s = i.second;
        ans += (ll)s*(s-1)/2;
    }

    cout << ans << endl;
}

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

#define fio ios_base::sync_with_stdio(false); cin.tie(NULL);

typedef long long int lli;

int main()
{
	fio;
	int n;
	cin>>n;
	for(int x=0;x<=100;x++)
	{
		int seveny=n-4*x;
		if(seveny>=0 and seveny%7==0)
		{
			cout<<"Yes"<<endl;
			return 0;
		}
	}
	cout<<"No"<<endl;
	cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
	return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstring>
int main() {
  char S[20];
  scanf("%s", S)+1;
  int len = strlen(S);
  
  int except9 = 0;
  for (int i = 1; i < len; i++) {
    if (S[i] != '9') except9 = 1;
  }
  
  int ans = 0;
  ans += S[0] - '0';
  ans += (len - 1) * 9;
  if (except9) ans--;
  printf("%d", ans);
  return 0;
}

Measured memory_bytes: 128
module Main where

import           Control.Monad
import           Debug.Trace
import qualified Data.Vector                   as V
import qualified Data.Vector.Mutable           as VM
import qualified Data.Vector.Unboxed           as VU
import qualified Data.Vector.Unboxed.Mutable   as VUM
import           Data.List
import qualified Data.ByteString               as BS
import qualified Data.ByteString.Char8         as BSC8
import           Data.Char
import           Data.Maybe
import           Data.Ord
--import           Data.Vector.Algorithms.Search
import qualified Data.Array                    as A
import qualified Data.Array.Unboxed            as AU
import qualified Data.Array.IO                 as AIO
import qualified Data.Set                      as S


getVUIL :: IO (VU.Vector Int)
getVUIL = VU.unfoldr (BSC8.readInt . BSC8.dropWhile isSpace) <$> BSC8.getLine
readI :: BSC8.ByteString -> Int
readI = fst . fromJust . BSC8.readInt
readIL :: BSC8.ByteString -> [Int]
readIL = map readI . BSC8.words
getI :: IO Int
getI = readI <$> BSC8.getLine
getIL :: IO [Int]
getIL = readIL <$> BSC8.getLine

getNIS :: Int -> IO [(Int, BSC8.ByteString)]
getNIS 0 = return []
getNIS n = do
    [aFst, aSnd] <- BSC8.words <$> BSC8.getLine
    next         <- (getNIS (n - 1))
    return ((readI aFst, aSnd) : next)

il2ss :: [Int] -> String
il2ss = unwords . map show

il2sn :: [Int] -> String
il2sn = unlines . map show

modNum = 10 ^ 9 + 7
newtype Mint = M Int
instance Num Mint where
    (M a) + (M b) = M $ mod (a + b) modNum
    (M a) - (M b) = M (a - b)
    (M a) * (M b) = M $ mod (a * b) modNum
    fromInteger a = M $ mod (fromInteger a) modNum
    abs (M a) = M $ abs a
    signum (M a) = M $ signum a
instance Show Mint where
    show (M a) = show (mod a modNum)

int2MInt :: Int -> Mint
int2MInt a = M (mod a modNum)

main :: IO ()
main = do
    aR <- getI
    let ret = pi * (realToFrac aR * 2)
    print ret


Measured memory_bytes: 4240
#include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
#define ll long long
#define rep(i,n) for (int i = 0; i < n; i++)
#define rrep(i,n) for (int i = n-1; i >= 0; i--)
#define MOD (1000000007)
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vvl vector<vl>
#define pii pair<int, int>
#define pb push_back

#define mp make_pair
#define all(a) (a).begin(),(a).end()

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

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

ll keta(ll n){
  string s = to_string(n);
  ll num = s.size();
  return num;
}

const ll INF = 1LL << 60;

const int dh[4] = {1,0,-1,0};
const int dw[4] = {0,1,0,-1};


int main(){
  int n; cin >> n;
  vector<queue<int>> a(n);
  rep(i,n) rep(j,n-1) {
    int x; cin >> x;
    x--;
    a[i].push(x);
  }

  int ans = 0;
  int cnt = 0;
  bool seen[n];
  bool ok = false;
  queue<int> q;
  memset(seen, false, sizeof(seen));
  ok = false;
  rep(i,n) {
    if(seen[i] || a[i].empty()) continue;
    int num = a[i].front();
    if(seen[num] || a[num].empty()) continue;
    if(a[num].front() != i) continue;
    ok = true;
    a[i].pop(); q.push(i);
    a[num].pop(); q.push(num);
    seen[i] = seen[num] = true;
    cnt += 2;
  }
  ans++;

  while(ok && cnt < n*(n-1)){
    memset(seen, false, sizeof(seen));
    queue<int> nq;
    ok = false;
    while(!q.empty()){
      int i = q.front(); q.pop();
      if(seen[i] || a[i].empty()) continue;
      int num = a[i].front();
      if(seen[num] || a[num].empty()) continue;
      if(a[num].front() != i) continue;
      ok = true;
      a[i].pop(); nq.push(i);
      a[num].pop(); nq.push(num);
      seen[i] = seen[num] = true;
      cnt += 2;
    }
    ans++;
    q = nq;
  }

  cout << (ok ? ans : -1) << endl;
}

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

int main(){
	int a[3],h,m,s,hh,mm,ss;
	for(int i=0;i<3;i++){
		cin>>h>>m>>s>>hh>>mm>>ss;
		a[i]=(hh*3600+mm*60+ss)-(h*3600+m*60+s);
	}
	for(int i=0;i<3;i++){
		cout<<a[i]/3600<<" "<<(a[i]/60)%60<<" "<<a[i]%60<<endl;
	}
	return 0;
}

Measured memory_bytes: 1156
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <math.h>
#include <complex>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <functional>
#include <assert.h>
using ll=long long;
#define int ll
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define SZ(x) x.size()
#define fi first
#define se second
const int INF = 1e9;
using namespace std;

template<class T> using vc=vector<T>;
template<class T> using vvc=vc<vc<T>>;

using pi=pair<int,int>;
using vi=vc<int>;

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

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout<<fixed<<setprecision(20);

    while(true){
        int n,x; cin >> n >> x;
        if(n==0 && x==0) break;
        int cnt=0;
        rep(i,n){
            rep(j,n){
                if(i==j) continue;
                int a=x-(i+1)-(j+1);
                if(a!=i+1 && a!=j+1 && a<=n && 1<=a){
                    cnt++;
                }
            }
        }
        cout << cnt/6 << endl;
    }
}


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

using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;
typedef vector < vector<ll> > Mat;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int N, P, A[55];
ll DP[55][2];

int main(){
  cin >> N >> P;
  for (int i = 1; i <= N; i++) cin >> A[i];
  DP[0][0] = 1;
  for (int i = 1; i <= N; i++){
    for (int j = 0; j < 2; j++){
      DP[i][j] = DP[i-1][j];
    }
    for (int j = 0; j < 2; j++){
      DP[i][(j + A[i]) % 2] += DP[i-1][j];
    }
  }
  cout << DP[N][P] << endl;

  return 0;
}


Measured memory_bytes: 3536
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  cin >> S;
  int Q;
  cin >> Q;
  int c = 0;
  string adb = "", ada = "";
  for (int i = 0; i < Q; i++) {
    int T;
    cin >> T;
    if (T == 1) {
      c++;
    } else {
      int F;
      char C;
      cin >> F >> C;
      if ((F == 1 && c % 2 == 0) || (F == 2 && c % 2 == 1)) {
        adb = C + adb;
      } else {
        ada = ada + C;
      }
    }
  }
  if (c % 2 == 1) {
    reverse(S.begin(), S.end());
    reverse(ada.begin(), ada.end());
    reverse(adb.begin(), adb.end());
    S = ada + S + adb;
  } else {
    S = adb + S + ada;
  }
  cout << S << endl;
}

Measured memory_bytes: 1512
#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;

int main(void){
    int A,B,ans=0;
    cin >> A >> B;

    for(int i=A; i<=B; i++){
        int a = i %10;
        int b = i / 10 % 10;
        int c = i / 1000 % 10;
        int d = i / 10000 % 10;
        if(a==d && b==c) ans++;
    }

    cout << ans << endl;

    return 0;
}

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

int main () {
  int N;
  cin >> N;
  vector<int> dia(N + 1);
  for (int i = 0; i < N; i++)
    cin >> dia.at(i);

  sort(dia.begin(), dia.end());
  reverse(dia.begin(), dia.end());
  int pile = 0;
  for (int i = 0; i < N - 1; i++) {
    if (dia.at(i) > dia.at(i + 1))
      pile++;
  }
  
  cout << pile + 1 << endl;
}

Measured memory_bytes: 256
loop do
  n,k = gets.split.map(&:to_i)
  break if (n|k) == 0
  seq = Array.new
  n.times do
    seq<<gets.to_i
  end
  max = -10000*k
  sum = seq[0,k].inject{|a,b| a+b}
  1.upto(n-k) do |i|
    sum=sum-seq[i-1]+seq[i+k-1]
    max = [max,sum].max
  end
  p max
end

Measured memory_bytes: 3580
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MAX = 1e5 + 2;
ll dp[MAX],n,k;
vector<ll> v;

int main()
{
    cin >> n >> k;
    for(int i=0;i<n;i++)
    {
        ll x; cin >> x;
        v.push_back(x);
    }
    //BOTTOM UP
    for(int i=n-1;i>=0;i--)
    {
        ll mn = INT_MAX;
        for(int j=1;j<=k && (i+j) < n;j++)
            mn = min(abs(v[i]-v[i+j])+dp[i+j],mn);
        dp[i] = (mn == INT_MAX )? 0 : mn;
    }
    cout<<dp[0];
    return 0;
}

Measured memory_bytes: 1908
#include <algorithm>
#include <cmath>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include <cstring>
#include <tuple>
using namespace std;
typedef long long ll;
int main() {
    vector<int> n(3);
    cin >> n[0] >> n[1] >> n[2];
    sort(n.begin(), n.end());
    cout << n[0] << " " << n[1] << " " << n[2] << endl;

    return 0;
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

#define DEBUG(x) cout<<#x<<": "<<(x)<<'\n'
#define DEBUG_VEC(v) cout<<#v<<":";REP(i, v.size())cout<<' '<<v[i];cout<<'\n'
#define ALL(a) (a).begin(), (a).end()

template<typename T> inline void CHMAX(T& a, const T b) {if(a<b) a=b;}
template<typename T> inline void CHMIN(T& a, const T b) {if(a>b) a=b;}

constexpr ll MOD=1000000007ll;
// constexpr ll MOD=998244353ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

const double EPS=1e-11;
#define EQ0(x) (abs((x))<EPS)
#define EQ(a, b) (abs((a)-(b))<EPS)

bool can[1252525];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	// cout<<setprecision(10)<<fixed;
	int n, d, x[20][20];
	cin>>n>>d;
	REP(i, n){
		REP(j, d){
			cin>>x[i][j];
		}
	}
	REP(i, 1000){
		can[i*i]=true;
	}
	int ans=0;
	REP(i, n){
		REP(j, i){
			int tmp=0;
			REP(k, d){
				tmp+=(x[i][k]-x[j][k])*(x[i][k]-x[j][k]);
			}
			if(can[tmp]){
				++ans;
			}
		}
	}
	cout<<ans<<'\n';
	return 0;
}


Measured memory_bytes: 1280
#include<bits/stdc++.h>
#define LL long long
#define fr(i,x,y) for(int i=(x);i<=(y);i++)
#define rf(i,x,y) for(int i=(x);i>=(y);i--)
#define frl(i,x,y) for(int i=(x);i<(y);i++)
using namespace std;
const int N=200002;
int n,a[N];
int b[N];
int h[N],w;
int to[N];
LL K;

void read(int &x){ scanf("%d",&x); }
void read(LL &x){ scanf("%lld",&x); }

void gooo(int p,int s){
	memset(b,0,sizeof b);
	fr(i,1,s){
		if (!b[a[p]]) h[++w]=a[p],b[a[p]]=1;
	 	 else{
	 	 	while(h[w]!=a[p]) b[h[w]]=0,w--;
	 	 	b[h[w]]=0;w--;
		  }
		p=p%n+1;
	}
	fr(i,1,w) printf("%d ",h[i]);
}

void goo(int p,LL s){
	while(to[p]-p+1<=s){
		s-=to[p]-p+1;
		p=to[p]%n+1;
	}
	gooo(p,s);
}

int main(){
	read(n);read(K);
	fr(i,1,n) read(a[i]);
	rf(i,n,1) b[a[i]]=i+n;
	rf(i,n,1) to[i]=b[a[i]],b[a[i]]=i;
	memset(b,0,sizeof b);
	int p=1;LL s=0,sc=0;
	while(!b[p]){
		b[p]=1;
		s+=to[p]-p+1;
		p=to[p]%n+1;
	}
	int q=p;
	while(1){
		sc+=to[q]-q+1;
		q=to[q]%n+1;
		if (q==p) break;
	}
	if (n*K<=s) goo(1,n*K);
	 else goo(p,(n*K-s)%sc);
	return 0;
}

Measured memory_bytes: 4608
#include <bits/stdc++.h>
#define N 10000001
using namespace std;
bool num[N];
int top;

bool compare(int x){return top>=0 && top>=max(0,N-x-1);}

void Minus(int x){
  while(1){
    num[x] = !num[x];
    if(num[x] == 0) break;
    x++;
  }
  while(top>=0&&num[top]==0)top--;
}

int main(){
  int n;
  cin>>n;
  num[N-1] = 1;
  top = N-1;
  while(n--){
    int x;
    scanf("%d",&x);
    if(top>=0&&compare(x))printf("Yes\n"),Minus(max(0,N-x-1));
    else printf("No\n");
    
  }
  return 0;
}

Measured memory_bytes: 12900
#include <iostream>
#define rep(i,n) for (int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pi;
typedef pair<pi, pi> pp;
typedef pair<ll, ll> pl;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int inf = 1 << 30;
const ll linf = 1LL << 60;

const int M = 13;

string s;
ll t = 1, ans;
ll dp[2][13];
int ind[13][13];
int ho = 0, ge = 1;

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

    rep(i,M) {
        rep(j,M) ind[i][j] = i*j%M;
    }

    cin >> s;
    dp[ho][0] = 1;
    for (int i = (int)s.size()-1; i >= 0; i--) {
        rep(j,M) dp[ge][j] = 0;
        if (s[i] != '?') {
            int x = t*(s[i]-'0')%M;
            rep(j,M) {
                if (x == M) x -= M;
                dp[ge][x++] = dp[ho][j];
            }
        } else {
            ll sum = 0;
            rep(j,9) sum += dp[ho][ind[t][j]];
            for (int j = 9; j < M; j++) {
                sum += dp[ho][ind[t][j]];
                dp[ge][ind[t][j]] = sum;
                sum = sum+10*MOD-dp[ho][ind[t][j-9]];
            }
            for (int j = 0; j < 9; j++) {
                sum += dp[ho][ind[t][j]];
                dp[ge][ind[t][j]] = sum;
                sum = sum+10*MOD-dp[ho][ind[t][j+4]];
            }
        }
        if (i%2) rep(j,M) dp[ge][j] %= MOD;
        t = t * 10 % M;
        swap(ho,ge);
    }
    cout << dp[ho][5]%MOD << endl;
}


Measured memory_bytes: 512
N,K=map(int,input().split())
A=list(map(int,input().split()))
F=list(map(int,input().split()))
A.sort(reverse=True)
F.sort()
import math
def isok(n):
  sum=0
  for i in range(N):
    sum+=max(0,A[i]-n//F[i])
  return sum<=K
ma=0
for i in range(N):
  ma=max(ma,A[i]*F[i])
start,end=-1,ma
while end-start>1:
  mid=(start+end)//2
  if isok(mid):
    end=mid
  else:
    start=mid
print(end)

Measured memory_bytes: 113824
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 12 21:17:12 2020

@author: over-
"""

N,M=map(int,input().split())
AC=[0]*N
WA=[0]*N
for i in range(M):
    p,S=input().split()
    p=int(p)-1
    
    if S=='AC':
        AC[p]=1
    if S=='WA':
        if AC[p]==0:
            WA[p]+=1
            
for i in range(N):
    if AC[i]==0:
        WA[i]=0
        
print(sum(AC),sum(WA))            
    

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

long long int  diff_maxmin(long long int *S){
    long long int maxS=0,minS=999999999999999;
    for(int i=0;i<3;i++){
        if(S[i]>maxS) maxS=S[i];
        if(S[i]<minS) minS=S[i];
    }
    return maxS-minS;
}


int main(){
    long H,W;
    long long int Sh[3];
    long long int Sw[3];
    long long int temp;
    long long int ansh,answ;

    cin >> H >> W;

    if( (H%3==0) || (W%3==0) ){
        cout << 0 << "\n";
        return 0;
    }

    long Hdash = H;
    temp=round(H/3.0);
    Sh[0] = temp*W;
    Hdash = H-temp;

    if( (Hdash%2==0) || (W%2==0) ){
        Sh[1] = Hdash*W/2;
        Sh[2] = Sh[1];
    }
    else{
        if(Hdash>W){
            temp = round(Hdash/2.0);
            Sh[1] = temp*W;
            Sh[2] = (Hdash-temp)*W;
        }
        else{
            temp = round(W/2.0);
            Sh[1] = temp*Hdash;
            Sh[2] = (W-temp)*Hdash;
        }
    }

    long Wdash=W;
    temp=round(W/3.0);
    Sw[0] = temp*H;
    Wdash = W-temp;

    if( (H%2==0) || (Wdash%2==0 )){
        Sw[1] = H*Wdash/2;
        Sw[2] = Sw[1];
    }
    else{
        if(H>Wdash){
            temp = round(H/2.0);
            Sw[1] = temp*Wdash;
            Sw[2] = (H-temp)*Wdash;
        }
        else{
            temp = round(Wdash/2.0);
            Sw[1] = temp*H;
            Sw[2] = (Wdash-temp)*H;
        }
    }

    ansh = diff_maxmin(Sh);
    answ = diff_maxmin(Sw);

    if(ansh>answ){
        cout << answ << "\n";
    }
    else{
        cout << ansh << "\n";
    }

    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <string>
#include <math.h>
#define ll long long
#define vl vector<ll>
#define vvl vector<vector<ll> >
#define vvvl vector<vector<vector<ll> > >
#define vd vector<double>
#define vvd vector<vector<double> >
#define vvvd vector<vector<vector<double> > >
#define mod 1000000007
#define inf 11451419198
using namespace std;

int main(){
	ll n, k; cin >> n >> k;
	vl x(n);
	ll ans = inf;
	for (ll i = 0; i < n; ++i) cin >> x[i];
	for (int i = 0; i + k - 1 < n; ++i){
		if (x[i] < 0 && 0 < x[i + k-1]) ans = min(ans, abs(x[i]) + x[i + k-1] + min(abs(x[i]), x[i + k-1]));
		else if (x[i] >= 0) ans = min(ans, x[i + k-1]);
		else ans = min(ans, abs(x[i]));
	}cout << ans << endl;
	return 0;
}

Measured memory_bytes: 1024
#!/usr/bin/env python3
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
a,b = map(int,input().split())
for i in range(320):
    if int(str(a)+str(b)) == i**2: 
        print("Yes")
        exit(0)
print("No")

Measured memory_bytes: 3060
#include <iostream>
#include <iomanip>
#include <sstream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <functional>
#include <iterator>
#include <limits>
#include <numeric>
#include <utility>
#include <cmath>
#include <cassert>
#include <cstdio>

// #include <unordered_set>
// #include <unordered_map>

#define rep(i, a, n) for(int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())

#define pb push_back
#define mp make_pair

int gcd(int a, int b) {
    int c;
    while (b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return a;
}

int lcm(int a, int b) {
    int c = gcd(a, b);
    a /= c;
    return a * b;
}

typedef std::pair<int, int> P;
typedef long long ll;
typedef long double ld;

const int MOD = 1000000007;
const int INF = 1e9;
const ll LINF = INF * 1ll * INF;
const ld DINF = 1e200;
const double EPS = 1e-10;
const double PI = acos(-1.0);


#define int long long

using namespace std;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {-1, 0, 1, 0};


vector<string> s;

bool used[4000][4000] = {false};
long long black_count;
long long white_count;

void dfs(int y, int x, int w, int h) {
    used[y][x] = true;

    if (s[y][x] == '#') black_count++;
    else if (s[y][x] == '.')white_count++;

    for (int i = 0; i < 4; i++) {

        // move to 4 directions
        int nx = x + dx[i];
        int ny = y + dy[i];

//        cout << ny << ' ' << nx << " " << s[ny][nx] << endl;

        if (used[ny][nx])continue; // already used
        if (nx < 0 || ny < 0 || nx >= w || ny >= h)continue; // invalid area
        if (s[ny][nx] == s[y][x])continue; // '#' => '.' -> '#' ... : not same char

        dfs(ny, nx, w, h);
    }

}


signed main() {
    int h, w;
    int ans = 0;
    cin >> h >> w;

    s.resize(h);

    for (int i = 0; i < h; i++) {
        cin >> s[i];
    }

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {

//            cout << y << " " << x << " " << s[y][x] << endl;

            if (s[y][x] == '.') continue; // black start, not white
            if (used[y][x])continue; // already used

            // counter init
            black_count = 0;
            white_count = 0;

            dfs(y, x, w, h);
            ans += black_count * white_count;
        }
    }

    cout << ans << endl;

    return 0;
}



Measured memory_bytes: 19584
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

//#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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

template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);

    int A[3][3];
    int B[3][3] = {};
    REP(i,3) REP(j,3){
        cin >> A[i][j];
    }
    int N; cin >> N;
    REP(i,N){
        int b; cin >> b;
        REP(j,3) REP(k,3){
            if(A[j][k] == b) B[j][k] = 1;
        }
    }

    //REP(i,3) {
    //    REP(j,3){
    //    cout << B[i][j] << " ";
    //    }
    //    cout << endl;
    //}
    REP(i,3){
        bool ok = true;
        REP(j,3){
            if(B[i][j] != 1) ok = false;
        }
        if(ok) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    dump("c1")
    REP(i,3){
        bool ok = true;
        REP(j,3){
            if(B[j][i] != 1) ok = false;
        }
        if(ok) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    dump("c2")

    bool ok = true;
    REP(i,3){
        if(B[i][i] != 1) ok = false;
    }
        if(ok) {
            cout << "Yes" << endl;
            return 0;
        }

    dump("c3")
    ok = true;
    REP(i,3){
        if(B[i][2-i] != 1) ok = false;

    }
        if(ok) {
            cout << "Yes" << endl;
            return 0;
        }

    cout << "No" << endl;


}


Measured memory_bytes: 256
#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define SZ(x) (int)(x.size())
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define inf 1000000007
#define mod 1000000007
#define x first
#define y second
#define pi acos(-1.0)
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
//#define dprintf(...) 
#define hash _hash
#define next _next
//#define dprintf(...) fprintf(outFile,__VA_ARGS__)
 
#define FOREACH(it,x) for(__typeof(x.begin()) it=x.begin();it!=x.end();it++)
#define ull unsigned long long
#define ll long long
#define N 100010
 
template <class T,class U>inline void Max(T &a,U b){if(a<b)a=b;}
template <class T,class U>inline void Min(T &a,U b){if(a>b)a=b;}
 
//FILE* outFile;
inline void add(int &a,int b){a+=b;if(a>=mod)a-=mod;}


int pow(int a,int b){
    int ans=1;
    while(b){
        if(b&1)ans=ans*(ll)a%mod;
        a=(ll)a*a%mod;b>>=1;
    }
    return ans;
}


int b[60];
int main(){
    //freopen("A.in","r",stdin);
    //freopen("A.out","w",stdout);
    //cout<<setprecision(9)<<fixed;
    //cerr<<setprecision(9)<<fixed;
    int T,i,j,k,ca=0,m,K;
    ll n;scanf("%lld",&n);
    n++;
    K=101;
    deque<int>q;
    //rep(i,1,K)v.pb(i);
    m=0;
    while(n)b[m++]=n&1,n>>=1;
    m--;
    i=1;
    while(i<K){
        if(m==0)break;
        q.pb(i++);
        if(b[m-1])q.push_front(i++);
        m--;
    }
    vi v;
    rep(i,1,K)v.pb(i);
    rep(i,0,SZ(q))v.pb(q[i]);
    printf("%d\n",SZ(v));
    rep(i,0,SZ(v))printf("%d ",v[i]);
    puts("");
    //cerr<<clock()*1./CLOCKS_PER_SEC<<"ms\n";
    return 0;
}

Measured memory_bytes: 256
// B.

#include <iostream>
#include <sstream>
#include <algorithm>

using namespace std;

int main(int argc, char *argv[]) {
	int n, a, low = 1 << 30, high = 0;
	cin >> n;
	for (int i = 0; i < n; ++i) {
		cin >> a;
		low = min(low, a);
		high = max(high, a);
	}
	cout << (high - low) << endl;
	return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<math.h>
#include<vector>
#include<algorithm>
#include<string.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
long long int mod=1e9+7;
bool debug=false;
typedef long long ll;
int main(){
	string s;
	cin>>s;
	if(s[0]=='S')cout<<"Cloudy\n";
	else if(s[0]=='C')cout<<"Rainy\n";
	else cout<<"Sunny\n";
	return 0;
}

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

using namespace std;

typedef long long ll;
const ll INF = 1LL<<60;

ll gcd(ll a, ll b) {
  if(b == 0) return a;
  return gcd(b, a % b);
}

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; }

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

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

  int H, W;
  cin >> H >> W;
  vector<vector<int>> in(H);
  vector<vector<int>> ans(H+2);
  string S;
  for(int i = 0; i < H; i++){
    cin >> S;
    for(int j = 0; j< W; j++){
      if(S[j] == '#'){
        in[i].push_back(1);
      }else{
        in[i].push_back(0);
      }
    }
  }

  for(int i = 0; i < H+2; i++){
    for(int j = 0; j < W+2; j++){
      ans[i].push_back(0);
    }
  }

  for(int i = 0; i < H; i++){
    for(int j = 0; j < W; j++){
      if(in[i][j] == 1){
        
        ans[i][j]     += 1;
        ans[i][j+1]   += 1;
        ans[i][j+2]   += 1;
        ans[i+1][j]   += 1;
        ans[i+1][j+2] += 1;
        ans[i+2][j]   += 1;
        ans[i+2][j+1] += 1;
        ans[i+2][j+2] += 1;

      }
    }
  }


  for(int i = 1; i < H+1; i++){
    S = "";
    for(int j = 1; j < W+1; j++){
      if(in[i-1][j-1] == 1){
        S = S + "#";
      }else{
        S = S + to_string(ans[i][j]);
      }
    }
    cout << S << endl;
  }

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long 
#define endl "\n"
#define pb push_back
#define f(i,n) for(i=0;i<n;i++)
#define F(i,a,b) for(i=a;a<=b;i++)
#define arr(a,n) for( i=0;i<n;i++)cin>>a[i];
#define fi first
#define se second
#define mp make_pair
#define mod 1000000007
#define YES cout<<"YES"<<endl;
#define Yes cout<<"Yes"<<endl;
#define NO cout<<"NO"<<endl;
#define No cout<<"No"<<endl;
#define yes cout<<"yes"<<endl;
#define no cout<<"no"<<endl;
#define vi vector<ll>
#define ed end()
#define bg begin()
#define sz size()
#define ln length()
#define s() sort(a,a+n);
#define sr() sort(a,a+n,greater<ll>());
#define v()  sort(v.begin(),v.end());
#define vr() sort(v.begin(),v.end(),greater<ll>());
#define mod 1000000007
#define fast() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);}
ll power(ll x,ll y,ll p){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
int main() {
/*    #ifndef ONLINE_JUDGE
      // for getting input from input.txt
      freopen("input.txt", "r", stdin);
      // for writing output to output.txt
      freopen("output.txt", "w", stdout);
  #endif*/
fast();
//ll  t;cin>>t;while(t--)
{
  ll a[3],n=3;
for(int i=0;i<n;i++)
{
  cin >> a[i];
}
sr()
cout<<abs(a[0]-a[1]+abs(a[1]-a[2])) << endl;
  
  

}
return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define forr(i, a, b) for(int i = (a); i < (b); i++)
#define rep(i,n) for(int i = 0; i < (n); i++)
#define rrep(i,n) for(int i = 1; i <= (n); i++)
#define ALL(a) (a.begin()),(a.end())
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll, ll> LP;
const ll LINF = 1LL<<60;
const int INF = 1001001001;

/* --------------------------------------------------- */

int main() {
    int d;
    cin >> d;
    vector<int> c(26);
    rep(i, 26) cin >> c[i];
    vector<vector<int>> s(d, vector<int>(26));
    rep(i, d) {
        rep(j, 26) cin >> s[i][j];
    }

    vector<int> lastDay(26, 0);
    ll ans = 0;
    rep(i, d) {
        int t;
        ll a = -1;
        rep(j, 26) {
            ll sum = ans;
            vector<int> copyDay;
            copy(ALL(lastDay), back_inserter(copyDay));
            sum += s[i][j];
            copyDay[j] = i + 1;
            ll tot = 0;
            rep(k, 26) {
                tot += c[k] * (i + 1 - copyDay[k]);
            }
            sum -= tot;
            if(sum >= a) {
                a = sum;
                t = j;
            }
        }
        ans += a;
        lastDay[t] = i + 1;
        cout << t + 1 << endl;
    }

    return 0;
}

Measured memory_bytes: 3684
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <string>
#include <cstring>
#include <numeric>
#include <iomanip>
#include <fstream>
using namespace std;
int main(){
	int n,m;
	int k[10];
	int s[10][10];
	int p[10];
	cin>>n>>m;
	for(int i=0;i<m;i++){
		cin>>k[i];
		for(int j=0;j<k[i];j++){
			cin>>s[i][j];
			s[i][j]--;
		}
	}
	for(int i=0;i<m;i++){
		cin>>p[i];
	}
	int ans=0;
	for(int i=0;i<(1<<n);i++){
		bool f[10]={};
		for(int j=0;j<n;j++){
			if(i&(1<<j)){
				f[j]=1;
			}
		}
		bool ok=1;
		for(int j=0;j<m;j++){
			int cnt=0;
			for(int l=0;l<k[j];l++){
				if(f[s[j][l]]){
					cnt++;
				}else{
					continue;
				}
			}
			if(cnt%2==p[j]){
				continue;
			}else{
				ok=0;
			}
		}
		if(ok){
			ans++;
		}
	}
	cout<<ans<<endl;
	return 0;
}

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

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
 
using namespace std;
 
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
 
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

using namespace std;
 
int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("in", "r", stdin);
#endif
	
	string s;
	int n;
	cin >> n;
	cin >> s;
	if (n & 1) {
		cout << "No" <<'\n';
		return 0;
	}
	int hf = n / 2;
	bool ok = true;
	forn(i, hf) {
		if (s[i] != s[i + hf]) {
			ok = false;
			break;
		}
	}
	cout << (ok ? "Yes" : "No") << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
	return 0;
}

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

int main() {
  vector<int>a(4);
  vector<int>b(4);
  while(cin >>a.at(0)>>a.at(1)>>a.at(2)>>a.at(3)>>b.at(0)>>b.at(1)>>b.at(2)>>b.at(3)){
    int hit=0;
    int blow=0;
    for(int i=0;i<4;i++){
      if(a.at(i)==b.at(i)){
        hit++;
      }
    }
    if(a.at(0)==b.at(1)){
      blow++;
    }
    if(a.at(0)==b.at(2)){
      blow++;
    }
    if(a.at(0)==b.at(3)){
      blow++;
    }
    if(a.at(1)==b.at(0)){
      blow++;
    }
    if(a.at(1)==b.at(2)){
      blow++;
    }
    if(a.at(1)==b.at(3)){
      blow++;
    }
    if(a.at(2)==b.at(0)){
      blow++;
    }
    if(a.at(2)==b.at(1)){
      blow++;
    }
    if(a.at(2)==b.at(3)){
      blow++;
    }
    if(a.at(3)==b.at(0)){
      blow++;
    }
    if(a.at(3)==b.at(1)){
      blow++;
    }
    if(a.at(3)==b.at(2)){
      blow++;
    }
  cout <<hit<<" "<<blow<<endl;
  }
  return 0;
}



Measured memory_bytes: 3012
N=int(input())
a=list(map(int,input().split()))
a.insert(0,0)
a.append(0)
cost=[]
for i in range(N+1):
    if i==0:
        cost.append(abs(a[i]))
    else:
        cost.append(abs(a[i-1]-a[i]))
cost.append(abs(a[N]))
S=sum(cost)
for i in range(N):
    print(S-cost[i+1]-cost[i+2]+abs(a[i+2]-a[i]))


Measured memory_bytes: 14176
#include <cstdio>
#include <algorithm>
using namespace std;

#define MAX_N 1000
#define MAX_W 10001

typedef int int32_t;

/*????????????
 * ?????????????????°???????????????????????????????????????????????????????????????????????????????§?????????¢??°???????????¨??????????¨?????????°????????????????????¨???????????????
 * ????????§??????????????°?????????????????????????°????????????????
 *   1. ?????°??????????°???????local min.?????¨??????????????°????????????
 *   2. ??°??????????°???????global min.?????¨??????????°???????????????????????????????????????°???????????????????????????
 * ?????????????????§????????????????????????????????????????¨????????????¨???????????°??????????????????????°??????????????????????????????¨?????????????????°?????????????????????????°????????????¨????????????????????????????????£?????????
 */

int main(void) {
    int n;
    int w[MAX_N];
    int index[MAX_W];
    bool moved[MAX_N] = {};

    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &w[i]);
        index[w[i]] = i;
    }

    sort(w, w + n);

    int gmin = w[0]; // global minimum
    int sum = 0;
    for (int i = 0; i < n; i++) {
        int j = index[w[i]];
        if (moved[j] || i == j) {
            continue;
        }
        int t = 0;
        int c = 0;
        int lmin = w[i]; // local minimum of given cyclic group
        for (; !moved[j]; j = index[w[j]], c++) {
            t += w[j];
            moved[j] = true;
        }

        sum += min((c-2)*lmin + t, 2 * (gmin + lmin) + (c-1)*gmin + (t - lmin));
    }

    printf("%d\n", sum);
    return 0;
}

Measured memory_bytes: 1060
#include <bits/stdc++.h>
using namespace std;
int fun(int a)
{
	int s=0;
	while(a!=0)
	{
		s+=a%10;
		a/=10;
	}
	return s;
}
int main()
{
	int n;
	int s;
	scanf("%d",&n);
	s=fun(n);
	if(n%s==0)
		printf("Yes");
	else
		printf("No");
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define MP make_pair

inline int gn(){
    int s=0,f=1; 
    char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-') f=-1;ch = getchar();}
    while(isdigit(ch)) {s=s*10+ch-'0';ch = getchar();}
    return s*f;
}

inline ll gl(){
    ll s=0,f=1; 
    char ch = getchar();
    while(!isdigit(ch)) {if(ch == '-') f=-1;ch = getchar();}
    while(isdigit(ch)) {s=s*10+ch-'0';ch = getchar();}
    return s*f;
}

const int N=2e5+100;
ll x;
bool check(ll mid){
    ll ans=100;
    ll i;
    while(1){
        i=ans/100;
        ll num=((i+1)*100-ans+i-1)/i;
        if(mid>num){
            ans+=num*i;
            mid-=num;
        }
        else{
            ans+=mid*i;
            break;
        }
    }
    return ans>=x;
}

void solve(){
    x=gl();
    ll l=0,r=4000;
    ll ans=0;
    while(l<=r){
        ll mid=(l+r)/2;
        if(check(mid)){
            ans=mid;
            r=mid-1;
        }
        else l=mid+1;
    }
    cout<<ans<<endl;
}

int main(){
    // int t;
    // t=gn();
    // while(t--)
        solve();
}

/**
* In every life we have some trouble
* When you worry you make it double
* Don't worry,be happy.
**/


Measured memory_bytes: 3620
#include<stdio.h>

int main(void)
{
	int a,b;
	scanf("%d %d",&a,&b);
	((a<=-1000)&&(b>=1000));
	if(a > b)
	{
		printf("a > b\n");
	}
	else
	{
		if(a < b)
		{
			printf("a < b\n");
		}
		else
		{
			printf("a == b\n");
		}
	}
	return 0;											
}

Measured memory_bytes: 580
#include<bits/stdc++.h>
using namespace std;
int main(){
  int N,X;
  cin >> N >> X;
  vector<int> D(N+1);
  D[0]=0;
  for(int i=0;i<N;i++){
    int x;
    cin >> x;
    D[i+1] = D[i]+x;
  }
  int ans=0;
  for(int i=0;i<=N;i++){
    if(D[i]<=X){
      ans++;
    }
  }
  cout << ans << endl;
}

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

#define int long long int
#define ff first
#define ss second
#define pb push_back

int bexp(int a, int b)
{
    int res = 1;

    while (b)
    {
        if (b & 1) res = (res * a);

        a = (a * a); 
        b >>= 1;
    }
    return res;
}

#define N 200010
#define MOD 1000000007

int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}


int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cout.precision(10);

    int n;
    cin >> n;

    int a[n];
    for (int i = 0; i < n; ++i)
    {
        cin >> a[i];
    }

    int ans = 0;
    for (int i = 0; i < n; i += 2)
    {
        if (a[i] & 1)
            ans++;
    }

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


Measured memory_bytes: 3604
#include <stdio.h>
int main() {
  int x, y, tmp;

  scanf("%d %d", &x, &y);

  if(x<y) {
    tmp = x;
    x = y;
    y = tmp;
  }
  tmp = x%y;
  while(tmp != 0) {
    x = y;
    y = tmp;
    tmp = x%y;
  }
  printf("%d\n", y);
  return 0;
}

Measured memory_bytes: 592
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define REPP(i,n) for(int i=1;i<=n;i++)
const double PI = acos(-1);
const double EPS = 1e-15;
long long INF=(long long)1E17;
#define i_7 (long long)(1E9+7)
long mod(long a){
    long long c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
using namespace std;
bool prime(int n){
  if(n==1){
    return false;
  }else if(n==2){
    return true;
  }else{
    for(int i=2;i<=sqrt(n);i++){
      if(n%i==0){
        return false;
      }
    }
    return true;
  }
}

long long gcd(long long a, long long b){
  if(a<b){
    swap(a,b);
  }
  if(a%b==0){
    return b;
  }else{
    return gcd(b,a%b);
  }
}
 
long long lcm(long long x, long long y){
  return (x/gcd(x,y))*y;
}
 
class UnionFind {
    public:
    //各頂点の親の番号を格納する。その頂点自身が親だった場合は-(その集合のサイズ)を入れる。
    vector<int> Parent;
    
    //クラスを作るときは、Parentの値を全て-1にする。
    //以下のようにすると全てバラバラの頂点として解釈できる。
    UnionFind(int N) {
        Parent = vector<int>(N, -1);
    }
    
    //Aがどのグループに属しているか調べる
    int root(int A) {
        if (Parent[A] < 0) return A;
        return Parent[A] = root(Parent[A]);
    }
    
    //自分のいるグループの頂点数を調べる
    int size(int A) {
        return -Parent[root(A)];//先祖をrootで取っておきたい。
    }
    
    //AとBをくっ付ける
    bool connect(int A, int B) {
        //AとBを直接つなぐのではなく、root(A)にroot(B)をくっつける
        A = root(A);
        B = root(B);
        if (A == B) {
            //すでにくっついてるからくっ付けない
            return false;
        }
        
        //大きい方(A)に小さいほう(B)をくっ付けたい
        //大小が逆だったらAとBをひっくり返す。
        if (size(A) < size(B)) swap(A, B);
        
        //Aのサイズを更新する
        Parent[A] += Parent[B];
        //Bの親をAに変更する
        Parent[B] = A;
        
        return true;
    }
};

int main(){
  int x,y;
  cin>>x>>y;
  if(x>=0 && y>=0){
    if(x<y){
      cout<<y-x<<endl;
    }else{
      cout<<min(x+y+1,x-y+2)<<endl;
    }
  }else if(x>=0 && y<0){
    if(x<abs(y)){
      cout<<abs(y)-x+1<<endl;
    }else{
      cout<<x-abs(y)+1<<endl;
    }
  }else if(x<0 && y>=0){
    if(abs(x)<y){
      cout<<y-abs(x)+1<<endl;
    }else{
      cout<<min(y-x,abs(x)-y+1)<<endl;
    }
  }else{
    if(abs(x)<abs(y)){
      cout<<abs(y)-abs(x)+2<<endl;
    }else{
      cout<<abs(x)-abs(y)<<endl;
    }
  }
  return 0;
}


Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=2000+10,mo=924844033;
int f[maxn][maxn][4],g[maxn],h[maxn],fac[maxn];
int i,j,k,l,r,t,n,m,ans;
int main(){
    scanf("%d%d",&n,&k);
    fac[0]=1;
    fo(i,1,n) fac[i]=(ll)fac[i-1]*i%mo;
    f[1][0][0]=1;
    f[1][1][2]=1;
    fo(i,1,n-1)
        fo(j,0,i)
            fo(t,0,3)
                if (f[i][j][t]){
                    (f[i+1][j][(t>>1)]+=f[i][j][t])%=mo;
                    if (t%2==0) (f[i+1][j+1][(t>>1)]+=f[i][j][t])%=mo;
                    (f[i+1][j+1][(t>>1)+2]+=f[i][j][t])%=mo;
                }
    g[0]=1;
    fo(r,1,k){
        t=r;
        l=1;
        while (t+k<=n){
            l++;
            t+=k;
        }
        fo(i,0,n) h[i]=0;
        fo(i,0,n)
            fo(j,0,l)
                fo(t,0,1)
                    if (i+j<=n) (h[i+j]+=(ll)g[i]*f[l][j][t]%mo)%=mo;
        fo(i,0,n) g[i]=h[i];
    }
    fo(i,0,n){
        t=(ll)g[i]*fac[n-i]%mo;
        if (i%2) (ans-=t)%=mo;else (ans+=t)%=mo;
    }
    (ans+=mo)%=mo;
    printf("%d\n",ans);
}


Measured memory_bytes: 61312