akhauriyash/Code-Regression huggingface dataset:
from collections import defaultdict
from heapq import heappop, heappush

class Graph(object):
    """
    隣接リストによる有向グラフ
    """
    def __init__(self):
        self.graph = defaultdict(list)

    def __len__(self):
        return len(self.graph)

    def add_edge(self, src, dst, weight=1):
        self.graph[src].append((dst, weight))

    def get_nodes(self):
        return self.graph.keys()

class Dijkstra(object):
    """
    ダイクストラ法（二分ヒープ）による最短経路探索
    計算量: O((E+V)logV)
    """
    def __init__(self, graph, start):
        self.g = graph.graph

        # startノードからの最短距離
        # startノードは0, それ以外は無限大で初期化
        self.dist = defaultdict(lambda: float('inf'))
        self.dist[start] = 0

        # 最短経路での1つ前のノード
        self.prev = defaultdict(lambda: None)

        # startノードをキューに入れる
        self.Q = []
        heappush(self.Q, (self.dist[start], start))

        while self.Q:
            # 優先度（距離）が最小であるキューを取り出す
            dist_u, u = heappop(self.Q)
            if self.dist[u] < dist_u:
                continue
            for v, weight in self.g[u]:
                alt = dist_u + weight
                if self.dist[v] > alt:
                    self.dist[v] = alt
                    self.prev[v] = u
                    heappush(self.Q, (alt, v))

    def shortest_distance(self):
        """
        startノードからgoalノードまでの最短距離
        """
        return self.dist

    def shortest_path(self, goal):
        """
        startノードからgoalノードまでの最短経路
        """
        path = []
        node = goal
        while node is not None:
            path.append(node)
            node = self.prev[node]
        return path[::-1]

n,m,r=map(int,input().split())
inputs = [[int(j) for j in input().split()]for i in range(m)]

g = Graph()
for src, dst, weight in inputs:
    g.add_edge(src, dst, weight)

d = Dijkstra(g,r)
ans=d.shortest_distance()
for i in range(n):
    if ans[i]==float("inf"):
        print("INF")
    else:
        print(int(ans[i]))


Measured memory_bytes: 176104
#include <bits/stdc++.h>
using namespace std;
#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++)

int main() {
  string s;
  cin >> s;
  if(s[0]!=s[1]||s[1]!=s[2]||s[0]!=s[2])cout << "Yes" << endl;
  else cout << "No" << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <set>
#include <vector>
using namespace std;
#define ll long long
int main(){

int a,b;
    cin>>a>>b;
    if(a < b){
        cout<<"0"<<endl;
        return 0;
    }
    cout<<"10"<<endl;

    return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <sstream>
#include <functional>
#include <map>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <list>
#include <numeric>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> P;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const ll INF = 1LL<<29;
const ll mod = 1e9+7;
#define rep(i,n) for(int (i)=0;(i)<(ll)(n);++(i))
#define repd(i,n,d) for(ll (i)=0;(i)<(ll)(n);(i)+=(d))
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset((m),(v),sizeof(m))
#define chmin(X,Y) ((X)>(Y)?X=(Y),true:false)
#define chmax(X,Y) ((X)<(Y)?X=(Y),true:false)
#define fst first
#define snd second
#define UNIQUE(x) (x).erase(unique(all(x)),(x).end())
template<class T> ostream &operator<<(ostream &os, const vector<T> &v){int n=v.size();rep(i,n)os<<v[i]<<(i==n-1?"":" ");return os;}

int main(){
	int n, m;
	cin>>n>>m;
	ll t = m*1900+(n-m)*100;
	cout<<(t<<m)<<endl;
	return 0;
}


Measured memory_bytes: 256
using System;
using System.Linq;

class Program
{
    static void Main(string[] args) {
        int[] s = Console.ReadLine().Split().Select(int.Parse).ToArray();
        Console.WriteLine(s[2]-s[0]>=0 && s[2]-s[0]<=s[1] ? "YES" : "NO");
    }
}

Measured memory_bytes: 11348
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i = 0; i< (n); i++)
using ll = long long;
using P = pair<int, int>;
int main()
{
    while(true){
        string ans;
        int m,f,r;
        cin >> m >> f >> r;
        if(m == -1 && f == -1 && r == -1) break;
        if(m ==-1 || f == -1) ans = "F";
        else if(m+f >= 80) ans = "A";
        else if(m+f >= 65) ans = "B";
        else if(m+f >= 50) ans = "C";
        else if(m+f >= 30){
            ans = "D";
            if(r >= 50) ans = "C";
        }
        else ans = "F";
        cout << ans << endl;
    }
    
return 0;
}


Measured memory_bytes: 3116
n,m = map(int,input().split())
a = [input() for _ in range(n)]
b = [input() for _ in range(m)]

for i in range(n-m+1):
    for j in range(n-m+1):
        if a[i][j] == b[0][0]:
            flag = 0
            for u in range(m):
                for v in range(m):
                    if a[i+u][j+v] != b[u][v]:
                        flag = -1
                        break
                if flag == -1:
                    break
            if flag == 0:
                print('Yes')
                exit()
print('No')

Measured memory_bytes: 3064
//#include <bits/stdc++.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>

using namespace std;

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


    int N;

    string S;

    cin >> N >> S;

    int count = 1;

    char change = S[0];

    for(int i = 1; i < N; i++) {
        if(S[i] != change) {
            count++;
            change = S[i];
        }
    }

    cout << count << endl;
}

Measured memory_bytes: 512
#include <stdio.h>

int main( void )
{
	long long A,B ;
	scanf("%ld %ld", &A, &B ) ;
   	long long M[2][4] = {{B^0,B^A,B^1,B^(A-1)},{A,1,A-1,0}};
	long long a = M[B%2][(B-A)%4] ;
	printf("%ld\n", a ) ;
	return 0 ;
}


Measured memory_bytes: 128
import sys
from fractions import gcd

N = int(input())
A = list(map(int, input().split()))

if N == 2:
  print(max(A))
  sys.exit()

g = gcd(A[0], A[1])
X = set([A[0], A[1]])

for a in A[2:]:
  XX = set([g])
  g = gcd(g, a)
  for x in X:
    XX.add(gcd(x, a))
  X = XX

print(max(X))

Measured memory_bytes: 16132
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
typedef pair<int, int> P;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int H, W, D;
  cin >> H >> W >> D;
  int A[H][W];
  P num[H*W+1];
  FOR(i,0,H) {
    FOR(j,0,W) {
      cin >> A[i][j];
      num[A[i][j]] = P(i, j);
    }
  }
  int cost[D+1][(H*W+D-1)/D+1];
  FOR(i,1,D+1) {
    int x = i;
    cost[i][0] = 0;
    int cnt = 1;
    while(x + D <= H * W) {
      // x -> x + D
      cost[i][cnt] = cost[i][cnt-1] + abs(num[x].first-num[x+D].first) + abs(num[x].second-num[x+D].second);
      x += D;
      cnt++;
    }
  }
  int Q; cin >> Q;
  
  FOR(i,0,Q) {
    int L, R; cin >> L >> R;
    int x = (L - 1) / D;
    int st = L - x * D;
    //cout << x << st << (R-st)/D<< endl;
    cout << cost[st][(R-st)/D] - cost[st][x] << endl;
  }
}

Measured memory_bytes: 2560
import java.util.Scanner;

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

   if(s.equals("ABC")){
     System.out.println("ARC");
   }else if(s.equals("ARC")){
     System.out.println("ABC");
   }
 
 }
}

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

#define SORT(x) sort((x).begin(),(x).end())
#define RSORT(x) sort((x).begin(),(x).end(),greater<int>())
#define SZ(x) ((int)(x).size())
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) " (L" << __LINE__ << ") " << __FILE__ << endl;

typedef long long ll;
const int mod = 1e9+7;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;

void solve()
{
  ll n; cin>>n;
  ll ans=0;
  ll tmp=n;
  for (ll i=1; (n-i)/i>1e7; i++) {
    ll p = n-i;
    if (p%i==0) {
      ans += p/i;
    }
    tmp = p/i;
  }
  for (ll i=tmp-1; i>=2; i--) {
    ll d=n/i;
    if(d == n%i) {
      ans += i;
      i = n/(d+1);
    }
  }
  cout << ans << endl;
}

int main()
{
  solve();
  return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<utility>
#include<map>
#include<set>
#include<queue>
#include<math.h>
using namespace std;
#define N (1000000000+7)
#define INF 1e16
typedef pair<int,int> P;
typedef long long ll;

ll a[100010];

int main(void){
    int n;
    cin>>n;
    if(n%2==1)printf("%.7lf\n",(double)(n-(n-1)/2)/n);
    else printf("%.7lf\n",(double)(n/2)/n);
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\n'
using namespace std;
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

using ll = long long;
using ull = unsigned long long;
const int INF = 1e9;
// const int MOD = 1e9 + 7;

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    set<int> s;
    s.insert(a);
    s.insert(b);
    s.insert(c);
    cout << (int)s.size() << endl;
}


Measured memory_bytes: 256
import numpy as np
from numba import njit
  
# @njit('(i4[::1],)', cache=True)
def solve(A):
    count = np.zeros(10**6 + 10, np.int32)
    for x in A:
        if count[x] > 1:
            continue
        count[::x] += 1
    ret = 0
    for x in A:
        ret += count[x] == 1
    return ret

N = int(input())
A = np.array(list(map(int,input().split())), np.int32)
 
print(solve(A))


Measured memory_bytes: 114896
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author
 */

#include <iostream>
#include <fstream>

#include <bits/stdc++.h>
#define rep(i,n) for (int i= 0;i<(n);++i)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

class BEasyLinearProgramming {
public:
	void solve(std::istream& cin, std::ostream& cout) {
        ll a,b,c,k;
        cin >>a>>b>>c>>k;

        ll sum = k;
        ll count =0 ;
        if(sum<=a){
            cout <<k<<endl;
        }

        else {
            count += a;
            sum -= a;
            if (sum <= b) {
                cout << count << endl;

            }
            else {
                sum -= b;

                if (sum >= c) {
                    count -= c;
                    cout << count << endl;

                } else {
                    count -= sum;
                    cout << count << endl;

                }
            }
        }

	}
};


int main() {
	BEasyLinearProgramming solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


Measured memory_bytes: 3680
#define _GLIBCXX_DEBUG
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>

using namespace std;

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

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    REP(i, n) {
        int b;
        cin >> b;
        a[i] = b;
    }

    int min = *min_element(ALL(a));
    int max = *max_element(ALL(a));
    cout << max-min << endl;
}


Measured memory_bytes: 256
D, N = map(int, input().split())
if D == 0:
  if N == 100:
    print(101)
  else:
  	print(N)
elif D == 1:
  if N == 100:
    print(10100)
  else:
    print(N*100)
else:
  if N == 100:
    print(1010000)
  else:
    print(N*10000)


Measured memory_bytes: 2940
#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair
typedef long long int lli;
const lli mod = 1e9+7;
using namespace std;

int main()
{
	fast;
	lli x;cin>>x;
	
	lli ans=0;
	ans+=(x/500)*1000;
	x=x%500;
	ans+=(x/5)*5;
	cout<<ans;
	

}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long int
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define BOLT ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define X first
#define Y second
#define endl "\n"
using namespace std;
void solve() {
	int n, m;
	cin >> n >> m;
	int sum = 0;
	for (int i = 1; i <= m; i++) {
		int x;
		cin >> x;
		sum += x;
	}
	if (sum > n) {
		cout << -1 << endl;
		return;
	}
	cout << n - sum << endl;
}
int32_t main() {
	BOLT;
	int t;
	t = 1;
	int counter = 0;
	while (t--) {
		solve();
	}
}


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

int main(){
  int x, y, t;
  cin >> x >> y;
  if (x < y) {
    t = x;
    x = y;
    y = t;
  }
  while(y) {
    y = x % (t = y);
    x = t;
  }
  cout << x << endl;
  return 0;
}

Measured memory_bytes: 1160
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for(int i = 0; i < N; i++)
    cin >> vec.at(i);
  sort(vec.begin(), vec.end());
  int count = 1;
  for(int i = 1; i < N; i++){
    if(vec.at(i) > vec.at(i - 1))
      count++ ;
  }
  cout << count <<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pb push_back
#define endl '\n'
#define MOD 1000000007
#define MOD2 998244353

using namespace std;
typedef long long int ll;

int main(void)
{
	fast;
	#ifdef LOCAL
	freopen("in", "r", stdin);
	freopen("out", "w", stdout);
	#endif
	
	int n; cin >> n;
	string a, b, c; cin >> a >> b >> c;
	int ans = 0;
	for (int i = 0; i < n; ++i)
	{
		set<char> s = {a[i], b[i], c[i]};
		if (s.size() == 2) ++ans;
		if (s.size() == 3) ans += 2;
	}
	cout << ans;
}

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

#define int long long

#define rep(i,n) for(int i=0;i<(n);i++)
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second

using vint=vector<int>;
using pint=pair<int,int>;
using vpint=vector<pint>;

template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

template<class A,class B>
ostream& operator<<(ostream& ost,const pair<A,B>&p){
	ost<<"{"<<p.first<<","<<p.second<<"}";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost,const vector<T>&v){
	ost<<"{";
	for(int i=0;i<v.size();i++){
		if(i)ost<<",";
		ost<<v[i];
	}
	ost<<"}";
	return ost;
}

inline int topbit(unsigned long long x){
	return x?63-__builtin_clzll(x):-1;
}

inline int popcount(unsigned long long x){
	return __builtin_popcountll(x);
}

inline int parity(unsigned long long x){
	return __builtin_parity(x);
}

int gcd(int a,int b){
	return b?gcd(b,a%b):a;
}

int extgcd(int a,int b,int &x,int &y){
	int g=a;
	x=1;
	y=0;
	if(b!=0){
		g=extgcd(b,a%b,y,x);
		y-=(a/b)*x;
	}
	return g;
}
int inv(int a,int m){
	int x,k;
	extgcd(a,m,x,k);
	return (x%m+m)%m;
}

void solve(){
	int A,X,B,Y,C;
	cin>>A>>X>>B>>Y>>C;



	//gcd(A,B,C)=1
	int tmp=gcd(gcd(A,B),C);
	A/=tmp;B/=tmp;C/=tmp;


	/*
	By=-Ax mod C
	gcd(B,C) | (Ax)
	gcd(B,C) | x   (<=  gcd(gcd(B,C),A)=1)
	A *=gcd(B,C)
	X/=gcd(B,C)
	A/=gcd(B,C);B/=gcd(B,C);C/=gcd(B,C);
	*/
	
	//gcd(B,C)=1
	tmp=gcd(B,C);
	X/=tmp;
	B/=tmp;C/=tmp;

	/*
	By=-Ax mod C
	y=-inv(B)Ax mod C
	h:=(-inv(B)A mod C)

	(x,hx mod C) (x=0,1,2,...)
	*/

	//gcd(A,C)=1
	tmp=gcd(A,C);
	Y/=tmp;
	A/=tmp;C/=tmp;

	if(C==1){
		cout<<X+Y<<endl;
		return;
	}

	/*
	gcd(h,C)=1
	*/

	int H=(C-inv(B,C))*A%C;

	int h=H,c=C;
	vint dx,dy,num;
	dy.pb(C-h);
	num.pb(1);

	/*
	(h,c)->(h-d,h)  (d=C%h)
		 ->(h-2d,h-d)->...->(g,g+d)  (g=h%d)

		gcd(g,g+d)=gcd(g,d)=gcd(h%d,d)=gcd(h,d)=gcd(h,c%h)=gcd(h,c)=1
	*/

	while(h){
		if(h==1){
			dy.pb(1);
			num.pb(1);
			break;
		}
		int d=c%h;
		int g=h%d;
		dy.pb(d);
		num.pb(h/d);
		h=g;
		c=g+d;
	}

	/*
	dx=-H^{-1} dy
	*/
	int coef=C-inv(H,C);
	rep(i,dy.size()){
		dx.pb(coef*dy[i]%C);
	}

	
	int lb=0,ub=X+Y+1;
	while(ub-lb>1){
		int mid=(ub+lb)/2;

		int x=0,y=C*mid;
		rep(i,dx.size()){
			if(x+dx[i]*num[i]*mid<=X){
				x+=dx[i]*num[i]*mid;
				y-=dy[i]*num[i]*mid;
				continue;
			}

			int k=(X-x)/dx[i];
			x+=dx[i]*k;
			y-=dy[i]*k;
			break;
		}
		if(y<=Y)lb=mid;
		else ub=mid;
	}
	cout<<lb<<endl;
}

signed main(){
	int T;cin>>T;
	while(T--)solve();
	return 0;
}


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

int main() {
    using namespace std;

    string s;
    cin >> s;

    unsigned long ans{s.size()}, N{s.size()};
    char pc{'.'};
    for(unsigned long i = 0; i < N; ++i)if(pc == s[i]){
        --ans;
        ++i;
        pc = '.';
    }else pc = s[i];

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 640
#include<iostream>
using namespace std;
 
 
int main()
{
     string s;
     cin >> s;
    if(s == "Sunny")
    {
        cout << "Cloudy\n";
    }
    else if(s == "Cloudy")
    {
        cout << "Rainy\n";
    }
    else
    {
        cout << "Sunny\n";
    }
    
}

Measured memory_bytes: 256
import numpy as np
n,m,c = map(int, input().split())
cond = list(map(int,input().split()))
char = []
for i in range(n):
    char.append(list(map(int, input().split())))
npcond = np.asarray(cond)
npchar = np.asarray(char)

npstat = npchar * npcond
npstat1 = np.sum(npstat, axis = 1) + c

print(np.count_nonzero(npstat1 > 0))

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

#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define MIN(a) *min_element(all(a))
#define MAX(a) *max_element(all(a))
#define SUM(a) accumulate(all(a), 0LL)
#define REP(i, n) for(int (i)=0; (i)<(n); (i)++)
#define RREP(i, n) for(int (i)=(n)-1; (i)>=0; (i)--)
#define FOR(i, m, n) for(int (i)=(m); (i)<(n); i++)
#define FORR(i, m, n) for(int (i)=(n)-1; (i)>=(m); i--)
#define debug(x) cerr << #x << " = " << x << endl
//#define int long long

typedef long long ll;
//ll const MOD = 998244353;
ll const MOD = 1e9+7;
int const inf = 1e9;
ll const INF = 1e18;

inline void ios_(){cin.tie(0); ios::sync_with_stdio(false);}
template<typename T> int size(const T& a){return (int)a.size();}
template<typename T> T Div(T a, T b){return (a + b - 1) / b;}
template<typename T> bool chmin(T& a, const T& b){if(a > b){a = b; return true;} return false;}
template<typename T> bool chmax(T& a, const T& b){if(a < b){a = b; return true;} return false;}



signed main(){
    ll x;
    cin >> x;

    for(ll i=-2000; i<2000; i++){
        for(ll j=-2000; j<2000; j++){
            ll a = i*i*i*i*i, b = j*j*j*j*j;
            if(a - b == x){
                cout << i << " " << j << endl;
                return 0;
            }
        }
    }

    return 0;
}


Measured memory_bytes: 3636
//
// Created on 2020/07/28.
//

#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <ios>
#include <iomanip>
#include <numeric>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
typedef long long ll;
using namespace std;
const long long LINF =1e18;
const int INF = 1e9;


int main() {
    ll n;
    cin>>n;
    vector<ll>t(5);
    ll MIN = LINF;
    int MIN_index;
    for (int i = 0; i < 5; ++i) {
        cin>>t[i];
        if(MIN > t[i]){
            MIN = t[i];
            MIN_index = i;
        }
    }
    ll ans = (n + (MIN - 1)) / MIN;
    ans += MIN_index - 0;
    ans += 4 - MIN_index;
    cout<<ans<<endl;

    return 0;
}



Measured memory_bytes: 3580
#include<iostream>
#include<map>
#include<set>
#include<string>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<stack>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef int itn;

const int inf = 99999999;
const ll linf = 999999999999999999;
const double pi = acos(-1);
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};

#define p_queue(i) priority_queue<i> 
#define rp_queue(i) priority_queue<i, vector<i>, greater<i>> 
#define P(p) cout<<(p)<<endl
#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define mod(i) ((i)%(ll)(1e9 + 7))
#define divm(a,b) (mod(a * modpow((ll)b,(ll)(1e9+5))))
#define rsort(a,b,c) sort(a, b, greater<c>())
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.end(), v.begin());
#define ft first
#define sd second
#define pb push_back
#define it insert
#define sz(x) ((int)(x).size())
#define lb(a,n,k) (lower_bound(a,a+n,k) - a) 
#define vlb(a,k)  (lower_bound(a.begin(),a.end(),k) - a.begin())
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return

ll modpow(ll i,ll j){ ll tmp=1; while(j){ if(j%2)tmp=mod(tmp*i);i=mod(i*i);j/=2;}return tmp;}
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 ncr[100][100];
//ll nCr(int n, int r){if(n==r) ret ncr[n][r] = 1; if(r==0) ret ncr[n][r] = 1; if(r==1) ret ncr[n][r] = n;if(ncr[n][r]) ret ncr[n][r]; ret ncr[n][r] = nCr(n-1,r) + nCr(n-1,r-1);}

//ll npr[100][100]={};
//ll nPr(int n,int r){if(npr[n][r])ret npr[n][r];if(r==0)ret npr[n][r] = 1;if(r==1)ret npr[n][r] = n;ret npr[n][r] = n * nPr(n-1,r-1);}

//ll nHr(int n,int r){ret nCr(n+r-1,r);}

///////////////////////////////////////////////////////////////////////////



int main(){
	
    string s;
    cin >> s;
    (s[0] == '9' || s[1] == '9')? Yes : No ;
	return  0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define int long long
#define endl "\n"
#define pii pair<int,int> 
#define ff first
#define ss second
#define mii map<int,int>
#define si set<int>
#define sti stack<int>
#define qi queue<int>
#define vi vector<int>
#define pq priority_queue<int>
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int M=1e9+7;

int32_t main(){
    IOS;
    string s;
    cin>>s;
    for(int i=0;i<s.length();i++){
        if(s[i]=='7'){
            cout<<"Yes"<<endl;
            return 0;
        }
    }
    cout<<"No";
}

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


int main(){
  int n,c;
  cin >> n;
  vector<int> a(n),b(n);

  rep(i,n){
    cin >> a[i];
    b[i] = a[i];
  }
  sort(all(a));

  rep(i,n){
    b[i] == a[n-1] ? c =  a[n-2] : c = a[n-1];
    cout << c << endl;
  }
}




Measured memory_bytes: 3200
/**
*    author:  boutarou
*    created: 11.07.2020 21:42:25
**/

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < int(n); i++)
using ll = long long;
using P = pair<int, int>;

long long mod_pow(long long n, long long p, long long m) {
    if (p == 0) return 1;
    if (p % 2 == 0) {
        long long t = mod_pow(n, p / 2, m);
        return (t * t) % m;
    }
    return n * mod_pow(n, p - 1, m) % m;
}

ll MOD;

ll solve(ll x) {
    ll res = 1;
    while (1) {
        if (x == 0) return res;
        ll moto = x, div = 0;
        while (x > 0) {
            if (x % 2 == 1) div++;
            x /= 2;
        }
        x = moto % div;
        res++;
    }
}

int main() {
    ll n;
    string x;
    cin >> n >> x;
    vector<ll>a(n);
    vector<ll>ans(n);
    rep(i, n) {
        if (x[i] == '0') a[i] = 0;
        else a[i] = 1;
    }

    ll cnt = 0;
    rep(i, n) if (a[i] == 1) cnt++;

    if (cnt == 1) {
        rep(i, n) if (a[i] == 1) ans[i] = 0;
    }
    else {
        MOD = cnt - 1;
        ll now = 0;
        rep(i, n) {
            now *= 2;
            now += a[i];
            now %= MOD;
        }
        rep(i, n) {
            if (a[i] == 1) {
                ll moto = now;
                now -= mod_pow(2, (n - 1 - i), MOD);
                now %= MOD;
                if (now < 0) now += MOD;
                ans[i] = solve(now);
                now = moto;
            }
        }
    }
    {
        MOD = cnt + 1;
        ll now = 0;
        rep(i, n) {
            now *= 2;
            now += a[i];
            now %= MOD;
        }
        rep(i, n) {
            if (a[i] == 0) {
                ll moto = now;
                now += mod_pow(2, (n - 1 - i), MOD);
                now %= MOD;
                ans[i] = solve(now);
                now = moto;
            }
        }
    }
    rep(i, n) {
        cout << ans[i] << endl;
    }

    return 0;
}

Measured memory_bytes: 6724
#include<bits/stdc++.h>
using namespace std;
int main(){
int H,W;
  cin>>H>>W;
  vector<vector<char>>A(H+2,vector<char>(W+2,'.'));
  A.at(0).at(1)='#';
  A.at(H+1).at(W)='#';
  for(int i=1;i<=H;i++){
    string s;
    cin>>s;
  for(int j=1;j<=W;j++)
    A.at(i).at(j)=s.at(j-1);
  }
  vector<vector<int>>B(H+2,vector<int>(W+2,2));
  for(int i=1;i<=H;i++){
  for(int j=1;j<=W;j++){
  if(A.at(i).at(j)=='.')
    B.at(i).at(j)=2;
    else{
    int a=0;
      if((A.at(i-1).at(j)=='#' && A.at(i).at(j-1)=='.')||
         (A.at(i-1).at(j)=='.' && A.at(i).at(j-1)=='#'))
        a++;
      if((A.at(i+1).at(j)=='#' && A.at(i).at(j+1)=='.')||
         (A.at(i+1).at(j)=='.' && A.at(i).at(j+1)=='#'))
      a++;
      B.at(i).at(j)=a;
    
    }
  }
  }bool x=true;
  for(int i=1;i<=H;i++){
  for(int j=1;j<=W;j++)
    if(B.at(i).at(j)!=2){x=false; break;}
  }if(x)
    cout<<"Possible"<<endl;
  else
    cout<<"Impossible"<<endl;
     return 0;
}
   


Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main()
{
    int k, a, b;
    cin >> k >> a >> b;

    for (int i = a; i <= b; i++)
    {
        if (i % k == 0)
        {
            cout << "OK" << endl;
            return 0;
        }
    }
    cout << "NG" << endl;
}


Measured memory_bytes: 3640
#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)
#define ALL(a)     (a).begin(),(a).end()
#define MOD 1000000007

typedef long long int ll;

const ll INF=(ll)1e18;
//const int INF=(1<<30);


int main(){
  int N;
  cin >> N;
  cout << (N+1)/2 << endl;
}


Measured memory_bytes: 256
def primes(n):
   is_prime = [True] * (n + 1)
   is_prime[0] = False
   is_prime[1] = False
   for i in range(2, int(n**0.5) + 1):
      if not is_prime[i]:
         continue
      for j in range(i * 2, n + 1, i):
         is_prime[j] = False
   return [i for i in range(n + 1) if is_prime[i]]

from fractions import gcd
import math
A,B=map(int,input().split())
C=gcd(A,B)
prime_numbers=primes(int(math.sqrt(C))+1)

if C==1:
   print(1)
else:
   count=1

   for p in prime_numbers:
      if C==1:
         break
      
      flag=False
      while C%p==0:
         C//=p
         flag=True
      if flag:
         count+=1
         p=p+1 if p==2 else p+2
      else:
         p=p+1 if p==2 else p+2
   
   if C!=1:
      count+=1

   print(count)

Measured memory_bytes: 16140
// AOJ 2953 Hokkaido University Hard
// 2019.8.10 bal4u

#include <stdio.h>

#if 1
#define gc() getchar_unlocked()
#else
#define gc() getchar()
#endif

int in() {   // 非負整数の入力
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0');
	return n;
}

void ins(char *s) { // 文字列の入力　スペース以下の文字で入力終了
	do *s = gc();
	while (*s++ > ' ');
	*(s-1) = 0;
}

#define INF 0x7fffffff
int H, W;
char a[1005];

int main()
{
	int r, c, ama, ami, sma , smi, ans;
	
	H = in(), W = in();
	ama = sma = -INF, ami = smi = INF;
	for (r = 0; r < H; r++) {
		ins(a);
		for (c = 0; c < W; c++) if (a[c] == 'B') {
			if (r+c > ama) ama = r+c;
			if (r+c < ami) ami = r+c;
			if (r-c > sma) sma = r-c;
			if (r-c < smi) smi = r-c;
		}
	}
	ans = ama - ami;
	if (ans < sma - smi) ans = sma - smi;
	printf("%d\n", ans);
	return 0;
}


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

int main() {
	int n,k;
cin>>n>>k;
  	int dp[n],a[n];
  	
  	for(int i=0; i<n; i++) {
    	cin>>a[i];
		dp[i]  =INT_MAX;
//cout<<a[i]<<endl;
    }
 	 
  	dp[0] = 0;
  	for(int i=1; i<n; i++) {
		for(int j=1; j<=k && j<=i; j++) {
			dp[i] = min(abs(a[i] - a[i-j]) + dp[i-j], dp[i]);
		}
		//cout<<dp[i]<<endl;
    }
  	cout<<dp[n-1];
  	return 0;
}

Measured memory_bytes: 1024
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<string>
#include<sstream>
#include<iomanip>
#include<utility>
#include<cmath>
#include<set>
#include<list>
#include<string>
#include<queue>
#include<stack>
#include<deque>
#include<map>
#include<set>
#include<cstring>
#include<iterator>
#include<bitset>
#include<assert.h>
//#include<chrono>
//#include<time.h>
//#include<windows.h>
using namespace std;
 
#define int ll
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,pair<int,int> > PP;
typedef vector<int> VI;
typedef vector<long long int> VL;
static const int MOD = 1000000007;
//static const int INF = 2147483647;
//static const long long INF = 9223372000000000000;
//static const long long INF = 9223372000000000000/2;
//static const int INF = 1000010000;
//int dx4[4] = {0,1,0,-1}, dy4[4] = {-1,0,1,0};
//int dx5[5] = {-1,0,0,0,1}, dy5[5] = {0,-1,0,1,0};
//int dx8[8] = {-1,0,1,1,1,0,-1,-1}, dy8[8] = {1,1,1,0,-1,-1,-1,0};
//int dx9[9] = {-1,0,1,1,1,0,-1,-1,0}, dy9[9] = {1,1,1,0,-1,-1,-1,0,0};
#define PB push_back
#define MP make_pair
#define MT make_tuple
#define FI first
#define SE second
#define NP next_permutation
#define PQ priority_queue
#define UB upper_bound
#define LB lower_bound
#define SZ(a) int((a).size())
#define LEN(a) int((a).length())
#define SORT(c) sort((c).begin(),(c).end())
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define REP1(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREP1(i,x) for(int i=((int)(x));i>0;i--)
#define ALL(x) (x).begin(),(x).end()
#define YESNO(x) puts((x)?"YES":"NO")
struct edge {int /*from,*/to,cost;};

main(){
    int n;
    int march[5]={};
    cin >> n;
    REP(i,n){
        string tmp;
        cin >> tmp;
        char b = tmp[0];
        if(b=='M') march[0]++;
        if(b=='A') march[1]++;
        if(b=='R') march[2]++;
        if(b=='C') march[3]++;
        if(b=='H') march[4]++;
    }

    int ans = 0;

    REP(i,1<<5){
        if(__builtin_popcount(i)==3){
            int t = 1;
            REP(j,5){
                if((i>>j)&1){
                    t *= march[j];
                }
            }
            ans += t;
        }
    }

    printf("%lld\n",ans);

    return 0;
}


Measured memory_bytes: 256
import java.util.*;

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

		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		TreeMap<String, Integer> s = new TreeMap<>();
		int max = 1;

		for (int i = 0; i < n; i++) {
			String tmp = sc.next();
			
			if (s.containsKey(tmp)) {
				s.put(tmp, s.get(tmp) + 1);
				max = Math.max(max, s.get(tmp));
			} else {
				s.put(tmp, 1);
			}
		}
		
		for (String key : s.keySet()) {
			if (max == s.get(key)) {
				System.out.println(key);
			}
		}
	}
}  


Measured memory_bytes: 82996
#pragma GCC optimize("O3")

#include <bits/stdc++.h>

using namespace std;

#define n_l '\n'
#define dbg(...) cout << "[" << #__VA_ARGS__ << "]: "; cout << to_string(__VA_ARGS__) << endl
template <typename T, size_t N> int SIZE(const T (&t)[N]){ return N; } template<typename T> int SIZE(const T &t){ return t.size(); } string to_string(const string s, int x1=0, int x2=1e9){ return '"' + ((x1 < s.size()) ? s.substr(x1, x2-x1+1) : "") + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(const bool b) { return (b ? "true" : "false"); } string to_string(const char c){ return string({c}); } template<size_t N> string to_string(const bitset<N> &b, int x1=0, int x2=1e9){ string t = ""; for(int __iii__ = min(x1,SIZE(b)),  __jjj__ = min(x2, SIZE(b)-1); __iii__ <= __jjj__; ++__iii__){ t += b[__iii__] + '0'; } return '"' + t + '"'; } template <typename A, typename... C> string to_string(const A (&v), int x1=0, int x2=1e9, C... coords); int l_v_l_v_l = 0, t_a_b_s = 0; template <typename A, typename B> string to_string(const pair<A, B> &p) { l_v_l_v_l++; string res = "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; l_v_l_v_l--; return res; } template <typename A, typename... C> string to_string(const A (&v), int x1, int x2, C... coords) { int rnk = rank<A>::value; string tab(t_a_b_s, ' '); string res = ""; bool first = true; if(l_v_l_v_l == 0) res += n_l; res += tab + "["; x1 = min(x1, SIZE(v)), x2 = min(x2, SIZE(v)); auto l = begin(v); advance(l, x1); auto r = l; advance(r, (x2-x1) + (x2 < SIZE(v))); for (auto e = l; e != r; e = next(e)) { if (!first) { res += ", "; } first = false; l_v_l_v_l++; if(e != l){ if(rnk > 1) { res += n_l; t_a_b_s = l_v_l_v_l; }; } else{ t_a_b_s = 0; } res += to_string(*e, coords...); l_v_l_v_l--; } res += "]"; if(l_v_l_v_l == 0) res += n_l; return res; } void dbgm(){;} template<typename Heads, typename... Tails> void dbgm(Heads H, Tails... T){ cout << to_string(H) << " | "; dbgm(T...); }
#define dbgm(...) cout << "[" << #__VA_ARGS__ << "]: "; dbgm(__VA_ARGS__); cout << endl

#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define PER(i,a,b) for(int i=(a);i>(b);i++)
#define SZ(a) int((a).size())
#define ALL(a) (a).begin(), (a).end()
#define MS(s, n) memset(s, n, sizeof(s))
#define IN(t, x) (t.find(x) != t.end())
#define PB push_back
#define MP make_pair
#define VI vector<int>

typedef vector<pair<int, int>> VPII;
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
const int MOD = (int) 1e9 + 7;//

int main() {
    int n, k;
    cin >> n >> k;
    if (k > ((n-2)*(n-1))/2) {
        cout << -1;
        return 0;
    }
    int num = ((n-2)*(n-1))/2;
    cout << (n - 1) + (num - k) << '\n';
    for (int i = 0; i < n-1; i++) {
        cout << 1 << ' ' << 2 + i << '\n';
    }
    int hi = n;
    int curr = hi - 1;
    while (num > k) {
        cout << hi << ' ' << curr << '\n';
        curr -= 1;
        if (curr == 1) {
            hi -= 1;
            curr = hi - 1;
        }
        num -= 1;
    }

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define INF 2000000000
#define MOD 998244353
#define MAXN 200005
#define REP(temp, init_val, end_val) for (int temp = init_val; temp <= end_val; ++temp)
#define REPR(temp, init_val, end_val) for (int temp = init_val; temp >= end_val; --temp)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> intpair;
int read(){
    int f = 1, x = 0;
    char c = getchar();
    while (c < '0' || c > '9'){if(c == '-') f = -f; c = getchar();}
    while (c >= '0' && c <= '9')x = x * 10 + c - '0', c = getchar();
    return f * x; 
}
inline int lowbit(int x){
    return x & (-x);
}
inline int modadd(int x, int y){
    return (x + y >= MOD ? x + y - MOD: x + y);
}
inline int sgn(int x){
    return (x < 0 ? -1: (x > 0 ? 1: 0));
}
template<typename T>
T gcd(T a, T b){
    return (!b) ? a: gcd(b, a % b);
}
int poww(int a, int b){
    int res = 1;
    while (b > 0){
        if (b & 1) res = 1ll * res * a % MOD;
        a = 1ll * a * a % MOD, b >>= 1;
    }
    return res;
}
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
const int ddx[] = {-1, -1, -1, 0, 0, 1, 1, 1}, ddy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

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

int seg[530000] = {0}, len[530000] = {0}, siz;
int tag[530000] = {0};
int pre[11][530005] = {0}, ten[530005];
int n, q;
int _l, _r, _v;
void maintain(int id){
    seg[id] = modadd(seg[id << 1 | 1], 
        1ll * ten[len[id << 1 | 1]] * seg[id << 1] % MOD);
}
void pushdown(int id){
    if (tag[id] > 0){
        seg[id << 1] = pre[tag[id]][len[id << 1]];
        tag[id << 1] = tag[id];
        seg[id << 1 | 1] = pre[tag[id]][len[id << 1 | 1]];
        tag[id << 1 | 1] = tag[id];
        tag[id] = 0;
    }
}
void init(){
    n = read(), q = read();
    for (siz = 1; siz < n; siz <<= 1)
        ;
    REP(i, 1, 9){
        pre[i][1] = i;
        REP(j, 2, siz)
            pre[i][j] = modadd(10ll * pre[i][j - 1] % MOD, i);
    }
    ten[0] = 1;
    REP(i, 1, siz)
        ten[i] = 10ll * ten[i - 1] % MOD;
    
    REP(i, siz, siz + n - 1)
        seg[i] = 1, len[i] = 1;
    REPR(i, siz - 1, 1)
        len[i] = len[i << 1] + len[i << 1 | 1], 
        maintain(i);
}
void update(int id, int l, int r){
    if (l > _r || r < _l) return ;
    if (l >= _l && r <= _r){
        seg[id] = pre[_v][len[id]];
        tag[id] = _v;
        return ;
    }
    pushdown(id);
    int mid = (l + r) >> 1;
    update(id << 1, l, mid);
    update(id << 1 | 1, mid + 1, r);
    maintain(id);
}
void solve(){
    while (q--){
        _l = read(), _r = read(), _v = read();
        update(1, 1, siz);
        printf("%d\n", seg[1]);
    }
}
int main(){
    int T = 1;
    while (T--){
        init();
        solve();
    }
    return 0;
}

Measured memory_bytes: 19284
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#define SIZE 100005

using namespace std;

int A[SIZE];
bool use[SIZE];
vector <int> query[SIZE];
int dp[SIZE];

int main()
{
	int n,m,q;
	scanf("%d %d",&n,&m);
	scanf("%d",&q);
	for(int i=0;i<q;i++)
	{
		scanf("%d",&A[i]);
		A[i]--;
		query[A[i]].push_back(i);
	}
	vector <int> ord;
	for(int i=q-1;i>=0;i--)
	{
		if(!use[A[i]])
		{
			ord.push_back(A[i]);
			use[A[i]]=true;
		}
	}
	for(int i=0;i<m;i++) if(!use[i]) ord.push_back(i);
	while(ord.size()>=2)
	{
		int sz=ord.size();
		if(ord[sz-2]>ord[sz-1]) break;
		ord.pop_back();
	}ord.pop_back();
	//for(int i=0;i<ord.size();i++) printf("%d ",ord[i]);puts("");
	for(int i=0;i<n;i++) dp[i]=q;
	for(int i=0;i<ord.size();i++)
	{
		int v=ord[i];
		int now=0;
		for(int j=((int) query[v].size())-1;j>=0;j--)
		{
			int p=query[v][j];
			if(dp[now]>p)
			{
				dp[now]=p;
				now++;
				if(now==n) break;
			}
		}
		if(now<n)
		{
			puts("No");
			return 0;
		}
	}
	puts("Yes");
	return 0;
}


Measured memory_bytes: 7160
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
using mii=map<int,int>;
using psi=pair<string,int>;
using pii=pair<int,int>;
using vlai=valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9
#define MOD ((ll)1e9+7)
void get(){}template<class H,class...T>void get(H&h,T&...t){cin>>h;get(t...);}
template<class T>void geti(T&a){for(auto&_:a)cin>>_;}
template<class T>void getii(T&a){for(auto&_:a)geti(_);}
void put(string d){}template<class H,class...T>void put(string d,H&h,T&...t){cout<<h;if(sizeof...(t))cout<<d;put(d,t...);}
template<class T>void puti(T&a,string d=" "){bool f=1;for(auto&_:a)cout<<(exchange(f,0)?"":d)<<_;cout<<endl;}
template<class T>void putii(T&a,string d=" "){for(auto&_:a)puti(_,d);}
#define dcl(...) ll __VA_ARGS__;get(__VA_ARGS__)
#define dclt(t,...) t __VA_ARGS__;get(__VA_ARGS__)
template<class F,class S>void add(map<F,S>&mp,F f,S s){if(mp.find(f)==mp.end())mp[f]=F();mp[f]+=s;}
 
int main(){
    int n,m;
    while(cin>>n>>m,n){
        vi where(n+1,-1);
        vector<map<int,ll>> w;
        char c;
        rep(i,m){
            cin>>c;
            if(c=='!'){
                ll a,b,x;
                cin>>a>>b>>x;
                if(where[a]==-1 && where[b]==-1){
                    int i=w.size();
                    where[a]=where[b]=i;
                    w.eb();
                    w[i][a]=0ll;
                    w[i][b]=x;
                }else if(where[a]==-1 || where[b]==-1){
                    if(where[a]==-1){
                        swap(a,b);
                        x*=-1;
                    }
                    int i=where[a];
                    where[b]=i;
                    w[i][b]=w[i][a]+x;
                }else{
                    int sa=w[where[a]].size();
                    int sb=w[where[b]].size();
                    if(sa<sb){
                        swap(a,b);
                        x*=-1;
                    }
                    int i=where[a];
                    int j=where[b];
                    int diff=w[i][a]+x-w[j][b];
                    for(auto data:w[j]){
                        where[data.fs]=i;
                        w[i].emplace(data.fs,data.sc+diff);
                    }
                }
            }else{
                int a,b;
                cin>>a>>b;
                if(where[a]!=where[b] || where[a]==-1){
                    cout<<"UNKNOWN"<<endl;
                }else{
                    int i=where[a];
                    cout<<w[i][b]-w[i][a]<<endl;
                }
            }
        }
    }
}



Measured memory_bytes: 7332
#include<iostream>
#include<string>
using namespace std;
int main(){
  string str;
  cin >> str;
  int i=0;
  for(i=0;i<8;i++){
    if(str.substr(0,7-i)+str.substr(str.size()-i,i)=="keyence"){
      cout << "YES\n";
      break;
    }
  }
  if(i==8) cout << "NO\n";
  return 0;
}


Measured memory_bytes: 256
#include <iostream> // cin, cout, cerr
#include <algorithm> // minmax, sort, swap
#include <numeric> // iota
#include <cstdio> // printf, scanf
#include <string> // string, stoi, to_string
#include <vector> // vector
#include <queue> // queue, priority_queue
#include <deque> // deque
#include <map> // key-value pairs sorted by keys
#include <set> // set
#include <iomanip> // cout<<setprecision(n)
#include <functional> // function<void(int)>
#include <cmath>

#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...)
#endif

#define int long long // at least int64 > 9*10^18
#define ENDL '\n'
#define rep(i,n) for(int i = 0; i < (n); i++)
#define print(i) std::cout << (i) << '\n'
#define all(v) (v).begin(), (v).end()
/* libraries */

signed main() {
	int h;
	std::cin >> h;
	int sum =0;
	for(int i=0;h>0;h>>=1,i++) {
		debug(h,i,sum);
		sum+=(1ll<<i);
	}
	print(sum);
	return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <vector>
#include <cmath>
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main(int argc, char const *argv[]) {
    map<string, int> map;
    int N;
    scanf("%d", &N);
    for (int i = 0; i < N; i++) {
        string s;
        cin >> s;
        map[s]++;
    }
    int M;
    scanf("%d", &M);
    for (int i = 0; i < M; i++) {
        string s;
        cin >> s;
        map[s]--;
    }
    int max = 0;
    for (auto itr = map.begin(); itr != map.end(); ++itr) {
        if (max < itr -> second) {
            max = itr -> second;
        }
    }
    cout << max << endl;
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

int main(){
  int h;
  int w;
  int i;
  int j;
  int ans_h;
  int ans_w;
  int naname_1;//???????????£??\??????????????°
  int naname_2;
  int naname_3;

  while(1){
    scanf("%d %d",&h,&w);
    if(h==0 && w==0) break;

    ans_h=160;
    ans_w=160;
    
    naname_1 =(h*h) + (w*w);
    naname_3=(ans_h*ans_h)+(ans_w*ans_w);

  for(i=1;i<=150;i++){
    for(j=1;j<=150;j++){
      naname_2 =(j*j)+(i*i);
      if(j>i){
	if(naname_1 < naname_2 || (naname_1==naname_2&&h<i)){
	  if(naname_2<naname_3 || (naname_2==naname_3&&i<ans_h)){
	    ans_h=i;
	    ans_w=j;
	    naname_3=(ans_h*ans_h)+(ans_w*ans_w);
	  }
	}

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

Measured memory_bytes: 600
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0;i<n;i++)
#define cY cout << "Yes" << endl;
#define cN cout << "No" << endl;

int main() {
  string n;
  cin >> n;
  bool a = false;
  rep(i,3){
    if(n.at(i)=='7')
    a = true;
  }
  if(a) {
    cY;
  }
  else{
    cN;
  }
  return 0;
}


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

void fnSelectionSort(vector<int> &a_rviSort, int &a_riSortCnt)
{
  for (int ixSorting = 0; ixSorting < a_rviSort.size() - 1; ixSorting++)
  {
    int ixMinimum = ixSorting;
    bool bSorting = false;

    for (int j = ixSorting + 1; j < a_rviSort.size(); j++)
    {
      if (a_rviSort[j] < a_rviSort[ixMinimum])
      {
        ixMinimum = j;
        bSorting = true;
      }
    }
    if (bSorting)
    {
      swap(a_rviSort[ixSorting], a_rviSort[ixMinimum]);
      a_riSortCnt++;
    }
  }
}

void fnOutput(const vector<int> &a_cnrviSort, int a_iSortCnt)
{
  for (int i = 0; i < a_cnrviSort.size(); i++)
  {
    if (i) cout << " ";
    cout << a_cnrviSort[i];
  }
  cout << endl;
  cout << a_iSortCnt << endl;
}

int main()
{
  int iSortCnt = 0;
  vector<int> viSort;
  int iMaxSize;
  
  cin >> iMaxSize;
  for (int i = 0; i < iMaxSize; i++)
  {
    int iNum;
    cin >> iNum;
    viSort.push_back(iNum);
  }

  fnSelectionSort(viSort, iSortCnt);
  fnOutput(viSort, iSortCnt);

  return 0;
}

Measured memory_bytes: 1220
#include <bits/stdc++.h>
#define pb push_back
#define REP(i, n) for (signed long long i = 0; i < (n); i++)
#define MOD 1000000007
#define INF 98765431219876543
#define bitcnt(a) (ll) __builtin_popcount((a))
#define lb(a, b) lower_bound((a).begin(), (a).end(), (b))
#define ub(a, b) upper_bound((a).begin(), (a).end(), (b))
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<string, string> PS;

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> s, e, same;
  REP(i, n) {
    ll p;
    cin >> p;
    s.pb(p);
    same.pb(p);
  }
  REP(i, m) {
    ll p;
    cin >> p;
    e.pb(p);
    same.pb(p);
  }
  sort(same.begin(), same.end());
  same.erase(unique(same.begin(), same.end()), same.end());

  sort(s.begin(), s.end());
  s.erase(unique(s.begin(), s.end()), s.end());

  sort(e.begin(), e.end());
  e.erase(unique(e.begin(), e.end()), e.end());

  if (s.size() != n || e.size() != m) {
    cout << 0 << endl;
    return 0;
  }

  ll ans = 1;
  REP(i, n) {
    ll cnt = 0;
    REP(j, m) {
      if (s[i] == e[j]) {
        cnt = 1;
        break;
      } else if (s[i] < e[j]) {
        cnt++;
      }
    }
    ans *= cnt;
    ans %= MOD;
  }

  REP(j, m) {
    ll cnt = 0;
    REP(i, n) {
      if (s[i] == e[j]) {
        cnt = 1;
        break;
      }
      if (e[j] < s[i]) {
        cnt++;
      }
    }
    ans *= cnt;
    ans %= MOD;
  }

  ll cnt = 0;

  for (ll i = n * m; i > 0; i--) {
    if (lb(same, i) == same.end() || *lb(same, i) == i) {
      continue;
    }

    ll x = distance(lb(e, i), e.end());
    ll y = distance(lb(s, i), s.end());
    ll sa = distance(lb(same, i), same.end());

    // cout << i << " " << x << " " << y << " " << sa << endl;
    ll aa = x * y - sa - cnt;
    if (aa <= 0) {
      cout << 0 << endl;
      return 0;
    }
    ans *= aa;
    ans %= MOD;

    cnt++;
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;


int main(){
    char s[10];
    scanf("%s",s);
    int i,ans=0;
    for(i=0;i<=5;i++){
        if(s[i]=='A'&&s[i+1]=='C')ans=1;
    }
    
    if(ans==0)printf("No");
    else printf("Yes");
    
    return 0;
}


Measured memory_bytes: 128