akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
 
using ll = long long;
using pll = pair<ll, ll>;
#define all(x) (x).begin(), (x).end()
#define call(x) (x).cbegin(), (x).cend()
#define rall(x) (x).rbegin(), (x).rend()
#define pb push_back
#define sz(x) ((x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define rep(i, a, b) for(ll i = ll(a); i < ll(b); i++)
const double PI = acos(-1);
const int mod = 1e9+7;
const long long INF = (1LL << 60);
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
ll gcd(ll a,ll b){return b ? gcd(b,a%b) : a;}
ll lcm(ll a,ll b){return a / gcd(a,b) * b;}
ll stringcount(string s, char c){return count(s.cbegin(), s.cend(), c);}
bool isInteger(double x){return floor(x) == x;}
ll ceil(ll a, ll b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmax(T &a, const T &b){ if(a < b){ a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b){ if(a > b){ a=b; return 1; } return 0; }

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

  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, 0, n) cin >> a[i];

  vector<ll> e, o;
  rep(i, 0, n){
    if(i % 2 == 0) e.pb(a[i]);
    else o.pb(a[i]);
  }
  sort(all(e)), sort(all(o));
  
  ll e1 = 0, e2 = 0, o1 = 0, o2 = 0;
  ll oindex = -1, eindex = -1;
  ll cnt = 1;
  for(ll i = 0; i < sz(e); i++){
    if(e[i] == e[i+1]) cnt++;
    else{
      if(cnt >= e1){
        eindex = i;
        e2 = e1;
        e1 = cnt;
      }else if(cnt >= e2){
        e2 = cnt;
      }
      cnt = 1;
    }
  }
  if(cnt != 1){
    if(cnt >= e1){
      eindex = sz(e)-1;
      e2 = e1;
      e1 = cnt;
    }else if(cnt >= e2){
      e2 = cnt;
    }
  }
  cnt = 1;
  for(ll i = 0; i < sz(o); i++){
    if(o[i] == o[i+1]) cnt++;
    else{
      if(cnt >= o1){
        oindex = i;
        o2 = o1;
        o1 = cnt;
      }else if(cnt >= o2){
        o2 = cnt;
      }
      cnt = 1;
    }
  }
  if(cnt != 1){
    if(cnt >= o1){
      oindex = sz(o)-1;
      o2 = o1;
      o1 = cnt;
    }else if(cnt >= o2){
      o2 = cnt;
    }
  }

  //cout << o2 << ' ' << e2 << endl;
  if(o[oindex] != e[eindex]) cout << n - e1 - o1 << endl;
  else cout << n - max(o1+e2, e1+o2) << endl;
  return 0;
}

Measured memory_bytes: 2044
#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <cctype>

using namespace std;
// for loop
#define F(i, L, R) for(int i = L; i < R; i++)
#define FE(i, L, R) for(int i = L; i <= R; i++)
// for (reversed)
#define FR(i, L, R) for(int i = L; i > R; i--)
#define FRE(i, L, R) for(int i = L; i >= R; i--)

// data types
#define Int long
#define ul unsigned long
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned shoft

// vectors
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<vi> vvi;
typedef pair<int, int> ii;
#define ALL(c) (c).begin(),(c).end()
#define last(vec) vec.size()-1
#define lastEle(vec) vec[last(vec)]
#define pb push_back
#define InputV(a, n) F(i,0,n){long l; cin >> l; a.pb(l);}

// debug
#define DBG(vari) cerr<<#vari<<" = "<<(vari)<<endl;
#define printA(a, L, R) FE(i, L, R) cout << a[i] << (i==R?'\n':' ')
#define printAF(a, L) printA(a, 0, L-1)
#define printV(a) printA(a, 0, a.size()-1)

// utils
#define char2int(c) (c-'0')
#define bit(x, i) (x&(1<<i))
#define space ' '
#define PI 3.14159265358979323

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

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

int main() {
    const ll INF = 1LL << 60;
    long n;
    long k;
    // cost table
    ll h[100100];
    cin >> n;
    cin >> k;
    // dp table has minimal cost
    ll dp[100100];
    FE(i, 0, n){
        dp[i] = INF;
    }
    F(i, 0, n){
        cin >> h[i];
    }
    dp[0] = 0;
    F(i, 0, n){
        FE(j, 0, k){
            chmin(dp[i+j], dp[i] + abs(h[i]-h[i+j]));
        }
    }
    //printA(dp, 0, n+1);
    cout << dp[n-1] << endl;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>    
//#include <ext/pb_ds/assoc_container.hpp> 
//#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//using namespace __gnu_pbds; 
#define fastio() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb  push_back
#define show(x) cout<<(#x)<<" : "<<x<<endl;
#define ll  long long
#define ld  long double
#define fill(a,val) memset(a,val,sizeof(a))
#define mp  make_pair
#define ff  first
#define ss  second
#define pii pair<ll,ll>
#define sq(x) ((x)*(x))
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define endl "\n"
#define int long long 
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
const ll MOD     = 1000*1000*1000+7;
const ll INF     = 1ll*1000*1000*1000*1000*1000*1000 + 7;
const ll MOD2    = 998244353;
const ll N       = 1000 * 100 + 10;
const ll N2      = 70;
const ld PI  = 3.141592653589793;
//template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll gcd(ll a, ll b){if(!b)return a;return gcd(b, a % b);} 
ll power(ll x,ll y,ll p = LLONG_MAX ){ll res=1;x%=p;while(y>0){if(y&1)res=(res*x)%p;y=y>>1;x=(x*x)%p;}return res;}
ll lcm(ll a , ll b){return (a*b)/gcd(a,b);}

signed main()
{
    fastio();
    //cout<<fixed<<setprecision(20);
    //CHECK for LONG LONG and LONG DOUBLE
    //*comment for all except cc/cf    
    #ifndef ONLINE_JUDGE    
           freopen("input.txt","r",stdin);
           freopen("output.txt","w",stdout);
    #endif//*/
    int n,x,m;
    cin>>n>>x>>m;
    map<int,int> store;
    std::vector<int> v(1e6);
    int c = 1;
    v[0] = x;
    store[x] = 0;
    int rep = -1;
    bool ok = true;
    for (int i = 1; i < n; ++i)
    {
        int temp = sq(v[i-1]);
        temp%=m;
        v[i] = temp;  
        if(!temp)
        {
            ok = false;
            break;
        }  
        if(store.find(temp)==store.end())
        {
            store[temp] =i;
        }
        else
        {
            rep = store[temp];
            break;
        }
        c++;
    }

    


    for(int i = 1 ; i < c ; i++)
        v[i]+=v[i-1];

    if(!ok || rep ==-1 )
    {
        cout<<v[c-1];
        exit(0);
    }   



    int ans = v[c-1];
    n-=c;

    int len = c - rep;
    int req = n/(len);
    int tempsum = v[c-1];
    if(rep)
        tempsum-=v[rep-1];

    ans+=(req*tempsum);

    int left = n%len;

    if((rep+left)>1)
        ans+=v[rep+left-1];
    if(rep)
        ans-=v[rep-1];


    cout<<ans;


    printclock;
    return 0;
}



Measured memory_bytes: 14308
// Problem : A - αlphabet
// Contest : AtCoder - AtCoder Beginner Contest 171
// URL : https://atcoder.jp/contests/abc171/tasks/abc171_a
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cp-editor)

/*
 * author :Sadik Hassan(_sad_)
 *
*/
#include "bits/stdc++.h"
using namespace std;
using   ll =          long long;
using   ld =          long double;
#define nl            "\n"
#define pb            push_back
#define fi            first
#define se            second
#define MP            make_pair
#define PI            (acos(-1.0))
#define rep1(i,n)     for(int i=1;i<=n;i++)
#define rep(i,n)      for(int i=0;i<n;i++)
#define SZ(s)         (int)s.size()
#define all(x)        (x).begin(), (x).end()
#define rall(x)       (x).rbegin(), (x).rend()
#define w(t)          int t;cin>>t;while(t--)
#define TSU(s)        transform(s.begin(),s.end(),s.begin(),::toupper);
#define TSL(s)        transform(s.begin(),s.end(),s.begin(),::tolower);
#define _SAD()     	  ios::sync_with_stdio(0),cin.tie(0),cout.tie(0),cout<<fixed<<setprecision(15);

typedef vector<int> vi;
typedef vector<ll> vii;
typedef set<int> si;
typedef set<ll> sii;
/*---------------------------------------------------------------------*/

const int N = (int)2e5+5;
const int MOD =(int)1e9+7;
const ll INF = (ll)1e18+5;

int main()
{
    _SAD()
	char a;
	cin>>a;
	if(a>='A'&&a<='Z')cout<<"A";
	else cout<<"a";
    return 0;
}


Measured memory_bytes: 3604
#include<bits/stdc++.h>
using namespace std;
int n,a,b,c,d,delta,whole;
double nxt,half=0.5,maxhalf,lft;
int main(){
    scanf("%d%d%d%d%d",&n,&a,&b,&c,&d);
    nxt+=c+d;
    nxt/=2;
    half=nxt-c;
    maxhalf=half*(n-1);
    delta=abs(a-b);
    if(delta>(n-1)*nxt+maxhalf){printf("NO\n");return 0;}//超过最远距离
    whole=(int)(delta/nxt);
    lft=delta-nxt*whole;
    if(n&1){//n%2==1
        if(whole&1){
            lft=nxt-lft;
            if(lft<=maxhalf){
                printf("YES\n");
                return 0;}
            else{printf("NO\n");return 0;}}
        else{if(lft<=maxhalf){
                printf("YES\n");
                return 0;}
            else{printf("NO\n");return 0;}}}
    else{//n%2==0
        if(whole&1){
            if(lft<=maxhalf){
                printf("YES\n");return 0;}
            else{printf("NO\n");return 0;}}
        else{lft=nxt-lft;
            if(lft<=maxhalf){
                printf("YES\n");return 0;}
            else{printf("NO\n");return 0;}}}
    return 0;
}

Measured memory_bytes: 256
#K = int(input())
N,K = map(int,input().split())
#N = int(input())
#A = list(map(int,input().split()))
flag=True
c=N
if N%K==0:
    print(0)
    exit()
else:
    l=N//K
    N-=l*K
    while flag==True:

        N=abs(N-K)
        if c <= N:
            flag=False
        else:
            c=N
print(c)


Measured memory_bytes: 2940
i = int(input())
num = list(map(int, input().split()))
count = 0
while True:
  flag = False
  for k in range(i):
    if num[k] % 2 != 0:
      flag = True
  if flag:
    break
  for k in range(i):
    num[k] /= 2
  count += 1
print(count)

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

string a;
int main(){
	cin>>a;
	for(int i=0;i<=a.length();i++){
		if(a[i]=='C'){
			for(int j=i;j<=a.length();j++){
				if(a[j]=='F'){
					cout<<"Yes"<<endl;
					return 0;
				}
			}
		}
	}
	cout<<"No"<<endl;
	return 0;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
    public static void main(String args[]){

        Scanner sc = new Scanner(System.in);

        int h = sc.nextInt();
        int w = sc.nextInt();

        String str[] = new String[h];
        for (int i=0; i<h; i++){
            str[i] = sc.next();
        }

        int point[][] = new int[h][w];

        for (int i=0; i<h; i++){
            for (int j=0; j<w; j++){
                point[i][j] = str[i].charAt(j);
            }
        }

        int height[][] = new int[h][w];
        int width[][] = new int[h][w];


        for (int i=0; i<w; i++){
            for (int j=0; j<h; j++){
                if (point[j][i]=='.'){
                    int tmp = j;
                    while(true){
                        j++;
                        if (j>=h || point[j][i]=='#'){
                            int ans = j - tmp;
                            for (int k=tmp; k<j; k++){
                                height[k][i] = ans;
                            }
                            break;
                        }
                    }
                }
            }
        }

        for (int i=0; i<h; i++){
            for (int j=0; j<w; j++){
                if (point[i][j]=='.'){
                    int tmp = j;
                    while(true){
                        j++;
                        if (j>=w || point[i][j]=='#'){
                            int ans = j - tmp;
                            for (int k=tmp; k<j; k++){
                                width[i][k] = ans;
                            }
                            break;
                        }
                    }
                }
            }
        }

        int ans = 0;

        for (int i=0; i<h; i++){
            for (int j=0; j<w; j++){
                ans = Math.max(ans, (height[i][j]+width[i][j]));
            }
        }
        System.out.println(ans-1);

    }
}


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


int main(){int N; 
cin >> N;
int ans;
if(N % 2 == 1){
    ans = (N + 1) * (N - 1) / 4;
}
else ans = N * N / 4;

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

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define range(i, a, n) for(int (i) = (a); (i) < (n); (i)++)
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
using namespace std;
int n, k, s;
int dfs(int d, int pnum, int cs){
    if(cs + pnum >= s) return 0;
    if(d == k - 1){
        int cnum = s - cs;
        if(pnum < cnum and cnum <= n){
            return 1;
        }
    }

    int ret = 0;
    range(i, pnum + 1, n + 1){
        ret += dfs(d + 1, i, cs + i);
    }
    return ret;
}
int main(void){
    for(; cin >> n >> k >> s, n;){
        cout << dfs(0, 0, 0) << endl;
    }

    return 0;
}

Measured memory_bytes: 2968
#include <bits/stdc++.h>
 
using namespace std;
 
const int MOD=1e9+7;
const double PI=3.14159265359;
 
 

 
int main(){
    
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    
    int n,m;
    cin>>n>>m;
    
    vector<int> p(n+1);
    for(int i=1; i<=n; i++){
        cin>>p[i];
    }
    
    vector<vector<int>> gr(n+1);
    while(m--){
        int x,y;
        cin>>x>>y;
        gr[x].push_back(y);
        gr[y].push_back(x);
    }
    
    vector<int> cc(n+1,-1);
    
    for(int i=1; i<=n; i++){
        if(cc[i]==-1){
            queue<int> q;
            q.push(i);
            cc[i]=i;
            while(q.size()){
                int cur=q.front();
                q.pop();
                for(int j:gr[cur]){
                    if(cc[j]==-1){
                        cc[j]=i;
                        q.push(j);
                    }
                }
            }
        }
    }
    
    vector<vector<int>> gr2(n+1);
    
    for(int i=1; i<=n; i++){
        gr2[cc[i]].push_back(i);
    }
    
    int ans=0;
    for(int i=1; i<=n; i++){
        if(gr2[i].size()){
            //cout<<i<<endl;
            vector<int> vals(gr2[i].size()), vals2(gr2[i].size());
            for(int j=0; j<gr2[i].size(); j++){
                vals[j]=p[gr2[i][j]];
                //cout<<vals[j]<<","<<gr2[i][j]<<" ";
            }
            //cout<<endl;
            sort(vals.begin(), vals.end());
            auto it=set_intersection(vals.begin(), vals.end(), gr2[i].begin(), gr2[i].end(), vals2.begin());
            ans+=it-vals2.begin();
        }
    }
    
    cout<<ans;
    return 0;
    
}

Measured memory_bytes: 10104
from collections import deque
h, w = map(int, input().split())
chiz = [[] for _ in range(w)]
cnt = 0
for _ in range(h):
    tmp_list = input()
    for i in range(w):
        if tmp_list[i]=='.':
            cnt += 1
        chiz[i].append(tmp_list[i])
dq = deque([(0,0)])
chiz[0][0] = 0
while dq:
    i,j = dq.popleft()
    for e in [(1,0),(-1,0),(0,-1),(0,1)]:
        if i+e[0]>=0 and i+e[0]<w and j+e[1]>=0 and j+e[1]<h:
            if chiz[i+e[0]][j+e[1]] == '.':
                chiz[i+e[0]][j+e[1]] = chiz[i][j] + 1
                dq.append((i+e[0],j+e[1]))
try:
    print(cnt-chiz[w-1][h-1]-1)
except:
    print(-1)

Measured memory_bytes: 74268
#include <stdio.h>

int main(){
    int n, x, i, j, k, cnt;
    while(1){
        scanf("%d%d", &n, &x);
        if(n == 0 && x == 0) break;
        cnt = 0;
        for(i=1; i<=n; i++){
            for(j=i+1; j<=n; j++){
                for(k=j+1; k<=n; k++){
                    if(i+j+k == x) cnt++;
                }
            }
        }
        printf("%d\n", cnt);
    }
    return 0;
}


Measured memory_bytes: 2048
s = input()
r = 0
a = 0
b = len(s)-1
while a < b:
  if s[a] == s[b]:
    a += 1
    b -= 1
  elif s[a] == "x":
    r += 1
    a += 1
  elif s[b] == "x":
    r += 1
    b -= 1
  else:
    print(-1)
    exit()
print(r)


Measured memory_bytes: 3316
n,x = map(int,input().split())
L = [int(i) for i in input().split()]
cnt = 0
Sum = [0 for i in range(len(L)+1)]
for i in range(len(L)):
    Sum[i+1] = Sum[i]+L[i]
for i in range(len(Sum)):
    if Sum[i] > x:
        break
    else:
        cnt += 1
print(cnt)

Measured memory_bytes: 3064
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <queue>
using namespace std;

long long N;

int main()
{
    scanf("%lld",&N);
    long long a=sqrt(N);
    long long sum=a*a;
    printf("%lld",sum);
    return 0;
}


Measured memory_bytes: 256
import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    private static boolean debug = false;
    private static boolean elapsed = false;

    private static PrintWriter _out = new PrintWriter(System.out);
    private static PrintWriter _err = new PrintWriter(System.err);

    private void solve(Scanner sc) {
        int N = sc.nextInt();
        int M = sc.nextInt();
        int[] a = new int[M];
        int[] b = new int[M];
        int[] c = new int[M];
        for (int i = 0; i < M; ++i) {
            a[i] = sc.nextInt() - 1;
            b[i] = sc.nextInt() - 1;
            c[i] = sc.nextInt() * -1;
        }

        // based on ベルマンフォード法
        long[] dist = new long[N];
        Arrays.fill(dist, Long.MAX_VALUE);
        dist[0] = 0;

        for (int i = 0; i < N - 1; ++i) {
            for (int j = 0; j < M; ++j) {
                if (dist[a[j]] == Long.MAX_VALUE) {
                    continue;
                }

                if (dist[b[j]] > dist[a[j]] + c[j]) {
                    dist[b[j]] = dist[a[j]] + c[j];
                }
            }
        }

        long ans = -dist[N - 1];

        boolean[] neg = new boolean[N];
        for (int i = 0; i < N; ++i) {
            for (int j = 0; j < M; ++j) {
                if (dist[a[j]] == Long.MAX_VALUE) {
                    continue;
                }

                if (dist[b[j]] > dist[a[j]] + c[j]) {
                    dist[b[j]] = dist[a[j]] + c[j];
                    neg[b[j]] = true;
                }

                if (neg[a[j]]) {
                    neg[b[j]] = true;
                }
            }
        }

        if (neg[N - 1]) {
            _out.println("inf");
        } else {
            _out.println(ans);
        }
    }
    private static BigInteger C(long n, long r) {
        BigInteger res = BigInteger.ONE;
        for (long i = n; i > n - r; --i) {
            res = res.multiply(BigInteger.valueOf(i));
        }
        for (long i = r; i > 1; --i) {
            res = res.divide(BigInteger.valueOf(i));
        }
        return res;
    }
    private static BigInteger P(long n, long r) {
        BigInteger res = BigInteger.ONE;
        for (long i = n; i > n - r; --i) {
            res = res.multiply(BigInteger.valueOf(i));
        }
        return res;
    }
    /*
     * 10^10 > Integer.MAX_VALUE = 2147483647 > 10^9
     * 10^19 > Long.MAX_VALUE = 9223372036854775807L > 10^18
     */
    public static void main(String[] args) {
        long S = System.currentTimeMillis();

        Scanner sc = new Scanner(System.in);
        new Main().solve(sc);
        _out.flush();

        long G = System.currentTimeMillis();
        if (elapsed) {
            _err.println((G - S) + "ms");
        }
        _err.flush();
    }
}

Measured memory_bytes: 28304
import java.io.BufferedReader
import java.util.*
import kotlin.math.absoluteValue
import kotlin.math.min

fun main() = abc175c()

@OptIn(ExperimentalStdlibApi::class)
fun abc175c() = SimpleFastIOWithToken_abc175c().exec {
    val (x, moveCount, moveDist) = List(3) { readLong() }

    var currentPosition = x.absoluteValue
    var remainMoves = moveCount

    val moves = min(currentPosition / moveDist, moveCount)
    currentPosition -= moves * moveDist
    remainMoves -= moves

    val answer = if (remainMoves % 2 == 0L) currentPosition else moveDist - currentPosition

    println(answer)
}

// region kokokara template dayo (^o^)
@Suppress("unused", "HasPlatformType", "MemberVisibilityCanBePrivate", "ClassName")
private class SimpleFastIOWithToken_abc175c(private val separator: String = System.lineSeparator()) {
    private val br: BufferedReader = System.`in`.bufferedReader()
    private var st: StringTokenizer = StringTokenizer("")
    private val sb: StringBuilder = StringBuilder()
    private fun prepareNext() = run { if (!st.hasMoreTokens()) st = StringTokenizer(br.readLine()) }

    fun readString(): String = run { prepareNext() }.run { st.nextToken() }
    fun readInt(): Int = Integer.valueOf(readString())
    fun readLong(): Long = java.lang.Long.valueOf(readString())

    fun println(): Unit = run { sb.append(separator) }
    fun print(any: Any): Unit = run { sb.append(any) }
    fun println(any: Any): Unit = run { sb.append(any.toString() + separator) }

    fun exec(action: SimpleFastIOWithToken_abc175c.() -> Unit) {
        var t: Throwable? = null
        Thread(null, { action() }, "solve", 128 * 1024 * 1024)
            .apply { setUncaughtExceptionHandler { _, t1 -> t = t1 } }
            .apply { start() }.join()
        t?.let { throw it }
        kotlin.io.print(sb)
    }

    fun readLine(): Nothing = error("readLine is disabled.")
}
// endregion kokomade template dayo (^o^)


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

using namespace std;
using ll = long long int;
using ull = unsigned long long int;
#define vll vector<ll>

#define FOR(i, b, e) for(int i = (b); i < (e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define REV(v) reverse((v).begin(), (v).end())
#define IN(v) cin >> (v);
#define VIN(v, n) REP(i, n) cin >> v[i];
#define OUT(v) cout << (v) << endl;
#define YN(b) OUT((b) ? "YES" : "NO")
#define Yn(b) OUT((b) ? "Yes" : "No")
#define yn(b) OUT((b) ? "yes" : "no")

const ll MOD = 1000000007;

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

  ll s, w;
  cin >> s >> w;
  if (s <= w) cout << "unsafe" << endl;
  else cout << "safe" << endl;

  return 0;
}

Measured memory_bytes: 3640
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef unsigned long long ll;
typedef pair<ll, ll> pii;
#define X first
#define Y second
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll
// change type to pair & use counter for multi-set
typedef tree<
    int,
    null_type,
    less<int>,
    rb_tree_tag,
    tree_order_statistics_node_update>
    ordered_set;

void fastInOut();
const double EPS = 1e-8;
const int MOD = 998244353 ;
const int N = 1e2 + 5;

int n, w, Ws[N], Vs[N];
ll dp[N][100005];

ll solve(int i, int v)
{
    if(v < 0) return 1e12;
    if(i == n) {
        if(v == 0) return 0;
        else return 1e12;
    }
    ll &res = dp[i][v];
    if(~res) return res;
    res = solve(i+1, v);
    res = min(res, solve(i+1, v-Vs[i]) + Ws[i]);
    return res;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
    fastInOut();
    cin >> n >> w;
    for(int i=0; i<n; ++i)
        cin >> Ws[i] >> Vs[i];
    memset(dp, -1, sizeof(dp));
    ll ans = 0;
    for(int i=0; i<100005; ++i)
        ans = (solve(0, i) > w) ? ans : i;
    cout << ans << '\n';
    return 0;
}

void fastInOut() {
    ios_base::sync_with_stdio(0);
    cin.tie(NULL), cout.tie(NULL);
}


Measured memory_bytes: 82304
#include <bits/stdc++.h>
using namespace std;
#define rep(i, m, n) for (int i = m; i < n; ++i)
#define rem(i, m, n) for (int i = m; i > n; --i)
typedef long long ll;
#define mod 1000000007

string solve() {
  string S; cin >> S;
  bool ym = false;
  bool my = false;
  int L = stoi(S.substr(0, 2));
  int R = stoi(S.substr(2, 4));
  if(1 <= R && R <= 12) ym = true;
  if(1 <= L && L <= 12) my = true;
  if(ym && my) return "AMBIGUOUS";
  if(!ym && !my) return "NA";
  if(ym) return "YYMM";
  else return "MMYY";
}

int main() {
  cout << solve() << endl;
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
#define INF 1000000
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, 1, -1};
using namespace std;
int main()
{
	int x;
	cin >> x;
	if(x == 1)
	{
		cout << 1 << endl;
		return 0;
	}
	int b = 2;
	int ans = 0;
	int y;
	while(b <= x)
	{
		int p = 2;
		while(1)
		{
			int tmp = pow(b, p);
			if(tmp > x)
				break;
			y = tmp;
			p++;
		}
		ans = max(ans, y);
		b++;
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
/**
*    created: 15.06.2020 10:59:12
**/
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
using namespace std;

signed main() {

    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    int m = 1000003;
    vector<int> lst(m);
    for (int x : a) {
        if (lst[x] != 0) lst[x]++;
        else {
            for (int j = x; j <= m; j+=x) lst[j]++;
        }
    }
    int cnt = 0;
    for (auto itr = a.begin(); itr != a.end(); itr++) {
        if (lst[*itr] == 1) cnt++;
    }
    cout << cnt << endl;
    return 0;
}

Measured memory_bytes: 12628
from collections import defaultdict as dd
n = int(input())
a = [int(i) for i in input().split()]
if sum(a):
    if n%3: f = 0
    else:
        d,x = dd(int),[]
        for i in a: d[i]+=1
        for i in d.keys(): x.append(i)
        if len(d)==2 and d[0]==n//3: f = 1
        elif len(d)==3 and x[0]^x[1]^x[2]==0 and d[x[0]]==d[x[1]]==d[x[2]]: f = 1
        else: f = 0
else: f = 1
if f: print("Yes")
else: print("No")

Measured memory_bytes: 14468
package main

import (
	"fmt"
	"math"
)

func main() {
	// 入力の受け取り
	var N, K int
	fmt.Scanf("%d%d", &N, &K)
	xs := make([]int, N)

	for i := 0; i < N; i++ {
		var c int
		fmt.Scanf("%d", &c)
		xs[i] = c
	}

	// 処理
	minDist := math.MaxInt64
	for l := 0; l < N-K+1; l++ {
		r := l + K - 1
		dist := abs(xs[l]-xs[r]) + min(abs(xs[l]), abs(xs[r]))
		minDist = min(minDist, dist)
	}

	fmt.Printf("%d", minDist)
}

func min(x int, y int) int {
	if x < y {
		return x
	} else {
		return y
	}
}

func abs(x int) int {
	if x < 0 {
		return -x
	} else {
		return x
	}
}


Measured memory_bytes: 5504
import math

n, y = [int(i) for i in input().split()]

flag = 0
for i in range(n+1):
    for j in range(n+1-i):
        if( y == 10000*i + 5000*j + 1000*(n-i-j)):
            flag = 1
            break
    if(flag == 1):
        break

if(flag == 1):
    print(i,j,n-i-j)
else:
    print("-1 -1 -1")

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

int main() {
  double a, b;
  double t1, t2;

  cin >> a >> b;
  t1 = (b-a) / 2;
  
  if ( t1 > 0 ) t2 = t1 - 180;
  else t2 = t1 + 180;

  //cout << t1 << " " << t2 << endl;
  double ans = a;
  if ( abs(t1) > abs(t2) ) ans += t2;
  else ans += t1;
  if ( ans < 0 ) ans += 360;
  if ( ans >= 360 ) ans -= 360;
  printf("%.10lf\n", ans);
}

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

struct state
{
  int d, n, k;
  state(int a, int b, int c)
    : d(a), n(b), k(c)
  {}
  bool operator<(const state& s) const
  {
    return d > s.d;
  }
};

int main()
{
  int N;
  while (cin >> N && N != 0) {
    vector<pair<int,int> > balloons(N+1);
    balloons[0] = make_pair(0, 0);
    for (int i = 1; i <= N; i++) {
      cin >> balloons[i].first >> balloons[i].second;
    }

    static const int INF = 10000000;
    vector<vector<int> > dist(N+1, vector<int>(4, INF));
    priority_queue<state> q;
    q.push(state(0, 0, 0));
    dist[0][0] = 0;
    int ans = 0;
    while (!q.empty()) {
      const int d = q.top().d;
      const int n = q.top().n;
      const int k = q.top().k;
      q.pop();
      ans = max(ans, n);
      const int t = balloons[n].second;
      if (n == N) {
        cout << "OK " << d+balloons[n].first << endl;
        goto NEXT;
      }

      if (k < 3) {
        // go to (n+1)-th balloon directly
        const int dd = abs(balloons[n+1].first - balloons[n].first);
        const int tt = dd * (k+1);
        if (t+tt <= balloons[n+1].second && d+dd < dist[n+1][k+1]) {
          dist[n+1][k+1] = d+dd;
          q.push(state(d+dd, n+1, k+1));
        }
      }
      // go home and go to (n+1)-th balloon
      const int dd = balloons[n].first + balloons[n+1].first;
      const int tt = balloons[n].first*(k+1) + balloons[n+1].first;
      if (t+tt <= balloons[n+1].second && d+dd < dist[n+1][1]) {
        dist[n+1][1] = d+dd;
        q.push(state(d+dd, n+1, 1));
      }
    }
    cout << "NG " << ans+1 << endl;
NEXT:
    ;
  }
  return 0;
}

Measured memory_bytes: 0
import sys

read = sys.stdin.readline

N = int(read())
A = list(map(int, read().split()))
B = list(map(int, read().split()))

if sum(A) < sum(B):
    print(-1)
    exit()

d = [i - j for i, j in zip(A, B)]
d.sort()
if d[0] >= 0:
    print(0)
    exit()

n = 0
below = 0
for i in d:
    if i < 0:
        below -= i
        n += 1
    else:
        break

tmp = 0
for i in d[::-1]:
    tmp += i
    n += 1
    if tmp > below:
        print(n)
        exit()


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

int main(){
    int num=0;
    int n;
    cin>>n;
    bool flag=false;
    for(int i=0; i<n; ++i){
        int x,y;
        cin>>x>>y;
        if(x==y) ++num;
        else num=0;
        if(num==3) flag=true;
    }
    if(flag) cout<<"Yes\n";
    else cout<<"No\n";
}

Measured memory_bytes: 3584
#include<bits/stdc++.h>
using namespace std;
///Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout<<#var<<" "<<var<<endl;
#define co(q) cout<<q<<endl;
#define all(q) (q).begin(),(q).end()
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define MODADD(ADD_X,ADD_Y)  (ADD_X+ADD_Y)%MOD;
#define MODSUB(SUB_X,SUB_Y)  (SUB_X-SUB_Y)+MOD)%MOD;
#define MODMUL(MUL_X,MUL_Y)  (MUL_X*MUL_Y)%MOD;
#define LCM(LCM_X,LCM_Y) (LCM_X*LCM_Y)/__gcd(LCM_X,LCM_Y);
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9+7;
const int MAX = 1e6;
int dx[]= {1,0,-1,0,1,-1,1,-1};
int dy[]= {0,1,0,-1,1,-1,-1,1};
ll cost[MAX];
vector<int>graph[MAX];
void dfs(int src,int par)
{
    for(auto i:graph[src])
    {
        if(i==par)
            continue;
        cost[i]+=cost[src];
        dfs(i,src);
    }
}
int main()
{
    FastRead
    //freopen("output.txt", "w", stdout);
    int n,q;
    cin>>n>>q;
    for(int i=1; i<n; i++)
    {
        int u,v;
        cin>>u>>v;
        graph[u].push_back(v);
        graph[v].push_back(u);
    }
    while(q--)
    {
        int a,b;
        cin>>a>>b;
        cost[a]+=b;
    }
    dfs(1,0);
    for(int i=1; i<=n; i++)
        cout<<cost[i]<<" ";


    return 0;
}


Measured memory_bytes: 47300
N = int(input())
B = list(map(int,input().split()))

A=[0]*N
A[0] = B[0]

for i in range(1,N):
    if i<=N-2:
        A[i] = min(B[i-1],B[i])
    else:
        A[i] = B[i-1]

print(sum(A))


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

const int N = 2e5 + 10;

int n, k;
int a[N], b[N];

bool can(int d) {
  int diff = 0;
  for (int i = 0; i < n; i++) {
    b[i] = a[i] % d;
    diff += d - b[i];
  }

  sort(b, b + n);

  int sum = 0;
  for (int i = 0; i < n; i++) {
    diff -= d - b[i];
    diff -= b[i];
    sum += b[i];
    if (!diff)
      return sum <= k;
  }

  assert(false);
}

int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", a + i);

  int sum = accumulate(a, a + n, 0);
  int ans = -1;
  for (int i = 1; i * i <= sum; i++) {
    if (sum % i == 0) {
      if (can(i))
        ans = max(ans, i);
      if (can(sum / i))
        ans = max(ans, sum / i);
    }
  }
  
  printf("%d\n", ans);
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
 
#define rep(i,n) for(ll i=0;i<n;++i)
#define all_map(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++)
#define ALL(a) (a).begin(),(a).end()

using namespace std;
using ll  = long long;
using pll = pair<ll, ll>;

const ll MOD  = 1e9 + 7;
const ll LINF = 1LL << 62;
const int INF = 1e9 + 7;

int main(){
    ll n;
    cin >> n;
    
    vector<ll> a(n);
    ll cnt = 0;
    ll sum = 0;
    ll low = INF;
    rep(i, n){
        ll a;
        cin >> a;
        if(a < 0){
            cnt++;
            a *= -1;
        }
        sum += a;
        low = min(low, a);
    }

    if(cnt&1)sum -= 2*low;
    
    cout << sum << endl;
}

Measured memory_bytes: 3956
#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n, k;
	cin>>n>>k;
	unordered_map<int, bool> um;
	for(int i =0;i<k;i++)
	{
		int a;
		cin>>a;
		um[a] = true;
	}
	while(true)
	{
		bool ok = true;
		int pom = n;
		while(pom > 0)
		{
			if(um[pom%10] == true)
			{
				ok = false;
				break;
			}
			pom/=10;
		}
		if(ok)
			break;
		else
			n++;
	}
	cout<<n<<endl;
	return 0;
}

Measured memory_bytes: 256
N = int(input())
barr = list(map(int,input().split()))
cntb = []
for i in range(N):
    cntb.append([barr[i],i])
task = []
ok = True
for n in range(N):
    for i in range(N)[::-1]:
        if cntb[i][1]+1 == cntb[i][0]:
            task.append(cntb[i][0])
            cntb[i][0] = -1
            break
        elif cntb[i][0] > 0 and cntb[i][1] + 1 < cntb[i][0]:
            ok = False
        else:
            cntb[i][1] -= 1

if ok:
    for i in range(N)[::-1]:
        print(task[i])
else:
    print(-1)

Measured memory_bytes: 3064
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
       Scanner scn = new Scanner(System.in);
       String s = scn.nextLine();
       if(s.equals("Sunny")){
          System.out.println("Cloudy");
       } else if(s.equals("Rainy")){
          System.out.println("Sunny");
       } else {
          System.out.println("Rainy");
       }
    }
}


Measured memory_bytes: 21460
#include<stdio.h>
int main(void){
	int S;
	int h,m,s;
	scanf("%d",&S);
	s = S % 60;
	h = S / 3600;
	m = S / 60 - (h * 60);
	printf("%d:%d:%d\n",h,m,s);
	return 0;
}


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

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define ALL(c) (c).begin(), (c).end()

typedef int Weight;
struct Edge {
	int src, dst;
	Weight weight;
	Edge(int src, int dst, Weight weight) :
		src(src), dst(dst), weight(weight) {
	}
};
bool operator < (const Edge &e, const Edge &f) {
	return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
	e.src != f.src ? e.src < f.src : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void visit(Graph& g, int a, vector<int>& path) {
	while(!g[a].empty()) {
		int b = g[a].back().dst;
		g[a].pop_back();
		visit(g, b, path);
	}
	path.push_back(a);
}
bool eulerPath(Graph g, int s, vector<int> &path) {
	int n = g.size(), m = 0;
	vector<int> deg(n);
	REP(u, n) {
		m += g[u].size();
		for(auto e : g[u]) --deg[e.dst]; //  in-deg
		deg[u] += g[u].size();      // out-deg
	}
	int k = n - count(ALL(deg), 0);
	if(k == 0) {
		path.clear();
		visit(g, s, path);
		reverse(ALL(path));
		return path.size() == m + 1;
	}
	return false;
}

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

	int n;
	while(cin >> n, n) {
		Graph G(26);
		for(int i = 0; i < n; i++) {
			string s;
			cin >> s;
			int  u = s.front() - 'a', v = s.back() - 'a';
			G[u].push_back({ u, v, 1 });
		}

		bool res = false;
		for(int i = 0; i < 26; i++) {
			vector<int> path;
			if(eulerPath(G, i, path)) {
				res = true;
				break;
			}
		}

		if(res) {
			cout << "OK" << endl;
		}
		else {
			cout << "NG" << endl;
		}
	}
}

Measured memory_bytes: 3184
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>

using namespace std;

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size()) //unsignedのサイズをint型に変換

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

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

const int MOD = 1e9+7;

int nth_bit(LL num, int n)
{
    return (num >> n) & 1;
}

//ここから書き始める
int main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);	
    int D,G;
    cin >> D >> G;
    VI p(D);
    VI c(D);
    REP(i,D)
    {
        cin >> p[i] >> c[i];
    }
    VI s(D);
    REP(i,D)
    {
        s[i] = (i+1) * 100 * p[i] + c[i];
    }
    int res = 10000000;

    REP(i,pow(2,D))
    {
        int sumpoint = 0;
        int sumcount = 0; 
        bool flag = 0;
        int incom;
        REP(j,D)
        {
            if(nth_bit(i,j) == 1)
            {
                sumpoint += s[D-1-j];
                sumcount += p[D-1-j];
            }
            else
            {
                if(!flag)
                {
                    incom = j;
                    flag = 1;
                }
            }
        }

        if(sumpoint >= G)
        {
            if(res > sumcount) res = sumcount;
        }
        else
        {
            int add = ((G - sumpoint)+((D-incom)*100) - 1) / ((D - incom)*100);
            if(add < p[D-1-incom])
            {
                sumcount += add;
                if(res > sumcount) res = sumcount;
            }
        }
    }
    cout << res << endl;

}


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

using namespace std;
#define rep(i, n) for(int i = 0 ; i < (int)(n) ; i++)
typedef long long ll;
typedef long double Double;

#ifndef INPUTS_DIR
#define INPUTS_DIR "./"
#endif


ll next(pair<ll, ll> s, ll pos) {
    if (s.first <= pos && pos <= s.second) return pos;
    if (s.second < pos) return s.second;
    return s.first;
}

void show(pair<ll, ll> s) {
    cout << s.first << " " << s.second << "<" << endl;
}

ll subSolve(vector<pair<ll, ll> > segs, int fst) {
    for (auto &s : segs) assert(s.first <= s.second);
    int N = segs.size();

    priority_queue<pair<ll, int> > lQ, rQ;

    for (int i = 0; i < N; i++) {
        lQ.push({segs[i].first, i});
        rQ.push({-segs[i].second, i});
    }
    vector<bool> dead(N);
    ll ans = 0;

    ll pos = 0;

    for (int i = 0; i < N; i++) {
        while (dead[lQ.top().second]) lQ.pop();
        while (dead[rQ.top().second]) rQ.pop();
        if ((i == 0 && fst) ||
            (i != 0 && abs(next(segs[lQ.top().second], pos) - pos) < abs(next(segs[rQ.top().second], pos) - pos))
                ) {
            ans += abs(next(segs[rQ.top().second], pos) - pos);
            pos = next(segs[rQ.top().second], pos);
//            show(segs[rQ.top().second]);
            dead[rQ.top().second] = true;
            rQ.pop();
        } else {
            ans += abs(next(segs[lQ.top().second], pos) - pos);
            pos = next(segs[lQ.top().second], pos);
//            show(segs[lQ.top().second]);
            dead[lQ.top().second] = true;
            lQ.pop();
        }
    }
//    cout << ans + abs(pos) << endl;
    return ans + abs(pos);
}

void solve(ll N, vector<ll> L, vector<ll> R) {
    vector<pair<ll, ll> > segs1;
//    vector<pair<ll, ll> > segs2;
    for (int i = 0; i < N; i++) {
        segs1.push_back({L[i], R[i]});
//        segs2.push_back({-R[i], -L[i]});
    }
    ll ans = -1e18;

    ans = max(subSolve(segs1, false), ans);
    ans = max(subSolve(segs1, true), ans);
    cout << ans << endl;

}


void main_(istream &cin) {
    ll N;
    cin >> N;
    vector<ll> R(N - 1 + 1);
    vector<ll> L(N - 1 + 1);
    for (int i = 0; i <= N - 1; i++) {
        cin >> L[i];
        cin >> R[i];
    }
    solve(N, L, R);
}

int main() {
#ifdef INTELLIJ
    ifstream ifs(string(INPUTS_DIR) + "in_1.txt");
    if (ifs.is_open()) {
        main_(ifs);
        ifs.close();
    } else {
        cerr << "Error: no input." << endl;
    }
#else
    ios::sync_with_stdio(false);
    cin.tie(0);
    main_(cin);
#endif
}





Measured memory_bytes: 11504
// finish date: 2019/6/24

#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)
typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<vector<vector<int>>> vvvi;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef vector<vector<vector<double>>> vvvd;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, pair<int, int>> plii;
const int bigmod = 1000000007;
const int INF = 1000000007;
const long long INFll = 100000000000000000;


int main() {
    int N;
    cin >> N;
    vi A(N);
    ll ans = 0;
    map<int, int> mp;
    rep(i, N) {
        cin >> A[i];
        mp[A[i]]++;
        ans += A[i];
    }
    int Q;
    cin >> Q;
    vi B(Q), C(Q);
    rep(i, Q) {
        cin >> B[i] >> C[i];
        ans += (C[i] - B[i]) * mp[B[i]];
        mp[C[i]] += mp[B[i]];
        mp[B[i]] = 0;
        cout << ans << endl;
    }


    return 0;
}


Measured memory_bytes: 8804
#define LENGTH 11
#include <stdio.h>

int check(char);

int main() {
  int i, count = 0, ans = 0;
  char str[LENGTH];
  scanf("%s", str);
  for(i = 0; i < LENGTH; i++) {
    if(check(str[i]) == 0) {
      count++;
    } else {
      ans = (count > ans)? count : ans;
      count = 0;
    }
  }
  printf("%d\n", ans);
  return 0;
}

int check(char ch) {
  switch(ch) {
    case 'A':
    case 'T':
    case 'C':
    case 'G':
      return 0;
    default:
      return 1;
  }
}


Measured memory_bytes: 128
d = { "N" => [0, 1],
      "E" => [1, 0],
      "W" => [-1, 0],
      "S" => [0, -1] }
 
while true do
  n = gets.to_i
  break if n == 0
 
  field = Array.new(21, 0).map{ Array.new(21, 0) }
  gems = []
  n.times do
    gems << gets.split.map(&:to_i)
  end
 
  x, y = 10, 10
  field[x][y] = 1
  gets.to_i.times do
    dir, dis = gets.split
    dis.to_i.times do
      x += d[dir][0]
      y += d[dir][1]
      field[x][y] = 1
    end
  end
 
  count = 0
  gems.each do |gem|
    if field[gem[0]][gem[1]] == 1
      count += 1
    else
      break
    end
  end
 
  puts count == n ? "Yes" : "No"
end

Measured memory_bytes: 9188
#include <cstdio>
#include <algorithm>

using namespace std;

long long n, k;

int main(void) {
  scanf("%lld%lld", &n, &k);
  if (n == 0) {
    printf("%lld\n", n);
  } else {
    printf("%lld\n", min(n%k, abs(n%k-k)));
  }
  return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second
#define pb push_back
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<60) - 1;
const int mod = 1000000007;

void solve() {
    int n, k;
    cin >> n >> k;
    vector<int> A(n);
    rep(i, n) cin >> A[i];
    sort(all(A), [&](int a, int b) {return abs(a) > abs(b);});
    ll ans = 1LL;
    int sign = 1;
    rep(i, k) {
        if (A[i] < 0) sign *= -1;
        ans *= A[i];
        ans %= mod;
    }
    if (sign > 0) {
        ans += mod;
        ans %= mod;
        cout << ans << endl;
        return;
    }
    ll P = LINF, M = LINF;
    for(int i = k; i < n; i++) {
        if(A[i] >= 0) {
            P = A[i];
            break;
        }
    }
    for(int i = k; i < n; i++) {
        if(A[i] < 0) {
            M = A[i];
            break;
        }
    }
    bool flag = 0;
    ll p = LINF, m = LINF;
    for(int i = k - 1; i >= 0; i--) {
        if (A[i] >= 0) {
            p = A[i];
            break;
        }
    }
    for(int i = k - 1; i >= 0; i--) {
        if (A[i] < 0) {
            m = A[i];
            break;
        }
    }
    bool f1 = p == LINF || M == LINF;
    bool f2 = m == LINF || P == LINF;
    if (f1 && f2) flag = 1;
    if (flag) {
        ll mans = 1LL;
        reverse(all(A));
        rep(i, k) {
            mans *= A[i];
            mans %= mod;
        }
        mans += mod;
        mans %= mod;
        cout << mans << endl;
        return;
    }
    int x, y;
    if (!f1 && !f2) {
        if (M * m > P * p) {
            x = p;
            y = M;
        } else {
            x = m;
            y = P;
        }
    } else if (!f1) {
        x = p;
        y = M;
    } else {
        x = m;
        y = P;
    }
    ans = 1LL;
    bool f = 1;
    rep(i, k) {
        if (A[i] == x && f) {
            f = 0;
            continue;
        }
        ans *= A[i];
        ans %= mod;
    }
    ans *= y;
    ans %= mod;
    if (ans < 0) ans += mod;
    cout << ans << endl;
}

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

Measured memory_bytes: 3992
s = int(input())
mod = 10**9+7
ans = 0
for n in range(1, s//3 + 1):
  left = s - n * 3
  # left -> n
  # left + n - 1 choose n-1
  count = 1
  for i in range(1, n):
    count *= left + i
    count %= mod
  for i in range(1, n):
    count *= pow(i, -1, mod)
    count %= mod
  ans += count
  ans %= mod
print(ans)
  
  

Measured memory_bytes: 9184
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    int i,j;
    cin >> n;
    vector<string> w(n);
    for(i=0;i<n;i++)cin >> w.at(i);
    for(i=0;i<n;i++){
        for(j=1;i+j<n;j++){
            if(w.at(i)==w.at(i+j)){
                cout << "No" << endl;
                return 0;
            }
        }
    }
    for(i=0;i<(n-1);i++){
        if((w.at(i)).at(w.at(i).size()-1)!=(w.at(i+1).at(0))){
            cout << "No" << endl;
            return 0;
        } 
    }
    cout << "Yes" << endl;
    return 0;
}

Measured memory_bytes: 256
IDENTIFICATION DIVISION.
        PROGRAM-ID. AtCoder.
      
        ENVIRONMENT DIVISION.
      
        DATA DIVISION.
        WORKING-STORAGE SECTION.
        01 INP PIC X(100).
        01 A PIC 9(15).
        01 B PIC 9(15).
        01 C PIC 9(15).
      	01 D PIC 9(15).
      	01 E PIC 9(15).
        01 F PIC 9(15).
      	01 t PIC 9(15).
	    01 temp1 PIC 9(15).
        01 temp2 PIC 9(15).
        01 N PIC 9(15).
      
        PROCEDURE DIVISION.
      	MAIN.
			ACCEPT INP.
      		IF INP(1:1) = "S"
      			DISPLAY "Cloudy"
      		END-IF.
      		IF INP(1:1) = "C"
      			DISPLAY "Rainy"
      		END-IF.
      		IF INP(1:1) = "R"
      			DISPLAY "Sunny"
      		END-IF.
		STOP RUN.

Measured memory_bytes: 384
D, N = map(int, input().split())

if D == 0:
    if N != 100:
        print(N)
    else:
        print(101)
elif D == 1:
    if N != 100:
        print(100*N)
    else:
        print(100*(N+1))
else:
    if N != 100:
        print(10000*N)
    else:
        print(10000*(N+1))


Measured memory_bytes: 3060