akhauriyash/Code-Regression huggingface dataset:
#include<iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int bn;
    int bsn;
    int mx;
    for(int i=0;i<n-1;i++){
        bn=bsn;
        cin>>bsn;
        if(i==0){mx=bsn;}
        else{
            if(bn>bsn){
                mx=mx+bsn;
            }
            else{mx=mx+bn;}
        }
    }
    cout<<mx+bsn<<endl;
    return 0;
}

Measured memory_bytes: 256
mod = 10**9+7
N = int(input())
a = [list(map(int, input().split())) for _ in range(N)]
dp = [0] * (1 << N)
dp[0] = 1
for bit in range(1, 1 << N):
    i = 0
    for j in range(N):
        if bit >> j & 1 == 1:
            i += 1
    i -= 1
    for j in range(N):
        if a[i][j] == 1 and bit >> j & 1 == 1:
            dp[bit] += dp[bit & ~(1 << j)]
            dp[bit] %= mod
print(dp[-1])

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

using u32 = uint32_t;
using u64 = uint64_t;
using i32 = int32_t;
using i64 = int64_t;

int main()
{
	std::ios_base::sync_with_stdio(0);
	
	u32 n;
	std::string s;

	std::cin >> n >> s;

	char pc = s[0];

	u32 ans = 1;

	for(u32 i=1; i<n; i++)
	{
		if(s[i] != pc)
		{
			ans++;
			pc = s[i];
		}
	}

	std::cout << ans << "\n";
	
	return 0;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define erep(i,a,b) for(int i=a;i<=(int)(b);++i)
#define per(i,a,b) for(int i=(b);i>(a);--i)
#define eper(i,a,b) for(int i=((int)(a));i>=b;--i)
#define pb push_back
#define mp make_pair
#define INF (1<<28)-1
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a, b)*b;}

int n, m;
ll fact[100005];   
int main() {
 ios::sync_with_stdio ( false );
 cin.tie ( 0 );
    fact[0] = 1;
    for (ll i = 1; i < 100001; i++) {
        fact[i] = fact[i-1] * i % MOD;
    }
    cin >> n >> m;
    if (n == m) cout << (fact[n] * 2 % MOD) * fact[m] % MOD << endl;
    else if (abs(n-m) <= 1) cout << (fact[max(n, m)] % MOD) * fact[min(n, m)] % MOD << endl;
    else cout << 0 << endl;
    return 0;
}


Measured memory_bytes: 1024
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <numeric>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iterator>
#include <cstring>

using namespace std;
const int INF = (1<<30);
typedef pair<int, int> P;
typedef long long ll;
#define PB push_back
#define rep(i, a) for (int i = 0; i < a; i++)

int sx, sy;

int gx, gy;

int qqq;

//各座標までの最短距離
int d[100][100];


//移動方向のベクトル
int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };

//汎用
ll ans = 0;
pair<int, int> red[101], blue[101];
ll BIG = 10000000000;

//累乗
ll POW(ll a, ll b) {
ll c = 1;
for (int i = 0; i < b; ++i) {
c *= a;
}
return c;
}

//階乗
int Kaijou(int a) {
if (a < 2) return 1;
return a * Kaijou(a - 1);
}


int A[200000];
int kinds[200001];


int main() 
{
	int n,m;
	
	while(1){
		cin >> n>>m;
		if (n == 0&&m==0) break;
		vector<int> taro(n);
		vector<int> hanako(m);
		int Staro = 0, Shanako = 0;
		rep(i, n) {
			cin >> taro[i];
			Staro += taro[i];
		}
		rep(i, m) {
			cin >> hanako[i];
			Shanako += hanako[i];
		}
		int dif = Staro - Shanako;
		int Ataro=-1, Ahanako=-1,min=INF;
		rep(i, n) rep(j, m) {
			if (2 * (taro[i] - hanako[j]) == dif) {
				if (min > taro[i] + hanako[j]) {
					min = taro[i] + hanako[j];
					Ataro = taro[i], Ahanako =hanako[j];
				}
			}
		}
		if (Ataro == -1) {
			cout << -1 << endl;
		}
		else {
			cout << Ataro << " " << Ahanako << endl;
		}
	}

	cin >> qqq;
}



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

int main() {
  int N, L; cin >> N >> L;
  vector<string> S(N);
  for (int i=0; i<N; i++) cin >> S[i];
  sort(S.begin(), S.end());
  string all;
  for (int i=0; i<N; i++) all+=S[i];
  cout << all << endl;
}

Measured memory_bytes: 256
n = int(input())
w = list(map(int, input().split()))
sum_w = sum(w)
res = 10**6
count_up = 0
for i in range(n):
    count_up += w[i]
    sum_w -= w[i]
    if abs(sum_w - count_up) < res:
        res = abs(sum_w - count_up)
print(res)
    


Measured memory_bytes: 9184
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

S = input()

if S[0] == "A":
	for i in range(2, len(S)-1):
		if S[i] == "C":
			num = i
			s = S[1:num] + S[num+1:]
			if s.islower():
				print("AC")
				os.sys.exit()
print("WA")







Measured memory_bytes: 5384
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>

using namespace std;
typedef long long LL;
const int maxn = 200005;

int ehead[maxn],ecnt;
struct edge {
	int u,v,next;
}edg[maxn];
void add(int u,int v) {
	edg[++ecnt]=(edge){u,v,ehead[u]};
	ehead[u]=ecnt;
}
int n,K,a[maxn],ans;
int dfs(int u)
{
	int mxdp=0;
	for (int j=ehead[u];j;j=edg[j].next)
		mxdp=max(mxdp,dfs(edg[j].v));
	if (u!=1&&mxdp+1+(a[u]!=1)>K)
		{++ans;return 0;}
	return mxdp+1;
}
int main()
{
	#ifdef Amberframe
		freopen("agc004d.in","r",stdin);
		freopen("agc004d.out","w",stdout);
	#endif
	scanf("%d %d",&n,&K);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	for (int i=2;i<=n;i++) add(a[i],i);
	dfs(1);
	printf("%d",ans+(a[1]!=1));
	return 0;
}

Measured memory_bytes: 6912
#include <iostream>
#include <set>

using namespace std;

int main() {
    int n, num;
    set<int> s;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> num;
        s.insert(num);
    }
    if (n == s.size()) {
        cout << "YES" << endl;
    } else {
        cout << "NO" << endl;
    }
}

Measured memory_bytes: 9600
// This file is a "Hello, world!" in C++ language by GCC for wandbox.
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>

using namespace std;

int main()
{
    string a, b, c;
    int m, n;
    
    cin >> a >> b;
    cin >> m >> n;
    cin >> c;
    
    if (a == c) {
        cout << m - 1 << " " << n << endl;
    } else {
        cout << m  << " " << n - 1<< endl;
    }

    return 0;
}

// GCC reference:
//   https://gcc.gnu.org/

// C++ language references:
//   https://cppreference.com/
//   https://isocpp.org/
//   http://www.open-std.org/jtc1/sc22/wg21/

// Boost libraries references:
//   https://www.boost.org/doc/


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

using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
using P = complex<double>;
using vp = vector<P>;

vp points;
vp cross;

int main()
{
  int n;
  while(cin >> n,n){
    points.clear();
    cross.clear();

    REP(i,n){
      double a,b;
      cin >> a >> b;
      points.push_back({a,b});

    }
    REP(i,n)REP(j,n){
      P dif = (points[i]-points[j]);
      if(abs(dif) <= 2){
        auto veci = P({dif.real()/abs(dif),dif.imag()/abs(dif)});
        auto semi = P({dif.real()/2,dif.imag()/2});

        cross.push_back(points[j]+semi+P({veci.real()*sqrt(1-abs(semi)*abs(semi)),veci.imag()*sqrt(1-abs(semi)*abs(semi))})*P(0,1));
        cross.push_back(points[j]+semi+P({veci.real()*sqrt(1-abs(semi)*abs(semi)),veci.imag()*sqrt(1-abs(semi)*abs(semi))})*P(0,-1));
      }
    }

    int ma = 0;
    REP(i,cross.size()){
      int now = 0;
      REP(j,n){
      if(abs(cross[i]-points[j]) <= 1.0001){
        now++;
      }
      ma = max(ma,now);
      }
    }
    cout << max(1,ma) << endl;
  }
}

Measured memory_bytes: 3920
#include <iostream>
#include <numeric>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <set>
#include <map>
using namespace std;
using ll=long long;
#define rep(i,a,b) for(ll i=a;i<b;i++)
int main(){
   int n,s=1;
   cin>>n;
   rep(i,1,n+1){
      rep(j,2,32){
         int t=i,cnt=0;
         while (t%j==0){
         t=t/j;
         cnt++;
         }
         if ((t==1)&&(cnt>1)&&(i>s)) s=i;
      }
   }
   cout<<s;
}


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

using namespace std;

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

int main() {
	int p, q;
	
	while (cin >> p >> q) {
		string ans;
		map<int, int> m;
		int pos = -1;
		
		m[p % q] = 0;
		FOR(i, 1, 100) {
			p *= 10;
			int a = p / q, b = p % q;
			
			ans += (char)(a + '0');
			
			if (b == 0) break;
			if (m.find(b) != m.end()) {
				pos = m[b];
				break;
			}
			m[b] = i;
			
			p = b;
		}
		
		cout << ans << endl;
		
		if (pos != -1) {
			REP(i, pos) putchar(' ');
			FOR(i, pos, ans.size()) putchar('^');
			puts("");
		}
		
	}
	return 0;
}

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

#define rep(i, x, n) for (int i = x; i < n; i++)
typedef long long ll;

const int INF = 1e9 + 7;

using namespace std;

int ans[1000000];

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

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

  vector<int> a(q);
  rep (i, 0, q)
    cin >> a[i];

  if (k > q) {
    rep (i, 0, n)
      cout << "Yes" << endl;
    return 0;
  }

  int th = q - k;
  rep (i, 0, q)
    ans[a[i] - 1]++;

  rep (i, 0, n) {
    if (ans[i] > th)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }

	return 0;
}


Measured memory_bytes: 1408
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<vector>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int INF = 0x3fffffff;

int x, y;

int gcd(int x, int y) { if(!y) return x; else return gcd(y, x%y); }

int main()
{
	while(scanf("%d%d",&x,&y) != EOF)
	{
		int ans = gcd(x, y);
		cout<<ans<<" "<<(ll)x*y/ans<<endl;
	}
	return 0;
}

Measured memory_bytes: 3212
#include <stdio.h>
#include <stdlib.h>
int main(){
	int a,b,c,d;
	scanf("%d %d %d %d",&a,&b,&c,&d);
	
	if( abs(a-c) <= d){
		printf("Yes");
		return 0;
	}
	
	if( abs(a-b) > d ){
		printf("No");
		return 0;
	}
	if( abs(b-c) > d){
		printf("No");
		return 0;
	}
	printf("Yes");
	return 0;
}

Measured memory_bytes: 128
from collections import defaultdict as dd
n,m = map(int,input().split())
a = list(map(int,input().split()))
a_s = [0]
for i in range(n):
    a_s.append((a_s[-1]+a[i])%m)
d = dd(int)
for i in a_s:
    d[i] += 1
res = 0
for k in d.values():
    if k>=2:
        res += k*(k-1)//2
print(int(res))


Measured memory_bytes: 16556
n = int(input())
A = [*map(int,input().split())]
ok = 0
ans = 1
lim = 10**18
for i in range(n):
  ans *= A[i]
  if(ans>lim):
      ok =1
      break
if(ok and 0 not in A):
    print(-1)
elif(0 in A):
    print(0)
else:
    print(ans)

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

using namespace std;

typedef long long ll;

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

  unordered_set<ll> exist;
  ll cur = 7;
  ll n = 1;
  while(1){
    cur = cur % K;
    if (cur == 0) break;

    if (exist.find(cur) != exist.end())
    {
      cout << "-1" << endl;
      return 0;
    }

    exist.emplace(cur);
    cur = cur * 10 + 7;
    ++n;
  }

  cout << n << endl;
  return 0;
}

Measured memory_bytes: 42700
import numpy as np
N, M = map(int, input().split())
a, b = np.array([input().split() for _ in range(N)], dtype=int).T
c, d = np.array([input().split() for _ in range(M)], dtype=int).T

dx = np.subtract.outer(a, c)
dy = np.subtract.outer(b, d)
dist = np.abs(dx) + np.abs(dy)
ans = np.argmin(dist, axis=1)+1
print(*ans, sep="\n")

Measured memory_bytes: 20676
#include <bits/stdc++.h>
using namespace std;
#define MAXN 100
#define ll long long
#define rint register int
inline int read(){int r;int s=0,c;for(;!isdigit(c=getchar());s=c);for(r=c^48;isdigit(c=getchar());(r*=10)+=c^48);return s^45?r:-r;}
int n, col[MAXN+5], d[MAXN+5]; vector<int> e[MAXN+5]; typedef pair<ll,ll> pll; pll Ans; int maxdep; ll leaf;
void Col(int p, int fa, int d){maxdep = max(maxdep,d), col[p] = d; for(auto v:e[p]) if(v^fa) Col(v,p,d+1);}
int main()
{
	n = read(); for(rint i = 2, a, b; i <= n; a = read(), b = read(), e[a].push_back(b), e[b].push_back(a), i++); Ans.first = n+1;
	for(rint i = 1, j; i <= n; i++)
	{
		maxdep = 0; Col(i,0,1); if(maxdep>Ans.first) continue; memset(d+1,0,n<<2);
		for(j = 1; j <= n; j++) d[col[j]] = max(d[col[j]],(int)e[j].size()-(j!=i));
		for(leaf = 1, j = 1; j < maxdep; leaf *= d[j], j++) if((long double)leaf*d[j] > 1e19) goto WARN1;
		Ans = min(Ans,pll(maxdep,leaf)); ;WARN1:;
	}
	for(rint u = 1, j, v; u <= n; u++) for(auto v:e[u])
	{
		maxdep = 0; Col(u,v,1), Col(v,u,1); if(maxdep>Ans.first) continue; memset(d+1,0,n<<2);
		for(j = 1; j <= n; j++) d[col[j]] = max(d[col[j]],(int)e[j].size()-1);
		for(leaf = 2, j = 1; j < maxdep; leaf *= d[j], j++) if((long double)leaf*d[j] > 1e19) goto WARN2;
		Ans = min(Ans,pll(maxdep,leaf)); ;WARN2:;
	}	cout << Ans.first << ' ' << Ans.second << endl;
	return 0;
}

Measured memory_bytes: 256
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for(int i=0;i<n;i++) {
			a[i] = sc.nextInt();
		}
		Arrays.sort(a);
		for(int i=0;i<n-1;i++) {
			if(a[i] == a[i+1]) {
				System.out.println("NO");
				return;
			}
		}
		System.out.println("YES");
	}

}


Measured memory_bytes: 65628
#include <iostream>
#include <iterator>
#include <vector>
#include <cmath>
#include <algorithm>
#include <string>
#include <numeric>
#include <iomanip>
#include <limits>

using namespace std;

int main(){

    int ans=0;
    int a,b;
    cin>>a>>b;

    bool check=false;

    if(a<10 && b<10){
        check=true;
        ans=a*b;
    }

    if(check){
        cout<<ans<<endl;
    }
    else{
        cout<<-1<<endl;
    }

    return 0;
}


Measured memory_bytes: 256
#pragma GCC optimize("O3")
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using T = tuple<int, int, int>;

template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;}
template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;}

constexpr int MOD = 1e9 + 7;
constexpr int inf = 1e9;
constexpr long long INF = 1e18;
constexpr double pi = acos(-1);
constexpr double EPS = 1e-10;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

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

    int d, g; cin>>d>>g;
    vector<int> p(d), c(d);
    for(int i=0; i<d; i++) cin>>p[i]>>c[i];

    vector<int> v(d);
    iota(v.begin(), v.end(), 0);

    int ans = inf;
    do{
        int score = 0;
        int cnt = 0;
        for(int i=0; i<d; i++){
            int index = v[i];
            if(g < score + 100 * (index + 1) * p[index] + c[index]){
                for(int j=1; j<=p[index]; j++){
                    if(g <= score) goto END;
                    score += 100 * (index + 1);
                    if(j == p[index]) score += c[index];
                    cnt++;
                }
            }
            else{
                score += 100 * (index + 1) * p[index] + c[index];
                cnt += p[index];
            }
        }

        END:;

        chmin(ans, cnt);
    }while(next_permutation(v.begin(), v.end()));

    cout << ans << endl;
}

Measured memory_bytes: 256
N = int(input())
A = [0] + list(map(int, input().split()))
A.append(0)

pay = []

for i in range(N):
    a, b, c = A[i], A[i + 1], A[i + 2]
    if a < c:
        if a <= b <= c:
            pay.append(0)
        elif b < a:
            pay.append(2 * abs(a - b))
        elif c < b:
            pay.append((2 * abs(b - c)))
    elif c < a:
        if c <= b <= a:
            pay.append(0)
        elif b < c:
            pay.append(2 * abs(c - b))
        elif a < b:
            pay.append(2 * abs(b - a))
    else:
        if a == b:
            pay.append(0)
        else:
            pay.append(2 * abs(b - a))

ans = 0
for i in range(N + 1):
    ans += abs(A[i] - A[i + 1])


for p in pay:
    print(ans - p)


Measured memory_bytes: 13920
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<vector>
#include<cmath>
#include<map> 
#include<string>
#include<queue>
#include<stack> 
#include<bitset>
#include<list>
#include<set>
#include<utility>
#include<functional>
#include<iomanip>
#define IO ios::sync_with_stdio(false)
#define eps 1e-7
#define int long long
using namespace std;
int x,a,b,c,d;
signed main()
{
	cin>>a>>b>>c>>d;
	cout<<max(a*c,max(a*d,max(b*c,b*d)));
 } 

Measured memory_bytes: 3628
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;
const int MOD = 1000000007;
const int MOD2 = 998244353;


// 最長増加部分列の長さを求める
int LIS(const vector<long long> &a) {
    int N = (int)a.size();
    vector<long long> dp(N, INF);
    for (int i = 0; i < N; ++i) {
        // dp[k] >= a[i] となる最小のイテレータを見つける
        auto it = upper_bound(dp.begin(), dp.end(), a[i]);

        // そこを a[i] で書き換える
        *it = a[i];
    }

    // dp[k] < INF となる最大の k に対して k+1 が答え
    // それは dp[k] >= INF となる最小の k に一致する
    return lower_bound(dp.begin(), dp.end(), INF) - dp.begin();
}


int main(){
    int n;
    cin >> n;
    vector<ll> a(n);
    rep(i, n) cin >> a[i];
    rep(i, n) a[i] *= -1;

    cout << LIS(a) << endl;

    return 0;
}


Measured memory_bytes: 1792
#include<bits/stdc++.h>
using namespace std;
map<string, int> mp;
int n;
int mx = 0;
int main()
{
	cin>>n;
	for(int i = 0; i < n; i++)
	{
		string s;
		cin>>s;
		mp[s]++;
		mx = max(mx, mp[s]);
	}
	for(map<string, int>::iterator it = mp.begin(); it != mp.end(); it++)
	{
		if(it -> second == mx)
		{
			cout<<it->first<<endl;
		}
	}
	return 0;
}

Measured memory_bytes: 25216
#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <cstdlib>
using namespace std;

vector<string> split(const string &str, char delim){
  vector<string> res;
  size_t current = 0, found;
  while((found = str.find_first_of(delim, current)) != string::npos){
    res.push_back(string(str, current, found - current));
    current = found + 1;
  }
  res.push_back(string(str, current, str.size() - current));
  return res;
}

int main()
{
	string s;
	while(cin >> s)
	{
		vector<string> _s = split(s,',');
		int *values = new int[11];
		double *v = new double[2];

		for(int i=0;i<11;i++)
		{
			if(i == 0)
			{
				values[i] = 0;
			}
			else
			{
				values[i] = std::atoi(_s[i-1].c_str()) + values[i-1];
			}
			
		}

		for(int i=0;i<2;i++)
		{
			v[i] = std::atoi(_s[i+10].c_str());
		}
		double temp = v[0] / (v[0]+v[1]);
		temp *= values[10];

		bool flag = true;
		for(int i=0;i<10;i++)
		{
			if(values[i] <= temp && temp <= values[i+1] && flag)
			{
				cout << i+1 << endl;
				flag = false;
			}
		}
	}
	return 0;
}

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

int main() 
{
    int A,B,C,D;
    cin >> A >> B >> C >> D;
    
    if ((abs(C - A) <= D)) {
        cout << "Yes" << endl;
    }else if ((abs(B - A) <= D) && (abs(C - B) <= D)) {
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
}

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

using namespace std;
typedef long long ll;
typedef long long int llint;
#define MM 1000000000
#define MOD MM+7
#define pi pair<int,int>
#define pl pair<ll,ll>
#define chmax(a,b) (a<b ? a=b:0)
#define chmin(a,b) (a>b ? a=b:0)

const long double PI = acos(-1);
int dx[4] = {-1,0,1,0};
int dy[4] = {0,-1,0,1};
int GCD(int x,int y){
    return y ? GCD(y,x%y) : x;
}
int n,p;
int a[50];

int main(){
    while(true){
        cin >> n >> p;
        if(n == 0 && p == 0) break;
        fill(a,a+n,0);
        int wan = p;
        int num = 0;
        while(true){
            if(wan > 0){
                a[num]++;
                wan--;
                if(a[num] == p){
                    //cout << "ans=";
                    cout << num << endl;
                    break;
                }
            } else if (wan == 0){
                wan += a[num];
                a[num] = 0;
            }
            num++;
            if(num == n) num = 0;
        }
    }
}





Measured memory_bytes: 2952
#include<bits/stdc++.h>
using namespace std;
#define rep(i,x,y) for(int i=x;i<y;i++)
#define range(a) (a).begin(),(a).end()
#define print(A,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i];}cout<<endl;
#define pprint(A,m,n) rep(j,0,m){print(A[j],n);}
const long mod=1e9+7;
const int size=1e3;
const int INF=1e9;
long area(long x,long y){
	if(x%2==0) return x/2;
	long res=INF;long a,b;
	rep(i,0,y+1){
		if((y-i)%2==0) res=min(res,abs((y-i)/2-i)*x);
		else{
			if(y-i>=x){ a=(y-i+1)/2*x; b=(y-i-1)/2*x; }
			else{ a=(x+1)/2*(y-i); b=(x-1)/2*(y-i); }
			res=min(res,max(a,x*i)-min(b,x*i));
		}//cout<<res<<" ";
	}return res;
}
int main(){
	long H,W;cin>>H>>W;
	if(H%3==0||W%3==0){ cout<<0<<endl;return 0; }
	cout<<min(area(H,W),area(W,H))<<endl;
}

Measured memory_bytes: 256
#include <vector> 
#include <list> 
 #include <map>
 #include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

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

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

//debug
#define dump(x)  cerr << #x << '=' << (x) << endl;
#define debug(x) cerr << #x << '=' << (x) << '('<<'L' << __LINE__ << ')' << ' ' << __FILE__ << endl;

#define YES_ cout<<"Yes"<<endl; return 0;
#define NO_ cout<<"No"<<endl; return 0;

int main(){
 

    int N,A;
    cin>>N;
    cin>>A;
    if(N%500<=A){
        YES_;

    }
    NO_;



}


Measured memory_bytes: 256
import java.util.OptionalInt;
import java.util.Scanner;
import java.util.stream.IntStream;

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

        int N = sc.nextInt();
        int T = sc.nextInt();
        int[] c = new int[N];
        int[] t = new int[N];
        for (int i = 0; i < N; ++i) {
            c[i] = sc.nextInt();
            t[i] = sc.nextInt();
        }

        System.out.println(solve(c, t, T));

        sc.close();
    }

    static String solve(int[] c, int[] t, int T) {
        OptionalInt minCost = IntStream.range(0, c.length).filter(i -> t[i] <= T).map(i -> c[i]).min();

        return minCost.isPresent() ? String.valueOf(minCost.getAsInt()) : "TLE";
    }
}

Measured memory_bytes: 36944
#!/usr/bin/env python3

from collections import Counter

def II(): return int(input())
def MII(): return map(int, input().split())
def LII(): return list(map(int, input().split()))

MOD = 998244353

def main():
    N = II()
    D = LII()

    if D[0] != 0 or D.count(0) > 1:
       print(0)
       return 
    
    c = Counter(D)
    res = 1

    if c[0] > 1:
       print(0)
       return
    
    mx = max(c.keys())
    for i in range(1, mx+1):
        if i not in c:
            print(0)
            return
        
        res = (res * c[i-1] ** c[i]) % MOD

    print(res)

if __name__ == '__main__':
    main()


Measured memory_bytes: 24444
#include <iostream>
#include <algorithm>
#include <string>
#include <functional> 
#include <vector>
#include <numeric>
#include <stack>
#include <queue>
#include <utility>
//#include <cmath>
#include <cstdlib>

using namespace std;

int main(){

        int n;
        cin >> n;
        int a[n+2];
        int cnt = 0;
        a[0] = 0;
        a[n+1] = 0;
        for(int i=1;i<=n+1;i++){
            cin >> a[i];
            cnt += abs(a[i] - a[i-1]);
        }

        for(int i=1;i<n+1;i++){
            if(a[i-1] <= a[i] && a[i] <= a[i+1]) cout << cnt;
            else if(a[i-1] >= a[i] && a[i] >= a[i+1]) cout << cnt;
            else cout << cnt - abs(a[i] - a[i-1]) - abs(a[i] - a[i+1]) + abs(a[i-1] - a[i+1]);
          	cout << endl;
        }


    return 0;
}

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

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep1(i, n) for (ll i = 1; i < (ll)(n); i++)
#define INF 1000000000000
#define MOD 1000000007
#define MAX 2000000000
#define PI 3.1415926535897932
using ll = long long;
using Graph = vector<vector<int>>;
//二項係数を求める。

int main(){
  int n;cin>>n;
  for(int i = 1; i < 10; i++){
    if(n%i==0){
      int tmp = n/i;
      if(tmp<=9){
        cout<<"Yes"<<endl;
        return 0;
      }
    }
  }
  cout<<"No"<<endl;
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main(){
	int n,x,y,z,res,ans;
	int a[24][60][60],b[24][60][60];
	char c1,c2;
	string s1,s2;
	while(1){
		cin >> n;
		if(n == 0)break;
		for(int i = 0;i < 24;i++){
			for(int j = 0;j < 60;j++){
				for(int k = 0;k < 60;k++){
					a[i][j][k] = b[i][j][k] = 0;
				}
			}
		}
		for(int i = 0;i < n;i++){
			cin >> s1 >> s2;
			istringstream iss1(s1);
			iss1 >> x >> c1 >> y >> c2 >> z;
			//cout << x <<" " << y << " " << z <<endl; 
			a[x][y][z]++;
			istringstream iss2(s2);
			iss2 >> x >> c1 >> y >> c2 >> z;
			b[x][y][z]++;
		}
		res = 0;
		ans = 0;
		for(int i = 0;i < 24;i++){
			for(int j = 0;j < 60;j++){
				for(int k = 0;k < 60;k++){
					if(b[i][j][k] != 0){
						res -= b[i][j][k];
					}
					if(a[i][j][k] != 0){
						res += a[i][j][k];
						ans = max(ans,res);
					}
				}
			}
		}
		cout << ans <<endl;
	}
	return 0;
}

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

int main() {
  long a, b, c, k;

  cin >> a >> b >> c >> k;

  long ans = 0;

  if (0 < k) {
    ans += min(a, k);
    k -= min(k, a);
  }
  if (0 <= k) {
    k -= min(k, b);
  }
  if (0 <= k) {
    ans -= min(c, k);
  }

  cout << ans;

  return 0;
}

Measured memory_bytes: 3636
import java.util.*;
import static java.lang.System.*;
import static java.lang.Math.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(in);
int n=Integer.parseInt(sc.next());
int k=Integer.parseInt(sc.next());
int ans=0;
for (int i = 0; i < n; i++) {
        int x=Integer.parseInt(sc.next());
        ans+=min(2*x,2*abs(x-k));
    }
    out.println(ans);
}
}

Measured memory_bytes: 21844
gets # n = gets.to_i
a = gets.split(' ').map(&:to_i)
b = gets.split(' ').map(&:to_i) << 0

result = a.zip(b).reduce(
  total_killed: 0,
  remaining_kill_ability: 0
) do |acm, (ai, bi)|
  # the previous saver kill all monsters at the current city
  if ai <= acm[:remaining_kill_ability]
    next {
      total_killed: acm[:total_killed] + ai,
      remaining_kill_ability: bi
    }
  end

  # the previous saver kill monsters
  prev_killed = acm[:remaining_kill_ability]
  remaining_monster_count = ai - prev_killed
  # the current saver kill remaining monsters
  current_killed = [remaining_monster_count, bi].min
  {
    total_killed: acm[:total_killed] + prev_killed + current_killed,
    remaining_kill_ability: bi - current_killed
  }
end

puts result[:total_killed]


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

int main() {
  int A, B, C, D;
  cin >> A >> B >> C >> D;

  if ((A + B) > (C + D)) {
    cout << "Left" << endl;
  }
  else if ((A + B) < (C + D)) {
    cout << "Right" << endl;
  }
  else{
    cout << "Balanced" << endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <stack>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define EPS 1e-8
#define DEB 0

/*
const int dx[] = {0,1,0,-1}; //u,r,d,l
const int dy[] = {-1,0,1,0};
*/
const int dx[] = {1,0,-1,0}; //r,d,l,u
const int dy[] = {0,1,0,-1};

int w,h,sx,sy,home,last;
int field[16][16];
int num[16][16];

class state{
public:
  char x,y;
  int s;
  state(char _x, char _y, int _s){
    x = _x;
    y = _y;
    s = _s;
  }
  
  bool operator<(const state& a)const{
    if( x==a.x ){
      if( y==a.y ) return s<a.s;
      else         return y<a.y;
    }else{
      return x<a.x;
    }
  }
  
};


map<state,int> msi;

bool ischeck(int s){
  return (((last&s)) == last) ;
}
bool inside(int x, int y){
  return (x<0 || y<0 || x>=w || y>=h);
}


int dfs(int x, int y, int s){
  if( (1<<home)-1 == s ){ //âÎÉßêé
    return 1;
  }
  if( ischeck(s) ){ //³ïÉßé½ßÌÆðÁïµ½©Ç¤©
    return 0;
  }
  if( msi.count(state(x,y,s)) ){ //ùÉÊÁ½óÔ
    return msi[state(x,y,s)];
  }
  
  int ret = 0;
  rep(k,4){
    for(int i=1; i<20; i++){
      int tx = x + dx[k]*i;
      int ty = y + dy[k]*i;
      if( inside(tx,ty) ) break;
      if( field[ty][tx]==1 ){
	field[ty][tx] = 0;
	ret += dfs(tx,ty,s|(1<<num[ty][tx]));
	field[ty][tx] = 1;
	break;
      }
    }
  }
  msi[state(x,y,s)] = ret;
  return ret;
}

int main(){

  while(scanf("%d%d",&w,&h),w|h){
    msi.clear();
    memset(num,-1,sizeof(num));
    home = last = 0;
    rep(i,h){
      rep(j,w){
	scanf("%d",&field[i][j]);
	if( field[i][j]==2 ){
	  sx = j;
	  sy = i;
	}
	if( field[i][j]==1 ){
	  num[i][j] = home;
	  home++;
	}
      }
    }

    //last homes
    rep(k,4){
      for(int i=1; i<20; i++){
	int tx = sx + dx[k]*i;
	int ty = sy + dy[k]*i;
	if( inside(tx,ty) )break;
	if( field[ty][tx]==1 ){
	  last |= (1<<num[ty][tx]);
	}
      }
    }

    printf("%d\n",dfs(sx,sy,0));
  }
  

  return 0;
}

Measured memory_bytes: 32000
#include<bits/stdc++.h>
using namespace std;
//ofstream fout("output.txt");
#define ll long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define IOS ios_base::sync_with_stdio(0);
#define pi 4*atan(1)
#define f(n) for(int i=0;i<n;++i)
#define fab(a,b) for(int i=a;i<=b;++i)
#define fba(b,a) for(int i=b;i>=a;--i)
#define reset(a,x) memset(a,x,sizeof(a))
typedef pair<int,int> ipair;
typedef vector<int> vi;
int main(){
    IOS
    cin.tie(0);

  long long n,k,p_n;
  cin>>n>>k;

  long long ans=n%k;
  p_n=ans;
  n=abs(ans-k);

  cout<<min(p_n,n)<<endl;

    return 0;
}










Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>
int main(){
  char line[202];
  int n;
  int i, j;
  fgets(line, sizeof(line), stdin);
  sscanf(line, "%d", &n);
  char taro[n];
  char hanako[n];
  int score_taro=0;
  int score_hanako=0;
  char *str_ptr;
  for(i=0; i<n; ++i){
	//文字列の読み込み
	fgets(line, sizeof(line), stdin);
	str_ptr=strtok(line, " ");
	strcpy(taro, str_ptr);
	str_ptr=strtok(NULL, " ");
	strcpy(hanako, str_ptr);
	hanako[strlen(hanako)-1]='\0';
	//得点の計算
	if(strcmp(taro, hanako)==0){
	  ++score_taro;
	  ++score_hanako;
	}else if(strcmp(taro, hanako)<0){
	  score_hanako+=3;
	}else
	  score_taro+=3;
  }
  
  printf("%d %d\n", score_taro, score_hanako);
  return 0;
}



Measured memory_bytes: 2036
#include<bits/stdc++.h>
#define _USE_MATH_DEFINES
using namespace std;
 
#define ll long long int
#define pb push_back
#define rep(i , j , n) for(ll i = j ; i < n ; i++)
#define per(i , j , n) for(ll i = j ; i >= n ; i--)
#define all(x) x.begin(), x.end()
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vll;

int main(){
    ll n,k;
    cin >> n >> k;
    ll j = 0;
    vll a(n),b(n);
    rep(i,0,n){
        cin >> a[i];
        b[i] = 0;
    }
    while(j < k && j <= 42){
        j++;
        rep(i,0,n){
            ll l,r;
            l = max((ll)0,i - a[i]);
            r = min(n - 1, i + a[i]);
            b[l]++;
            if(r + 1 < n) b[r + 1]--;
        }
        rep(i,0,n){ 
            if(i > 0) b[i] += b[i - 1];
            a[i] = b[i];
        }
        rep(i,0,n){
            b[i] = 0;
        }

    }
    rep(i,0,n){
        cout << a[i] << " ";
    }
    
}

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

#define pp push_back
#define pf push_front
#define mp make_pair
#define fs first
#define sc second
#define sf scanf

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

const int N = (int) 1e5;
const int INF = (int) 1e9 + 7;

int main(int argc, char *argv[])
{
	// ios_base::sync_with_stdio(false);
	// cin.tie(NULL);

	int n, m;
	sf("%d %d", &n, &m);
	vector<int> g[n + 7];
	for (int i = 0; i < m; ++i) {
		int x, y;
		sf("%d %d", &x, &y);
		g[x].pp(y);
		g[y].pp(x);
	}

	queue<int> q;
	q.push(1);
	bool vis[n + 7] = {0};
	int ans[n + 7];
	vis[1] = true;
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (int i = 0; i < g[u].size(); ++i) {
			int v = g[u][i];
			if (vis[v]) continue;
			ans[v] = u;
			q.push(v);
			vis[v] = true;
		}
	}

	for (int i = 1; i <= n; ++i) {
		if (!vis[i]) {
			puts("No");
			return 0;
		}
	}

	puts("Yes");
	for (int i = 2; i <= n; ++i) {
		printf("%d\n", ans[i]);
	}

	return 0;
}


Measured memory_bytes: 10632
#include <bits/stdc++.h>
using namespace std;
 
int main() {
    int k, n;
    cin >> k >> n;
    vector<int> a(n);
    for (auto i = 0; i < n; ++i)
        cin >> a.at(i);
    
    int dmax = 0;
    for (auto i = 0; i < n - 1; ++i)
    {
        dmax = max(dmax, a.at(i+1)-a.at(i));
    }
    dmax = max(dmax,min(a.at(n-1)-a.at(0),k+a.at(0)-a.at(n-1)));
    
    cout << k-dmax;
}

Measured memory_bytes: 1024
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define INF (1<<30)
#define INFLL (1ll<<60)
typedef pair<int, int> P;
typedef pair<ll, P> E;
#define MOD (1000000007ll)
#define l_ength size

void mul_mod(ll& a, ll b){
	a *= b;
	a %= MOD;
}

void add_mod(ll& a, ll b){
	b += MOD;
	a += b;
	a %= MOD;
}

vector<int> g[100100];
int dp[100100][15],a[100100],b[100100],c[100100],d[100100],v[100100];

void solve(int u, int dist, int k){
	int i;
	if(!dp[u][dist]){
		dp[u][dist] = k;
		if(!dist){
			return;
		}
		solve(u,dist-1,k);
		for(i=(g[u].l_ength()-1); i>=0; --i){
			solve(g[u][i],dist-1,k);
		}
	}
}

int main(void){
	int n,m,i,a,b,q;
	cin >> n >> m;
	for(i=0; i<m; ++i){
		cin >> a >> b;
		--a; --b;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	cin >> q;
	for(i=0; i<q; ++i){
		cin >> v[i] >> d[i] >> c[i];
		--v[i];
	}
	for(i=(q-1); i>=0; --i){
		solve(v[i],d[i],c[i]);
	}
	for(i=0; i<n; ++i){
		cout << dp[i][0] << endl;
	}
	return 0;
}


Measured memory_bytes: 13560
#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define rep2(i,s,n) for (int i = s; i < n; ++i)
#define all(a) a.begin(),a.end()
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main() {
    int n;
    cin >> n;
    cout << 111*((n+110)/111) << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"
#define all(s) s.begin(),s.end()
#define pb push_back
#define uno unordered_map
#define pi 3.1415926536
#define dig(a) fixed<<setprecision(a)
#define foreach(var,i) for(auto i = var.begin();i!=var.end();i++)
#define fast() ios_base::sync_with_stdio(false); cin.tie(NULL);
ll power(ll a, ll n) {ll res = 1; while (n) {if (n % 2) {res *= a;} n >>= 1; a *= a;} return res;}
ll powerm(ll a, ll n, ll m) {ll res = 1; while (n) {if (n % 2) {res = (res * a) % m;} n >>= 1; a = (a * a) % m;} return res;}
const int M=1e9+7;
ll mod(ll x){
      return ((x%M + M)%M);
}
ll add(ll a, ll b){
      return mod(mod(a)+mod(b));
}
ll mul(ll a, ll b){
      return mod(mod(a)*mod(b));
}

int main(){
	fast();
	int t=1;
	//cin>>t;
	while(t--){
		int x,n;
		cin>>x>>n;
		vector<int> a(n);
		for(int &x:a){
			cin>>x;
		}
		sort(all(a));
		auto it = find(all(a),x);
		if(it==a.end()){
			cout<<x<<endl;
			continue;
		}
		int ind = it-a.begin();
		int k=1;
		int f=0;
		for(int j=ind;j-k>=0;k++){
			if(a[j-k]!=x-k){
				//cout<<x-k<<endl;
				f=x-k;
				break;
			}
			else{
				if(a[j+k]!=x+k){
					//cout<<x+k<,endl;
					f=x+k;
					break;
				}
			}
		}
		if(!f) cout<<a[0]-1<<endl;
		else cout<<f<<endl;
	}
	
	return 0;
}


Measured memory_bytes: 3608
from itertools import *
N = int(input())
csum = list(0 for _ in range(2000+1))
L = []
for l in map(int,input().split()):
    L.append(l)
    csum[l] += 1
csum = list(accumulate(csum))
ans = 0
for ia in range(N-1):
    for ib in range(ia+1,N):
        a, b = L[ia], L[ib]
        minL, maxL = abs(a-b), a+b
        ans += csum[maxL-1] - csum[minL]
        if minL < a and a < maxL: ans -= 1
        if minL < b and b < maxL: ans -= 1
print(ans//3)

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

N = int(input())

values = [0]*N
value_count_map = {}
max_value = 0
next_max_value = max_value

for i in range(N):
    in_value = int(input())
    values[i] = in_value
    value_count = 0
    if in_value in value_count_map:
        value_count = value_count_map[in_value]
    value_count_map[in_value] = value_count + 1

    if in_value > max_value:
        next_max_value = max_value
        max_value = in_value
    elif in_value > next_max_value:
        next_max_value = in_value

max_value_count = value_count_map[max_value]

for row in range(N):
    if values[row] == max_value and max_value_count == 1:
        print(next_max_value)
    else:
        print(max_value)


Measured memory_bytes: 18648
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <tuple>
using namespace std;
using P = pair<int,int>;

int main(){
    int H,W;
    cin>>H>>W;
    int map[H][W];
    P princess;
    vector<P> soldier;
    for(int i=0;i<H;i++){
        for(int j=0;j<W;j++){
            char c;
            cin>>c;
            map[i][j]=1;
            if(c=='#') map[i][j]=0;
            if(c=='%') map[i][j]=2;
            if(c=='@') princess=P(j,i);
            if(c=='$') soldier.emplace_back(j,i);
        }
    }
    if(!soldier.size()){
        cout<<"Yes"<<endl;
        return 0;
    }
    int dist[H][W];
    queue<P> Q;
    Q.push(princess);
    fill(dist[0],dist[H],100000000);
    int dist_princess=-1;
    dist[get<1>(princess)][get<0>(princess)]=0;
    while(!Q.empty()){
        int x,y;
        tie(x,y)=Q.front(); Q.pop();
        if(map[y][x]==2){
            dist_princess=dist[y][x];
            while(!Q.empty()) Q.pop();
            break;
        }
        int d[5]={0,1,0,-1,0};
        for(int k=0;k<4;k++){
            int dx=d[k],dy=d[k+1];
            if(x+dx<0||W<=x+dx||y+dy<0||H<=y+dy) continue;
            if(map[y+dy][x+dx]){
                if(dist[y+dy][x+dx]>dist[y][x]+1){
                    Q.push(P(x+dx,y+dy));
                    dist[y+dy][x+dx]=dist[y][x]+1;
                }
            }
        }
    }
    int dist_soldier=100000000;
    fill(dist[0],dist[H],100000000);
    for(auto s:soldier){
        Q.push(s);
        dist[get<1>(s)][get<0>(s)]=0;
    }
    while(!Q.empty()){
        int x,y;
        tie(x,y)=Q.front(); Q.pop();
        if(map[y][x]==2){
            dist_soldier=dist[y][x];
            break;
        }
        int d[5]={0,1,0,-1,0};
        for(int k=0;k<4;k++){
            int dx=d[k],dy=d[k+1];
            if(x+dx<0||W<=x+dx||y+dy<0||H<=y+dy) continue;
            if(map[y+dy][x+dx]){
                if(dist[y+dy][x+dx]>dist[y][x]+1){
                    Q.push(P(x+dx,y+dy));
                    dist[y+dy][x+dx]=dist[y][x]+1;
                }
            }
        }
    }
    // cout<<dist_princess<<' '<<dist_soldier<<endl;
    if(dist_soldier<=dist_princess){
        cout<<"No"<<endl;
    }else{
        cout<<"Yes"<<endl;
    }
}

Measured memory_bytes: 3768
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <climits>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

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

    int n, a[212345], b[212345];
    cin >> n;
    REP(i, n)
    cin >> a[i + 1];

    fill(b, b + n + 1, 0);

    vector<int> m;

    for (int i = n; i > 0; i--)
    {
        int sum = 0;
        for (int j = i * 2; j <= n; j += i)
            sum += b[j];

        if (sum % 2 != a[i])
            b[i] = 1, m.push_back(i);
    }

    cout << m.size() << endl;
    for (int i = 0; i < m.size(); i++)
        cout << m[i] << (i < m.size() - 1 ? " " : "\n");

    return 0;
}

Measured memory_bytes: 3192
// 2020-09-29 19:03:15
// clang-format off
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
using P = pair<int, int>;
template<class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } else return false; }
template<class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
// #include <atcoder/convolution>
// #include <atcoder/dsu>
// #include <atcoder/fenwicktree>
// #include <atcoder/lazysegtree>
// #include <atcoder/math>
// #include <atcoder/maxflow>
// #include <atcoder/mincostflow>
// #include <atcoder/modint>
// #include <atcoder/scc>
// #include <atcoder/segtree>
// #include <atcoder/string>
// #include <atcoder/twosat>
// #include <atcoder/all>
// using namespace atcoder;

// clang-format on
const int MX = 100010;
const ll inf = 1e18;
struct E {
  int from, to;
  vector<ll> cost;
  E(int from_, int to_, vector<ll> cost_) : from(from_), to(to_), cost(cost_) {}
};
vector<E> G[MX];
ll dist[2][MX];
void answer() {
  int n, m;
  vector<int> s(2);
  cin >> n >> m >> s[0] >> s[1];
  s[0]--;
  s[1]--;
  rep(i, m) {
    int u, v;
    vector<ll> c(2);
    cin >> u >> v >> c[0] >> c[1];
    u--;
    v--;
    G[u].emplace_back(E(u, v, c));
    G[v].emplace_back(E(v, u, c));
  }
  using Q = pair<ll, int>;  // cost to
  rep(k, 2) {
    rep(i, n) dist[k][i] = inf;
    priority_queue<Q, vector<Q>, greater<Q> > que;
    que.emplace(0, s[k]);
    while (!que.empty()) {
      auto [c, u] = que.top();
      que.pop();
      if (dist[k][u] <= c) continue;
      dist[k][u] = c;
      for (const auto& e : G[u]) {
        if (c + e.cost[k] < dist[k][e.to]) {
          que.emplace(c + e.cost[k], e.to);
        }
      }
    }
  }
  priority_queue<Q, vector<Q>, greater<Q> > res;
  rep(i, n) {
    debug(i, dist[0][i], dist[1][i]);
    res.emplace(dist[0][i] + dist[1][i], i);
  }
  rep(i, n) {
    while (res.top().second < i) res.pop();
    cout << ll(1e15) - res.top().first << '\n';
  }
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}

Measured memory_bytes: 25268
#include<iostream>
#define DIM 200005
#define mod 1000000007
using namespace std;
int n, i;
int d[DIM], v[DIM], ff[DIM], nxt[DIM];
int main(){
    cin>> n;
    for(i = 1; i <= n; i++){
        cin>> v[i];
    }
    for(i = n; i >= 1; i--){
        nxt[i] = ff[ v[i] ];
        ff[ v[i] ] = i;
    }
    d[0] = 1;
    for(i = 1; i <= n; i++){
        d[i] = (d[i] + d[i - 1]) % mod;
        if(nxt[i] != 0 && nxt[i] != i + 1){
            d[ nxt[i] ] = (d[ nxt[i] ] + d[i]) % mod;
        }
    }
    cout<< d[n];
}


Measured memory_bytes: 3328
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <set>
using namespace std;

void listup(int cur, int lim, int mark, vector<vector<int> > &adj, vector<int> &ret){
    set<int> vlist;
    vlist.insert(cur);
    set<int>::iterator itr;
    for(int i=0; i<lim; i++){
        set<int> newset;
        for(itr = vlist.begin(); itr!=vlist.end(); itr++){
            int node = *itr;
            for(int j=0; j<(int)adj[node].size(); j++){
                newset.insert(adj[node][j]);
            }
        }
        vlist = newset;
    }
    for(itr = vlist.begin(); itr!=vlist.end(); itr++){
        ret[*itr] |= mark;
    }
}

int main(){
    int n,m,a,b,c;
    cin >> n >> m >> a >> b >> c;
    vector<vector<int> > revadj(n+1);
    for(int i=0; i<m; i++){
        int u,v;
        cin >> u >> v;
        revadj[v].push_back(u);
    }
    
    vector<bool> used(n+1, false), isnew(n+1, false);
    used[n] = true; isnew[n] = true;
    int ans = -1;
    int turn = 0;
    vector<int> cand(n+1, 0);
    while(1){
        if(used[1]){
            ans = turn;
            break;
        }
        for(int i=1; i<=n; i++){
            if(!isnew[i]) continue;
            listup(i, a, 1, revadj, cand);
            listup(i, b, 2, revadj, cand);
            listup(i, c, 4, revadj, cand);
        }
        isnew = vector<bool>(n+1, false);
        bool update=false;
        for(int i=1; i<=n; i++){
            if(cand[i] == 7 && !used[i]){
                used[i] = true;
                update = true;
                isnew[i] = true;
            }
        }
        if(!update) break;
        turn++;
    }
    
    if(ans==-1){
        cout << "IMPOSSIBLE" << endl;
    }else{
        cout << ans << endl;
    }
    return 0;
}

Measured memory_bytes: 3176