akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))
#define rep(i,n) for(int i = 0; i < (n); i++)
using ll = long long;
using namespace std;
const ll ber = 1e18;
const int M = 1000005;

int main(){
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i,n) cin >> a[i];
  vector<int> cnt(M);
  for(int x : a){//範囲ベースループ
    if(cnt[x] != 0){//重複回避用
      cnt[x] = 2;
      continue;
    }
    for(int i = x; i < M; i += x)cnt[i]++;
  }
  int ans = 0;
  for(int x: a){
    if(cnt[x] == 1)ans++;
  }
  cout << ans << endl;
}

Measured memory_bytes: 8008
module Main where

import Debug.Trace

main :: IO ()
main = do
  [m, d] <- map (read :: String -> Int) . words <$> getLine
  print $ length $ filter p [ (mm, dd) | mm <- [1 .. m], dd <- [1 .. d] ]
 where
  p (mm, dd) =
    let d1  = dd `div` 10
        d10 = dd `mod` 10
    in  d1 >= 2 && d10 >= 2 && d1 * d10 == mm


Measured memory_bytes: 508
class Node(val id: Long) {
    private val edges = mutableListOf<Edge>()
    val nextNodes = mutableListOf<Node>()
    var distance: Long = Long.MAX_VALUE
    var predecessor: Node? = null
    fun addEdge(edge: Edge, node: Node) {
        edges.add(edge)
        nextNodes.add(node)
    }
}

class Edge(val from: Long, val to: Long, val cost: Long) {

}

class Graph(val nodes: Array<Node>, val edges: Array<Edge>) {
    private val nodesMap = mutableMapOf<Long, Node>()

    init {
        for (node in nodes) {
            nodesMap[node.id] = node
        }
        for (edge in edges) {
            nodesMap[edge.from]?.addEdge(edge, nodesMap[edge.to]!!)
        }
    }

    fun node(id: Long): Node = nodesMap[id]!!

    fun solveBellmanFord(startId: Long) {
//        init
        nodesMap[startId]!!.distance = 0
//        relaxing
        for (i in 1 until nodes.size) {
            for (edge in edges) {
                val u = nodesMap[edge.from]!!
                val v = nodesMap[edge.to]!!
                if (u.distance == Long.MAX_VALUE) continue

                if (v.distance > u.distance + edge.cost) {
                    v.distance = u.distance + edge.cost
                    v.predecessor = u
                }
            }
        }
//        check negative circle
        for (edge in edges) {
            if (nodesMap[edge.from]!!.distance == Long.MAX_VALUE) continue

            val u = nodesMap[edge.from]!!
            val v = nodesMap[edge.to]!!
            if (v.distance > u.distance + edge.cost) {
//                閉路検出：ゴールへ行けるか判定
                val visited = mutableSetOf<Node>().run {
                    addAll(u.nextNodes)
                    this
                }
                while (true) {
                    val visitedTmp = visited.toSet()
                    for (node in visited) {
                        visited.addAll(node.nextNodes)
                    }
                    if (visited.size == visitedTmp.size) break
                }
                if (!visited.contains(nodes.last())) continue

                throw RuntimeException("negative circle detect!")
            }
        }
    }
}

fun main(args: Array<String>) {
    val (N, M, P) = readLine()!!.split(' ').map(String::toLong)
    val nodes = Array(N.toInt()) { Node(it.toLong() + 1) }
    val edges = Array(M.toInt()) {
        val (a, b, c) = readLine()!!.split(' ').map(String::toLong)
        Edge(a, b, -(c - P))
    }
    val graph = Graph(nodes, edges)
    try {
        graph.solveBellmanFord(1L)
        val ans = -graph.node(N).distance
        println(if (ans >= 0) ans else 0)
    } catch (e: RuntimeException) {
//        println(e)
        println(-1)
    }
}


Measured memory_bytes: 119320
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
using ll = long long;
using P = pair<int,int>;
template <class T> void chmin(T &a, const T &b) noexcept { if (b < a) a = b; }
template <class T> void chmax(T &a, const T &b) noexcept { if (a < b) a = b; }
void debug_out() { cout << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) { cout << H << " "; debug_out(T...); }
#ifdef _DEBUG
  #define debug(...) debug_out(__VA_ARGS__)
#else
  #define debug(...) 
#endif

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i,n) {
    cin >> a[i];
  }
  ll ans = 1LL<<60;
  for (ll x = -100; x <= 100; ++x) {
    ll sum = 0;
    rep(i,n) {
      sum += (a[i]-x)*(a[i]-x);
    }
    chmin(ans,sum);
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>

using namespace std;

int main() {
    int n;
    cin >> n;

    vector<int> problems;

    for (int i { 0 }; i < n; i++) {
        int l;
        cin >> l;
        problems.push_back(l);
    }

    sort(problems.begin(), problems.end());

    vector<int> firstHarf = vector<int>(problems.begin(), problems.begin() + n / 2);
    vector<int> lastHarf = vector<int>(problems.begin() + n / 2, problems.end());

    int firstHarfLast = firstHarf.back();
    int lastHarfFirst = lastHarf.front();
    cout << lastHarfFirst - firstHarfLast << endl;

    return 0;
}

Measured memory_bytes: 1016
#include <iostream>
#include <vector>
#include <fstream>
#include <stdio.h>
#include <algorithm>
#include <string>
#define REP(i, n) for(int i = 0; i < n; i++)
//#define REPR(i, n) for(int i = n; i >= 0; i--)
//#define FOR(i, m, n) for(int i = m; i < n; i++)
//#define INF 1e9
//#define ALL(v) v.begin(), v.end()

using namespace std;
//typedef long long ll;

int main() { //int argc, const char * argv[]

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

    string s, ans="";
    cin >> s;
    int n = s.length();

    REP(i, n){
        ans += "x";
    }
    
    cout << ans << endl;
}


Measured memory_bytes: 256
N = int(input())
S = input()
keta = 10
count = 0
for i in range(keta):
    for j in range(keta):
        for k in range(keta):
            nnn = str(i) + str(j) + str(k)
            now = 0
            nownnn = nnn[now]
            flag = False
            for l in range(N):
                if S[l] == nownnn:
                    if now == 2:
                        flag = True
                        break
                    else:
                        now += 1
                        nownnn = nnn[now]
            if flag:
                count += 1
print(count)

Measured memory_bytes: 41692
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int ab=scan.nextInt()-scan.nextInt();
		int c =scan.nextInt()-ab;
		if(c<=0){
			c=0;
		}
		System.out.println(c);
	}
}

Measured memory_bytes: 22868
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

typedef long long ll;

int main(){
	ll N ;
	cin >> N;
	ll a,b;

	for (ll i = 1; i*i <= N; i++)
	{
		if (N % i == 0)
		{
			a = i;
		}
	}
	b = N /a;
	cout << a + b -2<< endl;


	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;

int main(){
  int a,b,t;
  cin >> a >> b >> t ;
  int n = 0;
  for(int i = 1 ;a * i < t + 0.5;++i){
      n += b;
  }

  cout << n << endl;
}


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

using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> H(N);
  for (int i = 0; i < N; ++i) cin >> H[i];
  int ans = 1;
  int maxH = H[0];
  for (int i = 1; i < N; ++i) {
    if (H[i] >= maxH) {
      ans++;
      maxH = H[i];
    }
  }
  cout << ans << endl;
  return 0;
}



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

int main(){
  int n; cin >> n;
  int v[] = {1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51};
  cout << v[n-1] << endl;
  return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#define N 100
int bubblesort(int*,int);

int main(){
  int i,n,c;
  int A[N];

  scanf("%d",&n);
  for(i = 0; i < n; i++)
    scanf("%d",&A[i]);
 
  c = bubblesort(A,n);

  for(i = 0; i < n; i++) {
    if(i > 0) printf(" ");
    printf("%d",A[i]);
  }
  printf("\n%d\n",c);  
  return 0;
}

int bubblesort(int A[],int n) {
  
  int c = 0,tmp,i,j,flag = 1;
  
  while (flag){
    flag = 0;
    for(j = n-1; j >= 1; j--){
      if(A[j] < A[j-1]) 
	{
	  tmp = A[j-1];
	  A[j-1] = A[j];
	  A[j] = tmp; 
	  c++;
      flag = 1; 
	}
    }
  } 
  return c; 
}

Measured memory_bytes: 604
import Control.Monad
import Data.Function
import Data.Char
import Data.List
import Text.Read
import Text.ParserCombinators.ReadPrec

newtype Pair a b = Pair { getPair :: (a, b) }

instance (Read a, Read b) => Read (Pair a b) where
  readPrec = Pair `dot` (,) <$> readPrec <* get <*> readPrec

instance (Show a, Show b) => Show (Pair a b) where
  show (Pair (a, b)) = show a ++ " " ++ show b

readPair :: IO (Int, Int)
readPair = getPair <$> readLn

infixr 8 `dot`
dot :: (c -> d) -> (a -> b -> c) -> a -> b -> d
dot = ((.).(.))

main :: IO ()
main = do
  (n, d) <- readPair
  xys <- replicateM n readPair
  print $ solve d xys

solve :: Int -> [(Int, Int)] -> Int
solve d = length . filter (inBallOfRadius d)
 where
  inBallOfRadius d (x, y) = x^2 + y^2 <= d^2


Measured memory_bytes: 136716
#include <stdio.h>
#include <stdlib.h>
 
int mas[4][4];
int cpy[4][4];
 
void output() {
  int i, j;
  for(i = 0; i < 4; i++) {
    for(j = 0; j < 4; j++) printf("%3d", mas[i][j]);
    puts("");
  }
}
 
void copy(int m[4][4], int t[4][4]) {
  int i, j;
 
  for(i = 0; i < 4; i++)
    for(j = 0; j < 4; j++) m[i][j] = t[i][j];
}
 
int calc() {
  int i, sum = 0;
  for(i = 0; i < 16; i++) {
    int v = mas[i/4][i%4];
    if(v) {
      v--;
      sum += abs(i/4-v/4)+abs(i%4-v%4);
    }
  }
  return sum;
}
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
 
void swap(int *a, int *b) {
  int tmp = *a;
  *a = *b;
  *b = tmp;
}
 
int is_in(int x, int y) {
  return 0<=x&&x<4&&0<=y&&y<4;
}
 
int dfs(int x, int y, int p, int dep, const int lim) {
  int i, h = calc();
  //output();
  if(h == 0) return dep;
  //printf("%d %d %d\n", h, lim, p);
  if(dep+h > lim) return -1;
 
  for(i = 0; i < 4; i++) {
    int nx = x+dx[i], ny = y+dy[i], res;
    if(~p && i == (p+2)%4) continue;
    if(!is_in(nx, ny)) continue;
    swap(&mas[y][x], &mas[ny][nx]);
    res = dfs(nx, ny, i, dep+1, lim);
    if(~res) return res;
    swap(&mas[y][x], &mas[ny][nx]);
  }
 
  return -1;
}
  
void get_empty(int *x, int *y) {
  int i, j;
  for(i = 0; i < 4; i++) {
    for(j = 0; j < 4; j++) {
      if(!mas[i][j]) {
    *x = j;
    *y = i;
    return;
      }
    }
  }
}
 
int solve() {
  int x, y, lim, ans;
  get_empty(&x, &y);
  for(lim = 0; lim < 50; lim++) {
    copy(cpy, mas);
    if((ans = dfs(x, y, -1, 0, lim)) != -1) return ans;
    copy(mas, cpy);
  }
  return -1;
}
 
int main() {

   
  int i, j;
  for(i = 0; i < 4; i++)
    for(j = 0; j < 4; j++)
      scanf("%d", &mas[i][j]);

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



Measured memory_bytes: 2056
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int N = Reader.Int();
        var E = EdgeRead(N);
        int rem = DFS(0, -1, E);
        Console.WriteLine(rem > 0 ? "First" : "Second");
    }

    int DFS(int a, int prev, int[][] E)
    {
        int rem = 0;
        foreach (int b in E[a])
            if (b != prev)
                rem += DFS(b, a, E);
        if (rem == 0) return 1;
        if (rem == 1) return 0;
        return rem;
    }

    int[][] EdgeRead(int V, int E = -1, int origin = 1)
    {
        if (E == -1) E = V - 1;
        var es = new List<int>[V];
        for (int i = 0; i < V; i++) es[i] = new List<int>();
        for (int i = 0; i < E; i++)
        {
            int a = Reader.Int() - origin, b = Reader.Int() - origin;
            es[a].Add(b);
            es[b].Add(a);
        }
        return es.Select(a => a.ToArray()).ToArray();
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { Dispose(); A = new string[0]; }
    public static void Set(TextReader r) { Init(); reader = r; }
    public static void Set(string file) { Init(); reader = new StreamReader(file); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    public static void Dispose() { reader.Dispose(); }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}

Measured memory_bytes: 37000
n = int(input())
s = [sorted(list(input())) for i in range(n)]

s.sort()

ans = 0
cnt = 1
for i in range(n - 1):
    if (s[i] == s[i + 1]):
        cnt += 1
    else:
        ans += cnt * (cnt - 1) // 2
        cnt = 1
ans += cnt * (cnt - 1) // 2
print(ans)


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

int main() {
    int N, K, X, Y;
    int sum = 0;
    
    cin >> N >> K >> X >> Y;
    
    if ( N <= K ) {
        sum = N * X;
    } else {
        sum = K * X + (N - K) * Y;
    }
    
    cout << sum << endl;
    
    return 0;
}

Measured memory_bytes: 3628
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;
  
int n;
vector<int> G[MAX];
int color[MAX];
  
void dfs(int r, int c) {
    stack<int> S;
    S.push(r);
    color[r] = c;
    int i, u, v;
    while ( !S.empty() ) {
        u = S.top();
        S.pop();
        for ( i = 0; i < G[u].size(); i++ ) {
            v = G[u][i];
            if ( color[v] == NIL ) {
                color[v] = c;
                S.push(v);
            }
        }
    }
}
  
void assignColor() {
    int id = 0;
    int i, u;
    for ( i = 0; i < n; i++ ) color[i] = NIL;
    for ( u = 0; u < n; u++ ) {
        if ( color[u] == NIL ) dfs(u, id++);
    }
}
  
int main() {
    int s, t, m, q, i;
  
    cin >> n >> m;
  
    for ( i = 0; i < m; i++ ) {
        cin >> s >> t;
        G[s].push_back(t);
        G[t].push_back(s);
    }
  
    assignColor();
  
    cin >> q;
  
    for ( i = 0; i < q; i++ ) {
        cin >> s >> t;
        if ( color[s] == color[t] ) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }
  
    return 0;
}

Measured memory_bytes: 8992
#include<stdio.h>
int main()
{
	long long int n, s;
	scanf("%lld %lld", &n, &s);
	long long int i, j;
	long long int a[3003];
	for (i = 0; i < n; i++)
		scanf("%lld", &a[i]);
	long long int p = 998244353;
	long long int dp[3003][3003];
	for (i = 0; i <= s; i++)
		dp[0][i] = 0;
	dp[0][0] = 1;
	for (i = 0; i < n; i++)
	{
		for (j = 0; j <= s; j++)
			dp[i + 1][j] = dp[i][j];
		if (a[i] <= s)
			dp[i + 1][a[i]] = (dp[i + 1][a[i]] + i + 1) % p;
		for (j = 1; j <= s - a[i]; j++)
			dp[i + 1][j + a[i]] = (dp[i + 1][j + a[i]] + dp[i][j]) % p;
	}
	long long int ans = 0;
	for (i = n - 1; i >= 0; i--)
		ans = (ans + (dp[i + 1][s] - dp[i][s] + p) * (n - i) % p) % p;
	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 70656
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class AtCoderA
{
    static void Main()
    {
        var sc = new Scanner();

        int a = sc.NextInt();
        int b = sc.NextInt();

        string answer;
        if (a > 0 && b > 0)
        {
            answer = "Positive";
        }
        else if (a <= 0 && b >= 0)
        {
            answer = "Zero";
        }
        else
        {
            int count = b - a;
            if (count % 2 == 0)
            {
                answer = "Negative";
            }
            else
            {
                answer = "Positive";
            }
        }

        Console.WriteLine(answer);
    }

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}

Measured memory_bytes: 11092
i = int(input())
for n in range(1,i+1):
    if (n%3==0):
        print(' {}'.format(n),end="") # 개행하면 안되기 때문
    else:
        x = n
        while(x>0):
            if (x%10 == 3):
                print(' {}'.format(n),end="")
                break
            else:
                x = x//10
print()


Measured memory_bytes: 5876
/*----------------*/
/* 	^_HAR HAR MAHADEV_^
   	|Om namah shivaya|
*/
#include<bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
#define f(i,a,b) for(int i=a;i<b;i++)
#define ll long long int
#define fast std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define pb(temp) push_back(temp)
#define mp make_pair
#define mod 1000000007
#define br "\n"
//using namespace boost::multiprecision;
using namespace std ;
typedef unsigned long long int ulli;

ulli gcd(ulli x,ulli y)
{
    if(x==0) return y;
    return gcd(y%x,x);
}
ulli factorial(ulli n)
{
    if (n == 0)
    return 1;
    return n * factorial(n - 1);
}
ll findS(ll s)
{
	return (sqrtl(8*s + 1) - 1)/2;
}
ll prime(ll a){
    if(a==1)return 0;
    if(a==2 || a==3)return true;
    if(a%2==0 ||a%3==0)return 0;
    for(ll i=5;i<=sqrt(a);i+=6)
        if(a%i==0 || a%(i+2)==0)
            return 0;

    return 1;
}
ll power(ll x,ll y)
{
    if (y == 0)
        return 1;
    ll p = power(x, y/2) % mod;
    p = (p * p) % mod;

    return (y%2 == 0)? p : (x * p) % mod;
}
ll nCr(ll n,ll k)
{
    ll C[k+1];
    //memset(C, 0, sizeof(C));

    C[0] = 1;

    for (ll i = 1; i <= n; i++)
    {
        for (ll j = min(i, k); j > 0; j--)
            C[j] = (C[j] + C[j-1])%(ll)(1e9 + 7);
    }
    return C[k];
}
ll LOGN(ll n , ll r){
	return (n > r-1) ? (1 + LOGN(n / r , r)) : 0 ;
}
void dfs(vector<ll> v[], bool visited[], ll current_node){
	visited[current_node]=true;
	for(ll nxt_node : v[current_node]){
		if(visited[nxt_node]==false){
			dfs(v , visited, nxt_node);
		}
	}
	return;
}

void bfs(vector<ll> v[], bool visited[], ll root){

    int current_node;
    queue<int> q;

    q.push(root);

    while(!q.empty()){

        current_node=q.front();

        visited[current_node]=true;

        for(int x:v[current_node])
            if(!visited[x])
                q.push(x);
    }
}
int main(){
	fast;
	int  k , x ;
	cin >> k >> x ;
	if(k*500 >= x)cout << "Yes\n" ;
	else cout << "No\n" ;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long int a,b;
	cin>>a>>b;
	long int ans1;
	ans1=__gcd(a,b);;
	unsigned long long int ans= a*b;
	ans=ans/ans1;
	cout<<ans<<endl;
}

Measured memory_bytes: 3652
N=int(input())
W=list(map(int,input().split()))
all_left=sum(W)
all_right=0
ans=sum(W)
for w in W:
  all_left-=w
  all_right+=w
  if ans > abs(all_left-all_right):
    ans=abs(all_left-all_right)
print(ans)

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

int n, k;
string s, ss, ans;

string calc(string t){
    if (k==1) return t;
    string res=""; char c=t.back();
    while (t.size()&&t.back()==c){
        res+=t.back(); t.erase(--t.end());
    }
    for (int las=k-1;las--&&res.size()<n;){
        res=res+res; if (res.size()>n) res.resize(n);
    }
    while (res.size()<n){
        res+=t.back(); t.erase(--t.end());
    }
    return res;
}

int main(){
    cin>>n>>k>>s;
    ans="z"; ans[0]+=1;
    ss=s; reverse(ss.begin(),ss.end());
    ss=s+ss;
    for (int i=0;i+n<=ss.length();++i)
        ans=min(ans,calc(ss.substr(i,n)));
    cout<<ans<<endl;
}

Measured memory_bytes: 384
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#define For(i , j , k) for (int i = (int)(j) ; i <= (int)(k) ; ++ i)
#define Fordown(i , j , k) for (int i = (int)(j) ; i >= (int)(k) ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
using namespace std;

const int maxn = 100010;
int n , k , a[maxn] , flag;

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

int main()
{
	scanf("%d%d" , &n , &k);
	For(i , 1 , n)
	{
		scanf("%d" , &a[i]);
		if (a[i] >= k)
			flag = -1;
	}
	int tt = gcd(a[1] , a[2]);
	if (flag)
		For(i , 3 , n)
		{
			tt = gcd(a[i] , tt);
		}
	if (flag && k % tt == 0)
		puts("POSSIBLE");
	else
		puts("IMPOSSIBLE");
	return 0;
}


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

int main(){
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) cin >> a[i];
  int ball[n + 1];
  int ballCount = 0;

  // あとの方の箱から調べる
  for (int i = n; i >= 1; i--) {

     // その箱以降の倍数の箱のボールの状態をチェックしながらsumを作る
    int j = i * 2;
    int sum = 0;
    while (j <= n) {
      sum += ball[j];
      j += i;
    }

    // ボールを入れるか決める
    if(sum % 2 == a[i]){
      ball[i] = 0;
    }else{
      ball[i] = 1;
      ballCount++;
    }
  }

  // 出力用の箱のリストを作る
  ostringstream boxList;
  bool isFirstBox = true;
  for (int i = 1; i <= n; i++) {
    if (ball[i] == 1) {
      boxList << (isFirstBox ? "" : " ") << i;
      isFirstBox = false;
    }
  }

  cout << ballCount << endl;
  cout << boxList.str() << endl;
  return 0;
}


Measured memory_bytes: 4228
H, W, K = map(int, input().split())
cake = [input() for _ in range(H)]
# 答え
memo = [[0] * W for _ in range(H)]

# 行にケーキが何個あるか
cake_count_row = [0] * H
for y in range(H):
    count = 0
    for x in range(W):
        if cake[y][x] == "#":
            count += 1
    cake_count_row[y] = count

# イチゴがある行について色塗り
count = 1
for y in range(H):
    first = True
    if cake_count_row[y] == 0:
        continue 
    for x in range(W):
        if cake[y][x] == "#":
            if first:
                first = False
            else:
                count += 1
        memo[y][x] = count
    count += 1

# イチゴがない行について色塗り
# 上から下に
for y in range(1, H):
    if cake_count_row[y] == 0:
        for x in range(W):
            memo[y][x] = memo[y-1][x]
# 下から上に
for y in range(H-1)[::-1]:
    if cake_count_row[y] == 0:
        for x in range(W):
            memo[y][x] = memo[y+1][x]

# 出力
for i in range(H):
    print(" ".join(list(map(str, memo[i]))))

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

using namespace std;

int main() {

	// freopen("input.txt", "r", stdin);
	// freopen("output.txt", "w", stdout);

	int n;
	cin >> n;
	if (n % 2 == 0) cout << (n / 2) * (n + 1);
	else cout << n * ((n + 1) / 2);
	cout << '\n';
	return 0;
}

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

int main() {
 int n;
  string s;
  cin >> n>> s;
  int ans = 0;
  for(int i= 0;i<=n-3;++i){
    if(s.at(i) == 'A' && s.at(i+1) == 'B' && s.at(i+2) == 'C') ++ans;
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define INF 1e9
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define ALL(x) (x).begin(), (x).end()
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
const int MOD = 1000000007;

void solve() {
  int n;
  cin >> n;
  vector<pii> v(n);
  rep(i,n) cin >> v[i].first >> v[i].second;
  vector<double> u(n);
  rep(i,n) u[i] = i;
  double sum = 0;
  int count = 0;
  do {
    count++;
    double temp = 0;
    rep(i,n-1) {
      auto s = v[u[i]];
      auto t = v[u[i+1]];
      temp += sqrt(pow((s.first - t.first), 2) + pow((s.second - t.second), 2));
    }
    sum += temp;
  } while (next_permutation(u.begin(), u.end()));

  cout << setprecision(12) << sum / count << endl;
}

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

Measured memory_bytes: 3824
import java.util.Scanner;

class Main{
  public static void main(String[] args){
    Scanner sin = new Scanner(System.in);
    int a = sin.nextInt();
    int b = sin.nextInt();
    int ab = a*b;
    if(ab%2 == 0){
      System.out.println("Even");
    }else{
      System.out.println("Odd");
    }  
  }  
}

Measured memory_bytes: 23508
import java.util.Scanner;
     
    public class Main {
     
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int a = sc.nextInt();
    		int b = sc.nextInt();
    		int c = sc.nextInt();
    			if(a + b + c == 17 && a * b * c == 175){
    				System.out.println("YES");
    			}else
    				System.out.println("NO");
    	}
    }

Measured memory_bytes: 20820
use proconio::input;

fn main() {
    input! {
        mut a: u32,
        b: u32,
        mut c: u32,
        d: u32,
    }

    loop {
        c = c.saturating_sub(b);
        if c == 0 {
            println!("Yes");
            break;
        }
        a = a.saturating_sub(d);
        if a == 0 {
            println!("No");
            break;
        }
    }
}


Measured memory_bytes: 2116
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <algorithm>
#include <string>
#include <utility>
#include <set>
#include <stack>
#include <deque>
#include <ctime>
#include <random>
#include <cassert>
#include <cmath>
#include <climits>
#include <queue>
#include <cstring>
#include <bitset>
#include <iomanip>
#include <chrono>

/*
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("popcnt")
#pragma GCC target("avx2")
*/


#ifdef LOCAL
	#define dbg(x) cout << #x << " : " << x << endl;
#else 
	#define dbg(x)
#endif

#define int long long
#define fi(a,b) for (int i=a;i<b;i++)
#define fj(a,b) for (int j=a;j<b;j++)
#define fk(a,b) for (int k=a;k<b;k++)
#define fi1(a,b) for (int i=a-1;i>=b;i--)
#define fj1(a,b) for (int j=a-1;j>=b;j--)
#define fk1(a,b) for (int k=a-1;k>=b;k--)
#define fx(x,a) for (auto& x : a)
#define rep(i, a, b) for (int i = a; i < b; ++i)
#define rep1(i, a, b) for (int i = a - 1; i >= b; --i)
#define siz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;

template<typename T1, typename T2>inline void mine(T1 &x, T2 y) { if (x > y) x = y; }
template<typename T1, typename T2>inline void maxe(T1 &x, T2 y) { if (x < y) x = y; }

ostream& operator << (ostream &a, const vector<int> &b) {
    for (auto k : b) cout << k << ' ';
    return a;
}


typedef long long ll;
typedef unsigned long long ull;
typedef char ch;
typedef string str;
typedef pair<int, int> pii;
typedef map<int, int> mii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;
typedef vector<ch> vch;
typedef vector<vch> vvch;
typedef vector<str> vs;



const int MOD = 1000000007;
const int INF = 1000000050;
const long long BIG = (long long)2e18 + 50;
const int MX = 200010;
const double EPS = 1e-9;


mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    int n, k;
    cin >> n >> k;
    str s;
    cin >> s;
    int cur = 0;
    fi(0, n) {
    	cur += s[i] == 'R' && i != n - 1 && s[i + 1] == 'R';
    	cur += s[i] == 'L' && i != 0 && s[i - 1] == 'L';
    }

    cout << min(n - 1, cur + 2 * k) << '\n';

}

Measured memory_bytes: 512
def DepthFirst_search(S,pre):
    global out
    #print(out)
    #print('pre = {}'.format(pre))
    if len(S) == 1 :
        if pre + int(S[0]) == 7:
            out = out + '+{}'.format(int(S[0])) + '=7'
            return True
        if pre - int(S[0]) == 7:
            out = out + '-{}'.format(int(S[0])) + '=7'
            return True
        return False

    pre += int(S[0])
    if DepthFirst_search(S[1:len(S)],pre):
        #print(out)
        out = '+' + S[0] + out
        return True
    pre -= 2*int(S[0])
    if DepthFirst_search(S[1:len(S)],pre):
        #print(out)
        out = '-' + S[0] + out
        return True
    return False
out = ''
pre = 0
S = input()
DepthFirst_search(S,pre)

print(out[1:])


Measured memory_bytes: 3064
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long n,res=0;
  
  cin>>n;
  for(long long i=1;i<=n;i++)
  {
    if(i%3!=0&&i%5!=0)
      res=res+i;
  
  }
  cout<<res;
return 0;
}


Measured memory_bytes: 3636
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;

public class Main {

	int INF = 1 << 28;
	int w, h;
	boolean map[][][];
	
	void run() {
		Scanner sc = new Scanner(System.in);
		for(;;) {
			w = sc.nextInt();
			h = sc.nextInt();
			if( (w|h) == 0) break;
			map = new boolean[h][w][4];
			for(int i=0;i<h*2-1;i++) for(int j=0;j<(i%2==0? w-1:w);j++){
				int v = sc.nextInt();
				if(i%2==0) {
					map[i/2][j][2] = v == 0;
				}
				else {
					map[i/2][j][3] = v == 0;
				}
			}
			for(int i=0;i<h;i++) for(int j=1;j<w;j++) {
				map[i][j][0] = map[i][j-1][2];
			}
			for(int i=1;i<h;i++) for(int j=0;j<w;j++) {
				map[i][j][1] = map[i-1][j][3];
			}
//			for(boolean[][] a: map) debug(a);
			System.out.println(dfs());
		}
	}
	int dx[] = {-1,0,1,0};
	int dy[] = {0,-1,0,1};
	
	int dfs() {
		LinkedList<P> que = new LinkedList<P>();
		que.add(new P(0, 0, 1));
		boolean visited[][] = new boolean[h][w];
		visited[0][0] = true;
		for(;!que.isEmpty();) {
			P now = que.removeFirst();
			if( now.x == w-1 && now.y == h-1 ) return now.c;
			for(int i=0;i<4;i++) if(map[now.y][now.x][i]) {
				if(!visited[now.y+dy[i]][now.x+dx[i]]) {
					visited[now.y+dy[i]][now.x+dx[i]] = true;
					que.add(new P(now.x+dx[i], now.y+dy[i], now.c+1));
				}
			}
		}
		
		return 0;
	}
	
	class P {
		int x, y, c;
		P(int x, int y, int c) {
			this.x = x; 
			this.y = y;
			this.c = c;
		}
	}

	public static void main(String[] args) {
		new Main().run();
	}

	void debug(Object... os) {
		System.err.println(Arrays.deepToString(os));
	}
}

Measured memory_bytes: 12000
#include <bits/stdc++.h>
#include <vector>
#include <cmath>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP2(i, m, n) for (int i = (m); i < (int)(n); ++i)
#define REPR(i, n) for (int i = (n)-1; i >= 0; --i)
#define REPR2(i, m, n) for (int i = (n)-1; i >= (m); --i)
#define REPx(x, a) for(auto x : a)
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define SORTG(a) sort(ALL(a), greater<int>())
#define REVERSE(a) reverse(ALL(a))
#define bit_search(bit, n) REP(bit, 1<<(n))
#define bit_check(bit, i) (bit>>(i)) & 1
#define setpre(n) fixed << setprecision((n))
#define UNIQUE(a) do {SORT(a); (a).erase(unique(ALL(a)), (a).end());} while(0)
#define MAX(a) *max_element(ALL(a))
#define MIN(a) *min_element(ALL(a))
#define bisect_left(a, x) lower_bound(ALL(a), (x)) - a.begin()
#define bisect_right(a, x) upper_bound(ALL(a), (x)) - a.begin()
#define INPUT(a) REP(i, a.size()) cin >> a[i];
#define INPUTP(a) REP(i, a.size()) cin >> a[i].first >> a[i].second;
#define ENDL cout << endl;
using namespace std;
using ll = long long;
using ld = long double;
using PII = pair<int, int>;
using VPII = vector<PII>;
using VI = vector<int>;
using VVI = vector<VI>;
using Vll = vector<ll>;
using VVll = vector<Vll>;
using VC = vector<char>;
using VS = vector<string>;
using VB = vector<bool>;
const int INF = 1e9;
const ll MOD = 1e9 + 7;

VI ct(13, 0);
VB table(24, false);
VI uni;

int min_dis()
{
  int now = 0, pre = 0;
  int res = INF;
  REP(i, 23)
  {
    now++;
    if (table[now])
    {
      res = min(res, now-pre);
      pre = now;
    }
  }
  res = min(res, 24-pre);
  return res;
}

int main()
{
  int N;
  cin >> N;
  REP(i, N)
  {
    int d;
    cin >> d;
    ct[d]++;
  }
  ct[0]++;
  table[0] = true;
  if (MAX(ct) > 2 || ct[0] > 1 || ct[12] > 1)
  {
    cout << 0 << endl;
    return 0;
  }
  REP(i, 13)
  {
    if (i == 0 || i == 12) {if (ct[i] > 0) table[i] = true;}
    else
    {
      if (ct[i] == 1) uni.push_back(i);
      if (ct[i] == 2)
      {
        table[i] = true;
        table[24-i] = true;
      }
    }
  }
  int ans = 0;
  int len = uni.size();
  bit_search(bit, len)
  {
    VI set;
    REP(i, len)
    {
      if (bit_check(bit, i)) {table[uni[i]] = true; set.push_back(uni[i]);}
      else {table[24-uni[i]] = true; set.push_back(24-uni[i]);}
    }
    ans = max(ans, min_dis());
    REPx(x, set) table[x] = false;
  }

  cout << ans << endl;

  return 0;
}


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

int main()
{
	int a, b, c;
	scanf("%d %d %d", &a, &b, &c);
	if(a + b >= c) printf("Yes\n");
	else printf("No\n");
}

Measured memory_bytes: 128
# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce
# stdin = open("sample.txt")

MOD = 1000000007

def keta(kazu):
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers):
    return reduce(math.gcd, numbers)

def combination(m,n):
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  # (x**n)の計算の高速化
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x
    
def main():
    x = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
    print(x.index(0)+1)
main()

Measured memory_bytes: 9968
#include <bits/stdc++.h>
using namespace std;
using P = pair<int, int>;

const int INF = 1 << 30;

int h[110000];
int dp[110000];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) cin >> h[i];
  for (int i = 1; i < n; i++) {
    dp[i] = INF;
    for (int j = 1; j <= k; j++) {
      if (i - j < 0) break;
      dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
    }
  }
  cout << dp[n - 1] << endl;
}

Measured memory_bytes: 1024
import sys


def input():
    return sys.stdin.readline().rstrip()


def solve():
    n = int(input())
    c = list(map(lambda x : int(x)-1, input().split()))
    # c = [x-1 for x in c]
    # print(c)
    adj = [[] for i in range(n)]
    for i in range(n-1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        adj[a].append(b)
        adj[b].append(a)

    # size = [1 for i in range(n)]
    stack = [[] for color in range(n)]
    # in_time = [-1 for i in range(n)]
    # out_time = [-1 for i in range(n)]
    size = [1]*n
    # stack = [[]]*n
    in_time = [-1]*n
    out_time = [-1]*n
    timer = 0
    total = n*(n+1)//2
    # ans = [total for color in range(n)]
    ans = [total]*n
    # print(ans)

    def dfs(parent, root):
        nonlocal timer
        in_time[root] = timer
        timer += 1

        for child in adj[root]:
            if parent == child:
                continue
            dfs(root, child)
            size[root] += size[child]

            cnt = size[child]
            while stack[c[root]]:
                x = stack[c[root]][-1]
                if in_time[x] > in_time[root] and out_time[x] != -1:
                    cnt -= size[x]
                    stack[c[root]].pop()
                else:
                    break
            ans[c[root]] -= cnt*(cnt+1)//2

        out_time[root] = timer
        timer += 1

        stack[c[root]].append(root)

    sys.setrecursionlimit(10**6)

    dfs(0, 0)
    # print(size)
    for color in range(n):
        cnt = n
        while len(stack[color]) > 0:
            x = stack[color][-1]
            cnt -= size[x]
            stack[color].pop()
        # print("node:", -1, "color:", color, "cnt:", cnt)
        ans[color] -= cnt*(cnt+1)//2
        print(ans[color])


solve()


Measured memory_bytes: 413692
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define rep1(i, n) for (ll i = 1; i <= (n); ++i)
#define rrep(i, n) for (ll i = (n - 1); i >= 0; --i)
#define perm(c) sort(ALL(c));for(bool c##p=1;c##p;c##p=next_permutation(ALL(c)))
#define ALL(obj) (obj).begin(), (obj).end()
#define pb push_back
#define to_s to_string
#define len(v) (ll)v.size()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define print(x) cout << (x) << '\n'
#define debug(x) cout << #x << ": " << (x) << '\n'
using namespace std;
using ll = long long;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vector<ll>> vec2;
typedef vector<vector<vector<ll>>> vec3;
ll MOD = 1e9 + 7;
ll devc(ll x, ll y) { return 1 + (x - 1) / y; }
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; }

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

    ll A, B;
    cin >> A >> B;
    if((A + B) % 2 == 0) print((A+B)/2);
    else print("IMPOSSIBLE");

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const ll N = 1e9+7; 
const ll mx = 1e5+5;
vector<ll> g[mx];
ll ans=1;
ll n,k;
void dfs(ll x, ll f, ll r)
{
  ans*=(k-r);
  ans%=N;
  ll cnt=0;
  for(ll i=0;i<g[x].size();i++)
  {
    ll y= g[x][i];
    if(y==f) continue;
    if(x==1) dfs(y,x,cnt+1);
    else dfs(y,x,cnt+2);
    cnt++;
  }
}

int main()
{
  cin>>n>>k;
  ll i,j,a,b;
  for(i=1;i<n;i++)
  {
    cin>>a>>b;
    g[a].pb(b);
    g[b].pb(a);
  }
  dfs(1,0,0);
  cout<<ans<<"\n";
  return 0;
}

Measured memory_bytes: 10368
//#include"pch.h"
//#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;

int main() {
	int N;
	cin >> N;
	if (N % 10 == N / 10 % 10 && N % 10 == N / 100 % 10 || N / 10 % 10 == N / 100 % 10 && N / 10 % 10 == N / 1000) {
		puts("Yes");
	}else puts("No");
	
	return(0);
}

Measured memory_bytes: 256
s = list(input())
x = 0
y = 0

if (s.count("S") > 0 and s.count("N") > 0) or (s.count("S") == 0 and s.count("N") == 0):
    if (s.count("E") > 0 and s.count("W") > 0) or (s.count("E") == 0 and s.count("W") == 0):
        print("Yes")
        exit()
    
print("No")


Measured memory_bytes: 3060
#include<bits/stdc++.h>
#define int long long 
#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define all(a) a.begin(),a.end()
#define ld long double
#define ub upper_bound
#define lb lower_bound
#define pii pair<int,int>
using namespace std;
const int N = 1e3+1;
const int H = 1e4+1;
int dp[N][H];
int d[N],c[N];
int n;
int solve(int idx,int rem){
	if(idx == n+1){
		if(rem<=0)
			return 0;
		return 1e15;
	}
	if(rem<=0)
		return 0;
	if(dp[idx][rem]!=-1)
		return dp[idx][rem];
	int ans = 1e15;
	ans = min(c[idx]+solve(idx,rem-d[idx]),c[idx]+solve(idx+1,rem-d[idx]));
	ans = min(ans,solve(idx+1,rem));
	dp[idx][rem] = ans;
	return ans;
}
signed main(){
    fast;
   	int h;
   	cin>>h>>n;
   	for(int i=1;i<=n;i++){
   		cin>>d[i]>>c[i];
   	}
   	memset(dp,-1,sizeof(dp));
   	cout<<solve(1,h);
}  

Measured memory_bytes: 79104
#include <bits/stdc++.h>
#define all(a) (a).begin(),(a).end()
#define rep(i,n) for(ll i=0,i##_len=(n);i<i##_len;i++)
#define rep2(i,a,b) for(ll i=(a),i##_len=(b);i<=i##_len;i++)
#define rrep(i,n) ll i=(n);ll now=-1;while(i--&&(now++||1))
#define Yes(n) cout<<((n)?Yes:No)
#define vll vector<ll>
#define pb push_back
using namespace std;
using ll = long long;

static inline void ignore_ret(int x) {x++;}
template<class T> inline bool chmax(T&a,const T&b){if(a<b){a=b;return true;}return false;}
template<class T> inline bool chmin(T&a,const T&b){if(a>b){a=b;return true;}return false;}
template<class T> inline T gcd_vec(vector<T>v){T r=v.at(0);for(T i=1;i<v.size();i++)r=gcd(r,v.at(i));return r;}
template<class T> inline T lcm_vec(vector<T>v){T r=v.at(0);for(T i=1;i<v.size();i++)r=lcm(r,v.at(i));return r;}
template<class T> inline T factorial(T n){if(n==0)return 1;T r=1;for(T i=2;i<=n;i++)r*=i;return r;}
template<class T> inline T nCr(T n,T k){if(n<k||n<0||k<0)return 0;T r=1;k=min(k,n-k);for(T i=n-k+1;i<=n;i++)r*=i;return r/factorial(k);}
template<class T> inline T dot_product(vector<T>u,vector<T>v){T r=0;for(T i=0;i<u.size();i++)r+=u.at(i)*v.at(i);return r;}
template<class T> inline T sum(vector<T>v){return reduce(all(v),0LL);}

// Template ends here

vector<vll> dp(110, vll(115000, 0));

void solve(ll N, ll W, vector<ll> w, vector<ll> v){
    rep2(j, 1, 110000) {
        dp.at(0).at(j) = LLONG_MAX / 2;
    }

    rep2(i, 1, N) {
        rep2(j, 0, 110000) {
            dp.at(i).at(j) = dp.at(i - 1).at(j);
            if (j >= v.at(i - 1)) dp.at(i).at(j) = min(dp.at(i).at(j),
                                                       dp.at(i - 1).at(j - v.at(i - 1)) + w.at(i - 1));
        }
    }

    ll ans = 0;

    rep2(i, 0, 110000) {
        if (dp.at(N).at(i) <= W) ans = i;
    }

    cout << ans << endl;
}

int main() {
    ll N;
    ignore_ret(scanf("%lld",&N));
    ll W;
    ignore_ret(scanf("%lld",&W));
    vector<ll> w(N);
    vector<ll> v(N);
    for(int i = 0 ; i < N ; i++){
        ignore_ret(scanf("%lld",&w[i]));
        ignore_ret(scanf("%lld",&v[i]));
    }
    solve(N, W, move(w), move(v));
    return 0;
}


Measured memory_bytes: 100096
from itertools import permutations 
h,w = map(int,input().split())
c = [[int(i) for i in input().split()] for _ in range(10)]
a = [[int(i) for i in input().split()] for _ in range(h)]
ans = 0
lst = [int(i) for i in range(10) if i != 1]
mp = [0]*10

for i in lst:
    mp[i] = c[i][1]
    for j in range(1,9):
        for k in permutations(lst,j):
            newlst = list(k)
            cnt = c[i][newlst[0]]
            for l in range(j-1):
                cnt += c[newlst[l]][newlst[l+1]]
            cnt += c[newlst[-1]][1]
            if cnt < mp[i]:
                mp[i] = cnt

for i in range(h):
    for j in range(w):
        if abs(a[i][j]) != 1:
            ans += mp[a[i][j]]

print(ans)


Measured memory_bytes: 47332
#include <iostream>
#include <cstdint>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iomanip>
#include <sstream>
 
int main(void)
{
    int64_t N;
    std::cin >> N;

    std::vector<int64_t> A(N+1, 0);

    for (int64_t i = 1; i <= N; ++i) {
        std::cin >> A.at(i);
    }

    std::vector<int64_t> box(N+1, 0);

    int64_t ball_num = 0;
    std::vector<int64_t> ans;

    for (int64_t i = N; i >= 1; --i) {
        int64_t m_num = N / i;
        int64_t count = 0;
        for (int64_t j = m_num; j >= 1; --j) {
            int64_t idx = i * j;
            count += box.at(idx);
        }

        int64_t mod = count % 2;
        if (A.at(i) != mod) {
            box.at(i) = 1;
            ball_num++;
            ans.push_back(i);
        }
    }

    std::cout << ball_num << std::endl;
    if (ball_num == 0) return 0;

    std::sort(ans.begin(), ans.end());
    for (auto value : ans) {
        std::cout << value << " ";
    }
    std::cout << std::endl;

    return 0;
}

Measured memory_bytes: 4980
#ifdef _DEBUG
#include "MyLib.h"
#else
#define main_C main
#include "bits/stdc++.h"
#include <regex>
#define _USE_MATH_DEFINES
#include <math.h>
#define FOR(i,s,e) for (int i = int(s); i < int(e); ++i)
#define REP(i,e) FOR(i,0,e)
#define INF (INT_MAX/2)
#define EPS (1.0e-8)
#define LINF (LONG_MAX/2)
const int MGN = 8;
const int ARY_SZ_MAX = 10000000;
using namespace std;
using ll = long long; using ull = unsigned long long;
using vi = vector<int>; using vvi = vector<vi>; using vvvi = vector<vvi>;
using vb = vector<bool>; using vvb = vector<vb>; using vvvb = vector<vvb>;
using vl = vector<ll>; using vvl = vector<vl>;
using vd = vector<double>; using vs = vector<string>;
using pii = pair<int, int>; using pll = pair<ll, ll>;
// functions
ll count_div(ll n, ll m) {
    if (n%m != 0) return 0;
    else return 1 + count_div(n/m, m);
}
#endif


int main_C() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int D, G; cin >> D >> G;
    vi p(D+1,0), c(D+1,0); FOR(i, 1, D+1) cin >> p[i] >> c[i];
    
    int M = 0x01 << D;

    int ans = INF;
    REP(i,M) {
        int ptn = i;
        int mask = 0x01;
        int score = 0;
        int num = 0;
        for (int j = D; j >= 1; j--) {
            if (ptn  & mask) {
                score += 100 * j* p[j] + c[j];
                num += p[j];
                if (score >=G) ans = min(ans, num);
            } else {
                REP(k, p[j]) {
                    if (score + 100*j*k >=G) {
                        ans = min(ans, num+k);
                        break;
                    }
                }
            }
            mask <<= 1;
        }
    }
    
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
int main()
{
    int A,B;
    char op;
    scanf("%d %c %d",&A,&op,&B);
    if(op=='+')
    {
        printf("%d",A+B);
    }
    else
    {
       printf("%d",A-B);
    }
    return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); i++)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
 
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
        return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

int main() {
        ll n, k;
        cin >> n >> k;

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

        vector<ll> x(60, 0);
        rep (i, 60) {
                for (int j = 60; j >= 0; j--) {
                        if (x[i] + (1LL << j) > k) continue;
                        int t = 0;
                        rep (k, n) {
                                if ((1LL << j) & a[k]) {
                                        t++;
                                }
                        }

                        if ((j < i && t < n - t) || j >= i) {
                                x[i] += 1LL << j;
                        }
                }
        }

        vector<ll> r(60, 0);
        rep (i, 60) {
                rep (j, n) r[i] += x[i] ^ a[j];
        }

        cout << *max_element(r.begin(), r.end()) << endl;

        return 0;
}


Measured memory_bytes: 1024