akhauriyash/Code-Regression huggingface dataset:
from collections import deque


H, W = map(int, input().split())
field = [list(input()) for _ in range(H)]
q = deque([(0, 0, [(0, 0)])])
visited = {(0, 0)}
while q:
    i, j, path = q.popleft()
    if (i, j) == (H - 1, W - 1):
        for pi, pj in path:
            field[pi][pj] = "."
        break
    for di, dj in ((1, 0), (0, 1)):
        ni, nj = i + di, j + dj
        if (
            0 <= ni < H
            and 0 <= nj < W
            and field[ni][nj] == "#"
            and (ni, nj) not in visited
        ):
            visited.add((ni, nj))
            q.append((ni, nj, path + [(ni, nj)]))

if any("#" in row for row in field):
    print("Impossible")
else:
    print("Possible")


Measured memory_bytes: 3316
#include<stdio.h>
 
int isPrime(int x)
{
  int i;
  if(x <= 1)return 0;
  else if (x == 2)return 1;
   
  if(x % 2 == 0)return 0;
   
  for(i = 3 ; i*i <= x ;i += 2)
    {
      if(x % i == 0)return 0;
    }
  return 1;
}
 
int main()
{
  int x,i,n;
  int cnt = 0;
  scanf("%d", &n);
  for(i = 0 ; i < n ; i++)
    {
      scanf("%d", &x);
      if(isPrime(x) )cnt++;
    }
  printf("%d\n", cnt);
   
  return 0;
}

Measured memory_bytes: 596
#include <bits/stdc++.h>
using namespace std;
using lint = long long int;
int main()
{
    int n;
    cin >> n;
    vector< pair<int,int> > v(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i].second >> v[i].first;
    }
    sort(v.begin(), v.end());
    lint t = 0;
    for (int i = 0; i < n; ++i) {
        int a = v[i].second;
        int b = v[i].first;
        
        if (t + a > b) {
            cout << "No" << endl;
            return 0;
        }
        t += a;
    }
    cout << "Yes" << endl;
    
    return 0;
}


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

using namespace std;

int main() {
    int a, b;

    cin >> a >> b;
    cout << a * b << endl;

}


Measured memory_bytes: 3632
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
const ll mod = 1000000007;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
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;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n-1);
  return ans*n;
}
ll gcd(ll a, ll b) {
  if (a < b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }
  ll r = a%b;
  while(r != 0) {
    a = b;
    b = r;
    r = a%b;
  }
  return b;
}
ll lcm(ll a, ll b) {
  return (a/gcd(a, b))*b;
}
bool prime(ll n) {
  if (n == 1) {
    return false;
  } else {
    for (ll i = 2; i <= (ll)pow(n, 0.5); i++) {
      if (n%i == 0) return false;
    }
  }
  return true;
}

int main() {
  ll n, m;
  cin >> n;
  vector <ll> v(n);
  rep(i, n) cin >> v.at(i);
  cin >> m;
  rep(i, m) {
    ll k;
    cin >> k;
    ll left = 0, right = n, tmp = 0;
    while (left < right) {
      ll mid = (left+right)/2;
      if (k == v.at(mid)) {
        cout << 1 << endl;
        tmp++;
        break;
      } else if (k < v.at(mid)) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    if (tmp == 0) {
      cout << 0 << endl;
    }
  }
}


Measured memory_bytes: 3564
import java.util.Scanner;
 
public class Main {
    static Scanner stdIn = new Scanner(System.in);
    public static void main(String[] args){
        int n = stdIn.nextInt();
        int[] a = new int[n];
        for(int i=0; i<n; i++){
            a[i] = stdIn.nextInt();
        }

        int[] left = new int[n];
        left[0] = a[0];
        for(int i=1; i<n; i++){
            if(left[i-1] < a[i]){
                left[i] = a[i];
            }else{
                left[i] = left[i-1];
            }
        }

        int[] right = new int[n];
        right[n-1] = a[n-1];
        for(int i=n-2; i>=0; i--){
            if(right[i+1] < a[i]){
                right[i] = a[i];
            }else{
                right[i] = right[i+1];
            }
        }

        for(int i=0; i<n; i++){
            if(i == 0){
                System.out.println(right[1]);
            }else if(i == n-1){
                System.out.println(left[n-2]);
            }else{
                if(left[i-1] < right[i+1]){
                    System.out.println(right[i+1]);
                }else{
                    System.out.println(left[i-1]);
                }
            }
        }
    }
}


Measured memory_bytes: 62852
#include <iostream>
#include <vector>
#include <string>
#include <cmath>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
int main()
{
    ll n;
    cin >> n;
    ll pt = sqrt(n);
    cout << pt * pt << endl;

    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		int count = 0;
		for (int i = 0; i < s.length(); i++) {
			if (String.valueOf(s.charAt(i)).equals("1")) {
				count++;
			}
		}
		System.out.println(count);

	}

}


Measured memory_bytes: 20820
from itertools import permutations

n, m, r = map(int, input().split())
A = list(map(int, input().split()))
d = [[1e18]*n for i in range(n)]
for i in range(m):
        a, b, c = map(int, input().split())
        d[a-1][b-1] = d[b-1][a-1] = c

for k in range(n):
        for i in range(n):
                for j in range(n):
                        d[i][j] = min(d[i][j], d[i][k]+d[k][j])


ans = 1e18
for x in permutations(A):
        l = 0
        for i in range(r-1):
                l += d[x[i]-1][x[i+1]-1]
        ans = min(ans, l)
print(ans)

Measured memory_bytes: 72664
#include<iostream>
#include<cmath>
#include<numeric>
#include<functional>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<n;i++)
#define ll long long
#define modbig (10*10*10*10*10*10*10*10*10+7)
using namespace std;

int main(){
  int n,count=0;
  cin>>n;
  int p[n];
  rep(i,n){
    cin>>p[i];
  }
  rep(i,n){
    if(p[i]!=i+1){
      count++;
    }
  }
  if(count<=2){
    cout<<"YES";
  }else{
    cout<<"NO";
  }
}

Measured memory_bytes: 256
import java.util.*;

public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
      	int k = Integer.parseInt(sc.next());
      	ArrayList<Integer> list = new ArrayList<>();
      	for(int cnt = 0;cnt < n;cnt++){
        	list.add(Integer.parseInt(sc.next()));
        }
      	Collections.sort(list);
      	int total = 0;
      	for(int cnt = 0;cnt < k;cnt++){
        	total += list.get(cnt);
        }
      	System.out.println(total);
	}
}

Measured memory_bytes: 38220
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5;
ll n,m,d;
int main()
{
    while(cin>>n>>m>>d)
    {
        if(d==0)
        {
            printf("%.10f\n",(m-1)*1.0/n);
        }
        else
        {
            printf("%.10f\n",(m-1)*2*(n-d)*1.0/(n*n));
        }
    }
    return 0;
}

Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Main {

    private static Scanner sc;

    public static void main(String[] args) {
        Main instance = new Main();
        sc = instance.new Scanner();
        instance.solve();
    }

    private void solve() {
        try {
            int n = sc.nextInt();
            int k = sc.nextInt();
            List<Integer> list = new ArrayList<>();
            for(int i=0; i<n; i++){
                list.add(sc.nextInt());
            }
            Collections.sort(list, (p1, p2) -> p1 - p2);
            int ans = 0;
            for(int i=0; i<k; i++){
                ans += list.get(i);
            }
            System.out.println(ans);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private class Scanner {
        String[] s;
        int i;
        BufferedReader br;
        String regex = " ";

        public Scanner() {
            s = new String[0];
            i = 0;
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        @Override
        protected void finalize() throws Throwable {
            try {
                super.finalize();
            } finally {
                destruction();
            }
        }

        private void destruction() throws IOException {
            if (br != null) br.close();
        }

        public String next() throws IOException {
            if (i < s.length) return s[i++];
            String st = br.readLine();
            while (st == "") st = br.readLine();
            s = st.split(regex, 0);
            i = 0;
            return s[i++];
        }

        public int nextInt() throws NumberFormatException, IOException {
            return Integer.parseInt(next());
        }

        public Long nextLong() throws NumberFormatException, IOException {
            return Long.parseLong(next());
        }

        public Double nextDouble() throws NumberFormatException, IOException {
            return Double.parseDouble(next());
        }
    }
}

Measured memory_bytes: 34044
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <vector>
#include <string>
using namespace std;

#pragma GCC optimize("O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//(UNCOMMENT WHEN HAVING LOTS OF RECURSIONS)
#pragma comment(linker, "/stack:200000000")
//(UNCOMMENT WHEN TRYING TO BRUTEFORCE WITH A LOT OF LOOPS)
#pragma GCC optimize("unroll-loops")

#define DEBUG(x) cout << ">> " << #x << ": " << x << endl;
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int MOD = 1000000007;
typedef long long ll;
typedef long double ld;

int main() { _
  string s; cin >> s;
  int half = s.size()/2;
  int ans = 0;
  for (int i = 0; i < half; i++) {
    if(s[i] != s[s.size()-i-1]) ans++;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define SORT(a) sort(a.begin(), a.end())
#define RSORT(a) sort(a.rbegin(), a.rend())
#define REP(i, n) for (long long i = 0; i < n; i++)
#define FOR(i, start, end) for (long long i = start; i < end; i++)
#define ALL(a) a.begin(), a.end()
#define MOD(a) a %= 1'000'000'007
using ll = long long;
using namespace std;
const int INF32       = 1'050'000'000;
const long long INF64 = 4'000'000'000'000'000'000;
const int MOD7        = 1'000'000'007;
const int MOD9        = 1'000'000'009;
template <class T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
void print() { std::cout << '\n'; }
template <class H, class... T> void print(H &&head, T &&... args) {
    std::cout << head;
    sizeof...(args) == 0 ? std::cout << "" : std::cout << ' ';
    print(std::forward<T>(args)...);
}
template <class T> void print(std::vector<T> &v) {
    for (int i = 0; i < v.size(); i++) {
        std::cout << v[i];
        i == v.size() - 1 ? std::cout << '\n' : std::cout << ' ';
    }
}
template <class T> void print(std::vector<std::vector<T>> &v) {
    for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < v[i].size(); j++) {
            std::cout << v[i][j];
            j == v[i].size() - 1 ? std::cout << '\n' : std::cout << ' ';
        }
    }
}

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

    int N;
    cin >> N;
    vector<int> a(N);
    for (auto &&i : a) {
        cin >> i;
    }
    SORT(a);
    vector<int> f(8, false);
    for (auto &&i : a) {
        if (i < 3200)
            f[i / 400] = 1;
    }
    int ans = 0;
    for (auto &&i : f) {
        ans += i;
    }

    int mx = a.end() - lower_bound(ALL(a), 3200);
    print(max(ans, 1), ans + mx);

    return 0;
}

Measured memory_bytes: 256
use std::io;
use std::cmp;


fn read<T>() -> Vec<T>
where T:
    std::str::FromStr,
    T::Err: std::fmt::Debug {

    let mut buf = String::new();
    io::stdin().read_line(&mut buf).unwrap();

    buf.split(' ')
        .map(|s| s.trim().parse().unwrap())
        .collect()
}


fn main() {

    let (n, m) = {
        let val = read::<isize>();
        (val[0], val[1])
    };

    if n == 2 || m == 2 {
        println!("0");
    } else {
        let x = cmp::max(1, n - 2);
        let y = cmp::max(1, m - 2);
        println!("{}", x * y);
    }
}

Measured memory_bytes: 4352
#include <stdio.h>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef vector<vector<int>> v2int;
typedef vector<ll> vll;
typedef vector<vector<ll>> v2ll;
typedef list<int> liint;
typedef pair<int, int> pint;
const int INF = int(2e9);
const ll LINF = ll(2e9) * ll(2e9);
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T, class C> void chmax(T& a, C b) { a > b ? : a = b; }
template<class T, class C> void chmin(T& a, C b) { a < b ? : a = b; }



int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int N, K;
    cin >> N >> K;
    ll ans = 0;
    REP(a, 1, N + 1) {
        if (((a % K) * 2) % K == 0){
            ll t = (N + a % K) / K;
            ans += t * t;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>

int main(void){
    int A,B,ans;
    scanf("%d",&A);
    scanf("%d",&B);

    if((0<A && A<10) && (0<B && B<10)){
        ans = A*B;
        printf("%d",ans);
    }
    else
    {
        ans = -1;
        printf("%d",ans);
    }
    
}

Measured memory_bytes: 128
import sys
input = sys.stdin.readline

def solve():
    n=int(input())
    count=0
    for i in range(1,n):
        for j in range(1,n):
            if n - i*j>0:
                count+=1
            else:
                break
    print(count)

if __name__=="__main__":
    solve()



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

using namespace std;

#define int long long 
#define F first 
#define S second 
#define pii pair<int,int> 
#define mpr make_pair

typedef long long ll;

const int maxn = 1e6+10;
const int mod = 998244353;
const int inf = 1e9+10;

int n, k, l;
int a[maxn], sum[maxn], cnt[maxn], dp[maxn];

ll pw(ll a, ll b)
{
	if(b == 0) return 1;
	ll x = pw(a,b/2); x = (x*1ll*x) % mod;
	if(b&1) x =(x*1ll*a) % mod;
	return x;
}

signed main()
{
//	ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);	
		
	cin>> n;
	for(int i = 1; i <= n; i++)
	{
		cin>> a[i];
		cnt[a[i]]++;
	}

	ll repo = pw(2,mod-2);
	for(int i = 1; i < maxn; i++)
	{
		int sum1 = 0,  sum2 = 0;
		for(int j = i; j < maxn; j += i)
		{
			(sum1 += (j * cnt[j]) % mod) %= mod;
			(sum2 += (((j * j) % mod) * cnt[j]) % mod) %= mod; 	
		}
		sum[i] = ((((sum1 * sum1) % mod - sum2) % mod) * repo) % mod;
	//	if(i < 7)
	//	cout<< sum[i] <<" ";
	}
	
	ll ans = 0;
	for(int i = maxn-1; i >= 1; i--)
	{
		dp[i] = (sum[i] * pw(i,mod-2)) % mod;
		for(int j = 2*i; j < maxn; j += i)
		{
			(dp[i] -= (dp[j] * (j/i)) % mod) %= mod;
		} 
		(ans += dp[i]) %= mod;
	}
	cout<< (ans + mod) % mod;
}

















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

#define N 3001

using namespace std;

int n;
bool p;
long long a[N];
long long dp[N][N];

long long solve2()
{
	for (int len = 1; len <= n; len++)
	{
		for (int i = 0; i <= n - len; i++)
		{
			int j = i + len - 1;
			int s = ((j - i + 1) & 1) == p;
			if (i == j)
				dp[i][j] = (s == 1 ? 1 : -1) * a[i];
			else if (s == 1)
				dp[i][j] = max(a[i] + dp[i + 1][j], a[j] + dp[i][j - 1]);
			else
				dp[i][j] = min(dp[i + 1][j] - a[i], dp[i][j - 1] - a[j]);
		}
	}
	return dp[0][n - 1];
}

int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	p = n & 1;
	cout << solve2() << endl;
	return 0;
}


Measured memory_bytes: 70144
import java.util.*;
public class Main{
	public static void main(String[] args){
    	Scanner sc = new Scanner(System.in);
      int a = sc.nextInt();
      int b = sc.nextInt();
      int c = sc.nextInt();
      int d = sc.nextInt();
      if(a+b == c+d)System.out.println("Balanced");
      else if(a+b > c+d)System.out.println("Left");
      else if(a+b < c+d)System.out.println("Right");
    }
}


Measured memory_bytes: 23120
#include<stdio.h>
#include<math.h>
int main() {
    double a, b,m;
    long q, i,cm,flag;
    long ans;
    scanf("%d", &q);
    for (i = 0; i < q; i++) {
        flag = 0;
        scanf("%lf%lf", &a, &b);
        if (a == b) {
            ans = (a - 1) * 2;
        }
        else {
            m = sqrt(a*b);
            cm = m;
            if (cm == m)
                cm--;
            else if (cm*(cm + 1) >= a*b)
                flag = 1;
            ans = cm * 2 - 1;
            if (flag == 1)
                ans--;
        }
        printf("%ld\n", ans);
    }
    return 0;
}

Measured memory_bytes: 128
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
 
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<stdarg.h>
#include<malloc.h>

int main()
{
	char str[128];
	char *buf;
	double num[64]={0};
	int i;

	while(gets(str) != NULL) {

		i=0;

		buf = strtok(str, " ");
		
		if(buf[0] == '+'){
			num[i-2] = num[i-2] + num[i-1];
			num[i-1] = 0;
			i--;
		} else if(buf[0] == '-' && (buf[1] < '0' || buf[1] > '9')) {
			num[i-2] = num[i-2] - num[i-1];
			num[i-1] = 0;
			i--;
		} else if(buf[0] == '*'){
			num[i-2] = num[i-2] * num[i-1];
			num[i-1] = 0;
			i--;
		} else if(buf[0] == '/'){
			num[i-2] = num[i-2] / num[i-1];
			num[i-1] = 0;
			i--;
		} else {
			num[i] = atof(buf);
			i++;
		}

		while(buf != NULL){
			buf = strtok(NULL, " ");
			if(buf != NULL){
				
				if(buf[0] == '+'){
					num[i-2] = num[i-2] + num[i-1];
					num[i-1] = 0;
					i--;
				} else if(buf[0] == '-' && (buf[1] < '0' || buf[1] > '9')) {
					num[i-2] = num[i-2] - num[i-1];
					num[i-1] = 0;
					i--;
				} else if(buf[0] == '*'){
					num[i-2] = num[i-2] * num[i-1];
					num[i-1] = 0;
					i--;
				} else if(buf[0] == '/'){
					num[i-2] = num[i-2] / num[i-1];
					num[i-1] = 0;
					i--;
				} else {
					num[i] = atof(buf);
					i++;
				}
			}
		}
		printf("%f\n", num[0]);
	}
	return 0;
}

Measured memory_bytes: 572
#include <iostream>
#include <queue>
#include <functional>
#include <vector>

using namespace std;

struct edge {
	int to;
	int cost;
};

typedef pair<int, int> P;

int V;
vector<edge> G[3010];
vector<edge> G2[3010];
int d[3010];

void dijkstra(int s, int h) {
	priority_queue<P, vector<P>, greater<P> > que;
	fill(d, d+V, 10000000);
	d[s] = 0;
	que.push(P(0, s));

	while (!que.empty()) {
		P p = que.top();
		que.pop();
		int v = p.second;
		if (d[v] < p.first) {
			continue;
		}
		if (h == 0) {
			for (int i = 0; i < G[v].size(); i++) {
				edge e = G[v][i];
				if (d[e.to] > d[v] + e.cost) {
					d[e.to] = d[v] + e.cost;
					que.push(P(d[e.to], e.to));
				}
			}
		} else {
			for (int i = 0; i < G2[v].size(); i++) {
				edge e = G2[v][i];
				if (d[e.to] > d[v] + e.cost) {
					d[e.to] = d[v] + e.cost;
					que.push(P(d[e.to], e.to));
				}
			}
		}
	}
}

int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) {
			break;
		}
		for (int i = 1; i <= m; i++) {
			G[i].clear();
			G2[i].clear();
		}
		V = m+1;
		int a, b, time, cost;
		for (int i = 0; i < n; i++) {
			cin >> a >> b >> cost >> time;
			edge e;
			e.to = b;
			e.cost = cost;
			G[a].push_back(e);
			e.to = a;
			G[b].push_back(e);
			e.to = b;
			e.cost = time;
			G2[a].push_back(e);
			e.to = a;
			G2[b].push_back(e);
		}
		int w;
		cin >> w;
		int p, q, r;
		for (int i = 0; i < w; i++) {
			cin >> p >> q >> r;
			dijkstra(p, r);
			cout << d[q] << endl;
		}
	}
	return 0;
}

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

using namespace std;

#define MAX 50

int main(){
  double r;
  int n;
  while(cin >> r >> n, r){
    int H[MAX] = {0},xl,xr,h;
    for(int i = 0 ; i < n ; i++){
      cin >> xl >> xr >> h;
      xl += 20; xr += 20;
      for(int j = xl ; j < xr ; j++){
        H[j] = max(H[j],h);
      }
    }
    double res = (1<<29);
    for(int i = 20-r ; i < 20+r ; i++){
      double rr = i-20+(i<20);
      res = min(res,r+H[i]-sqrt(r*r-rr*rr));
    }
    printf("%.8f\n",res);
  }
  return 0;
}

Measured memory_bytes: 1248
from sys import stdin
from collections import deque

n = int(stdin.readline().rstrip())
li = [int(stdin.readline().rstrip())-1 for _ in range(n)]
now = 0
point = 1
for i in range(n):
    if li[now] == 1:
        print(point)
        exit()
    else:
        now = li[now]
        point += 1

print(-1)

Measured memory_bytes: 7808
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b = LI()
    if not (a % 3 and b % 3 and (a + b) % 3):
        print("Possible")
    else:
        print("Impossible")
    return

#B
def B():
    return

#C
def C():
    return

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == '__main__':
    A()


Measured memory_bytes: 38512
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
template<typename T>
void fin(T const& t){ std::cout << t << std::endl; exit(0); }

int main() {
  std::string S; int K; std::cin >> K >> S;
  if(S.size() <= K) fin(S);
  fin(S.substr(0,K)+"...");
  return 0;
}


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

int main(){
  string S, T;
  cin >> S;
  cin >> T;
  int mfout_max = 0;

  for (int n = 0; n <= S.size() - T.size(); n++){
    int mfout = 0;
    for (int m = 0; m < T.size(); m++){
      if (S[n+m] == T[m]){
	mfout++;
      }
    }
    //cout << mfout << endl;
    if (mfout > mfout_max){
      mfout_max = mfout;
    }
  }

  cout << T.size() - mfout_max << endl;
  return 0;
}


Measured memory_bytes: 3628
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define pp pair<ll,ll>
#define ppp pair<ll,pp >
#define trace(...) cout<<"Line:"<<__LINE__<<" "; __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>
void __f(const char* name, Arg1&& arg1) {
	cout<<name<<" : "<<arg1<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
	const char* comma=strchr(names+1,',');
	cout.write(names,comma-names)<<" : "<<arg1<<" | ";
	__f(comma+1,args...);
}
typedef tree<pp,null_type,less<pp>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define ld long double
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin() , (x).end()
#define rep(i,a,b) for( ll i = a ; i < b ; i+=1 )
#define sz(v) (ll)(v.size())
#define hell (ll)998244353
#define slld(x) scanf("%lld",&x)
ll t=1, tt;
const ll N=100005, inf=1e18;
ll h[N];
vector<ll> adj[N];
void solve(){
	ll n, m, x, y;
	cin>>n>>m;
	rep(i,1,n+1)
		cin>>h[i];
	rep(i,0,m){
		cin>>x>>y;
		adj[x].pb(y);
		adj[y].pb(x);
	}
	x=0;
	rep(i,1,n+1){
		y=1;
		for(auto u : adj[i])
			if(h[u]>=h[i])y=0;
		x+=y;
	}
	cout<<x;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   	//cin>>t;
   	tt=t;
    while(t--){
        solve();
        if(t)cout<<"\n";
    }
    return 0 ;
}


Measured memory_bytes: 10284
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws Exception {
        try (Scanner sc = new Scanner(System.in)) {
            int N = sc.nextInt();
            long M = sc.nextLong();
            long[] A = new long[N];
            for (int i = 0; i < N; i++) {
                A[i] = sc.nextLong() / 2;
            }

            long lcm = 1;
            for (int i = 0; i < N; i++) {
                lcm = lcm(lcm, A[i]);
                if (lcm > M) {
                    System.out.println(0);
                    return;
                }
            }

            for (int i = 0; i < N; i++) {
                if ((lcm / A[i]) % 2 == 0) {
                    System.out.println(0);
                    return;
                }
            }

            long ans = (M / lcm + 1) / 2;

            System.out.println(ans);
        }
    }

    public static long gcd(long M, long N) {
        if (M < N) {
            return gcd(N, M);
        }
        return N > 0 ? gcd(N, M % N) : M;
    }

    public static long lcm(long M, long N) {
        return M * N / gcd(M, N);
    }

}


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

int main(){
int n,i;
cin >>n;
int a[100];
 for (i=0;i<n;i++){
  cin>>a[i];
 }
 while(n>1){
   n--;
   cout<<a[n]<<" ";
 }
 cout <<a[0]<<endl;

    return 0;
}

Measured memory_bytes: 1160
#include <stdio.h>

int main(void)
{
    char c_inputCharacterBuffer, c_decoy;
    char inputString[200001] = {'\0'}, addForward[200001] = {'\0'}, addBackward[200001] = {'\0'};
    int operateFlag, reverseFlag = 0, addFlag;
    int operateCount, stringCount = 0, addForwardCount = 0, addBackwardCount = 0;
    scanf("%s", inputString);
    for (int index = 0; inputString[index] != '\0'; index++)
    {
        stringCount++;
    }
    scanf("%d", &operateCount);

    for (int index = 0; index < operateCount; index++)
    {
        scanf("%d", &operateFlag);
        if (operateFlag == 1)
        {
            if (reverseFlag == 0)
            {
                reverseFlag = 1;
            }
            else if (reverseFlag == 1)
            {
                reverseFlag = 0;
            }
        }
        else if (operateFlag == 2)
        {
            scanf("%d%c%c", &addFlag, &c_decoy, &c_inputCharacterBuffer);
            if (reverseFlag == 0)
            {
                if (addFlag == 1)
                {
                    addForward[addForwardCount] = c_inputCharacterBuffer;
                    addForwardCount++;
                }
                else if (addFlag == 2)
                {
                    addBackward[addBackwardCount] = c_inputCharacterBuffer;
                    addBackwardCount++;
                }
            }
            else if (reverseFlag == 1)
            {
                if (addFlag == 1)
                {
                    addBackward[addBackwardCount] = c_inputCharacterBuffer;
                    addBackwardCount++;
                }
                else if (addFlag == 2)
                {
                    addForward[addForwardCount] = c_inputCharacterBuffer;
                    addForwardCount++;
                }
            }
        }
    }

    if (reverseFlag == 0)
    {
        if (addForwardCount > 0)
        {
            for (int index = addForwardCount - 1; index >= 0; index--)
            {
                printf("%c", addForward[index]);
            }
        }
        printf("%s", inputString);
        printf("%s", addBackward);
    }
    else if (reverseFlag == 1)
    {
        if (addBackwardCount > 0)
        {
            for (int index = addBackwardCount - 1; index >= 0; index--)
            {
                printf("%c", addBackward[index]);
            }
        }
        for (int index = stringCount - 1; index >= 0; index--)
        {
            printf("%c", inputString[index]);
        }
        printf("%s", addForward);
    }

    return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <math.h>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
const ll INF = 1e18;
const int MX = 20;
ll dp[MX][MX];

int main(void)
{
    int n, m, x;
    cin >> n >> m >> x;

    vector<int> c(n);
    int a[12][12];

    rep(i,n){
        cin >> c.at(i);
        rep(j,m){
            cin >> a[i][j];
        }
    }

    ll ans = INF;

    rep(s, 1<<n)
    {
        ll mny = 0;
        vector<int> d(m);
        rep(i,n)
        {
            if(s>>i&1)
            {
                mny += c.at(i);
                bool b = true;
                rep(j, m)
                {
                    d.at(j) += a[i][j];
                    if (d.at(j) < x)
                        b = false;
                }
                if (b)
                {
                    ans = min(ans, mny);
                }
            }
        }
    }

    cout << (ans==INF?-1:ans) << endl;

    return 0;
}

Measured memory_bytes: 3644
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;

const ll MAX_N = 1E+05;
const ll MOD = 1E+09 + 7;

const int rdigit = 30;
const ll R = 1LL << rdigit;
const ll R2 = (R * R) % MOD;
const ll ND = 79133769;

int N, M;

ll X[MAX_N], Y[MAX_N];

ll calc(ll l, ll* x);
ll MR(ll T);

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

    cin >> N >> M;

    for (int i = 0; i < N; i++) {
        cin >> X[i];
    }
    for (int i = 0; i < M; i++) {
        cin >> Y[i];
    }

    cout << MR(MR(calc(N, X) * calc(M, Y))) << "\n";

    return 0;
}

ll calc(ll l, ll* x)
{
    ll ans = 0;
    for (int i = 0; i < l; i++) {
        ans += MR(MR(R2 * (2 * i - l + 1)) * MR(R2 * x[i]));
        ans = ans >= MOD ? ans - MOD : ans;
    }
    return ans;
}

ll MR(ll T)
{
    ll c = (T & (R - 1)) * ND;
    c &= (R - 1);
    c *= MOD;
    c += T;
    c >>= rdigit;
    if (c >= MOD) {
        c -= MOD;
    }
    return c;
}

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

const int mx = 1e5 + 5;

struct Edge{
    int to;
    int nxt;
}edges[2*mx]; // virtual vertex

int head[mx],depth[mx],tot;

void AddEdge(int u, int v) {
    edges[tot].to = v;
    edges[tot].nxt = head[u];
    head[u] = tot++;
}

int DFS(int cur){
    if(depth[cur]) return depth[cur];

//    printf("[DFS] cur : %d\n", cur);
    int ret = 0;
    for(int i = head[cur]; i != -1; i = edges[i].nxt) {
        ret = max(ret,1 + DFS(edges[i].to));
    }
    depth[cur] = ret;
    return ret;
}

int main(){
    memset(head, -1, sizeof(head));


    int N, M;
    scanf("%d%d",&N, &M);
    for(int i = 0 ;i < M; ++i){
        int xi,yi;scanf("%d%d",&xi,&yi);
        AddEdge(xi,yi);
    }
    for(int i = 1; i <= N; ++i)
        AddEdge(0,i);

    DFS(0);
    printf("%d\n",depth[0]-1);
    return 0;
}

Measured memory_bytes: 7464
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll read()
{
	char c;
	ll w=1;
	while((c=getchar())>'9'||c<'0')if(c=='-')w=-1;
	ll ans=c-'0';
	while((c=getchar())>='0'&&c<='9')ans=(ans<<1)+(ans<<3)+c-'0';
	return ans*w;
}
ll n,m;
priority_queue<ll,vector<ll>,greater<ll> >q;
ll ans=0;
struct node
{
	ll a,b;
	bool operator<(const node &w)const
	{
		return b>w.b;
	}
}e[200005];
int main(){
	n=read();
	m=read();
	for(int i=1;i<=n;i++)
	{
		ll w=read();
		ans+=w;
		q.push(w);
	}
	for(int i=1;i<=m;i++)
	{
		e[i].a=read();
		e[i].b=read();
	}
	sort(e+1,e+m+1);
	for(int i=1;i<=m;i++)
	{
//		cout<<e[i].b<<endl;
		if(e[i].b<=q.top())break;
		for(int j=1;j<=e[i].a;j++)
		{
			if(e[i].b<=q.top())break;
			ans-=q.top();
			ans+=e[i].b;
			q.push(e[i].b);
			q.pop();
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 5444
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <math.h>
#include <string>
#include <map>
using namespace std;
typedef long long ll;

int main()
{
    int a, b;
    cin >> a >> b;
    if((a+b)%2==1) cout << "IMPOSSIBLE";
    else{
        cout << (a+b)/2;
    }

    
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <iterator>
#pragma warning(disable:4996)
 
typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223372036854775807
#define INF 2147483647
#define MOD 1000000007
using namespace std;


int main(int argc, char* argv[])
{
    long h,w;
    scanf("%ld%ld", &h, &w);

    char str[105][105]={0};

    long i;
    for(i=0; i<h; i++) {
        scanf("%s", str[i]);
    }

    for(i=0; i<w+2; i++) {
        printf("#");
    }
    printf("\n");

    for(i=0; i<h; i++) {
        printf("#%s#\n", str[i]);
    }

    for(i=0; i<w+2; i++) {
        printf("#");
    }
    printf("\n");

    return 0;
}



Measured memory_bytes: 128
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );
using namespace std;
using ll = long long;
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 ll INF = 1e9;
const ll MOD = 1e9 + 7;

int main(){
    string s;
    cin >> s;

    int n = s.size();
    int ans = INF;
    rep(i, n-1){
        if(s[i] != s[i+1]) chmin(ans, max(i+1, n-i-1));
    }

    if(ans == INF) ans = n;
    
    cout << ans << endl;
}

Measured memory_bytes: 512
#include<iostream>
#include<map>

int main(){

	int N;
	std::cin >> N;

	std::map<int, int> bucket;

	for( int i{}; i < N; ++i )
	{
		int t;
		std::cin >> t;

		++bucket[t];
	}

	int ans{};

	for( int i{}; i < bucket.size(); ++i )
		for( int j{ i + 1 }; j < bucket.size(); ++j )
			for( int k{ j + 1 }; k < bucket.size(); ++k )
			{
				const auto beg = std::cbegin( bucket );
				auto lit = std::next( beg, i );
				auto jit = std::next( beg, j );
				auto kit = std::next( beg, k );

				if( lit->first + jit->first <= kit->first )
					break;

				ans += lit->second * jit->second * kit->second;
			}

	std::cout << ans << "\n";
	return 0;
}

Measured memory_bytes: 3192
#include <bits/stdc++.h>
using namespace std;
/*
int  main() {
string s;
int n,q;
cin>>n>>q;
cin>>s;

int arr[n+1]={0};
int length=0;
for(int i=1;i<n;i++)
{
    if(s[i]=='C'&&s[i-1]=='A'){
     arr[i]=1; }

    arr[i]+=arr[i-1];
}


int l,r;
while(q--)
{
    cin>>l>>r;
    cout<<arr[r-1]-arr[l-1]<<endl;
}
return 0;
}
*/
 int main(){

 int n,Q;
 cin>>n>>Q;
  int a[n+1] = {0};
  string s;
  cin>>s;

  for(int i=1;i<s.length();i++){

     if(s[i] == 'C' && s[i-1] == 'A'){
        a[i] = 1; }

    a[i] += a[i-1];
  }


 while(Q--){
        int l,r;
  cin>>l>>r;
  cout<<a[r-1]-a[l-1]<<endl;
 }


 return 0;
 }



Measured memory_bytes: 1408
n,m = map(int,input().split())
x = list(map(int,input().split()))

if n >= m:
    print(0)
else:
    x = sorted(x)
    x = [x[i+1]-x[i] for i in range(len(x)-1)]

    x = sorted(x, reverse=True)
    x = x[n-1:]
    ans = 0
    for i in x:
        ans+=i
    print(ans)


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

#define for_(i,a,b) for(int i=(a);i<(b);++i)

int main() {
	int H, W;
	cin >> H >> W;
	
	vector< string > grid(H);
	for_(i,0,H) cin >> grid[i];
	
	for_(y,0,H) {
		for_(x,0,W) {
			int xx = x;
			while (xx >= 0 && grid[y][xx] == '.') --xx;
			cout << (xx >= 0 ? x - xx : -1) << (x < W - 1 ? " " : "\n");
		}
	}
}

Measured memory_bytes: 1232
using System;

class Program {
  static void Main(string[] args) {
    string[] str = Console.ReadLine().Split(' ');
    int sx = int.Parse(str[0]);
    int sy = int.Parse(str[1]);
    int tx = int.Parse(str[2]);
    int ty = int.Parse(str[3]);

    // First Forward
    for(int i = sx; i < tx; ++i) {
      Console.Write("R");
    }
    for(int i = sy; i < ty; ++i) {
      Console.Write("U");
    }

    // First Return
    for(int i = sx; i < tx; ++i) {
      Console.Write("L");
    }
    for(int i = sy; i < ty; ++i) {
      Console.Write("D");
    }

    // Second Forward
    Console.Write("D");
    for(int i = sx; i <= tx; ++i) {
      Console.Write("R");
    }
    for(int i = sy; i <= ty; ++i) {
      Console.Write("U");
    }
    Console.Write("L");

    // Second Return
    Console.Write("U");
    for(int i = sx; i <= tx; ++i) {
      Console.Write("L");
    }
    for(int i = sy; i <= ty; ++i) {
      Console.Write("D");
    }
    Console.Write("R\n");

  }
}


Measured memory_bytes: 11092
//
//  main.cpp
//  A
//
//  Created by Sultan.
//  Copyright © 2017 Sultan. All rights reserved.
//

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>

#define ll long long
#define ioi exit(0);

#define f first
#define s second

#define inf 1e9 + 7;

#define NFS ios_base :: sync_with_stdio(0), cin.tie(0), cout.tie(0);

#define lb(x) lower_bound(x)
#define ub(x) upper_bound(x)

#define pb(x) push_back(x)
#define ppb(x) pop_back(x)

#define in(x) insert(x)

#define rep(x,y,z) for(int x=y;x<=z;x++)
#define per(x,y,z) for(int x=y;x>=z;x--)

using namespace std;

ll n;

pair <int ,int > a[100500];

bool cmp(pair<int ,int> a,pair<int ,int> b){
    return a.f>b.f;
}

int main() {
    
    //freopen("A.in","r",stdin);
    //freopen("A.out","w",stdout);
    
    NFS
    
    cin >> n;
    
    for(int i=1;i<=n;i++){
        cin >> a[i].f >> a[i].s;
    }
    
    sort(a+1,a+n+1,cmp);
    
    cout << a[1].f + a[1].s << endl;
    
    ioi
}

Measured memory_bytes: 1024
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <tuple>
#include <limits>

using namespace std;

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

    int capacity = 2*d+1;
    cout << (n+capacity-1)/capacity << endl;

    return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define SF scanf
#define PF printf
#define MAXN 300010
using namespace std;
typedef long long ll;
ll f[MAXN],f1[MAXN],f2[MAXN];
ll t[MAXN];
ll st[MAXN],q[MAXN];
ll sum[MAXN],tmp[MAXN];
int n,m;
void prepare(ll fx[]){
	fx[0]=0;
	int top=0;
	st[++top]=0;
	for(int i=1;i<=n;i++)
		sum[i]=sum[i-1]+t[i];
	for(ll i=1;i<=n;i++){
		fx[i]=fx[i-1];
		while(top>1&&q[st[top]]-q[st[top-1]]<=2ll*i*(st[top]-st[top-1]))
			top--;
		fx[i]=max(fx[i],fx[st[top]]+(i-st[top])*(i-st[top]+1)/2ll-sum[i]+sum[st[top]]);
		q[i]=i*i-i+2ll*(fx[i]+sum[i]);
		while(top>1&&(q[st[top]]-q[st[top-1]])*(i-st[top-1])<=(q[i]-q[st[top-1]])*(st[top]-st[top-1]))
			top--;
		st[++top]=i;
	}
}
void solve(ll f1[],ll f2[],ll l,ll r){
	if(l==r){
		f[l]=max(f[l],f1[l-1]+f2[l+1]-t[l]+1ll);
		return ;
	}
	int mid=(l+r)>>1,top=0;
	for(ll i=l;i<=mid;i++){
		q[i]=i*i-i+2ll*(f1[i]+sum[i]);
		while(top>1&&(q[st[top]]-q[st[top-1]])*(i-st[top-1])<=(q[i]-q[st[top-1]])*(st[top]-st[top-1]))
			top--;
		st[++top]=i;
	}
	for(ll i=mid+1;i<=r;i++){
		while(top>1&&q[st[top]]-q[st[top-1]]<=2ll*i*(st[top]-st[top-1]))
			top--;
		tmp[i]=f1[st[top]]+f2[i+1]+(i-st[top])*(i-st[top]+1)/2ll-sum[i]+sum[st[top]];
	}
	for(int i=r-1;i>mid;i--)
		tmp[i]=max(tmp[i],tmp[i+1]);
	for(int i=mid+1;i<=r;i++)
		f[i]=max(f[i],tmp[i]);
	solve(f1,f2,l,mid);
	solve(f1,f2,mid+1,r);
}
int main(){
	SF("%d",&n);
	for(int i=1;i<=n;i++){
		SF("%lld",&t[i]);
		f[i]=-t[i]+1ll;
	}
	prepare(f1);
	reverse(t+1,t+1+n);
	prepare(f2);
	reverse(t+1,t+1+n);
	reverse(f2+1,f2+1+n);
	for(int i=1;i<=n;i++)
		sum[i]=sum[i-1]+t[i];
	solve(f1,f2,0,n);
	reverse(t+1,t+1+n);reverse(f1+1,f1+1+n),reverse(f2+1,f2+1+n);reverse(f+1,f+1+n);
	for(int i=1;i<=n;i++)
		sum[i]=sum[i-1]+t[i];
	solve(f2,f1,0,n);
	reverse(t+1,t+1+n);reverse(f1+1,f1+1+n),reverse(f2+1,f2+1+n);reverse(f+1,f+1+n);
	SF("%d",&m);
	for(int i=1;i<=m;i++){
		int p;
		ll t1;
		SF("%d%lld",&p,&t1);
		PF("%lld\n",max(f1[p-1]+f2[p+1],f[p]+t[p]-t1));
	}
}

Measured memory_bytes: 21376
import numpy as np

N = int(input())

def solve():
    arr = [0] * 100
    ans = 0
    
    for i in range(1, N + 1):
        strNum  = str(i)
        arr[int(strNum[0] + strNum[-1])] += 1
        
    for i in range(1, N + 1):
        strNum = str(i)
        ans += arr[int(strNum[-1] + strNum[0])]
    
    return ans

print(solve())

Measured memory_bytes: 12492
#define _GLIBCXX_DEBUG
#include"bits/stdc++.h"

using namespace std;

//loops
#define REP(i,n) for(int i=0;i<(ll)(n);i++)
#define REPD(i,n) for(int i=(ll)(n)-1;i>=0;i--)
#define OneToN(i,n) for(int i=1;i<(ll)(n+1);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)

//arrays
#define ALL(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define add push_back

//pair
#define fir first
#define sec second

//types
#define ll long long
#define vec vector
//UNCOMMENT WHEN NEEDED
//#define int ll

//input
template <class T> T get(){ T s; cin >> s; return(s);}
#define gi get<int>()
#define gs get<string>()

template <class T> vector<T> getv(int n) { vec<T> v(n); REP(i, n) cin >> v[i]; return v; }
#define gim(n) getv<int>(n)
#define gsm(n) getv<string>(n)

//output
void print(int a){ cout << a << endl; }
void print(string a){ cout << a << endl; }
void print(double a, int dig){ cout << std::setprecision(dig) << a << endl; }

template <class T = int>
void print(vec<T> v){ for (T item: v) { cout << item << " "; } cout << endl; }

void YesNo1(bool i = true){ return print(i?"Yes":"No"); }
void YESNO2(bool i = true){ return print(i?"Yes":"N1010o"); }

//debug output
#define var_name(var) #var
template <class T> void debug(T &var, int nest = 0, string before = "") { cout << string("          ", nest) << before; print(var); }

//name replacement
#define y0 y0__
#define y1 y1__
#define j0 j0__
#define j1 j1__
#define next next__
#define prev prev__

//bool lambdas
#define lamb(exp) [](auto i){return exp;}
#define isEven [](int i){return i % 2 == 0;}
#define isOdd [](int i){return i % 2 != 0;}

//constants
const ll INF = 1e18;
const int MOD = 10000007;

void Main() {
    //code here
    int n = gi;
    vec<int> nums = gim(n);
    sort(ALL(nums));
    reverse(ALL(nums));
    int alice = 0;
    int bob = 0;
    REP(i, nums.size()) {
        if (i%2 == 0) {
            alice += nums[i];
        } else {
            bob += nums[i];
        }
    }
    print(alice - bob);
}

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    Main();
}

Measured memory_bytes: 256
h,w = map(int,input().split())
field = [list(input()) for i in range(h)]
 
tmp=0
for i in range(h):
    for j in range(w):
        if field[i][j]=='#':
            tmp+=1
if (h+w-1)==tmp:
    print('Possible')
else:
    print('Impossible')

Measured memory_bytes: 3060
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
using namespace std;

int main()
{
  string word;
  int page;
  map<string, vector<int> > index;
  while (cin >> word >> page)
    index[word].push_back(page);

  for (map<string, vector<int> >::iterator it = index.begin(); it != index.end(); ++it) {
    vector<int>& pages = it->second;
    sort(pages.begin(), pages.end());
    cout << it->first << endl;
    for (int i = 0; i < pages.size()-1; ++i)
      cout << pages[i] << " ";
    cout << pages[pages.size()-1] << endl;
  }
  return 0;
}

Measured memory_bytes: 0
#include <iostream>

int main() {
    using namespace std;
    int N;
    cin >> N;
    if(N<=4){
        cout << "2 3 25";
        if(N==4){
            cout << " 30";
        }
        cout << endl;
        return 0;
    }
    if(N<15000){
        if(N%2==1){
            cout << "6 ";
            N--;
        }
        cout << "2 4 3 9 ";
        N-=4;
        if(N==0){
            cout << endl;
            return 0;
        }
        for(int i=7;i<=30000;i++){
            if(i%2==0&&i%6!=0){
                cout << i << " ";
                N--;
            }
            if(N==0){
                cout << endl;
                return 0;
            }
        }
        for(int i=13;i<=30000;i++){
            if(i%3==0&&i%6!=0){
                cout << i << " ";
                N--;
            }
            if(N==0){
                cout << endl;
                return 0;
            }
        }
    }
    if(N>=15000){
        N-=15000;
        for(int i=1;i<=30000;i++){
            if(i%6==2||i%6==3||i%6==4){
                cout << i << " ";
            }
            if(i%6==0&&N>=i/6){
                cout << i << " ";
            }
        }
        cout << endl;
    }
}

Measured memory_bytes: 3172
/*input
3 5 2
3 2 3 4
.....
.@..@
..@..
*/
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
 
using namespace std;
 

int dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0};
struct NOD
{
  int x, y;
}u, v;
 
queue<NOD> que;
 
int main()
{
  int n, m, k;
  cin >> n >> m >> k;
  int bx, by, ex, ey;
  cin >> bx >> by >> ex >> ey;
  char mp[n + 5][m + 5];
  int d[n + 5][m + 5];
  for(int i=1; i<=n; i++)
    scanf("%s", mp[i]+1);
  memset(d, 33, sizeof d);
  
  d[bx][by] = 0;
  for(que.push((NOD){bx, by}); !que.empty(); que.pop())
  {
    u = que.front();
    if(u.x == ex && u.y == ey) return 0*printf("%d", d[u.x][u.y]);
    for(int di = 0; di < 4; di++)
    {
      for(int i=1; i<=k; i++)
      {
        v = (NOD){u.x+dx[di]*i, u.y+dy[di]*i};
        if(mp[v.x][v.y] != '.' || d[v.x][v.y] <= d[u.x][u.y]) break;
        if(d[v.x][v.y] > d[u.x][u.y]+1)
        {
          d[v.x][v.y] = d[u.x][u.y] + 1;
          que.push(v);
        }
      }
    }
  }
  printf("-1");
 
  return 0;
}

Measured memory_bytes: 28108