akhauriyash/Code-Regression huggingface dataset:
package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

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

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

// Sort
type Val struct {
	id, num int
}
type ByNum []Val

func (a ByNum) Len() int           { return len(a) }
func (a ByNum) Less(i, j int) bool { return a[i].num < a[j].num }
func (a ByNum) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

type Info struct {
	x, y, h int
}

type ByH []Info

func (a ByH) Len() int           { return len(a) }
func (a ByH) Less(i, j int) bool { return a[i].h > a[j].h }
func (a ByH) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	info := make([]Info, N)
	for i := 0; i < N; i++ {
		info[i] = Info{sc.nextInt(), sc.nextInt(), sc.nextInt()}
	}
	sort.Sort(ByH(info))
	for x := 0; x <= 100; x++ {
		for y := 0; y <= 100; y++ {
			hDecided := false
			var H int
			flag := true
			for i := 0; i < N; i++ {
				in := info[i]
				h, ok := f(x, y, in.x, in.y, in.h)
				if ok {
					if !hDecided {
						hDecided = true
						H = h
					}
					if h != H || h < 1 {
						flag = false
						break
					}
				} else {
					if !hDecided {
						hDecided = true
						H = abs(x-in.x) + abs(y-in.y)
					} else if H-abs(x-in.x)-abs(y-in.y) <= 0 {
						continue
					} else {
						flag = false
						break
					}
				}
			}
			if flag {
				fmt.Fprintln(wtr, x, y, H)
				wtr.Flush()
				return
			}
		}
	}
}

func f(X, Y, cx, cy, ch int) (int, bool) {
	if ch == 0 {
		return 0, false
	}
	return (ch + abs(X-cx) + abs(Y-cy)), true
}


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

#define for_(i,a,b) for(int i=a;i<b;++i)
#define ALL(a) a.begin(),a.end()
#define INIT(a,b) memset(a,b,sizeof(a))
using lint = long long int;

int main() {
    int N, M;
    cin >> N >> M;

    vector<string> S(N);
    for_(i,0,N) cin >> S[i];

    auto get_id = [&](int y, int x) {
        return M * y + x;
    };

    using pii = pair<int, int >;
    vector<pii> L, R;

    mf_graph<int> mf(N * M + 2);
    for_(y,0,N) for_(x,0,M) {
        int u = get_id(y, x);
        bool flag = (y + x) & 1;
        if (x < M - 1 and S[y][x] != '#' and S[y][x + 1] != '#') {
            int v = get_id(y, x + 1);
            if (!flag) mf.add_edge(u, v, 1);
            else mf.add_edge(v, u, 1);
            L.push_back(pii(y, x));
            R.push_back(pii(y, x + 1));
        }
        if (y < N - 1 and S[y][x] != '#' and S[y + 1][x] != '#') {
            int v = get_id(y + 1, x);
            if (!flag) mf.add_edge(u, v, 1);
            else mf.add_edge(v, u, 1);
            L.push_back(pii(y, x));
            R.push_back(pii(y + 1, x));
        }
        if (!flag) mf.add_edge(N * M, u, 1);
        else mf.add_edge(u, N * M + 1, 1);
        L.push_back(pii(-1, -1));
        R.push_back(pii(-1, -1));
    }

    int f = mf.flow(N * M, N * M + 1);
    cout << f << endl;

    int m = L.size();
    for_(i,0,m) {
        mf_graph<int>::edge e = mf.get_edge(i);
        if (e.flow == 1 and L[i].first != -1) {
            int sy = L[i].first, sx = L[i].second;
            int ey = R[i].first, ex = R[i].second;
            if (sy == ey) {
                S[sy][sx] = '>';
                S[ey][ex] = '<';
            }
            else {
                S[sy][sx] = 'v';
                S[ey][ex] = '^';
            }
        }
    }

    for_(y,0,N) cout << S[y] << endl;
}

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

using namespace std;

#define debug(n) cerr << #n << ':' << n << endl;
#define dline    cerr << __LINE__ << endl;

using ll = long long;
using ull = unsigned long long;
template<class T, class U> using P = pair<T,U>;
template<class T> using Heap = priority_queue<T>;
template<class T> using heaP = priority_queue<T,vector<T>,greater<T>>;
template<class T,class U> using umap = unordered_map<T,U>;
template<class T> using uset = unordered_set<T>;

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

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

template<class T, size_t N, class U>
void Fill(T (&a)[N], const U&v){
    fill((U*)a,(U*)(a+N),v);
}

template<class T>
istream& operator >> (istream&is, vector<T>&v){
  for(auto&e:v)is >> e;
  return is;
}

int main(){
  int n; cin >> n;
  vector<int> v(n);
  cin >> v;
  sort(v.begin(), v.end());
  cout << v.back() - v.front() << endl;
}


Measured memory_bytes: 3600
#include <iostream>

using namespace std;

int main(void)
{
  long long x;
  cin >> x;
  int i = 1;
  long long n = 100;
  while (1) {
    n *= 1.01;
    if (n >= x) break;
    i++;
  }
  cout << i << endl;
  return 0;
}


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

#define each(i, c) for (auto& i : c)
#define unless(cond) if (!(cond))

using namespace std;

typedef long long int lli;
typedef unsigned long long ull;
typedef complex<double> point;

template <typename P, typename Q>
ostream& operator<<(ostream& os, pair<P, Q> p)
{
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <typename T>
ostream& operator<<(ostream& os, vector<T> v)
{
  os << "(";
  each (i, v)
    os << i << ",";
  os << ")";
  return os;
}

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

  const lli mod = 1e9 + 7;
  const int N = 1e5 + 40;
  const int K = 32;

  static int nck[N][K];
  fill(&nck[0][0], &nck[N - 1][K - 1] + 1, 0);
  for (int i = 0; i < N; ++i) {
    nck[i][0] = 1;
  }
  for (int n = 1; n < N; ++n) {
    for (int k = 1; k < K; ++k) {
      nck[n][k] = nck[n - 1][k - 1] + nck[n - 1][k];
      nck[n][k] %= mod;
    }
  }

  int n, m;
  while (cin >> n >> m) {
    map<int, int> cnt;
    for (int i = 2; i * i <= m; ++i) {
      while (m % i == 0) {
        ++cnt[i];
        m /= i;
      }
    }
    if (m != 1) ++cnt[m];

    lli x = 1;
    each (i, cnt) {
      x *= nck[n + i.second - 1][i.second];
      x %= mod;
    }
    cout << x << endl;
  }

  return 0;
}


Measured memory_bytes: 12800
#include<iostream>
#include<string>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<tuple>
#include<vector>
#include<map>
#include<numeric>
#include<limits>
#include<type_traits>
using namespace std;


int main(void){
  //入力
  long long int N;
  vector<long long int > a, sort_a;
  cin >> N;
  for(int i = 0; i < N; i++){
    long long int temp;
    cin >> temp;
    a.push_back(temp);
    sort_a.push_back(temp);
  }
  //まず全体のmaxとその次を記録する．
  sort(sort_a.begin(), sort_a.end(), greater<long long int>());
  long long int max = sort_a[0], sec_max = sort_a[1];
  //2番目まで同じだったら全て同じ値を出力
  if(max == sec_max){
    for(int i = 0; i < N; i++){
      cout << max << endl;
    }
  }
  //maxとsec_maxが違う場合はmaxのところのみsec_max
  else{
    for(int i = 0; i < N; i++){
      if(a[i] == max){
        cout << sec_max << endl;
      }
      else{
        cout << max << endl;
      }
    }
  }
  return 0;
}


Measured memory_bytes: 5476
#define _USE_MATH_DEFINES
#include  <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include <queue>
#include<map>
#include <sstream>
#include<set>
#include<stack>

//#include<bits/stdc++.h>


using namespace std;



int main() {
	
	int n, m;
	cin >> n >> m;
	vector<pair<int, int>>x(n);
	for (int i = 0; i < n; i++) {
		cin >> x[i].first >> x[i].second;
	}
	sort(x.begin(), x.end());
	long long int t = 0;
	int y = m;
	for(int i=0;i<n;i++){
		long long int a = min(x[i].second, y);
		t += a*x[i].first;
		y -= a;
	}
	cout << t << endl;
	return 0;

}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i< int (n); i++)
int main(){

long long int x=0;
cin >> x;
long long int a=0, b=0;
a=x/500;
b=(x%500)/5;
cout << 1000*a+5*b << endl;

}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary1
{
    public class ListNode
    {
        public int val;
        public ListNode next;
        public ListNode(int x) { val = x; }
    }


    public class Class1
    {

        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split(' ');
            int a = Int32.Parse(s[0]);
            int b = Int32.Parse(s[1]);

            if ( a > 0 && a < 10)
            {
                if (b > 0 && b < 10)
                {
                    Console.WriteLine(a * b);
                    return;
                }

            }
            Console.WriteLine(-1);



        }

        static int rec(int[] n, int i, int j, int x)
        {
            if (i + 1 == j)
            {
                return Math.Min(n[j], x);
            }
            return rec(n, i + 1, j, n[i]) + rec(n, i, j - 1, n[j]);
        }


    }
}


Measured memory_bytes: 13140
#import sys
MOD = 10 ** 9 + 7
#input = sys.stdin.readline
import math
import itertools

#import bisect

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default


def main():
#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]
    a=int(input())
    b=int(input())
    print(6-a-b)

if __name__ == "__main__":
    main()

Measured memory_bytes: 3064
#include <iostream>
using namespace std;
main(){
int s,t;
while(cin>>s>>t){
if((s||t)==0) break;
int m=s+t,mi=0;
for(int i=1;i<5;i++){
cin>>s>>t;
if(m<s+t){
m=s+t;
mi=i;
}
}
cout<<(char)('A'+mi)<<" "<<m<<endl; 
}
}

Measured memory_bytes: 0
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <cstdio>
#include <cmath>
#include <sstream>
#include <algorithm>

using namespace std;

vector<vector<int> > d;
const int INF = 100000000;

void warshall_floyd(int n) {
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			for (int k = 0; k < n; k++) {
				d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
			}
		}
	}
}

int main() {
	int n, m, r;
	cin >> n >> m >> r;
	vector<int> rr;
	for (int i = 0; i < r; i++) {
		int t;
		cin >> t;
		rr.push_back(t);
	}

	d.resize(n+1);
	for (int i = 0; i < n+1; i++) {
		d[i].resize(n+1);
		for (int j = 0; j < n+1; j++) {
			d[i][j] = INF;
			d[i][i] = 0;
		}
	}
	for (int i = 0; i < m; i++) {
		int from, to, cost;
		cin >> from >> to >> cost;
		d[from][to] = cost;
		d[to][from] = cost;
	}
	warshall_floyd(n+1);
	sort(rr.begin(), rr.end());
	int min = INF+1;
	do{
		int total = 0;
		for (int i = 1; i < r; i++) {
			total += d[rr[i - 1]][rr[i]];
		}
		if (min > total) min = total;
	} while (next_permutation(rr.begin(), rr.end()));
	cout << min << endl;
}


Measured memory_bytes: 384
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt(), drinkIdx, drinkVal, ans = 0;
		int[] t = new int[n+1];
		for (int i = 1; i <= n; i++) t[i] = sc.nextInt();
		int m = sc.nextInt();
		for (int i = 1; i <= m; i++) {
			drinkIdx = sc.nextInt();
			drinkVal = sc.nextInt();
			ans = 0;
			for (int j = 1; j <= n; j++) {
				if (drinkIdx != j) ans += t[j];
				else ans += drinkVal;
			}
			System.out.println(ans);
		}
	}

}

Measured memory_bytes: 21716
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
using namespace std;

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

int main(){
    int x;
    cin >> x;
    if(x >= 400 && x < 600) cout << 8 << endl;
    if(x >= 600 && x < 800) cout << 7 << endl;
    if(x >= 800 && x < 1000) cout << 6 << endl;
    if(x >= 1000 && x < 1200) cout << 5 << endl;
    if(x >= 1200 && x < 1400) cout << 4 << endl;
    if(x >= 1400 && x < 1600) cout << 3 << endl;
    if(x >= 1600 && x < 1800) cout << 2 << endl;
    if( x >= 1800 && x < 2000) cout << 1 << endl;
    return 0;
}

Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int A,B,C,D;
  cin>>A>>B>>C>>D;
  if(A+B > C+D){
    cout<<"Left"<<endl;
  }
  if(A+B == C+D){
    cout<<"Balanced"<<endl;
  }
  if(A+B < C+D){
    cout<<"Right"<<endl;
  }
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long int ll;
int N;
ll A[200000];
ll AA[200000];
int l = 0;
int r = 1;
int LL,RR,CC;
int main()
{
	int i,j,k;
	ll ans = 0;
	ll sum = 0;
	ll sum1; 
	ll sum2;
	scanf("%d", &N);
	for (i = 0; i < N; i++){
		scanf("%lld", &A[i]);
		A[i + N] = A[i];
	}
	AA[0] = A[0];
	for (i = 1; i < 2*N; i++)AA[i] = AA[i - 1] + A[i];
	while (l < N){
		if(l!=0)sum = AA[r-1] - AA[l-1];
		else sum = AA[r-1];
		//printf("%d %d %lld\n", l, r,sum);
		if (sum > AA[N-1] / 3){
			l++;
		}
		else{
			LL = r; RR = N + l;
			while (RR - LL>1){
				CC = (LL + RR) / 2;
				//printf(":%d %d %lld\n", LL, RR, AA[CC] - AA[r-1]);
				if(AA[CC] - AA[r-1] < sum)LL = CC;
				else RR = CC;
			}
			LL-=3;
			if (LL <= -1)LL = 0;
			while (AA[LL] - AA[r - 1] < sum)LL++;
			sum1 = AA[LL] - AA[r - 1];
			sum2 = AA[N-1]-sum-sum1;
			//printf("::%d %lld %lld\n", LL, sum1, sum2);
			if (sum <= sum1&&sum <= sum2){
				ans = max(ans, sum);
				r++;
			}
			else l++;
		}
	}
	printf("%lld\n", ans);
	return 0;
}

Measured memory_bytes: 4316
N = int(input())

max_count = 0
max_num = 1
for num in range(1, N+1):
    num_ = num
    count = 0
    rest = 0
    while rest == 0:
        rest = num_ % 2
        num_ = num_ / 2
        if rest == 1:
            continue
        else:
            count += 1
    if count > max_count:
        max_count = count
        max_num = num
print(max_num)

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

using namespace std;

int W, H, x, y;

int main() {
    scanf("%d%d%d%d", &W, &H, &x, &y);
    printf("%.12lf %d\n", double(W) * H / 2, int(x * 2 == W && y * 2 == H));
}


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


int main() {
    int a, b;
    cin >> a >> b;
    if(a > 12){
        cout << b;
    }  
    else if(a <= 12 && a > 5){
        cout << b/2;
    }
    else{
        cout << 0;
    }
    cout << endl;
}


Measured memory_bytes: 256
use std::io::*;
use std::str::FromStr;
use std::cmp;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn read_str() -> Vec<char> {
    return read::<String>().chars().collect();
}

pub fn main() {
    let b = read_str();
    let mut max_len = 0;
    for begin in 0..b.len() {
        for end in begin..b.len() + 1 {
            //b[begin..end].iter().all(|c1| "ATGC".chars().any(|c2| c1 == c2));
            if b[begin..end].iter().all(
                |c| "ATGC".chars().collect::<Vec<char>>().contains(c)) {
                let len = end - begin;
                max_len = cmp::max(max_len, len);
            }
        }
    }
    println!("{}", max_len);
}


Measured memory_bytes: 4476
import itertools
 
N,M,Q=map(int,input().split())
all=[]
 
for i in range(Q):
  all.append(list(map(int,input().split())))
#print(all)
#print(len(all))
#print(all[0][1])
 
x=sorted([i for i in range(1,M+1)])
maxdsum=0
 
 
for A in itertools.combinations_with_replacement(x, N):
  dsum=0
  #print("A=="+str(A))
  for num in range(Q):
    if A[all[num][1]-1]-A[all[num][0]-1]==all[num][2]:
      dsum+=all[num][3]
  if maxdsum<dsum:
    maxdsum=dsum
print(maxdsum)

Measured memory_bytes: 9216
//
// Created by moono on 2018/10/27.
//

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



int main() {
    //
    int H, W;
    cin >> H >> W;

    //
    char table[H][W];
    for (int i = 0; i < H; ++i) {
        for (int j = 0; j < W; ++j) {
            cin >> table[i][j];
        }
    }

    //
    for (int i = 0; i < W+2; ++i) {
        cout << "#";
    }
    cout << endl;

    //
    for (int i = 0; i < H; ++i) {
        cout << "#";
        for (int j = 0; j < W; ++j) {
            cout << table[i][j];
        }
        cout << "#" << endl;
    }

    //
    for (int i = 0; i < W+2; ++i) {
        cout << "#";
    }
    cout << endl;


    return 0;
}

Measured memory_bytes: 256
open Scanf
open Printf

let stoa s = Array.init (String.length s) (String.get s)

module CharArray = struct
  type t = char array
  let compare = compare
end

module CMap = Map.Make(CharArray)

let () =
  let n = scanf " %d" (fun a -> a) in
  Array.init n (fun _ -> scanf " %s" (fun a -> a))
  |> Array.fold_left (fun (ans, mp) s ->
    let s = stoa s in
    Array.sort compare s;
    if not (CMap.mem s mp) then
      (ans, CMap.add s 1 mp)
    else let x = CMap.find s mp in
      (ans + x, CMap.add s (x + 1) mp)) (0, CMap.empty)
  |> fst
  |> printf "%d\n";


Measured memory_bytes: 28416
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define rep(i,m,n) for(int i=m;i<n;i++)
const ll mod=998244353;

int main(){
  int n,k;
  cin>>n>>k;
  int l[k],r[k];
  rep(i,0,k) cin>>l[i]>>r[i];
  ll a[n];
  rep(i,0,n) a[i]=0;
  a[0]=1;
  ll sum[n+1];
  sum[0]=0;
  sum[1]=1;
  rep(i,1,n){
    rep(j,0,k){
      if(i-l[j]+1>=0) a[i]+=sum[i-l[j]+1];
      if(i-r[j]>=0) a[i]-=sum[i-r[j]];
      if(a[i]<0) a[i]+=mod;
      if(a[i]>mod) a[i]-=mod;
    }
    sum[i+1]=sum[i]+a[i];
    if(sum[i+1]>mod) sum[i+1]-=mod;
  }
  cout<<a[n-1]<<endl;
}

Measured memory_bytes: 6708
#include<bits/stdc++.h>
 
using namespace std;
 
#define rep(i,n) for(int i=0 ; i<(n) ; i++)
#define repr(i,n) for(int i=(n) ; i>=0 ; i--)
#define ALL(x) (x).begin(),(x).end()
 
int GCD(int a,int b){ return b ? GCD(b,a%b) : a ;}
int LCM(int a,int b){ return a*b / GCD(a,b) ;}
 
typedef long long ll;
typedef unsigned long long ull;
 
const int INF = 1e9;

int main(){
      int n,m;
      cin >> n >> m;
      vector<pair<ll,ll>> card(n+m);
      rep(i,n){
            ll a;cin >> a;
            card[i] = make_pair(a,1);
      }
      for(int i=n;i<n+m;i++){
            ll b,c;cin >> b >> c;
            card[i] = make_pair(c,b);
      }
      sort(ALL(card),greater<pair<ll,ll>>());
      ll ans = 0;
      for(int i=0;n>0;i++){
            for(int j=0;j<card[i].second&&n>0;j++){
                  ans += card[i].first;
                  n--;
            }
      }
      cout << ans << endl;
      return 0;
}


Measured memory_bytes: 3328
import collections
from collections import defaultdict
n, m = map(int, input().split())
s = []
for i in range(m):#h:高さ
    s.append([int(m) for m in input().split()])

d = defaultdict(int)
#e = defaultdict(int)
for i in range(1, n + 1):
    d[i] = 0
for i in range(m):
    d[s[i][0]] += 1
    d[s[i][1]] += 1
for i in range(1, n + 1):
    print(d[i])


Measured memory_bytes: 3316
#include <bits/stdc++.h>
using namespace std;
#define _overload3(_1,_2,_3,_4,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,n) (int i=a;i<n;i++)
#define per(i,a,n,s) (int i=n-1;i>=a;i--)
#define range(...) _overload3(__VA_ARGS__,per,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define vecin(x) for(auto&youso_: (x) ) cin >>  youso_
#define vecout(x) for(auto&youso_: (x) ) cout << youso_ << " " ; cout << endl
template<class T>inline bool chmax(T &a, const T b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T b) { if (b<a) { a=b; return 1; } return 0; }
const int inf = 1e9+10;
const int mod = 1e9+7;
const long long llinf = 1e18;
#define _GLIBCXX_DEBUG
// head

int main(){
  // vector<vector<int>> vec;
  int n,q;
  cin >> n >> q;
  vector<vector<int>> event;
  for range(i,n){
    int si,ti,xi;
    cin >> si >> ti >> xi;
    event.push_back(vector<int>{si-xi,0,xi});
    event.push_back(vector<int>{ti-xi,-1,xi});
  }
  for range(i,q){
    int d;
    cin >> d;
    event.push_back(vector<int>{d,1,d});
  }
  sort(all(event));
  set<int> S;
  for range(i,event.size()){
    int query = event[i][1];
    int number = event[i][2];
    if (query == 0) { S.emplace(number);}
    else if (query == -1) { S.erase(number);}
    else if (query == 1) {
      if (S.empty()) { cout << -1 << endl;}
      else {cout << *S.begin() << endl;}
    }
  }
}

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

using namespace std;

typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli>> mat;

lli r,c,n;
mat p;
map<lli,lli> m;
stack<lli> s;

bool isEdgeToEdge(lli x){
  return (p[x][0] == 0 || p[x][1] == 0 || p[x][0] == r || p[x][1] == c)
  && (p[x][2] == 0 || p[x][3] == 0 || p[x][2] == r || p[x][3] == c);
}

int main(){
  cin >> r >> c >> n;
  p = mat(n,vll(4));
  for(lli i = 0;i < n;i++){
    cin >> p[i][0] >> p[i][1] >> p[i][2] >> p[i][3];
  }
  for(lli i = 0;i < n;i++){
    if(isEdgeToEdge(i)){
      if(p[i][0] == 0) m[-(p[i][0]+p[i][1])] = i;
      else if(p[i][0] == r) m[p[i][0]+p[i][1]] = i;
      else if(p[i][1] == 0) m[p[i][0]+p[i][1]] = i;
      else if(p[i][1] == c) m[-(p[i][0]+p[i][1])] = i;
      if(p[i][2] == 0) m[-(p[i][2]+p[i][3])] = i;
      else if(p[i][2] == r) m[p[i][2]+p[i][3]] = i;
      else if(p[i][3] == 0) m[p[i][2]+p[i][3]] = i;
      else if(p[i][3] == c) m[-(p[i][2]+p[i][3])] = i;
    }
  }
  for(auto itr = m.begin();itr != m.end();itr++){
    if(!s.empty() && s.top() == itr->second) s.pop();
    else s.push(itr->second);
  }
  cout << (s.empty() ? "YES" : "NO") << endl;
  return 0;
}


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

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef vector<string> VS;
typedef pair<int, int> P;
typedef tuple<int,int,int> tpl;

#define ALL(a)  (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define REVERSE(c) reverse((c).begin(),(c).end())
#define EXIST(m,v) (m).find((v)) != (m).end()
#define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i)
#define RREP(i,n) RFOR(i,n,0)

#define en "\n"

constexpr double EPS = 1e-9;
constexpr double PI  = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL<<60;
constexpr long long MOD = 1000000007; // 998244353;

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

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

void Main(){
    int k;
    cin >> k;
    int ans = 1;
    int now = 7;
    while (now % k != 0 && ans <= 10000000) {
        ans++;
        now *= 10;
        now += 7;
        now %= k;
    }
    if (ans > 10000000) {
        cout << -1 << en;
    } else {
        cout << ans << en;
    }
}

int main(void){
    cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);cout<<fixed<<setprecision(15);
    int t=1; //cin>>t;
    REP(_,t) Main();
    return 0;
}


Measured memory_bytes: 3632
#include <stdio.h>
#include <string.h>
char x[2000], y[2000];
int c[2000][2000];
int m, n;


int lcs(void) {
	int i, j;

	m = strlen(x);
	n = strlen(y);

	for (i = 1; i <= m; i++) {
		c[i][0] = 0;
	}
	for (j = 0; j < n; j++) {
		c[0][j] = 0;
	}
	for (i = 1; i <= m; i++) {
		for (j = 1; j <= n; j++) {
			if (x[i - 1] == y[j - 1]) {
				c[i][j] = c[i - 1][j - 1] + 1;
			}
			else if (c[i - 1][j] >= c[i][j - 1]) {
				c[i][j] = c[i - 1][j];
			}
			else {
				c[i][j] = c[i][j - 1];
			}
		}
	}
	return c[i - 1][j - 1];

}

int main()
{
	int a[200];
	int i, num;

	scanf("%d", &num);

	for (i = 0; i < num; i++) {
		scanf("%s", x);
		scanf("%s", y);
		lcs();
		a[i] = lcs();
	}

	for (i = 0; i < num; i++) {
		printf("%d\n", a[i]);
	}

	return 0;	
}


Measured memory_bytes: 9536
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
using namespace std;
#define ll int64_t
const long MOD = 1e9 + 7;
const long LINF = 1e13;
const long LLINF = 1e18;
const long ALPHABET = 26;

template <class T>
void Swap(T& r, T& l) {
  T tmp = r;
  r = l;
  l = tmp;
}


vector<long> dp(1000000, -1);
long n;


long search(vector<long>&h, long pos) {
  long mincost = LINF;

  if (pos < n - 1 ) {
    if (dp[pos + 1] < 0) {
      dp[pos + 1] = search(h,pos + 1);
    }
    mincost = min(mincost,dp[pos + 1] + abs(h[pos] - h[pos+1]));
  }
  if (pos < n - 2 ) {
    if (dp[pos + 2] < 0) {
      dp[pos + 2] = search(h,pos + 2);
    }
    mincost = min(mincost,dp[pos + 2] + abs(h[pos] - h[pos+2]));
  }
  if (pos == n-1)mincost = 0;


  return mincost;
}



int main() {


  cin >> n;

  vector<long> h(n);
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  long ans = 0;


  ans = search(h, 0);


  cout << ans;


}























Measured memory_bytes: 15104
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
#define MOD 1000000007
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
//name[i][j]-> vvl name(i,vl(j))
//map<int,int>mp
//余りを切り上げる =>  (a + b - 1) / b 
//なんかむずいんだけどw
// fuck
signed main(){
	int a,b; cin>>a>>b;
	int c=a*b;
	if(c % 2 == 0){
		cout<<"Even";
	}
	else{
		cout<<"Odd";
	}
	
	return 0;
}

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

int main() {
  int n, d, x;
  cin >> n >> d >> x;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  int sum = 0;
  for (int j = 0; j < n; j++) {
    int index = 0;
    while ((a[j] * index) + 1 <= d) {
      sum++;
      index++;
    }
  }

  cout << sum + x << endl;
}

Measured memory_bytes: 256
h,w = map(int,input().split())
l = [["."]*(w+2)]
for i in range(h):
    l.append(["."] + [i for i in input()] + ["."])
l.append(["."]*(w+2))

def count(i,j):
    if l[i][j] == "#":
        return "#"
    c = 0
    for n in range(i-1,i+2):
        for m in range(j-1,j+2):
            if l[n][m] == "#":
                c += 1
    return str(c)

ans = ["".join([count(i,j) for j in range(1,1+w)]) for i in range(1,1+h)]

print("\n".join(ans))


Measured memory_bytes: 9220
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for(ll i = a; i < b; i++)
#define rrep(i, a, b) for(ll i = b - 1; i >= a; i--)
typedef pair<ll,ll> pll;

int main(){
    ll N, X, D; cin >> N >> X >> D;
    unordered_map<ll, vector<ll>> mp;
    if(D < 0){
        D = -D;
        X = -X;
    }
    if(D == 0){
        if(X == 0){
            cout << 1 << endl;
            return 0;
        }
        cout << N + 1 << endl;
        return 0;
    }
    ll t;
    if(X >= 0) t = X % D;
    else t = (X + ((-X) / D + 1) * D) % D;
    cerr << "t " <<   t << endl;
    rep(i, 0, N + 1){
        mp[(t * i) % D].push_back(i);
    }
    //for(auto e: mp) for(auto f: e.second) cout << f << endl;
    ll ans = 0;

    for(auto e: mp){
        //l premin = 2, premax = 1;
        cerr << e.first << " ";
        for(auto f: e.second) cerr << f << " ";
        cerr << endl;
        vector<pll> section;
        for(auto f: e.second){
            ll summax = 0, summin = 0;
            if(f%2 == 0) summax = ((X + D * (N - f)) + (X + D * (N - 1))) * (f / 2);
            else summax = ((X + D * (N - f)) + (X + D * (N - 1))) / 2 * f;
            if(f%2 == 0) summin = (X + X + D  * (f - 1)) * (f / 2);
            else summin = (X + X + D  * (f - 1))/ 2 * f;
            section.push_back({summin, summax});
        }
        sort(section.begin(), section.end());
        ll premin, premax;
        rep(i, 0, section.size()){
            if(i == 0) premin = section[i].first, premax = section[i].second;
            else{
                //if(premin == section[i].first) premax = section[i].second;
                if(premax < section[i].first){
                    ans += (premax - premin) / D + 1;
                    premax = section[i].second;
                    premin = section[i].first;
                }else if(premax < section[i].second) premax = section[i].second;
            }
        }
        ans += (premax - premin) / D + 1;
        cerr << ans << endl;
    }
    cout << ans << endl;
}

Measured memory_bytes: 17864
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 13 17:57:26 2018


N*Mで裏返しのカード
あるマスとその8近傍のカードを裏返す
全てのマスに同様の処理を行う
表のカードの枚数を出力する


入力
1 7

出力
5


@author: Yuya
"""

N, M = map(int,input().split())

if N > 1 and M > 1:
    print((N-2)*(M-2))
else:
    ans1 = N-2
    ans2 = M-2
    ans = abs(ans1*ans2)
    print(ans)
    

Measured memory_bytes: 2940
def main():
	N,Y = map(int,input().split())
	for a in range(N+1):
		for b in range(N+1-a):
			y = 10000*a+5000*b+1000*(N-a-b)
			if y == Y :
				print(a,b,N-a-b)
				exit()
	print(-1,-1,-1)

if __name__ == '__main__':
	main()

Measured memory_bytes: 9072
x,n = map(int,input().split())
p = list(map(int,input().split()))
p = set(p)
i = 0
while True:
    ans1 = x+i
    ans2 = x-i
    if ans1 not in p and ans2 not in p:
        print(min(ans1,ans2))
        exit()
    elif ans1 not in p:
        print(ans1)
        exit()
    elif ans2 not in p:
        print(ans2)
        exit()
    else:
        i += 1
        continue


Measured memory_bytes: 9192
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,n) for(int i = 0;i < (n);i++)
#define REP(i,l,r)for(int i=(l);i<(r);i++)

int main(){
    int N, K;
    cin >> N >> K;
    vector<int> d(K);
    vector<int> cnt(N+1);
    int ans = 0;
    rep(i, K){
        cin >> d[i];
        rep(j,d[i]){
            int A;
            cin >> A;
            cnt[A]++;
        }
    }
    REP(i,1,N+1){
        if(cnt[i] == 0) ans++;;
    }
    cout << ans << endl;
}

Measured memory_bytes: 3636
#include<bits/stdc++.h>
using namespace std;
using Int = long long;
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;}

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  Int a,b,c,d;cin >> a >> b >> c >> d;
  auto mlcm=[](Int p,Int q){return p/__gcd(p,q)*q;};
  auto calc = [&](Int x){
    Int p = x/c;
    Int q = x/d;
    Int r = x/mlcm(c,d);
    return x-p-q+r;
  };
  cout << calc(b)-calc(a-1) << endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
#define REP(i, j) for(int i = 0; i < j; ++i)
#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define P pair<int, int>
const double EPS = 1e-12;
const int N = 10;
#define EQ(a,b) (abs((a)-(b)) < EPS)

int v[N], a, b;

int solve(){
  double d = 0, sum = 0;
  REP(i, N) sum += v[i];
  if(a >= b){
    REP(i, N - 1){
      double st = d / a, et = (d + v[i]) / a, sb = min(sum - b * et, sum - b * st), eb = max(sum - b * et, sum - b * st);
      //cout <<i + 1 <<" | " <<st <<", " <<et <<" | " <<d <<", " <<d + v[i] <<", " <<sb <<", " <<eb <<endl;
      if((eb >= d && eb <= d + v[i]) || (sb >= d && sb <= d + v[i])) return i + 1;
      d += v[i];
    }
    return 10;
  } else{
    for(int i = 9; i > 0; --i){
      double st = d / b, et = (d + v[i]) / b, sb = min(sum - a * et, sum - a * st), eb = max(sum - a * et, sum - a * st);
      //cout <<i + 1 <<" | " <<st <<", " <<et <<" | " <<d <<", " <<d + v[i] <<", " <<sb <<", " <<eb <<endl;
      if(EQ(d + v[1], sb)) return i;
      if((eb >= d && eb <= d + v[i]) || (sb >= d && sb <= d + v[i])) return i + 1;
      d += v[i];
    }
    return 1;
  }
}

int main() {
  while(scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7], &v[8], &v[9], &a, &b) != EOF) cout <<solve() <<endl;
  return 0;
}

Measured memory_bytes: 1208
#include <stdio.h>
#include <math.h>
int dp[31];
void solve() {
	int i;
	for(i = 0; i < 31; ++ i) dp[i] = 0;
	dp[0] = 1;
	for(i = 0; i < 31; ++ i) {
		if(i + 1 < 31) dp[i + 1] += dp[i];
		if(i + 2 < 31) dp[i + 2] += dp[i];
		if(i + 3 < 31) dp[i + 3] += dp[i];
	}
}
int main() {
	int N;
	solve();
	while(1) {
		scanf("%d", &N);
		if(N == 0) break;
		printf("%d\n", (int) ceil((double) dp[N] / 3650));
	}
}

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

using namespace std;

#define ll long long 
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define mkp make_pair
#define pb push_back
#define pf push_front
#define popb pop_back
#define popf pop_front
#define ff first
#define ss second
#define endl "\n"
#define MOD 1000000007
#define MOD1 998244353

ll GCD(ll x,ll y){if(y==0)return x;return GCD(y,x%y);}
ll LCM(ll x,ll y){return (x*y)/(GCD(x,y));}
ll LOGK(ll x,ll k){if(x>=k)return 1+LOGK(x/k,k);return 0;}
ll MPOW( ll a, ll b, ll m) { if(b==0) return 1; ll x=MPOW(a,b/2,m); x=(x*x)%m; if(b%2==1) x=(x*a)%m; return x;}
ll MINV(ll a,ll m) {return MPOW(a,m-2,m);}

class pnc{
  ll FACT_MAX,MODU;
  vector<ll> fact;
public:
  pnc( ll n, ll m) { FACT_MAX = n; fact.resize(FACT_MAX); MODU = m; MFACT_INIT(MODU);}
  void MFACT_INIT( ll m) { fact[0]=1; for(ll i=1;i<FACT_MAX;++i) fact[i]=(i*fact[i-1])%MODU;}
  ll MFACT( ll n) { return fact[n];}
  ll PERM( ll n, ll r) { return (fact[n]*::MINV(fact[n-r],MODU))%MODU;}
  ll COMB( ll n , ll r) { return (PERM(n,r)*::MINV(fact[r],MODU))%MODU;}
};

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

    ll test=1;
    //cin>>test;
    while(test--)
    {
        ll n;
        ll x;
        cin>>n;

        map<ll,ll> mp;
        for(ll i=0;i<n-1;i++)
        {
            cin>>x;
            mp[x]++;
        }

        for(ll i=1;i<=n;i++)
        {
            cout<<mp[i]<<endl;
        }
        
    }
}

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

using namespace std;
// ascending order
#define vsort(v) sort(v.begin(), v.end())
// descending order
#define vsort_r(v) sort(v.begin(), v.end(), greater<int>())
// ascending order
#define asort(array, N) sort(array, array + N)
// descending order
#define asort_r(array, N) sort(array, array + N, greater<int>())
#define vunique(v) unique(v.begin(), v.end())
#define mp make_pair
#define ts(x) to_string(x)
typedef long long ll;
typedef pair<int, int> P;
const ll INF = 1e18;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int N, K;
	cin >> N >> K;
	int l[N];
	for(int i = 0; i < N; i++) cin >> l[i];
	asort_r(l, N);
	int ans = 0;
	for(int i = 0; i < K; i++) ans += l[i];
	cout << ans << endl;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
struct node{
  int key;
  struct node *r;
  struct node *l;
  struct node *p;
    };

typedef struct node  Node;
Node *root,*NIL;
Node *t;
void insert(int);
void inorder(Node *);
void preorder(Node *);
Node *find (Node *,int );
int main()
{
  int n,i,x,f;
  char A[20],b[20]="insert",c[20]="print",d[20]="find";
  scanf("%d",&n);

  for(i=0;i<n;i++)
    {

      scanf("%s",A);
      if(strcmp(A,b)==0){
	scanf("%d",&x);
	insert(x);
      }
      else if(strcmp(A,d)==0){
	scanf("%d",&f);
	t=find(root,f);
	if(t !=NIL)printf("yes\n");
	else printf("no\n");
      }
      else if(strcmp(A,c)==0){
	inorder(root);
	printf("\n");
	preorder(root);
	printf("\n");
	
      }
    }

 



  return 0;
}
  
void insert(int a)
{
  
  Node  *y=NIL;
  Node *x=root;
  Node *z;
  z=(Node *)malloc(sizeof(Node));
  z->key =a;
  z->l=NIL;
  z->r=NIL;
  while(x!=NIL)
    {
      y=x;
      if(z->key < x->key) x=x->l;
      else x=x->r;
    }
  z->p=y;
  if( y==NIL){
    root=z;
  }
  else if(z->key <y->key) y->l=z;
  else y->r=z;
  
}
void inorder(Node *a)
{
  if(a==NIL) return ;
  inorder(a->l);
  printf(" %d",a->key);
  
  inorder(a->r);
}
void preorder(Node *a)
{
  if(a==NIL) return ;
  printf(" %d",a->key);
  preorder(a->l);
  preorder(a->r);
  
}
Node *find (Node *x,int y)
{
  while (x!=NIL &&y !=x->key)
    {
      if(y<x->key)x=x->l;
      else x= x->r;
    }
  return x;


}



Measured memory_bytes: 20456
#include <bits/stdc++.h>
using namespace std;
#define ll	    long long
#define vi      vector <int>
#define vii     vector <pair<int,int>>
#define ii      pair<int,int>
#define loop(n)	for(int i=0;i<n;i++)
const ll mod=1e9+7;
bool is_prime(int n)
{
    if(n==2)
        return true;
    if(n%2==0||n<2)
        return false;
    for(int i=3; i<=sqrt(n); i+=2)
    {
        if(n%i==0)
            return false;
    }
    return true;
}
long long power(ll n, ll k){
    if(k==0) return 1;
    ll sq=(power(n,k/2))%mod;
    sq=(sq*sq)%mod;
    if(k%2==1)
        sq=(sq*n)%mod;
    return sq%mod;
}
long long gcd(long long a,long long b)
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
// lcm = (a*b)/gcd(a,b)
//freopen("input.txt" ,"r",stdin );
//freopen("output.txt","w",stdout);
int memo[100005];
int n,m,x,y,res;
vector<int>adj[100005];
int fun(int node)
{
	int &ret=memo[node];
	if(~ret)return ret;
	for(int i=0;i<adj[node].size();i++)
    {
        ret=max(ret,fun(adj[node][i])+1);
    }
    return ret;
}

void fast();
int main()
{
    fast();
    cin>>n>>m;
    loop(m)
    {
        cin>>x>>y;
        adj[x].push_back(y);
    }
    memset(memo,-1,sizeof memo);
    for(int i=1;i<=n;i++)
    {
        res=max(res,fun(i));
    }
    cout<<res+1;
}
void fast(){
std::ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
}
/*

*/


Measured memory_bytes: 8192
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>

/*
 *変数
 */
std::string s;

/*
 *関数
 */
int ctoi(char c){
  switch(c){
    case '0': return 0;
    case '1': return 1;
    case '2': return 2;
    case '3': return 3;
    case '4': return 4;
    case '5': return 5;
    case '6': return 6;
    case '7': return 7;
    case '8': return 8;
    case '9': return 9;
    default : return -1;
  }
}

/*
 *解 
 */
void solve(){
    if(ctoi(s[6])<=4&&ctoi(s[8])*10+ctoi(s[9])<=30&&ctoi(s[5])==0)printf("Heisei");
    else printf("TBD");
}

/*
 *入力 
 */
int main(){
       std::cin.tie(0);
       std::ios::sync_with_stdio(false);
       std::cin>>s;
       solve();
       return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <tuple>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstdio>
#include <numeric>
#include <string.h>
#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define repn(i, x) for (int i = 1; i <= (int)(x); i++)
#define repr(i, x) for (int i = ((int)(x) - 1); i >= 0; i--)
#define reprn(i, x) for (int i = ((int)(x)); i > 0; i--)
#define all(x) (x).begin(),(x).end()
typedef long long int64;
using namespace std;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<29;
const long long INFL = 1e18;
const int MOD = 1000000007;
const int MAX = 210000;

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

//逆元を求める関数
long long fac[MAX], finv[MAX], inv[MAX];
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++) {
      fac[i] = fac[i-1] * i % MOD;
      inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD;
      finv[i] = finv[i-1] * inv[i] % MOD;
    }
}
//組み合わせ
long long COM (int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n-k] % MOD) % MOD;
}

void solve() {
		int n, k; cin >> n >> k;
		string s; cin >> s;
		int score = 0;
		for (int i = 0; i < n - 1; ++i) {
			if (s[i] == s[i + 1]) score++;	
		}
		int ans = min(score + (2 * k), n - 1);
		cout << ans << endl;
    return;
}

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



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

int main() {
  long long n;
  cin>>n;
  long long ans=0;
  for(int i=1;i<n+1;i++){
    if(i%3!=0&&i%5!=0)ans+=i;
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 3596
#include <iostream>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <map>

using namespace std;

int main()
{
	char a[1005];
	scanf("%s",a);
	int len = strlen(a);
	int sum = 0;
	for(int i=0;i<len;i++)
	{
		if(a[i] == 'x')
		{
			sum++;
		}
	}
	if(sum>=8)
	{
		printf("NO\n");
	}
	else
	{
		printf("YES\n");
	}
	return 0;
}

Measured memory_bytes: 256
from math import ceil

n, k = map(int, input().split())
array = [int(x) for x in input().split()]

if n <= k:
    print(1)
    exit()
res = 0
# 1の範囲を増やしていくという方向性？
range_left = array.index(1)
range_right = n - range_left - 1
# 一度に変更できるのはk-1個(1を含めないといけないため)
k -= 1
if range_left == 0:
    res += ceil(range_right / k)
    print(res)
    exit()
elif range_right == 0:
    res += ceil(range_left / k)
    print(res)
    exit()

if range_left <= k:
    res += 1
    range_right -= (k - range_left)
    res += ceil(range_right / k)
else:
    res += ceil(range_left / k)
    tmp = 0
    while tmp < range_left:
        tmp += k
    range_right -= tmp % range_left
    res += ceil(range_right / k)
print(res)


Measured memory_bytes: 13812
#include <algorithm>
// #include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <bitset>
using namespace std;
typedef long long LL;
typedef double db;
const int oo=2139063143;
const int N=1010000;
const int P=998244353;
const db eps=1e-7;
#define pritnf printf
//char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T>
inline void sc (T &x)
{
    x=0; static int p; p=1; static char c; c=getchar();
    while (!isdigit(c)) { if (c=='-') p=-1; c=getchar(); }
    while ( isdigit(c)) { x=(x<<1)+(x<<3)+(c-48); c=getchar(); }
    x*=p;
}
template <typename T>
inline void print (T x)
{
    if (x< 0) putchar('-'),x=-x;
    if (x>=10) print(x/10);
    putchar(x%10+'0');
}
template <typename T>
inline void pr (T x) { print(x),putchar('\n'); }
template <typename T1,typename T2>
void chkmax (T1 &A,T2 B) { A=A>=B?A:B; }
template <typename T1,typename T2>
void chkmin (T1 &A,T2 B) { A=A<=B?A:B; }
int del(int x) { return x>=P?x-P:x; }
void add(int &x,int y) { x=del(x+y); }
int ksm (int a,int b)
{
	int ans=1;
	while (b)
	{
		if (b&1) ans=(LL)ans*a%P;
		a=(LL)a*a%P,b>>=1;
	}
	return ans;
}
int jc[N],inv[N];
void init (int n)
{
	jc[0]=1;
	for (int i=1; i<=n; i++)
		jc[i]=(LL)i*jc[i-1]%P;
	inv[n]=ksm (jc[n],P-2);
	for (int i=n-1; i>=0; i--)
		inv[i]=(LL)(i+1)*inv[i+1]%P;
}
int C(int n,int m) { return (LL)jc[n]*inv[m]%P*inv[n-m]%P; }
int n,m,m1[N],m2[N];
int main ()
{
    // freopen (".in","r",stdin);
    // freopen (".out","w",stdout);
	sc(n),sc(m); int k=max (n,m),ans=0; init (k);
	m1[0]=m2[0]=1;
	for (int i=1; i<=k; i++)
		m1[i]=(LL)m1[i-1]*(m+1)%P,m2[i]=(LL)m2[i-1]*(n+1)%P;
	k=min (n,m);
	for (int i=0; i<=k; i++)
	{
		int g=(LL)C(n,i)*C(m,i)%P*jc[i]%P*m1[n-i]%P*m2[m-i]%P;
		add(ans,(i&1)?P-g:g);
	}
	pr(ans);

    return 0;
}

Measured memory_bytes: 14464
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1000000007;

signed main() {
    int N, M;
    cin >> N >> M;
    
    int T = 0;
    for( int i = 0; i < M; i++ ){
        int A;
        cin >> A;
        T += A;
    }
    if( T <= N ) cout << N-T << endl;
    else cout << -1 << endl;
}


Measured memory_bytes: 3624
#include <bits/stdc++.h>
using namespace std;
const unsigned int MOD = 1000000007;
#define ll long long
#define PI 3.1416
string day[]={"SUN","MON","TUE","WED","THU","FRI","SAT"};
int main() {
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    int a,b,c;
    while(cin>>a>>b>>c){
        if(c-b<=0)cout<<"delicious"<<endl;
        else if(c-b<=a)cout<<"safe"<<endl;
        else cout<<"dangerous"<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define irep(i, n) for (int i = (n); i >= 0; i--)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1 << 25;
const int MOD = 1e9+7;
 
int main() {
  int n;
  cin >> n;
  int d;
  set<int> s;
  rep(i,n) {
    cin >> d;
    s.insert(d);
  }
  cout << s.size() << endl;
  return 0;
}

Measured memory_bytes: 3600
N,M = map(int,input().split())
mat = [[int(i) for i in input().split()] for k in range(M)]
p = [int(i) for i in input().split()]
switch = [0]*N
O = [0]*M
L = [False]*M
ans = 0

for i in range(2 ** (N)):
    switchsub = list(format(i,'b'))
    for j in range(len(switchsub)):
        switch[-j-1] = switchsub[-j-1]
    for j in range(N):
        if(switch[j] == "1"):
            for k in range(M):
                for l in range(1,len(mat[k])):
                    if(mat[k][l] == j+1):
                        O[k] += 1
    for j in range(M):
        if(O[j] % 2 == p[j]):
            L[j] = True
    if(False not in L):
        ans += 1
        switch = [0]*N
        O = [0]*M
        L = [False]*M
    if(False in L):
        switch = [0]*N
        O = [0]*M
        L = [False]*M

print(ans)

Measured memory_bytes: 9280