akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

const vector<int> dx = {0, 0, -1, 1};
const vector<int> dy = {-1, 1, 0, 0};

int main() {
    int h, w;
    cin >> h >> w;

    char ch;
    vector<vector<char>> a(h, vector<char> (w));
    for (int i=0; i<h; i++){
        for (int j=0; j<w; j++){
            cin >> ch;
            a.at(i).at(j) = ch;
        }
    }

    vector<vector<int>> dist(h, vector<int> (w, -1));
    queue<pair<int, int>> que;

    for (int i=0; i<h; i++){
        for (int j=0; j<w; j++){
            if(a.at(i).at(j) == '#'){
                que.push(make_pair(i,j));
                dist.at(i).at(j) = 0;
            }
        }
    }

    while(!que.empty()){
        auto p = que.front();
        int y = p.first;
        int x = p.second;
        que.pop();

        for (int d=0; d<4; d++){
            int n_y = y + dy.at(d);
            int n_x = x + dx.at(d);

            if(n_y < 0 || n_y >= h || n_x <0 || n_x >=w) continue;
//            if(a.at(n_y).at(n_x) == '#') continue;
            if(dist.at(n_y).at(n_x) != -1) continue;

            dist.at(n_y).at(n_x) = dist.at(y).at(x) + 1;
//            a.at(n_y).at(n_x) = '#';
            que.push(make_pair(n_y, n_x));
        }
    }

    int max_count = 0;
    for(int i=0; i<h; i++){
        for(int j=0; j<w; j++){
            max_count = max(max_count, dist.at(i).at(j));
        }
    }

    cout << max_count << endl;

    return 0;
}


Measured memory_bytes: 13328
lststr = [i for i in input()]

lststr_replace = [s.replace("A", "target") for s in lststr]
lststr_replace = [s.replace("C", "target") for s in lststr_replace]
lststr_replace = [s.replace("G", "target") for s in lststr_replace]
lststr_replace = [s.replace("T", "target") for s in lststr_replace]

con = 0
maxcon = 0
for i in range(len(lststr)):
    if lststr_replace[i] == "target":
        con += 1
        if maxcon < con:
            maxcon = con
    else:
        con = 0
print(maxcon)


Measured memory_bytes: 3064
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector as V
import qualified Data.ByteString.Char8 as C
import Data.List
import Control.Monad



r' :: IO [Int]
r'  =   unfoldr    (C.readInt . C.dropWhile (==' ')) <$> C.getLine


toTup [a,b] = (a,b)


main :: IO ()
main = do
    [n,m] <- r'
    (ls,rs) <- unzip . map toTup <$> replicateM m r'
    print $ max 0 (1 + minimum rs - maximum ls)


Measured memory_bytes: 31868
package dsu;
sub new{
	return bless[(-1)x$_[1]],$_[0];
}
sub merge{
	my$self=shift;
	my$x=$self->leader($_[0]);
	my$y=$self->leader($_[1]);
	return$x if$x==$y;
	($x,$y)=($y,$x)if-$self->[$x]<-$self->[$y];
	$self->[$x]+=$self->[$y];
	return$self->[$y]=$x;
}
sub same{
	my$self=shift;
	return$self->leader($_[0])==$self->leader($_[1]);
}
sub leader{
	my$self=shift;
	my$a=shift;
	return$self->[$a]<0?$a:($self->[$a]=$self->leader($self->[$a]));
}
sub size{
	return-$_[0]->[$_[1]];
}
sub groups{
}
package main;
my($N,$Q)=split$",<>;
my$dsu=dsu->new($N);
for(1..$Q){
	my($t,$u,$v)=split$",<>;
	if($t==0){
		$dsu->merge($u,$v);
	}
	else{
		print $dsu->same($u,$v)?1:0,$/;
	}
}

Measured memory_bytes: 15044
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<cstdio>
#include<cmath>
using namespace std;
#define int long long
#define rep(i,n) for(int i=0;i<n;++i)
#define rep1(i,n) for(int i=1;i<=n;++i)
int MOD=1000000007;
class Point{
    public:
    int x;
    int y;
    int h;
    int dist(int cx,int cy,int ch){
        return max((int)0,ch-abs(x-cx)-abs(y-cy));
    }
};
signed main(){
    int n;cin>>n;
    vector<Point> p(n);
    rep(i,n){
        cin>>p[i].x>>p[i].y>>p[i].h;
    }
    rep(cx,101){
        rep(cy,101){
            int i=0;
            while(p[i].h==0)i++;
            int ch=p[i].h+abs(p[i].x-cx)+abs(p[i].y-cy);
            bool flag=true;
            rep(j,n){
                if(p[j].h!=p[j].dist(cx,cy,ch))flag=false;
            }
            if(flag){
                cout<<cx<<" "<<cy<<" "<<ch<<endl;
                goto FINE;
            }
        }
    }
    FINE:
    return 0;
}

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

#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)

using namespace std;

using ll = long long;
using vi = vector<int>;
using vll = vector<ll>;

constexpr int M = 1046527;
constexpr int L = 13;
char H[M][L];

ll str_to_ll(const char str[L]) {
    ll val = 0;
    int power = 1;

    rep(i, 0, L) {
        if (str[i] == 'A') {
            val += 1 * power;
        } else if (str[i] == 'C') {
            val += 2 * power;
        } else if (str[i] == 'G') {
            val += 3 * power;
        } else if (str[i] == 'T') {
            val += 4 * power;
        } else {
            break;
        }
        power *= 5;
    }
    return val;
}

int h1(ll key) { return key % M; }

int h2(ll key) { return 1 + (key % (M - 1)); }

int insert(char str[L]) {
    ll key = str_to_ll(str);
    ll i = 0;

    while (true) {
        ll h = (h1(key) + i * h2(key)) % M;
        if (strcmp(H[h], str) == 0) {
            return 1;
        } else if (H[h][0] == '\0') {
            strcpy(H[h], str);
            return 0;
        }
        ++i;
    }
}

int find(char str[L]) {
    ll key = str_to_ll(str);
    ll i = 0;

    while (true) {
        ll h = (h1(key) + i * h2(key)) % M;
        if (strcmp(H[h], str) == 0) {
            return 1;
        } else if (H[h][0] == '\0') {
            return 0;
        }
        ++i;
    }
}

int main() {
    rep(i, 0, M) { rep(j, 0, L) { H[i][j] = '\0'; }}
    int n;
    cin >> n;

    rep(i, 0, n) {
        char com[10], str[L];
        cin >> com >> str;

        if (com[0] == 'i') {
            insert(str);
        } else if (find(str)) {
            cout << "yes" << endl;
        } else {
            cout << "no" << endl;
        }
    }

    return 0;
}


Measured memory_bytes: 16400
from collections import deque
alpha = "abcdefghijklmnopqrstuvwxyz"
A = input()
n = len(A)
B = ord('a')

links = [None]*(n+3)

link = [n]*26
for i in range(n-1, -1, -1):
    links[i] = link[:]
    link[ord(A[i]) - B] = i
links[-1] = link

deq = deque()
deq.append(-1)
prev = {-1: (None, 0)}
while deq:
    v = deq.popleft()
    if v == n:
        break
    link = links[v]
    for c in range(26):
        if link[c] in prev:
            continue
        prev[link[c]] = (v, c)
        deq.append(link[c])
v = n
ans = []
while v is not None:
    v, c = prev[v]
    ans.append(chr(c+B))
ans.reverse()
print("".join(ans[1:]))



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

typedef long long ll;
typedef pair<int,int> ii;

int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
	int x,y; cin>>x>>y; cout << x+y/2 << endl;
	return 0;
}


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

int main(){
    int n, in;
    ll n0 = 0;
    ll n1 = 0;
    ll n2 = 0;
    cin >> n;
    vector<int> ns(n);
    rep(i, n){
        cin >> ns.at(i);
    }
    sort(ns.begin(), ns.end());
    vector<int> ans2(0);
    vector<int> ans4(0);
    rep(i, n-1){
        if(ns.at(i) == ns.at(i+1) && n1 != ns.at(i)){
            ans2.push_back(ns.at(i));
            n0 = n1;
            n1 = ns.at(i);
        }
    }
    rep(i, n-3){
        if(ns.at(i) == ns.at(i+1) && ns.at(i+1) == ns.at(i+2) && ns.at(i+2) == ns.at(i+3) && n2 != ns.at(i)){
            ans4.push_back(ns.at(i));
            n2 = ns.at(i);
        }
    }
    cout << max(n0*n1, n2*n2) << endl;
    return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  int n;
  cin >> n;
  int arr[n + 1], bit[25][n + 1];
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  for (int p = 0; p < 25; p++)
    bit[p][0] = 0;
  for (int i = 1; i <= n; i++) {
    for (int p = 0; p < 25; p++) bit[p][i] = bit[p][i - 1];
    int num = arr[i], p = 0;
    while (num) {
      if (num & 1)
        bit[p][i]++;
      num >>= 1;
      p++;
    }
  }
  int e = 1;
  long long ans = 0;
  for (int s = 1; s <= n; s++) {
    // shift e to right s.t. all bits either never appear or only once appear between s to e
    while (true) {
      bool stop = false;
      for (int p = 0; p < 25; p++) {
        if (bit[p][e] - bit[p][s - 1] > 1)
          stop = true;
      }
      if (stop) {
        e--;
        break;
      }
      else if (e == n)
        break;
      else
        e++;
    }
    ans += e - s + 1;
  }
  cout << ans;
  return 0;
}

Measured memory_bytes: 23936
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cstdio>
#include<cmath>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#include<tuple>
#include<utility>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define rep(i, a, n)  for(int i=a; i<n; i++)
#define REP(i, n)  for(int i=0; i<(int)(n); i++)
#define REPS(i, n)  for(int i=1; i<=(int)(n); i++)
#define PER(i, n) for(int i=(int)(n)-1; i>= 0; i--)
#define PERS(i, n)  for(int i=(int)(n)-1; i>0; i--)
#define FOR(i, c) for(__typeof((c).begin()) i = (c).begin(); i!=(c).end(); i++)
#define RFOR(i, c)  for(__typeof((c).rbegin()) i=(c).rbegin(); i!=(c).end(); i++)
#define ALL(container)  (container).begin(), (container).end()
#define RALL(container) (container).rbegin(), (container).rend()
#define SZ(container) (container).size()
#define FILL0(n)  setfill('0') << right << setw(n)
#define mp(a, b)  make_pair(a, b)
#define toLower(c)  c+0x20
#define toUpper(c)  c-0x20
#define pb  push_back
#define eb  emplace_back

const int INF = 1<<28;
const int MOD = 1000000007;

int main(){
  string s; cin >> s;
  vector<int> num(4);
  int month;
  int ans = 0;
  REP(i, 4) num[i] = s[i] - '0';

  month = num[0]*10 + num[1];
  if(month > 0 and month <= 12) ans += 2;
  month = num[2]*10 + num[3];
  if(month > 0 and month <= 12) ans += 1;

  if(ans == 0)  cout << "NA" << endl;
  else if(ans == 1)  cout << "YYMM" << endl;
  else if(ans == 2)  cout << "MMYY" << endl;
  else cout << "AMBIGUOUS" << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
long long ans, A,B,C,X,Y;
int main(){
    cin>>A>>B>>C>>X>>Y;
    ans=X*A+Y*B;
    if(Y>X){
        ans=min(ans,(Y-X)*B+C*X*2);
        ans=min(ans,Y*C*2);
    }
    else{
        ans=min(ans,(X-Y)*A+C*Y*2);
        ans=min(ans,X*C*2);
    }
    cout<<ans<<'\n';
    return 0;
}


Measured memory_bytes: 256
#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::ops::Bound::*;
use itertools::Itertools;
use num_traits::clamp;
use ordered_float::OrderedFloat;
use proconio::{input, marker::*, fastout};
use superslice::*;

#[fastout]
fn main() {
    input! {
        R: usize, C: usize, K: usize,
        yxv: [(Usize1, Usize1, i64); K],
    }

    let mut items = vec![vec![0; C]; R];
    for (y, x, v) in yxv {
        items[y][x] = v;
    }

    let mut dp = vec![vec![vec![0; 5]; C+1]; R+2];
    for y in 1..R+1 {
        for x in 0..C {
            for k in (1..=3).rev() {
                dp[y][x][k] = max(dp[y][x][k], dp[y][x][k-1] + items[y-1][x]);
                dp[y][x+1][k] = max(dp[y][x+1][k], dp[y][x][k]);
                dp[y+1][x][0] = max(dp[y+1][x][0], dp[y][x][k]);
            }
        }
    }
    let mut ans = 0;
    for i in 0..=3 {
        ans = max(ans, dp[R][C-1][i]);
    }
    println!("{}", ans);
}


Measured memory_bytes: 708980
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;

int a[100050]={0};

int main()
{
    int n;
    scanf("%d",&n);
    int sum=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
        sum+=abs(a[i]-a[i-1]);
    }
    sum+=abs(a[n+1]-a[n]);
    for(int i=1;i<=n;i++){
        printf("%d\n",sum-abs(a[i]-a[i-1])-abs(a[i]-a[i+1])+abs(a[i-1]-a[i+1]));
    }

}


Measured memory_bytes: 1664
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split(/\n/g);

var cards = {'S':[],'H':[],'C':[],'D':[]};

for (var key in cards) {
    for (var i = 1; i <= 13; i++) {
        cards[key].push(i);
    }
}

for (var i = 1; i <= lines[0]; i++) {
    var line = lines[i].split(' ');
    
    var index = cards[line[0]].indexOf(+line[1]);
    cards[line[0]].splice(index, 1);
}

for (var key in cards) {
    for (var i = 0; i < cards[key].length; i++) {
        console.log(key + ' ' + cards[key][i]);
    }
}

Measured memory_bytes: 15628
import  math


def length(l1, l2):
    total = 0
    for i in range(len(l1)):
        total += (l1[i] - l2[i]) ** 2
    return math.sqrt(total)


N, D = map(int, input().split())
X = []
ans = 0
for _ in range(N):
    X.append(list(map(int, input().split())))

for i in range(N-1):
    for j in range(i+1, N):
        tmp = length(X[i], X[j])
        if tmp.is_integer():
            ans += 1
print(ans)


Measured memory_bytes: 3064
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
using namespace std;
#define rep(i,n) for(int i = 0;i<n;i++)
const long long MOD = 1000000007LL;
const string alpha = "abcdefghijklmnopqrstuvwxyz";
long long sanbeki(long long n){
    long long a = 1;
    long long x = 3;
    while(n>0){
        if(n%2==1){
            a*=x;
            a%=MOD;
        }
        n/=2;
        x = x*x;
        x%=MOD;
    }
    return a;
}
int main(){
    string s;
    cin >> s;
    long long n = s.size();
    long long ans = 0;
    long long tem = 1;
    for(long long i = 0;i<n;i++){
        if(s[i] == '1'){
            ans+=((tem*sanbeki(n-i-1))%MOD);
            tem*=2;
            tem%=MOD;
            ans%=MOD;
        }
    }
    ans+=tem;
    ans%=MOD;
    cout << ans << endl;
}

Measured memory_bytes: 512
#include "iostream"
#include "vector"

using namespace std;

int main()
{
	int x = 0, y = 0;
	vector<vector<int>> hw;
	do {
		cin >> x >> y;
		hw.push_back(vector<int>{x, y});
	} while (x != 0 && y != 0);

	for (int i = 0; i < hw.size();++i) {
		for (int h = 0; h < hw[i][0]; ++h) {
			for (int w = 0; w < hw[i][1]; ++w) {
				cout << "#";
			}
			cout << endl;
		}
		if(hw[i][0] != 0 && hw[i][1] != 0) cout << endl;
	}

    return 0;
}

Measured memory_bytes: 1192
import java.util.Scanner;
import java.lang.StringBuilder;

public class Main{
	public static void main(String[] args)
	{
		Scanner sc =new Scanner(System.in);
		String s=sc.next(); //入力
		
		//空白を挿入する
		StringBuilder sb=new StringBuilder(s);
		sb.insert(4, " ");
		System.out.println(sb);
	}
} 

Measured memory_bytes: 22996
#include "bits/stdc++.h"
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
//#define INF (1LL<<59)
 
struct hand{
    int win,lose;
    string status;
};
 
vi AI_used;
hand calc[11];
int confident_hand = -1;
vi vec;
int n;
 
void data_to_calc(vector<string> data){
    rep(i,n){
        calc[i].status = data[i];
        rep(j,data[i].size()){
            if(data[i][j]=='o')calc[i].win++;
            else if(data[i][j]=='x')calc[i].lose++;
        }
    }
}
 
void pre(){
    rep(i,n){
        if(calc[i].win==n-1) confident_hand = i;
    }
    
    rep(i,n){
        rep(j,calc[i].win){
            vec.pb(i);
        }
    }
}
 
 
int solve(){
    if( confident_hand!=-1 )return confident_hand;
    if( AI_used.size()>5 &&
       AI_used[AI_used.size()-1]==AI_used[AI_used.size()-2] &&
       AI_used[AI_used.size()-2]==AI_used[AI_used.size()-3] &&
       AI_used[AI_used.size()-3]==AI_used[AI_used.size()-4] &&
       AI_used[AI_used.size()-4]==AI_used[AI_used.size()-5] ){
        int tmp = AI_used[AI_used.size()-1];
        rep(i,calc[tmp].status.size()){
            if(calc[tmp].status[i]=='x')return i;
        }
    }
    if( vec.size() )return vec[rand()%vec.size()];
    
    return 0;
}
 
 
int main(){
    cin>>n;
    vector<string> data(n);
    rep(i,n)cin>>data[i];
    
    data_to_calc(data);
    
    pre();
    
    
    rep(i,1000){
        int judge_ai_hand;
        printf("%d\n",solve()+1); fflush(stdout);
        scanf("%d", &judge_ai_hand);
        judge_ai_hand--;
        AI_used.pb(judge_ai_hand);
    }
}

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

int N, M;
vector<vector<pair<int, long long>>> g;
vector<long long> pos;

bool dfs(int i, vector<bool>& check) {
    check[i] = true;
    for (auto& p : g[i]) {
        if (check[p.first]) {
            if (pos[p.first] != pos[i] + p.second) return false;
            continue;
        }
        pos[p.first] = pos[i] + p.second;
        dfs(p.first, check);
    }
    return true;
}

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

    g.resize(N);
    pos.assign(N, 0);

    for (int i = 0; i < M; ++i) {
        int l, r, d;
        cin >> l >> r >> d;
        --l, --r;
        g[l].emplace_back(r, d);
        g[r].emplace_back(l, -d);
    }

    bool ans = true;

    vector<bool> check(N);
    for (int i = 0; i < N; ++i) {
        ans &= dfs(i, check);
    }

    cout << (ans ? "Yes" : "No") << endl;
}


Measured memory_bytes: 16512
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String line = sc.nextLine();
		int n = Integer.parseInt(line);

		if(n==1){
			n=0;
        }
		else if(n==0){
			n=1;
        }
		System.out.println(n);
	}
}

Measured memory_bytes: 35580
#include <iostream>

using namespace std;

int main(void) {
	int i = 0, min = 0;
	long long num, a[5], ans = 4;
	cin >> num;
	for (; i < 5; i++) {
		cin >> a[i];
		if (a[i] <= a[min])
			min = i;
	}
	if (num % a[min] > 0)
		ans++;
	ans += num / a[min];
	cout << ans << "\n";
	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int main(void)
{
	int i,j,N,M;
	char k;
	int card[4][14];
	
	for(i=0;i<4;i++){
		for(j=1;j<14;j++){
			card[i][j] = 0;
		}
	}
	
	scanf("%d",&N);
	
	for(i=0;i<N;i++){
		scanf("%s",&k);
		scanf("%d",&j);
		if(k=='S'){
			M=0;
		}
		else if(k=='H'){
			M=1;
		}
		else if(k=='C'){
			M=2;
		}
		else if(k=='D'){
			M=3;
		}
		card[M][j]=1;
	}
		for(i=0;i<4;i++){
			for(j=1;j<14;j++){
				if(card[i][j]==0){
					if(i == 0){
						printf("S %d\n",j);
					}
					else if(i == 1){
						printf("H %d\n",j);
					}
					else if(i == 2){
						printf("C %d\n",j);
					}
					else if(i == 3){
						printf("D %d\n",j);
					}
				}
			}
		}
		return 0;
	}

Measured memory_bytes: 596
// First Grader
import java.util.Scanner;

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

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] number = new int[n];
		for (int i = 0; i < n; i++)
			number[i] = sc.nextInt();
		long[][] dp = new long[n - 1][21];
		dp[0][number[0]] = 1;
		for (int i = 1; i < n - 1; i++) {
			for (int j = 0; j < 21; j++) {
				if (dp[i - 1][j] > 0) {
					if (j + number[i] < 21)
						dp[i][j + number[i]] += dp[i - 1][j];
					if (j - number[i] >= 0)
						dp[i][j - number[i]] += dp[i - 1][j];
				}
			}
		}
		System.out.println(dp[n - 2][number[n - 1]]);
		sc.close();
	}
}

Measured memory_bytes: 18152
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <iterator>
#include <cassert>
#include <numeric>
#include <functional>
//#include <numeric>
#pragma warning(disable:4996) 
 
typedef long long ll;
typedef unsigned long long ull;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF  9223300000000000000
#define LINF2 1223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
//const long long MOD = 998244353;

using namespace std;

char str[105];

void solve()
{
    scanf("%s", str);
    int n = strlen(str);
    
    int ans = INF;
    int k;
    for (k = 0; k < 26; k++) {
        int max = 0;
        int i;
        int prev = -1;
        for (i = 0; i <= n; i++) {
            if (i==n || str[i] == 'a' + k) {
                int tmp=(i - prev);
                max=MAX(max, tmp);
                prev = i;
            }
        }
        ans = MIN(ans, max);
    }
    printf("%d\n", ans-1);

    return;
}


int main(int argc, char* argv[])
{
#if 1
    solve();
#else
    int T;
    scanf("%d", &T);
    int t;
    for(t=0; t<T; t++) {
        //printf("Case #%d: ", t+1);
        solve();
    }
#endif
    return 0;
}


Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
const int N=2005;
char a[N][N];
int a1[N][N],a2[N][N],b1[N][N],b2[N][N];
int main (){
	int n,m;scanf ("%d%d",&n,&m);
	for (int i=1;i<=n;i++)
		scanf ("%s",a[i]+1);
	for (int i=1;i<=m;i++)
		a1[1][i]=1,a2[n][i]=n;
	for (int i=1;i<=n;i++)
		b1[i][1]=1,b2[i][m]=m;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			if (a[i][j]=='#') a1[i+1][j]=i+1,a2[i-1][j]=i-1,b1[i][j+1]=j+1,b2[i][j-1]=j-1;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			if (!a1[i][j]) a1[i][j]=a1[i-1][j];
	for (int i=n;i>=1;i--)
		for (int j=1;j<=m;j++)
			if (!a2[i][j]) a2[i][j]=a2[i+1][j];
	for (int j=1;j<=m;j++)
		for (int i=1;i<=n;i++)
			if (!b1[i][j]) b1[i][j]=b1[i][j-1];
	for (int j=m;j>=1;j--)
		for (int i=1;i<=n;i++)
			if (!b2[i][j]) b2[i][j]=b2[i][j+1];
	int ans=0;
	for (int i=1;i<=n;i++)
		for (int j=1;j<=m;j++)
			if (a[i][j]!='#') ans=max(ans,a2[i][j]-a1[i][j]+b2[i][j]-b1[i][j]+1);
	printf ("%d",ans);
	return 0;
} 

Measured memory_bytes: 66944
#include<stdio.h>
 
int main() {
  long N, M;
  scanf("%ld %ld", &N, &M);
  if(N == 1 || M == 1) {
    if(N == 1 && M == 1) printf("1\n");
 	else printf("%ld\n", (N > M ? N : M) - 2);
  } else {
    printf("%ld\n", (N - 2) * (M - 2));
  }
  return 0;
}

Measured memory_bytes: 128
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace At
{
    class Program
    {
        static void Main(string[] args)
        {
            var input = ReadInts();
            if (input[1] - input[0] == input[2] - input[1])
            {
                WriteLine("YES");
            }
            else
            {
                WriteLine("NO");
            }
        }

        private static string Read() { return ReadLine(); }
        private static string[] Reads() { return (Read().Split()); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}


Measured memory_bytes: 11092
foldlWithState :: (c -> a -> b -> (a, c)) -> c -> a -> [b] -> a
foldlWithState f s a l = fst $ foldl (\(a, s) -> f s a) (a, s) l

sqrtInt :: Integer -> Integer
sqrtInt x = floor (sqrt (fromIntegral x))

factors :: Integer -> [Integer]
factors n = foldlWithState step False [1, n] $ reverse [2..m]
    where m = sqrtInt n
          step s a x | s = (a, s)
                     | otherwise = if mod n x == 0 then ([x, div n x], True) else (a, s)

main = do
  n <- readLn :: IO Integer
  putStrLn $ show $ (\x -> (sum x) - 2) $ factors n


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

int main()
{
    long long n,a,b;
    cin>>n>>a>>b;
    long long s1=(n/(a+b)),s2=0;
    if(n%(a+b)>a)
        s1++;
    else
        s2=n%(a+b);
    cout<<s1*a+s2<<endl;
}

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

// #define int long long
#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v+n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, a) replace(s.begin(),s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) int(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

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

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

signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  ll n,m; cin>>n>>m;
  map<ll,ll> ma;
  rep(i,m){
    ll a,b; cin>>a>>b;
    ma[a]++;
    ma[b]++;
  }
  bool ok=true;
  for(auto e: ma){
    if(e.second%2) ok=false;
  }
  if(ok) cout<<"YES"<<endl;
  else cout<<"NO"<<endl;
}

Measured memory_bytes: 5632
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
class book {
 public:
  int price = 0;
  int value[12] = {0};
  bool operator<=(const book& a) { return this->price <= a.price; }
  book() = default;
  book(int price) : price(price) {}
} store[12];
int n, m, x;
int check(int num) {
  int curValue[12] = {0}, curMoney = 0;
  for (int i = 0; i < n; i++) {
    bool flag = ((num >> i) & 1 == 1);
    if (flag) {
      for (int j = 0; j < m; j++) curValue[j] += store[i].value[j];
      curMoney += store[i].price;
    }
  }
  for (int i = 0; i < m; i++)
    if (curValue[i] < x) return -1;
  return curMoney;
}
int main() {
  cin >> n >> m >> x;
  for (int i = 0; i < n; i++) {
    cin >> store[i].price;
    for (int j = 0; j < m; j++) cin >> store[i].value[j];
  }
  int last = pow(2, n), minMoney = 1e9;
  for (int i = 1; i <= last; i++) {
    int result = check(i);
    if (result != -1) minMoney = min(minMoney, result);
  }
  cout << ((minMoney == 1e9) ? -1 : minMoney) << endl;
  return 0;
}

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

int main(){
  int n,a,b;
  cin >> n >> a >> b;
  cout << min(n*a, b) << endl;
  return 0;
}


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

using ll = long long;
using P = pair<int, int>;

const int INF = 1e9;
const int MOD = 1e9 + 7;

// 4近傍、8近傍
int dx[] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

int main() {
  // 入力
  int n, a, b;
  cin >> n >> a >> b;

  cout << min(a, b) << " " << max(0, a + b - n) << endl;
  return 0;
}



Measured memory_bytes: 256
from copy import deepcopy
from sys import exit,setrecursionlimit
import math
from collections import defaultdict,Counter,deque
from fractions import Fraction as frac
import fractions
from functools import reduce
from operator import mul
import bisect
import sys
import logging
import heapq
import itertools

logging.basicConfig(level=logging.ERROR)

#input = sys.stdin.readline

setrecursionlimit(1000000)


def main():
    s =  input()
    querry = int(input())
    f = []
    l = []
    r = 0
    for _ in range(querry):
        q = input().split(' ')
        if(q[0] == '1'):
            r += 1
        else:
            if((int(q[1]) + r) % 2 == 0):
                l.append(q[2])
            else:
                f.append(q[2])
    for i in f[::-1] if r%2 == 0 else l[::-1]:
        print(i,end = '')
    print(s if r%2==0  else s[::-1],end = '')
    for i  in l if(r%2==0) else f:
        print(i,end='')
    print()

main()


Measured memory_bytes: 116692
import java.util.*;

class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.next());
		HashMap<String,Integer> map_1 = new HashMap<String, Integer>();
		for (int i=0;i<N;i++) {
			String tmp = sc.next();
			int sum = 1;
			if (map_1.containsKey(tmp)) {
				sum += map_1.get(tmp);
			}
			map_1.put(tmp,sum);
		}
		int M = Integer.parseInt(sc.next());
		for (int i=0;i<M;i++) {
			String tmp = sc.next();
			int sum = -1;
			if (map_1.containsKey(tmp)) {
				sum += map_1.get(tmp);
			}
			map_1.put(tmp,sum);
		}
		int max=0;
		for (int i: map_1.values()) {
			max=Math.max(max,i);
		}
		System.out.println(max);
	}
}

Measured memory_bytes: 21716
/*********************************************************************
 *-------------------------------------------------------------------*
 *                                                                   *
 *   Life is just a life. Lead it, Feel it & Enjoy It Properly.      *
 *   Nothing is impossible. Just believe in yourself & Almighty.     *
 *                                                                   *
 *-------------------------------------------------------------------*
 *********************************************************************/

//#include<bits/stdc++.h>
#include<iostream>
using namespace std;
//---------------------Define Section--------------------------------
#define nl "\n"
#define tb "\t"
#define sp " "
#define eps 1e-7
typedef long long ll;
typedef unsigned long long ull;
typedef long double ldb;
typedef double db;
#define pi acos(-1.0)
#define mem(arr,val)  memset(arr,val,sizeof(arr))
//---------------------Input Section---------------------------------
#define si1(x)          scanf("%d",&x)
#define si2(x,y)        scanf("%d %d", &x, &y)
#define si3(x,y,z)      scanf("%d %d %d", &x, &y, &z)
#define sd1(x)          scanf("%lf",&x)
#define sd2(x,y)        scanf("%lf %lf", &x, &y)
#define sd3(x,y,z)      scanf("%lf %lf %lf", &x, &y, &z)
#define sli1(x)         scanf("%lld",&x)
#define sli2(x,y)       scanf("%lld %lld", &x, &y)
#define sli3(x,y,z)     scanf("%lld %lld %lld", &x, &y, &z)
//-------------------source code section------------------------------

//-------------------------- End -------------------------------------
#define MAXX 87
ll DP[MAXX];
ll Locus(ll n)
{
    if(n==0) return 2;
    if(n==1) return 1;

    if(DP[n]!=-1) return DP[n];
    DP[n]=Locus(n-1)+Locus(n-2);
    return DP[n];
}

void DenTofieM()
{
    mem(DP,-1);
    ll n;
    sli1(n);
    cout << Locus(n) << nl;
}

int main()
{

    DenTofieM();
    return 0;
}


Measured memory_bytes: 888
// AOJ 2529
// Brainf*ck
//@kankichi573
#include <stdio.h>

char input[1001];
int buf[12];
int bp;
int from;

void init_and_write_header()
{
        int i,j;

        for(i=0;i<10;i++)
        {
                buf[i]=10*(3+i);
                bp=9;
                from=120;

                printf(">");
                for(j=0;j<i+3;j++)
                        printf("+");
                printf("[<");
                for(j=0;j<10;j++)
                        printf("+");
                printf(">-]");
#ifdef DEBUG
                putchar('\n');
#endif
        }
        putchar('<');
}

void write_futougou(int from,int to)
{
        int i,diff;

        diff=to-from;
        if(diff>0)
                for(i=0;i<diff;i++)
                        putchar('>');
        else if(diff<0)
                for(i=0;i<-diff;i++)
                        putchar('<');

}
void write_plusminus(int from,int to)
{
        int i,diff;

        diff=to-from;
        if(diff>0)
                for(i=0;i<diff;i++)
                        putchar('+');
        else if(diff<0)
                for(i=0;i<-diff;i++)
                        putchar('-');
}


void write_diff(char prev,char cur)
{
        int nbp,diff,i;

        nbp=cur/10-3;
        //printf("C=%c NBP=%d %d\n",cur,nbp,buf[nbp]);

        write_futougou(prev/10,cur/10);
        write_plusminus(buf[nbp],cur);
        buf[nbp]=cur;
}
void write_one(char prev,char cur)
{
        write_diff(prev,cur);
        printf(".");
#ifdef DEBUG
        putchar('\n');
#endif
}
main()
{
        char *p;

        init_and_write_header();
        scanf("%s",input);
        p=input;

        while(*p)
        {
                write_one(from,*p);
                from = *p;
                p++;
        }
        fflush(stdout);
        return(0);
}

Measured memory_bytes: 532
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <cmath>
#include <map>
#include <tuple>

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

 
int main(){
    int n; cin >> n;
    if(n%2 == 0){
        cout << n/2 - 1 << endl;
        return 0;
    }
    if(n%2 != 0){
        cout << n/2 << endl;
        return 0;
    }
    
    
}


Measured memory_bytes: 256
N, W = map(int, input().split())
value = [0] * (N+1)
weight = [0] * (N+1)
for i in range(N):
  value[i+1], weight[i+1] = map(int, input().split())

dp = [[0] * (W+1) for _ in range(N+1)]

for i in range(1, N+1):
  for w in range(1, W+1):
    if(w - weight[i] >= 0):
      dp[i][w] = max(dp[i-1][w-weight[i]]+value[i], dp[i-1][w])
    else:
      dp[i][w] = dp[i-1][w]

print(dp[N][W])


Measured memory_bytes: 41888
#include <iostream>
#include <algorithm>
#include <limits>
#include <vector>
#include <map>
#include <cmath>
#include <limits>
#include <iomanip>
#include <queue>
#include <string.h>
#include <bitset>
#define ll long long int
#define ld long double
#define rep(i,n) for(int i = 0;i < n;i++)
#define rep1(i,n) for(int i = 1;i < n;i++)
#define co(x) cout << x << endl
#define cosp(x) cout << x << " "
#define all(x) x.begin(),x.end()
#define allr(x) x.begin(),x.end(),greater<int>()
#define P pair<ll,ll>
#define pb push_back
#define mp make_pair
#define MOD 1000000007
#define INF 1ll << 60
#define PI 3.14159265359

using namespace std;

int main(){
	int n,k;
	cin >> n >> k;
	if(n != 1) printf("%d\n",k*(int)pow(k-1,n-1));
	else cout << k << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <sstream>
#include <queue>
#include <deque>
#define ll long long
#define MP make_pair
#define PII pair<int, int>
#define F first
#define S second
using namespace std;
int n,m;
int a[100010];
int main()
{
	cin >>n>>m;
	for (int i=0;i<n;i++) cin >>a[i];
	int t=(n-1)/(m-1);
	if ((n-1)%(m-1)) t++;
	cout <<t;
	return 0;
}

Measured memory_bytes: 640
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
struct Fast {Fast() {std::cin.tie(0); ios::sync_with_stdio(false);}} fast;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define MOD1 1000000007

int main() {
  string S;
  cin >> S;
  map<char, int> q;
  rep(i, 4) q[S[i]]++;
  if (q.size() != 2) {
    cout << "No" << endl;
    return 0;
  }
  for (auto x : q) {
    if (x.second == 2) {
      cout << "Yes" << endl;
      return 0;
    } else {
      cout << "No" << endl;
      return 0;
    }
  }

}


Measured memory_bytes: 256
#include <cmath>
#include <random>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
	int N;
	cin >> N;
	vector<int> X(N), Y(N);
	for (int i = 0; i < N; ++i) {
		cin >> X[i] >> Y[i];
	}
	double xa = 0.0, ya = 0.0, cur = 0.0, delta = 1.0;
	for (int i = 0; i < N; ++i) {
		cur = max(cur, (X[i] - xa) * (X[i] - xa) + (Y[i] - ya) * (Y[i] - ya));
	}
	mt19937 mt(2002042358);
	uniform_real_distribution<double> p(-1.0, 1.0);
	for (int i = 0; i < 5000000; ++i) {
		double nxa = xa + delta * p(mt);
		double nya = ya + delta * p(mt);
		double ncur = 0.0;
		for (int j = 0; j < N; ++j) {
			ncur = max(ncur, (X[j] - nxa) * (X[j] - nxa) + (Y[j] - nya) * (Y[j] - nya));
		}
		if (cur > ncur) {
			cur = ncur;
			xa = nxa;
			ya = nya;
		}
		delta *= 0.99999;
	}
	cout.precision(15);
	cout << sqrt(cur) << endl;
	return 0;
}

Measured memory_bytes: 256
L=input()

mod=10**9+7

f=2
nf=1

for c in L[1:]:
    x=int(c)

    if x==1:
        nf=3*nf+f
        f=2*f
    else:
        nf=3*nf
    
    nf%=mod
    f%=mod

print((f+nf)%mod)


Measured memory_bytes: 73572
#include <bits/stdc++.h>
using namespace std;
#define rep2(i,m,n) for (int i = (m); i < (n); ++i)
#define rep(i,n) rep2(i,0,n)
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> P;
template<typename T> struct V : vector<T> { using vector<T>::vector; };
V() -> V<int>;
V(size_t) -> V<int>;
template<typename T> V(size_t, T) -> V<T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }
template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
const int INF = 1<<30;
const ll LINF = 1LL<<61;
const ll MOD = 1000000007;
const int M = 20000;
const int C = M * 2 + 1;
typedef bitset<C> bs;

int main()
{
    int h, w;
    cin >> h >> w;
    auto a = make_vec(h,w,0), b = make_vec(h,w,0);
    rep(i,h) rep(j,w) cin >> a[i][j];
    rep(i,h) rep(j,w) cin >> b[i][j];
    // bs dp[h][w];
    auto dp = make_vec(h+1,w+1,bs(0));
    dp[0][1][M] = 1; //i,j = 0,0でのみ使われる
    rep(i,h) rep(j,w) //dp[i+1][j+1] f(dp[i+1][j], dp[i][j+1])
    {
        int d = abs(a[i][j] - b[i][j]);
        dp[i+1][j+1] |= dp[i+1][j] >> d;
        dp[i+1][j+1] |= dp[i+1][j] << d;
        dp[i+1][j+1] |= dp[i][j+1] >> d;
        dp[i+1][j+1] |= dp[i][j+1] << d;
    }
    int ans = INF;
    rep(i,C) if(dp[h][w][i]) chmin(ans, abs(i - M));
    cout << ans << endl;
    
    return 0;
}


Measured memory_bytes: 36200
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.PrintWriter
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.sqrt

val br = BufferedReader(InputStreamReader(System.`in`))
val out = PrintWriter(System.out)

fun main() {
    br.use { out.use {
        solve()
        out.flush()
    } }
}

fun solve() {
    val d = readInteger()
    val cList = readLongList()
    val sPerDay = Array(d) { readLongList() }
    val tArray = Array(d) { readInteger() }

    calcScore(d, cList, sPerDay, tArray)
}

fun calcScore(d: Int, cList: List<Long>, sPerDay: Array<List<Long>>, tArray: Array<Int>): Long {
    var score = 0L
    val last = IntArray(26)
    for (i in 0 until d) {
        val contestNo = tArray[i] - 1
        score += sPerDay[i][contestNo]

        last[contestNo] = i + 1

        for (j in 0 until 26) score -= cList[j] * ((i + 1) - last[j])

        out.println(score)
    }

    return score
}


fun readInteger() = Integer.parseInt(br.readLine())
fun readLong() = java.lang.Long.parseLong(br.readLine())
fun readStringList() = br.readLine()!!.split(' ')
fun readIntegerList() = readStringList().map(Integer::parseInt)
fun readLongList() = readStringList().map(java.lang.Long::parseLong)
fun readDoubleList() = readStringList().map(java.lang.Double::parseDouble)


Measured memory_bytes: 38304
// Ver19.03
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 1 << 29
#define LLINF 4545454545454545454
#define MOD 1000000007
#define ll long long
#define ull unsigned long long
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
int upll(const void *a, const void *b) { return *(ll *)a < *(ll *)b ? -1 : *(ll *)a > *(ll *)b ? 1 : 0; }
int downll(const void *a, const void *b) { return *(ll *)a < *(ll *)b ? 1 : *(ll *)a > *(ll *)b ? -1 : 0; }
void sortup(ll *a, int n) { qsort(a, n, sizeof(ll), upll); }
void sortdown(ll *a, int n) { qsort(a, n, sizeof(ll), downll); }

int main()
{
  ll a, b;
  scanf("%lld%lld", &a, &b);
  char s[a + b + 2];
  scanf("%s", s);
  if (s[a] == '-')
  {
    for (int i = 0; i < a + b + 1; i++)
    {
      if (i == a)
        continue;
      if (s[i] < '0' || s[i] > '9')
      {
        puts("No");
        return 0;
      }
    }
    puts("Yes");
  }
  else
    puts("No");
}

Measured memory_bytes: 128
#include<stdio.h>
int main(){
  int n,m,d[100],p,pp,s,a,b,c,i,j;
  while(scanf("%d",&n),n){
    for(j=0;j<n;j++){
      s=0;
      a=b=c=1;
      scanf("%d",&m);
      for(i=0;i<10;i++){
	scanf("%d",&p);
	s+=p*a;//printf("!%d %d %d %d %d",s,i,a,b,c);
	if(p==10){
	  if(i<9){
	    b++;
	    c++;
	  }
	}
	a=b;
	b=c;
	c=1;
	if(p-10||i==9){
	  scanf("%d",&pp);
	  s+=pp*a;//printf("!!%d %d %d %d %d",s,i,a,b,c);
	  if(p+pp==10){
	    b++;
	  }
	  a=b;
	  b=c;
	  c=1;
	}//printf("\n");
      }
      if(p+pp>=10){
 	scanf("%d",&p);
	s+=p;
      }
      s=(300-s)*10000+m;
      //printf("%d\n",s);
      for(i=j;j&&d[i-1]>s;i--)d[i]=d[i-1];
      d[i]=s;
      //for(i=0;i<=j;i++)printf("!%d %d",300-d[i]/10000,d[i]%10000);printf("\n");
    }
    for(i=0;i<n;i++)printf("%d %d\n",d[i]%10000,300-d[i]/10000);
  }
  return 0;
}



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

int main(){
  int N, K; cin >> N >> K;
  int ans = 0;
  for(int i = 0; i < N; i++){
    int x; cin >> x;
    ans += 2 * min(x, K - x);
  }
  cout << ans;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,m,q;
    scanf("%d%d%d",&n,&m,&q);
    int grid[2*n][2*m];
    memset(grid,0,sizeof(grid));
    for (int i = 0; i < n; i++){
        for (int j = 0; j < m; j++){
            char x;
            scanf(" %c",&x);
            grid[2*i][2*j] = x-'0';
        }
    }
    for (int i = 0; i < 2*n; i+=2){
        for (int j = 0; j < 2*m; j+=2){
            if (i + 2 < 2*n){
                grid[i+1][j] = -1*(grid[i][j]&grid[i+2][j]);
            }
            if (j + 2 < 2*m){
                grid[i][j+1] = -1*(grid[i][j]&grid[i][j+2]);
            }
        }
    }
    int p[2*n][2*m];
    memset(p,0,sizeof(p));
    for (int i = 1; i < 2*n; i++){
        for (int j = 1; j < 2*m; j++){
            p[i][j] = p[i-1][j] + p[i][j-1] - p[i-1][j-1] + grid[i-1][j-1];
        }
    }
    for (int i = 0; i < q; i++){
        int x1,y1,x2,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        int a = 2*x2-1,b = 2*y2-1,c = 2*x1-2,d = 2*y1-2;
        printf("%d\n",p[a][b]-p[a][d]-p[c][b]+p[c][d]);
    }
}



Measured memory_bytes: 126848
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <vector>
using namespace std;

using ll=long long;
ll ls = 0, rs = 0;
priority_queue<int> lq, rq;
inline int qry(ll &s){
	s = rs - ls;
	int x = lq.top();
	if(lq.size()>rq.size()) s += x;
	return x;
}
void add(int x){
	lq.push(x);
	ls += x;
	if(rq.size() && lq.top() > -rq.top()){
		int t = lq.top(); lq.pop();
		int rt = -rq.top(); rq.pop();
		ls += rt - t; rs += t - rt;
		lq.push(rt);
		rq.push(-t);
	}
	if(lq.size() > rq.size()+1){
		int t = lq.top(); lq.pop();
		ls -= t;
		rs += t;
		rq.push(-t);
	}
}

int main(){
	int T;
	ll sb=0, sa;
	scanf("%d", &T);
	while(T--){
		int i,a,b;
		scanf("%d", &i);
		if(i == 1){
			scanf("%d%d", &a, &b);
			sb += b;
			add(a);
		}
		else{
			int x = qry(sa);
			printf("%d %lld\n", x, sa+sb);
		}
	}
	return 0;
}


Measured memory_bytes: 3452
// 2010/12/27 Tazoe

#include <iostream>
using namespace std;

bool prime3200[3200];

void eratos3200()
{
	prime3200[0] = false;
	prime3200[1] = false;

	for(int i=2; i<3200; i++)
		prime3200[i] = true;

	for(int i=2; i<3200; i++){
		if(!prime3200[i])
			continue;
		for(int j=i+i; j<3200; j+=i)
			prime3200[j] = false;
		if(3200<i*i)
			return;
	}
}

bool primeq(int n)
{
	if(n<13)
		return false;

	if(n%2==0)
		return false;

	if(n<3200)
		return prime3200[n]&&prime3200[n-2]&&prime3200[n-6]&&prime3200[n-8];

	for(int i=3; i*i<=n; i+=2){
		if(!prime3200[i])
			continue;
		if(n%i==0||(n-2)%i==0||(n-6)%i==0||(n-8)%i==0)
			return false;
	}

	return true;
}

int main()
{
	eratos3200();

	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		for(int i=n; i>=13; i--)
			if(primeq(i)){
				cout << i << endl;
				break;
			}
	}

	return 0;
}

Measured memory_bytes: 868
#include<bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long

#define I32_MAX 2147483647
#define I64_MAX 9223372036854775807LL
#define I64_MAX2 1223372036854775807LL
#define INF I64_MAX2
#define MOD 1000000007
// #define MOD 998244353 
#define MEM_SIZE 100010
#define DEBUG_OUT true
#define ALL(x) (x).begin(), (x).end()

template<typename T> void DEBUG(T e){if(DEBUG_OUT == false)return; std::cout << e <<" ";}
template<typename T> void DEBUG(const std::vector<T>& v){if(DEBUG_OUT == false)return;for(const auto& e : v){std::cout<< e << " "; } std::cout << std::endl;}
template<typename T> void DEBUG(const std::vector<std::vector<T> >& vv){if(DEBUG_OUT == false)return;for(const auto& v : vv){ DEBUG(v); } }
template<class T,class... Ts> void DEBUG(T d, Ts... e){if(DEBUG_OUT == false)return;DEBUG(d);DEBUG(e...);}
template <class T> void corner(bool flg, T hoge) {if (flg) {cout << hoge << endl; abort();}}
template< typename T1, typename T2 > inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template< typename T1, typename T2 > inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }


void solve(void)
{
  int N,K; cin>>N; cin>>K;  
  vector<int> vec (N,0);
  for (int i = 0; i < N; i++)
  {
     cin>>vec[i];
  }
  for (int i = 0; i < N - K ; i++)
  {
    if(vec[i] >= vec[i+K])
    {
      cout<<"No"<<endl;
    }
    else
    {
      cout<<"Yes"<<endl;
    }
    
  }
  
  return;
}
int32_t main(int32_t argc, const char *argv[])
{
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);

  std::cout << std::fixed;
  std::cout << std::setprecision(11);
  solve();

  return 0;
}


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

#define ff              first
#define ss              second
//#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mci             map<char,int>
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define rep(i,x,y) for(int i=x;i<y;i++)
#define repr(i,x,y) for(int i=x;i>=y;i--)
using namespace std;
//template <typename T> 

  

signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);
//       #ifndef ONLINE_JUDGE
//     // for getting input from input.txt
//     freopen("input.txt", "r", stdin);
//     // for writing output to output.txt
//     freopen("output.txt", "w", stdout);
// #endif 
//we can create an array of list of size v. Each key will store what neighbour it has
string str;
int K;
cin>>K;
cin>>str;
int n = str.length();
if(n<=K)
{
	cout<<str<<endl;
}
else if(n>K)
{
	string s = str.substr(0,K);
	s.append("...");
	cout<<s;
}

 cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;

return 0;
}

Measured memory_bytes: 3760