akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define REP(i, a, n) for (long long i = (a); i < (long long)(n); ++i)
#define REPC(i, a, n) for (long long i = (a); i <= (long long)(n); ++i)
#define ALL(t) t.begin(), t.end()
#define RALL(t) t.rbegin(), t.rend()
#define MATINIT(type, row, col, init) \
  vector<vector<type>>(row, vector<type>(col, init));
#define Yes(cond) cout << (cond ? "Yes" : "No") << endl;
#define YES(cond) cout << (cond ? "YES" : "NO") << endl;
using namespace std;
using LL = long long;
using ULL = unsigned long long;
template <class T>
using VEC = std::vector<T>;
template <class T>
using MAT = std::vector<std::vector<T>>;
void DUMP() { cerr << endl; }
template <class Head, class... Tail>
void DUMP(Head &&head, Tail &&... tail) {
  cerr << head << ", ";
  DUMP(std::move(tail)...);
}
template <typename T>
ostream &operator<<(ostream &os, vector<T> &vec) {
  os << "{";
  for (auto v : vec) os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, set<T> &s) {
  os << "{";
  for (auto p : s) os << p << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, map<T1, T2> &m) {
  os << "{";
  for (auto p : m) os << p << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, pair<T1, T2> p) {
  os << "[" << p.first << " " << p.second << "]";
  return os;
}
long long gcd(long long a, long long b) { /* the greatest common divisor*/
  while (b != 0) {
    long long r = a % b;
    a = b;
    b = r;
  }
  return a;
}
long long lcm(long long a, long long b) { /*the least common multiple*/
  return (a / gcd(a, b)) * b;
}

int main() {
  LL A, B, C, D;
  cin >> A >> B >> C >> D;
  LL N = B - A + 1;
  LL g = lcm(C, D);
  LL divC = B/C - (A-1)/C;
  LL divD = B/D - (A-1)/D;
  LL divCD = B/g - (A-1)/g;
  LL ans = N - divC - divD + divCD;
  // DUMP(N, divC, divD, divCD);
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256
N = int(input())
p = list(map(int, input().split()))
a = [i + 1 for i in range(N)]
count = 0
for i in range(N):
    if a[i] != p[i]:
        count += 1
if count == 2 or a == p:
    print('YES')
else:
    print('NO')

Measured memory_bytes: 3060
#include<stdio.h>
int main(void) {
	int n, k, *p, q, i;//n参加者人数 k手持ち点数 q問題数
	int a[100001] ; //各問題の正解者
	int m[100001] = {0};  //各参加者の点数
	scanf("%d %d %d", &n, &k, &q);
	for (i = 0; i < q; i++) {
		scanf("%d", &a[i]);
	}
	for (i = 0; i < q; i++) {
		p = &m[0];
		p = p + a[i] - 1;
		*p = *p +1;
	}
	for (i = 0; i < n; i++) {
		m[i] = m[i] + k - q;
		if (m[i] > 0) puts("Yes");
		else puts("No");
	}
	return 0;
}

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

vector<vector<int>> G;
vector<vector<int>> W;
vector<int> dist;
vector<bool> visited;

void dfs(int u) {
    visited[u] = true;

    for (int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        if (visited[v]) continue;
        dist[v] = dist[u] + W[u][i];
        dfs(v);
    }
}

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> L(M), R(M), D(M);
    G.resize(N); W.resize(N);
    for (int i = 0; i < M; i++) {
        int l, r, d;
        cin >> l >> r >> d;
        l--; r--;
        L[i] = l; R[i] = r; D[i] = d;
        G[l].push_back(r); W[l].push_back(d);
        G[r].push_back(l); W[r].push_back(-d);
    }

    dist.resize(N);
    visited.resize(N, false);
    for (int i = 0; i < N; i++) {
        if (!visited[i]) {
            dfs(i);
        }
    }

    for (int i = 0; i < M; i++) {
        if (D[i] != dist[R[i]] - dist[L[i]]) {
            cout << "No" << endl;
            return 0;
        }
    }

    cout << "Yes" << endl;
    return 0;
}

Measured memory_bytes: 18816
N,K = map(int, input().split())
A = list(map(int, input().split()))

accum = [0] * N
accum[0] = A[0]
for i in  range(1,N):
    accum[i] = accum[i-1] + A[i]

B = []
for l in range(N):
    for r in range(l,N):
        if l == 0:
            B.append(accum[r])
        else:
            B.append(accum[r] - accum[l-1])


ans = 0
for i in range(45,-1,-1):
    used = []
    cnt = 0
    for b in B:
        if b & (1 << i):
            cnt += 1
            used.append(b)

    if cnt >= K:
        ans += pow(2,i)
        B = used


print(ans)

Measured memory_bytes: 23852
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include<limits.h>
#define rep(i, n) for(int i = 0; i < n; i++)
#define REP(i, m, n) for(int i = m; i < n; i++)
#define ALL(v) v.begin(),v.end()
#define RALL(v) v.rbegin(),v.rend()
#define check(v) rep(i,v.size()) cout << v[i] << " ";\
cout << endl
#define INF 1e8
typedef long long ll;
using namespace std;
//オーバーフローに気をつけろよおおおおおお
//確認忘れるなよおおおおおお

int main() {
    int n, m; cin >> n >> m;
    int ans = 0;
    ans += (n-m)*100;
    ans += m*1900;
    cout << ans * pow(2,m) << endl;

    return 0;
}

Measured memory_bytes: 768
n, a, b = map(int, input().split())
ans = 0
for i in range(n+1):
  str_i = str(i)
  sum_i = 0
  for j in range(len(str_i)):
    sum_i += int(str_i[j])
  if (sum_i >= a) & (sum_i <= b):
    ans+=i
print(ans)

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


int main()
{
    int N, H;
    cin >> N >> H;
    vector<int> a(N), b(N);
    for( int i = 0; i < N; ++i ) cin >> a[i] >> b[i];


    int amax = 0;
    for( int i = 0; i < N; ++i ) amax = max( amax, a[i] );

    sort( b.begin(), b.end(), greater<int>() );
    int ans = 0;
    for( int i = 0; i < N; ++i ){
        if( b[i] > amax ){
            H -= b[i];
            ans++;
        } 
        if( H <= 0 ) break;
    }
    if( H > 0 ) ans += (H + amax - 1) / amax;

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

Measured memory_bytes: 1024
#include<iostream>
#include<iomanip>
#include<climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<complex>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#include<tuple>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;


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

	string S;
	cin >> S;
	int a = atoi(S.substr(0, 2).c_str());
	int b = atoi(S.substr(2).c_str());
	bool m_a = false, m_b = false;
	if (0 < a && a <= 12) m_a = true;
	if (0 < b && b <= 12) m_b = true;
	string res;
	if (m_a && m_b) res = "AMBIGUOUS";
	else if (m_a) res = "MMYY";
	else if (m_b) res = "YYMM";
	else res = "NA";
	cout << res << endl;

	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h> 
using namespace std; 
int gi() { 
	int res = 0, w = 1; 
	char ch = getchar(); 
	while (ch != '-' && !isdigit(ch)) ch = getchar(); 
	if (ch == '-') w = -1, ch = getchar(); 
	while (isdigit(ch)) res = res * 10 + ch - '0', ch = getchar(); 
	return res * w; 
} 
const int INF = 1e9; 
const int MAX_N = 2e5 + 5;
int N, a[MAX_N], val[MAX_N]; 
#define lson (o << 1)
#define rson (o << 1 | 1)
struct SGT { 
	int mx[MAX_N << 2]; 
	void pushup(int o) { mx[o] = max(mx[lson], mx[rson]); } 
	void build(int o = 1, int l = 1, int r = N) { 
		if (l == r) return (void)(mx[o] = -INF);
		int mid = (l + r) >> 1; 
		build(lson, l, mid), build(rson, mid + 1, r); 
		pushup(o); 
	} 
	void mdf(int pos, int v, int o = 1, int l = 1, int r = N) { 
		if (l == r) return (void)(mx[o] = v); 
		int mid = (l + r) >> 1; 
		if (pos <= mid) mdf(pos, v, lson, l, mid); 
		else mdf(pos, v, rson, mid + 1, r); 
		pushup(o); 
	} 
	int qry(int ql, int qr, int o = 1, int l = 1, int r = N) { 
		if (ql <= l && r <= qr) return mx[o]; 
		int mid = (l + r) >> 1, res = -INF; 
		if (ql <= mid) res = max(res, qry(ql, qr, lson, l, mid)); 
		if (qr > mid) res = max(res, qry(ql, qr, rson, mid + 1, r)); 
		return res; 
	} 
} t[2]; 
int cnt[MAX_N]; 
bool check(int mx, int v) {
	if (v < 0) return 0; 
	int len = t[v & 1].qry(mx, N); 
	return len >= v; 
}

int main () { 
#ifndef ONLINE_JUDGE 
    freopen("cpp.in", "r", stdin); 
#endif 
	N = gi(); 
	for (int i = 1, mx = 0; i <= N; i++) { 
		a[i] = gi(); 
		if (a[i] > mx) mx = a[i], val[i] = 2; 
		else val[i] = 1; 
	} 
	t[1].build(); 
	for (int i = N; i; i--) { 
		int mx0 = t[0].qry(a[i], N), mx1 = t[1].qry(a[i], N); 
		if (val[i] & 1) t[0].mdf(a[i], mx1 + 1), t[1].mdf(a[i], mx0 + 1); 
		else t[0].mdf(a[i], mx0 + 2), t[1].mdf(a[i], mx1 + 2); 
	} 
	for (int i = N; i; i--) cnt[i] = cnt[i + 1] + (val[i] == 2); 
	vector<int> ans; 
	int cntX = 0, cntY = 0, mxX = 0, mxY = 0; 
	for (int i = 1; i <= N; i++) { 
		t[1].mdf(a[i], -INF), t[0].mdf(a[i], 0); 
		int _1 = cntX, _2 = mxX; 
		if (a[i] > mxX) mxX = a[i], ++cntX; 
		if (check(mxY, cntX + cnt[i + 1] - cntY) || check(mxX, cntY + cnt[i + 1] - cntX)) ans.push_back(0); 
		else { 
			cntX = _1, mxX = _2; 
			if (a[i] > mxY) mxY = a[i], ++cntY; 
			ans.push_back(1); 
		} 
	}
	if (cntX != cntY) return puts("-1") & 0; 
	for (int i = 0; i < (int)ans.size(); i++) putchar(ans[i] + '0'); 
	putchar('\n'); 
    return 0; 
} 

Measured memory_bytes: 10692
#include <bits/stdc++.h>
#include <cstring>
#define ll long long
const int mod = 1e9 + 7;
const int MAXN = 2e5+5;
using namespace std;
int main()
{
    int ans = 0;
    for(int i = 0;i<5;i++) {
        int x;
        cin>>x;
        if(x == 0) ans = i+1;
    }
    cout<<ans<<endl;

}


Measured memory_bytes: 3592
#include<bits/stdc++.h>
using namespace std;
int main()
{
    long n,a[2005],m=1000000007;
    cin>>n;
    a[0]=1;a[1]=0;a[2]=0;
    for (int i=3;i<=n;i++){
        a[i]=0;
        for (int j=0;j<i-2;j++)
            a[i]=(a[i]+a[j])%m;
    }
    cout<<a[n];
    return 0;
}


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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N=I()
    A=LI()
    from collections import defaultdict
    dd = defaultdict(int)
    
    for i in range(N):
        dd[A[i]]+=1
        
    S=0
    for k,v in dd.items():
        S+=(v*(v-1))//2
        
    for i in range(N):
        v=dd[A[i]]
        ans=S-(v-1)
        print(ans)

main()


Measured memory_bytes: 82892
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct bstr{
  string str;
  bstr(string s){str=s;}
  bool operator<(const bstr& a) const{
    return str.size()==a.str.size() ? (str.compare(a.str) < 0): str.size() > a.str.size();
  }
};
int main(){
  int h,w;
  int i,j,k;
  int ai[]={ 1, 1, 1, 0, 0,-1,-1,-1};
  int aj[]={ 1, 0,-1, 1,-1, 1, 0,-1};
  cin >> h >> w;
  while(h!=0){
    string str[10];
    for(i=0;i<h;i++) cin >> str[i];
    vector <bstr> li;
    vector <string> ans;
    int mal=1;
    for(i=0;i<h;i++){
      //cout << i << endl;
      for(j=0;j<w;j++){
	for(k=0;k<8;k++){
	  int bi=i,bj=j;
	  string buf;
	  buf += str[i][j];
	  bi+=ai[k];bj+=aj[k];
	  if(bi<0) bi=h-1;
	  if(bi>=h) bi=0;
	  if(bj<0) bj=w-1;
	  if(bj>=w) bj=0;
	  while(bi!=i||bj!=j){
	    buf+=str[bi][bj];
	    li.push_back(bstr(buf));
	    
	    bi+=ai[k];bj+=aj[k];
	    if(bi<0) bi=h-1;
	    if(bi>=h) bi=0;
	    if(bj<0) bj=w-1;
	    if(bj>=w) bj=0;
	  }
	}
      }
    }
    sort(li.begin(),li.end());
    for(i=0;i<li.size()-1;i++){
      if(li[i].str==li[i+1].str) {
	ans.push_back(li[i].str);
	break;
      }
    }
    //for(i=0;i<li.size();i++) cout << li[i] << endl;
    sort(ans.begin(),ans.end());
    //for(i=0;i<ans.size();i++) cout << ans[i] << endl;
    if(ans.size()!=0) cout << ans[0] << endl;
    else cout << 0 << endl;
    cin >> h >> w;
  }

  return 0;
}

Measured memory_bytes: 29180
#include "bits/stdc++.h"
using namespace std;
#define MOD 1000000007
#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,n)  FOR(i,0,n)
#define ITR(itr,mp) for(auto itr = (mp).begin(); itr != (mp).end(); ++itr)
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
typedef long long ll;
typedef pair<ll,ll> P;
typedef vector<P> Graph;

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

  int a,b;
  cin >> a >> b;
  
  REP(i,a+2){
    REP(j,b+2){
      if(i == 0 || i == a+1 || j == 0 || j == b+1){
        cout << '#';
      }
      else{
        char c;
        cin >> c;
        cout << c;
      }
    }
    cout << endl;
  }

  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long  ll;
#define FOR(i,a,n) for(ll i=(ll)a;i<(ll)n;i++)
#define RFOR(i,a,n) for(ll i=(ll)n-1;i >= (ll)a;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,0,n)
#define ALL(v) v.begin(), v.end()
#define bra(first,second) '(' << first << ',' << second << ')'
ll MOD = 1000000007;
//ll INF = 21474836470000000;
ll INF = 100100100100100100;
long double EPS = 1e-11;
long double PI = 3.141592653589793238;
template<typename T>
void remove(std::vector<T>& vector, unsigned int index){
    vector.erase(vector.begin() + index);
}

using Graph = vector<vector<ll>>;

int main(){
  string S;
  cin >> S;
  rep(i,S.size()-1){
    if(S.substr(i,2) == "AC"){
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout  << "No" << endl;
}


Measured memory_bytes: 256
n = int(input())
x = list(map(int, input().split()))
s = sum(x)
p = float(s/n)
q = int(10*p)
if q%10 < 5:
    p = int(s/n)
else:
    p = int(s/n)+1
r = 0
for y in x:
    r += (y - p)**2
print(r)

Measured memory_bytes: 3060
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
a.sort()
ma = a.pop()
mi = -1
dif = mod
for i in range(n-1):
    if abs(ma/2-a[i]) < dif:
        mi = i
        dif = abs(ma/2-a[i])
print(ma,a[mi])

Measured memory_bytes: 59372
#include<math.h>
#include<iostream>
#include <algorithm>
#include <string>
#include<vector>
#include<array>
#include<tuple>
#include<queue>
#include<stack>
#include<iomanip>
#include<bitset>
//AtCoderC++.cpp

using namespace std;

vector<long long> comb = vector<long long>(100 * 100,-1);

long long combination(long long n, long long r) {
	if (n == 1 || n == 0)return 1;
	if (n == r)	return 1;
	else if (r == 0)return 1;
	else if (r == 1)return n;
	else if (comb[100 * (n - 1) + (r - 1)] != -1) return comb[100 * (n - 1) + (r - 1)];
	else {
		auto res = combination(n - 1, r - 1) + combination(n - 1, r);
		comb[100 * (n - 1) + (r - 1)] = res;
		return res;
	}
}


//0-9のコンバートを考えるのみ。
//1との最短距離を出せばよい。(全頂点間を出す必要はない。でも出しちゃった)
void minimize(vector<vector<int>>& map) {
	for (size_t k = 0; k < 10; k++) {
		for (size_t i = 0; i < 10; i++) {
			for (size_t j = 0; j < 10; j++) {
				map[i][j] = min(map[i][j], map[i][k] + map[k][j]);
			}
		}
	}
}


//書かれていない部分は1として良い
int main() {
	int H, W; cin >> H >> W;
	auto cost = vector<vector<int>>(10);
	for (size_t i = 0; i < 10; i++) {
		cost[i] = vector<int>(10);
		for (size_t j = 0; j < 10; j++) {
			cin >> cost[i][j];
		}
	}
	minimize(cost);
	int answer = 0;
	int c;
	for (size_t i = 0; i < W; i++) {
		for (size_t i = 0; i < H; i++) {
			cin >> c;
			if (c != -1)answer += cost[c][1];
		}
	}
	cout << answer << endl;
}

Measured memory_bytes: 384
from heapq import heappush, heappop, heapify

def readinput():
    n=int(input())
    v=list(map(int,input().split()))
    return n,v

def main(n,v):
    heapify(v)
    while(len(v)>1):
        x=heappop(v)
        y=heappop(v)
        heappush(v,(x+y)/2)
    
    return v[0]

if __name__=='__main__':
    n,v=readinput()
    ans=main(n,v)
    print(ans)


Measured memory_bytes: 9028
//
// Created by Tetsuya Shiota on 2019-03-03.
//

#include<iostream>
#include<algorithm>
#include <vector>
#include <set>

using namespace std;

set<int> divs(int N){
    set<int> ret;
    for(int i =1; i<=N; i++){
        if(N%i == 0)ret.insert(i);
    }
    return ret;
}

int main(){
    int A, B, K;
    cin >> A  >>  B >> K;
    auto setA = divs(A), setB = divs(B);
    for(int i =100; i>0; i--){
        if(setA.count(i) == 1 && setB.count(i) == 1){
            K--;
        }
        if(K==0){
            cout << i <<endl;
            return 0;
        }
    }
    return 1;
}

Measured memory_bytes: 888
//   _/                _/                 _/_/_/   _/
//_/_/_/_/   _/_/   _/_/_/_/   _/_/    _/       _/_/
// _/     _/    _/   _/     _/    _/  _/_/_/     _/
//_/     _/    _/   _/     _/    _/  _/    _/   _/
// _/_/   _/_/       _/_/   _/_/      _/_/     _/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const double pi=3.14159265358979323846;
const int inf=1e9;
const ll INF=1e18;
typedef pair<int,int> P;
vector<P> G[/*ここにノードの値+5を入れる*/100005];
vector<ll> dis;
/*呼び出しはmain関数内でdijkstra(始点)*/
void dijkstra (int start){
  priority_queue<P,vector<P>,greater<P>> q;
  dis=vector<ll>(/*ここにノードの値+5を入れる*/100005,INF);
  dis[start]=0;
  q.push(P(start,0));
  while(!q.empty()){
    P now=q.top();q.pop();
    int nowv=now.first;
    if(dis[nowv]<now.second)continue;
    for(P next:G[nowv]){
      if(dis[next.first]>dis[nowv]+next.second){
        dis[next.first]=dis[nowv]+next.second;
        q.push(P(next.first,dis[next.first]));
      }
    }
  }
}
int main() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(false);
  ll v,e,r,s,t,d;
  cin >> v >> e >> r;
  for(int i=0; i<e; i++) {
    cin >> s >> t >> d;
    G[s].push_back(P(t,d));
  }
  dijkstra(r);
  for(int i=0; i<v; i++) {
    if(dis[i]==INF) {
      cout << "INF" << "\n";
    }
    else {
      cout << dis[i] << "\n";
    }
  }
}


Measured memory_bytes: 20844
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <map>
#include <stack>
#include <set>

using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-10;
const int INF = 0x3f3f3f3f;
const ll mod  = 1e9 + 7;
const ll maxn = 1e6 + 5;

int n,Q;
char s[maxn];
set<int>ss[30];

int main(){
	cin>>n>>s+1>>Q;
	for(int i=0;i<26;i++) ss[i].insert(n+1);//防止越界
	for(int i=1;i<=n;i++) ss[s[i]-'a'].insert(i);
	while(Q--){
		int t,i,l,r;
		char c;
		cin>>t;
		if(t==1){
			cin>>i>>c;
			if(s[i]!=c) ss[s[i]-'a'].erase(i);
			s[i]=c;
			ss[c-'a'].insert(i);
		}else{
			int ans=0;
			cin>>l>>r;
			for(int i=0;i<26;i++)
				if(*ss[i].lower_bound(l)<=r) ans++;
			cout<<ans<<'\n';
		}
	}
	return 0;
}



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

typedef long long ll;
typedef pair<ll, ll> pll;

#define m0(x) memset(x, 0, sizeof(x))
#define all(x) x.begin(), x.end()
#define rep(i, n) for(int i = 0; i < (n); i++)
#define asort(x) sort(all(x));
#define dsort(x, t) sort(x.begin(), x.end(), greater<t>());
#define dump(x)  cout << #x << " = " << (x) << endl
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define MINS(m, s) m.insert(make_pair(s, 1));
#define MFIN(m, s) m.find(s) != m.end()
#define YES(n) cout << ((n) ? "YES" : "NO" ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl

const int INF = 1e9;
const ll LINF = 1e18;
const int mod = 1e9+7;

int main(){
    ll x, k, d;
    cin >> x >> k >> d;

    // 移動回数
    ll tmp1 = abs(x) / d;

    ll ans = 0;
    if(tmp1 < k){
        // 奇数か偶数か
        ll tmp2 = k - tmp1;
        if(tmp2 % 2 == 0) {
            ans = abs(x) - tmp1 * d;
        }else{
            ll ans1 = abs(x) - (tmp1 - 1) * d;
            ll ans2 = abs(x) - (tmp1 + 1) * d;
            ans = min(abs(ans1), abs(ans2));
        }
    }else{
        ll ans1 = abs(x) - k * d;
        ll ans2 = abs(x) + k * d;
        ans = min(abs(ans1), abs(ans2));
    }   

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 3556
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;

bool testinPrimeSet(int num, vector<int> & pset) {
	for (vector<int>::iterator it = pset.begin(); it < pset.end() && sqrt(num) >= (*it); it++) {
		if ((num % (*it) == 0))
			return false;
	}
	return true;
}

int CountPrime(int num, vector<int> & pset) {
	int be = pset.at(pset.size() - 1);
	while (++be <= num) {
		if(testinPrimeSet(be, pset))
			pset.push_back(be);
	}
	return 0;
}

int getdst(int num, vector<int> & pset) {
	int i = 0;
	for (vector<int>::iterator it = pset.begin(); it < pset.end(); it++)
		if ((*it) <= num)
			i++;
		else
			break;
	return i;
}

int main() {
	int n = 0;
	int a[5] = { 2,3,5,7,11 };
	vector<int> dst;
	vector<int> primeset(a, a + 5);
	while (cin >> n) {
		CountPrime(n, primeset);
		dst.push_back(n);
	}
	for (vector<int>::iterator it = dst.begin(); it < dst.end(); it++)
		cout << getdst(*it, primeset) << endl;
	return 0;
}


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

using namespace std;

#define ll long long 
#define REPL(i,n) for(ll i=0, i##_len=(n); i<i##_len; ++i)
#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define radrad(a)  180.0 * a / M_PI
#define pow(x) x*x
static const int MAX = 10010;
static const ll INFTY = 1e12;
static const ll NUMMAX = 1000000007;
static const ll MOD = 1000000007;
 #define LOOP 10000000



int main() {

  ll N, K, S;
  ll num;
  ll count = 0;
  cin >> N >> K >> S;
  vector<ll> A(N);

  //num = N / K;
//
  //count = 0;
  //for(int i = 0; i < num-1; i++){
  //  A[i] = 1;
  //  count ++;
  //}
  //A[num-1] = S - count;
//
  //
  //REP(j, K-1){
  //  cout << A[i] << endl;
  //}
  REP(i, N){
    if(i < K){
      //A[i] = S;
      cout << S;
    } else {
      //A[i] = S+1;
      if(S > 50)
        cout << S-1;
      else {
        cout << S+1;
      }
    }
    cout << " ";
  }

  cout << endl;

  return 0;
}
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         pass System Test!
*/


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

int cnt[5];

int main() {
    int n;
    scanf("%d", &n);
    for (int i = 0, a; i < n; i++) {
        scanf("%d", &a);
        if (a & 1) cnt[1]++; else cnt[a % 4]++;
    }
    if (cnt[1] <= cnt[0] + (cnt[2] == 0)) return printf("Yes"), 0;
    printf("No");
}


Measured memory_bytes: 256
from collections import defaultdict as dd
from math import sqrt
d=dd(int)
def pr(n): 
    while n % 2 == 0:
        d[2]+=1
        n = n / 2
    for i in range(3,int(sqrt(n))+1,2): 
        while n % i== 0: 
            d[i]+=1
            n = n / i 
    if n > 2: 
        d[n]+=1

n=int(input())
pr(n)
ans=0
for i in d:
    ans+=(sqrt(1+8*d[i])-1)//2
print(int(ans))


Measured memory_bytes: 68272
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<unordered_map>
using namespace std;
typedef long long ll;
#define chmax(a,b) a=max(a,b)
#define chmin(a,b) a=min(a,b)
#define mod 1000000007
#define mad(a,b) a=(a+b)%mod
#define N 200010
int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    double a,b,q,p,r;
    cin>>a>>b>>p>>q>>r;
    cout<<(p*b-(b-a)*q)/(r+q)+b<<endl;
}





Measured memory_bytes: 3308
#include<cstdio>
#include<string>
#include<iostream>
#include<cstring>

using namespace std;

int n;
string name[15];
int f[11],w[11],s[11];
int order[11],flag[11],res[11];
int ming,sumw;

void func(int x,int g,int lw){
	if(x==n){
		if(ming>g){
			for(int i=0;i<n;i++)res[i]=order[i];
			ming=g;
		}
	}else{
		for(int i=0;i<n;i++){
			if(!flag[i]){
				flag[i]=1;
				order[x]=i;
				if(lw-w[i]<=s[i])func(x+1,g+(x+1)*w[i],lw-w[i]);
				flag[i]=0;
			}
		}
	}
}

int main(void){
	while(1){
		scanf("%d",&n);
		if(n==0)break;
		memset(order,0,sizeof(int));
		memset(res,0,sizeof(int));
		memset(flag,0,sizeof(int));
		ming=99999999;
		sumw=0;
		for(int i=0;i<n;i++){
			cin >> name[i] >> w[i] >> s[i];
			sumw+=w[i];
		}
		for(int i=0;i<n;i++){
			flag[i]=1;
			order[0]=i;
			if(sumw-w[i]<=s[i])func(1,w[i],sumw-w[i]);
			flag[i]=0;
		}
		for(int i=0;i<n;i++)cout << name[res[i]] << endl;
	}
	return 0;
}

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

#define mp(x,y) make_pair(x,y) 
#define MAX 999999999999999
#define M 1000000007
#define endl "\n"
#define pb(x) push_back(x)
#define llip(t) long long t ; cin >> t ;
#define llip2(a,b) long long a,b ; cin >> a >> b ;
#define dip(a) double a ; cin >> a ;
#define dip2(a,b) double a,b ; cin >> a >> b ; 
#define pll pair<long long,long long>  
typedef long long ll;

ll topoSort(vector<vector<ll>>& G,queue<ll>& ans){
	int n = G.size();
	vector<ll> indegree(n) , LPI(n);
	for(auto v:G){
		for(auto to:v) indegree[to]++ ;
	}
	priority_queue<ll,vector<ll>,greater<ll>> pq ;
	vector<bool> visited(n) ;
	for(int i = 1 ; i < n ; i++){
		if(indegree[i] == 0) {pq.push(i) ; visited[i] = true ;}
	}
	while(!pq.empty()){
		ll curr = pq.top() ;
		pq.pop() ;
		ans.push(curr) ;
		for(auto b:G[curr]){
			if(!visited[b]) {indegree[b]-- ; LPI[b] = max(LPI[b],LPI[curr]+1) ;} 
			if(indegree[b] == 0) {pq.push(b) ; visited[b] = true ;}
		}
	}
	return *max_element(LPI.begin(), LPI.end()) ;
}

int main(){ 
    ios_base:: sync_with_stdio(false); cin.tie(0); cout.tie(0);
    llip2(n,m) 
    vector<vector<ll>> G(n+1) ;
    for(int i = 0 ; i < m ; i++){
    	llip2(x,y) ;
    	G[x].pb(y) ;
    }
    queue<ll> q ;
    cout << topoSort(G,q) << endl ;   
}
 

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

// using namespace std;

// vector<pair<int , pair<pair<int , int> , int>>> a;
// vector<int> c;

// int n  , m , q , ans = 0;

// void go(int i){
// 	if(i >= q) return;
// 	if(c[a[i].second.first.second] - c[a[i].second.first.first] == a[i].second.second) ans+=a[i].first;
// 	if(c[a[i].second.first.second] == 0 && c[a[i].second.first.first] == 0)
// 		for(int t = 1 ; t + a[i].second.second < 11 ; t++) c[a[i].second.first.first] = i , c[a[i].second.first.second] = t + a[i].second.second , go(i+1);
// 	else if(c[a[i].second.first.second] == 0 && c[a[i].second.first.first] != 0) c[a[i].second.first.second] = a[i].second.second + c[a[i].second.first.first];
// 	else if(c[a[i].second.first.second] != 0 && c[a[i].second.first.first] == 0) c[a[i].second.first.first] = c[a[i].second.first.second] - a[i].second.second;
// 	else go(i+1);
// }


// int main(){
// 	cin>>m>>n>>q;
// 	a.resize(q);
// 	c.resize(n);
// 	for(int i = 0 ; i < q ; i++) cin>>a[i].second.first.first>>a[i].second.first.second>>a[i].second.second>>a[i].first;
// 	sort(a.begin() , a.end());
// 	go(0);
// 	cout<<ans<<endl;
// }


#include <bits/stdc++.h>

using namespace std;

int n , m , q , ans = 0 , mx = 0;

vector<int> a , b , c , d, v;

void go() {
	if(v.size() == n) {
		for(int i = 0 ; i < q ; i++) if(v[b[i] - 1] - v[a[i] - 1] == c[i]) ans+=d[i];
		mx = max(ans , mx);
		ans = 0;
		return;
	}
	for(int i = v.back(); i <= m; i++) {
		v.push_back(i);
		go();
		v.pop_back();
	}
}

int main(){
	cin>>n>>m>>q;
	a.resize(q);
	b.resize(q);
	c.resize(q);
	d.resize(q);
	for(int i = 0 ; i < q ; i++) cin>>a[i]>>b[i]>>c[i]>>d[i];
	v.push_back(1);
	go();
	cout<<mx<<endl;
}

Measured memory_bytes: 3644
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) rep(_,v.size())cout<<" "<<v[_];cout<<endl;
#define show2d(v) rep(_,v.size()){rep(__,v[_].size())cout<<" "<<v[_][__];cout<<endl;}cout<<endl;
using namespace std;
//kaewasuretyuui
typedef long long ll;
//#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
// #define mt make_tuple
// typedef tuple<int,int,int> tp;
// typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 1e18+10 : 1e9+10;
// int dx[]={0,1,0,-1,1,1,-1,-1};
// int dy[]={1,0,-1,0,1,-1,1,-1};//RDLU
int dx[]={-1,-1,0,0,1,1};
int dy[]={-1,0,-1,1,0,1};//RDLU
int main(){
	int n;
	cin>>n;
	vi in(n);
	rep(i,n)cin>>in[i];
	vvd dp(n,vd(300000,inf));
	rep(i,300000)dp[0][i]=(double)abs(in[0]-i)/in[0];
	rep(i,n-1)loop(j,1,300000){
		for(int k=j;k<300000;k+=j){
			cmin(dp[i+1][k],max(dp[i][j],(double)abs(in[i+1]-k)/in[i+1]));
		}
	}
	double out=inf;
	rep(i,300000)cmin(out,dp[n-1][i]);
	cout<<shosu(11)<<out<<endl;
}	
	
	
/*
int main(){
	int n,l,t,co=0;
	cin>>n>>l>>t;
	
	vvi in(n,vi(2));
	rep(i,n)rep(j,2)cin>>in[i][j];
	rep(i,n)if(in[i][1]==2)in[i][1]=-1;
	
	vvi g(n,vi(2));
	rep(i,n){
		if(in[0][1]!=in[i][1]){
			ll w=t;
			co+=w/l;
			w%=l;
			ll dis=(l+in[i][0]-in[0][0])%l;
			if(in[0][1]==-1)dis=(l+in[0][0]-in[i][0])%l;
			if(dis<=2*w)co++;
		}
		g[i]={(in[i][0]+t*in[i][1])%l,in[i][1]};
	}
	if(in[0][1]==-1)co=n-1-co%n;
	else co%=n;
	cout<<co<<endl;
	show2d(in);
	show2d(g);
}	
*/









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

using namespace std;

int main()
{
  	ios_base::sync_with_stdio(0);
  	cin.tie(0);
  
	int h,w,m,maxr=0,maxc=0;
	cin>>h>>w>>m;
  
  	int mat[m][2];
  	for(int i=0;i<m;i++)
    cin>>mat[i][0]>>mat[i][1];

	int row[h+1]={0};
	int col[w+1]={0};

	set<pair<int,int>>s;
	for(int i=0;i<m;i++)
	{
		row[mat[i][0]]++;
		maxr=max(maxr,row[mat[i][0]]);
		col[mat[i][1]]++;
		maxc=max(maxc,col[mat[i][1]]);
		s.insert({mat[i][0],mat[i][1]});
	}

	vector<int>posr,posc;
	for(int i=1;i<=h;i++)
	{
		if(row[i]==maxr)
			posr.push_back(i);
	}

	for(int i=1;i<=w;i++)
	{
		if(col[i]==maxc)
			posc.push_back(i);
	}

	int ans=maxc+maxr-1;
	for(int i=0;i<posr.size();i++)
	{
		for(int j=0;j<posc.size();j++)
		{
			if(s.count({posr[i],posc[j]})==0)
			{
				ans++;
				break;
			}
		}
      if(ans==(maxc+maxr))
        break;
	}

	cout<<ans;

	return 0;
}

Measured memory_bytes: 26140
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <algorithm>
using namespace std;

int main(void){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

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

  int cnt = 0;
  for(int i = 0; i < N; i++) {
    if(a[i] % 2 != 0 && (i+1) % 2 != 0) cnt++;
  }

  cout << cnt << endl;

  return 0;
}


Measured memory_bytes: 3612
X = int(input())
A = 0
B = 1010
while B==1010:
    l = -1000
    r = 1000
    while abs(l-r)!=1:
        if A**5-((l+r)//2)**5<X:
            r = (l+r)//2
        elif A**5-((l+r)//2)**5>X:
            l = (l+r)//2
        else:
            B = (l+r)//2
            break
    if B==1010:
        A += 1
print(A,B)

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

int main() {
  int n;
  bool b;
  b=true;
  cin>>n;
  for (int i=1 ; i<=9;i++){
    for (int j=1;j<=9;j++){
      if (i*j==n && b){
        b=false;
        cout<<"Yes"<<endl;
        break;
      }
    }
  }  
  if (b==true){
    cout<<"No"<<endl;
  }
}



Measured memory_bytes: 256
// https://atcoder.jp/contests/abc142/tasks/abc142_c

#include <bits/stdc++.h>
using namespace std;
#define watch(x) cerr << (#x) << " is " << (x) << endl;
#define endl '\n' 

const int maxn = 100500;

int ans[maxn];
int N;
signed main() {
    ios::sync_with_stdio(0);
    cin.sync_with_stdio(0);
    cin.tie(0);

    cin >> N;

    for(int i = 1; i <= N; i++) {
        int a;
        cin >> a;

        ans[a] = i;
    }

    for(int i = 1; i <= N; i++) {
        cout << ans[i] << " ";
    }
    cout << endl;
}


Measured memory_bytes: 4032
// 2011/03/01 Tazoe

#include <iostream>
#include <string>
using namespace std;

int main()
{
	while(true){
		string brd[3];
		cin >> brd[0];
		if(brd[0]=="0")
			break;

		cin >> brd[1];
		cin >> brd[2];

		if(brd[0][0]!='+'&&brd[0][0]==brd[0][1]&&brd[0][1]==brd[0][2])
			cout << brd[0][0] << endl;
		else if(brd[1][0]!='+'&&brd[1][0]==brd[1][1]&&brd[1][1]==brd[1][2])
			cout << brd[1][0] << endl;
		else if(brd[2][0]!='+'&&brd[2][0]==brd[2][1]&&brd[2][1]==brd[2][2])
			cout << brd[2][0] << endl;
		else if(brd[0][0]!='+'&&brd[0][0]==brd[1][0]&&brd[1][0]==brd[2][0])
			cout << brd[0][0] << endl;
		else if(brd[0][1]!='+'&&brd[0][1]==brd[1][1]&&brd[1][1]==brd[2][1])
			cout << brd[0][1] << endl;
		else if(brd[0][2]!='+'&&brd[0][2]==brd[1][2]&&brd[1][2]==brd[2][2])
			cout << brd[0][2] << endl;
		else if(brd[0][0]!='+'&&brd[0][0]==brd[1][1]&&brd[1][1]==brd[2][2])
			cout << brd[0][0] << endl;
		else if(brd[0][2]!='+'&&brd[0][2]==brd[1][1]&&brd[1][1]==brd[2][0])
			cout << brd[0][2] << endl;
		else
			cout << "NA" << endl;
	}

	return 0;
}

Measured memory_bytes: 0
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <utility>
#include <memory>
#include <functional>
#include <deque>
#include <cctype>
#include <ctime>
#include <numeric>
#include <list>
#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll;
typedef vector<vector<long long> > vvll;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _rep(i,n) _rep2((i),0,(n))
#define _rep2(i,a,b) for(int i=(a);i<(b);++i)
#define rep(...) GET_MACRO(__VA_ARGS__, _rep2, _rep)(__VA_ARGS__)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define fi first
#define se second
#define mkp make_pair
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

void mainmain(){
	int n;
	cin>>n;
	ll A,B;
	cin>>A>>B;
	vll v(n);
	rep(i,n) cin>>v[i];
	sort(ALL(v));
	ll l = 0;
	ll r = 1e9;
	while(r-l>1){
		ll mid = (r+l)/2;
		ll cnt = 0;
		rep(i,n){
			ll t = v[i] - B*mid;
			if(t>0){
				cnt += (t+A-B-1)/(A-B);
			}
		}
		if(cnt<=mid){
			r = mid;
		}
		else{
			l = mid;
		}
		// cout<<mid<<" "<<cnt<<endl;
	}
	cout<<r<<endl;
}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(20);
    mainmain();
}

Measured memory_bytes: 1024
#include <stdio.h>

int main()
{
	char O[51], E[51];
	scanf("%s%s", O, E);
	char ans[101];
	int i, j;
	for (i = 0; O[i] != '\0'; i++) {
		;
	}
	for (j = 0; E[j] != '\0'; j++) {
		;
	}
	int max = i+j;
	for (i = 0, j = 0; max / 2+1 > i; i++) {
		ans[j] = O[i];
		j++;
		ans[j] = E[i];
		j++;
	}
	ans[++j] = '\0';
	printf("%s\n", ans);
	return 0;
}


Measured memory_bytes: 128
A,B = map(int,input().split())
S = list(input())
T = ["0","1","2","3","4","5","6","7","8","9"]
flag = 0
for i in range(len(S)) :
    if 0 <= i <= A-1 or A+1 <= i <= len(S)-1 :
        if S[i] in T :
            continue
        else :
            flag = 1
            break
    else :
        if S[i] != "-" :
            flag = 1
            break
if flag == 0 :
    print("Yes")
else :
    print("No")


Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
typedef pair<int,int> P;
typedef pair<P,P> P2;
 
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
int d[1<<5][50][50][6],INF=1<<28,n,m,l;
bool check(int x, int y) {return x>=0&&x<n&&y>=0&&y<m;}
 
int main() {
  cin >> n >> m >> l;
  string s[n];
  for(int i=0; i<n; i++) cin >> s[i];
  int sx,sy,tx,ty;
  for(int i=0; i<n; i++) {
    for(int j=0; j<m; j++) {
      if(s[i][j]=='S') sx=i,sy=j;
      if(s[i][j]=='G') tx=i,ty=j;
      for(int t=0; t<(1<<5); t++) {
    for(int k=0; k<6; k++) d[t][i][j][k]=INF;
      }
    }
  }
  d[0][sx][sy][5]=0;
  int ans=-1;
  queue<P2> que;
  que.push(P2(P(sx,sy),P(5,0)));
  while(!que.empty()) {
    P2 p=que.front();que.pop();
    int nx=p.F.F,ny=p.F.S,k=p.S.F,t=p.S.S;
    if(nx==tx && ny==ty && k>=4) {
      ans=d[t][nx][ny][k];
      break;
    } 
    for(int i=0; i<4; i++) {
      int x,y,z,tt=t;
      if(!k) {
    if(!i) x=nx-l,y=ny,z=4;
    else if(i==1) x=nx,y=ny+1,z=0;
    else if(i==2) x=nx+1,y=ny,z=5;
    else x=nx,y=ny-1,z=0;
      } else if(k==1) {
    if(!i) x=nx-1,y=ny,z=1;
    else if(i==1) x=nx,y=ny+l,z=4;
    else if(i==2) x=nx+1,y=ny,z=1;
    else x=nx,y=ny-1,z=5;
      } else if(k==2) {
    if(!i) x=nx-1,y=ny,z=5;
    else if(i==1) x=nx,y=ny+1,z=2;
    else if(i==2) x=nx+l,y=ny,z=4;
    else x=nx,y=ny-1,z=2;
      } else if(k==3) {
    if(!i) x=nx-1,y=ny,z=3;
    else if(i==1) x=nx,y=ny+1,z=5;
    else if(i==2) x=nx+1,y=ny,z=3;
    else x=nx,y=ny-l,z=4;
      } else if(k==5) {
    if(!i) x=nx-1,y=ny,z=0;
    else if(i==1) x=nx,y=ny+1,z=1;
    else if(i==2) x=nx+1,y=ny,z=2;
    else x=nx,y=ny-1,z=3;
      } else if(k==4) {
    if(!i) x=nx-l,y=ny,z=2;
    else if(i==1) x=nx,y=ny+l,z=3;
    else if(i==2) x=nx+l,y=ny,z=0;
    else x=nx,y=ny-l,z=1;
      }
      if(z<4) {
    bool ck=1;
    for(int j=0; j<l; j++) {
      int xx=x+dx[z]*j,yy=y+dy[z]*j;
      if(!check(xx,yy) || s[xx][yy]=='#') {
        ck=0;
        break;
      }
      if(s[xx][yy]>='a' && s[xx][yy]<='e' && !(t&(1<<(s[xx][yy]-'a')))) ck=0;
    }
    if(!ck) continue;
      } else {
    if(!check(x,y)) continue;
    if(s[x][y]=='^' || s[x][y]=='#') continue;
    if(s[x][y]>='a' && s[x][y]<='e' && !(t&(1<<(s[x][y]-'a')))) continue;
    if(s[x][y]>='A' && s[x][y]<='E') tt=tt^(1<<(s[x][y]-'A'));
      }
      if(d[tt][x][y][z]<=d[t][nx][ny][k]+1) continue;
      d[tt][x][y][z]=d[t][nx][ny][k]+1;
      que.push(P2(P(x,y),P(z,tt)));
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 2324
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <limits>
#include <climits>
#include <utility>
#include <algorithm>
#include <numeric>

using namespace std;

using ll = long long;
using ull = unsigned long long;

ll f(ll i, ll l, const vector<ll>& lnum, const vector<ll>& pnum)
{
    if (l == 0) {
        return 1;
    }

    if (i == 1) {
        return 0;
    } else if (2 <= i && i <= lnum[l-1] + 1) {
        return f(i - 1, l - 1, lnum, pnum);
    } else if (i == lnum[l-1] + 2) {
        return pnum[l-1] + 1;
    } else if (lnum[l-1] + 3 <= i && i <= 2*lnum[l-1] + 3) {
        return pnum[l-1] + 1 + f(i - lnum[l-1] - 2, l - 1, lnum, pnum);
    } else {
        return 2 * pnum[l-1] + 1;
    }
}

int main()
{
    ll N, X;
    cin >> N >> X;

    vector<ll> lnum(N+1);
    lnum[0] = 1;
    for (int i = 0; i < N; ++i) {
        lnum[i+1] = 2*lnum[i] + 3;
    }

    vector<ll> pnum(N+1);
    pnum[0] = 1;
    for (int i = 0; i < N; ++i) {
        pnum[i+1] = 2*pnum[i] + 1;
    }

    ll ans = f(X, N, lnum, pnum);
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define boost ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define int long long
#define M 1000000007
#define eb emplace_back
#define watch(x) cout << (#x) <<" is "<< x <<"\n";


using namespace std;
//const int N = 1e6;

int32_t main(){
  boost;
  int tc = 1; //cin >> tc;
  while(tc--)
  {
    int x; cin >> x;
    int ans = 360/__gcd(x, 360ll);
    cout << ans << "\n";
  }
}



Measured memory_bytes: 3608
import sys
S = set(input())
ls = ['N','W','S','E']
if ls[0] in S and not ls[2] in S:
    print('No')
    sys.exit()
if ls[1] in S and not ls[3] in S:
    print('No')
    sys.exit()
if ls[2] in S and not ls[0] in S:
    print('No')
    sys.exit()
if ls[3] in S and not ls[1] in S:
    print('No')
    sys.exit()
print('Yes')

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;

namespace mp = boost::multiprecision;

mp::cpp_int lcm(mp::cpp_int a, mp::cpp_int b) {
  if (a < b) {
    swap(a, b);
  }
  mp::cpp_int x = a * b;
  mp::cpp_int r = a % b;
  while (r != 0) {
    a = b;
    b = r;
    r = a % b;
  }
  return x/b;
}

int main() {
  int N;
  cin >> N;
  mp::cpp_int ans = 1, t;
  for (int i = 0; i < N; i++) {
    cin >> t;
    ans = lcm(ans, t);
  }
  cout << ans << endl;
}

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

n,m = map(int,input().split())

edge = [[] for _ in [0]*n]
back = [[] for _ in [0]*n]

for _ in [0]*m:
    a,b = map(int,input().split())
    edge[a-1].append(b-1)
    back[b-1].append(a-1)


res = [None]*(n+1)

for end in range(n):
    for start in edge[end]:
        cost = [-1]*n
        cost[start] = 0
        new = [start]
        tank = []
        judge = True

        while len(new) > 0 and judge:
            tank = []
            for fr in new:
                for go in edge[fr]:
                    if cost[go] < 0:
                        cost[go] = cost[fr] + 1
                        tank.append(go)
                        if go == end:
                            judge = False
            new = tank

        
        if cost[end] >= 0 and cost[end] < len(res):
            p = end
            res = [p+1]
            while p != start:
                for e in back[p]:
                    if cost[p] == cost[e]+1:
                        p = e
                        res.append(p+1)
                        break
if res[0] == None:
    print(-1)
else:                      
    print(len(res))
    for e in res:
        print(e)

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

#define reep(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()

typedef unsigned long long ull;
const ull c = 1145148931919LL;

ull ro[1000100];
ull tei[1000100];
int nn;
bool check(int a, int b, int c, int d) {
	if(a < 0 || c < 0) return false;
	if(nn <= b || nn <= d) return false;
	ull aa = ro[b];
	ull bb = ro[d];
	if(a) aa += -ro[a - 1] * tei[b - a + 1];
	if(c) bb += -ro[c - 1] * tei[d - c + 1];
	return aa == bb;
}

vector<int> calc_z(string s){
	int n = s.size();
	vector<int> z(n,0);
	int l=0,r=0;
	for(int i=1;i<s.size();i++){
		if(i>r){
			l=r=i;
			while(r<n&&s[r-l]==s[r]) r++;
			z[i]=r-l;
			r--;
		}
		else{
			if(z[i-l]<r-i+1){
				z[i]=z[i-l];
			}
			else{
				l=i;
				r++;
				while(r<n&&s[r-l]==s[r]) r++;
				z[i]=r-l;
				r--;
			}
		}
	}
	return z;
}


template<class T>
struct RollingHash {
	static const int NN = 2; // the number of hash table
	T base[NN] = {1009, 1007};
	T PMOD[NN] = {1000000007, 1000000009};
	vector<T> hash[NN];
	vector<T> mypow[NN];
	int sz;
	void init(string s) {
		sz = s.size();
		for(int i = 0; i < NN; i++) {
			hash[i].assign(sz + 1, 0);
			mypow[i].assign(sz + 1, 0);
			mypow[i][0] = 1;
		}
		for(int i = 0; i < NN; i++) {
			for(int j = 0; j < sz; j++) {
				hash[i][j + 1] = (hash[i][j] * base[i] + s[j]) % PMOD[i];
				mypow[i][j + 1] = mypow[i][j] * base[i] % PMOD[i];
			}
		}
	}
	RollingHash() {
	}
	// hash[t] value of s[l..r]
	T get(int l, int r, int t) {
		T ret = (hash[t][r + 1] - hash[t][l] * mypow[t][r - l + 1] % PMOD[t] + PMOD[t]) % PMOD[t];
		return ret;
	}
	// compare s[a..b] and s[c..d] O(1)
	bool comp(int a, int b, int c, int d) {
		if(a < 0 || sz <= b || c < 0 || sz <= d) return false;
		for(int i = 0; i < NN; i++) {
			if(get(a, b, i) != get(c, d, i)) return false;
		}
		return true;
	}
	// longest common extension s[l..LCE(l,r)] == s[r..LCE(l,r)] && s[l..LCE(l,r)+1] != s[r..LCE(l,r)+1]
	int LCE(int l,int r){
		tie(l,r)=minmax(l,r);
		if(l==r) return sz-r+1;
		int ll = 0;
		int rr = sz;
		while(rr-ll>1){
			int mid = (ll+rr)>>1;
			if(comp(l,l+mid-1,r,r+mid-1)) ll=mid;
			else rr=mid;
		}
		return ll;
	}
};

RollingHash<long long> rhash1;

int calc1(int l, int r) {
	return rhash1.LCE(l,r);
	int ll = 0;
	int rr = INT_MAX / 10;
	while(rr - ll > 1) {
		int mid = (ll + rr) / 2;
		if(rhash1.comp(l, l + mid - 1, r, r + mid - 1)) {
			ll = mid;
		}
		else {
			rr = mid;
		}
	}
	return ll;
}

int calc2(int l, int r) {
	int ll = 0;
	int rr = INT_MAX / 10;
	while(rr - ll > 1) {
		int mid = (ll + rr) / 2;
		if(rhash1.comp(l - mid + 1, l, r - mid + 1, r)) {
			ll = mid;
		}
		else {
			rr = mid;
		}
	}
	return ll;
}

int main() {
	string s, t;
	cin >> s >> t;
	string a = t+s;
	nn = a.size();
	int n = s.size();
	int m = t.size();
	int ans = 0;
	reverse(ALL(t));
	reverse(ALL(s));
	string b = t+s;
	vector<int> z1=calc_z(a);
	vector<int> z2=calc_z(b);
	rep(i, n) {
		if(i + m - 1 >= n) break;
		int l = z1[m+i];
		int r = z2[n-i];
		if(l + r + 1 == m) ans++;
	}
	cout << ans << endl;
}

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

using namespace std;
typedef long long ll;
#define int ll

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
#define MAX_N 100010

string S;
ll dp[MAX_N][4];

// 今 i を見ていて、状態が pat のときの ABC 数の合計
// [pat] 0 : 何も見つかってない, 1 : A はあった, 2 : B まであった, 3 : C まであった
ll dfs(ll i = 0, ll pat = 0) {
    if(i >= S.length()) {
        return (pat == 3) ? 1 : 0;
    }

    ll& ret = dp[i][pat];
    if(~ret) { return ret; }
    ret = 0;

    if(pat == 3) {
        // もう ABC を見つけ終わっているので、あとはパターンぶん埋めるだけ
        if(S[i] == '?') {
            ret += dfs(i+1, pat) * 3;
        } else {
            ret += dfs(i+1, pat);
        }
        ret %= MOD;
    }
    else {
        if(pat == 0 && (S[i] == 'A' || S[i] == '?')) {
            ret += dfs(i+1, 1);
            ret %= MOD;
        }
        else if(pat == 1 && (S[i] == 'B' || S[i] == '?')) {
            ret += dfs(i+1, 2);
            ret %= MOD;
        }
        else if(pat == 2 && (S[i] == 'C' || S[i] == '?')) {
            ret += dfs(i+1, 3);
            ret %= MOD;
        }
        if(S[i] == '?') {
            ret += dfs(i+1, pat);
            ret %= MOD;
            ret += dfs(i+1, pat);
            ret %= MOD;
        }
        ret += dfs(i+1, pat);
        ret %= MOD;
    }

    return ret;
}

signed main()
{
    cin >> S;
    MINUS(dp);
    printf("%lld\n", dfs());
    return 0;
}


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

main()
{
    int n; cin>>n;
    vector<int> a(n),b(n);
    for(int i=0; i<n; ++i)
    {
        cin>>a[i]>>b[i];
    }
    sort(begin(a),end(a));
    sort(begin(b),end(b));
    if(n&1)
    {
        cout << (b[n/2]-a[n/2])+1 << "\n";
    }
    else
    {
        cout << (b[n/2]+b[n/2-1])-(a[n/2]+a[n/2-1])+1 << "\n";
    }
}

Measured memory_bytes: 4808