akhauriyash/Code-Regression huggingface dataset:
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> P;

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

int N;

int X[MAX_N];

map<ll, int, greater<ll>> mp;

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

    cin >> N;

    for (int i = 0; i < N; i++) {
        ll l;
        cin >> l;
        if (mp.count(l)) {
            mp[l]++;
        } else {
            mp.insert(make_pair(l, 1));
        }
    }

    ll l1 = 0;
    ll l2 = 0;
    for (auto ite = mp.begin(); ite != mp.end(); ite++) {
        int n = ite->second;
        if (n >= 2) {
            if (l1 != 0) {
                l2 = ite->first;
                break;
            } else {
                l1 = ite->first;
                if (n >= 4) {
                    l2 = ite->first;
                    break;
                }
            }
        }
    }

    cout << l1 * l2 << "\n";

    return 0;
}

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

namespace atcoder.abc168.b
{
    class Program
    {
        static void Main(string[] args)
        {
            int K = getIntParam();
            string S = getStringParam();

            string ret = S.Substring(0, (S.Length > K ? K : S.Length));
            if (S.Length > K)
            {
                ret = String.Concat(ret, "...");
            }
            Console.WriteLine(ret);
        }
        //↓↓↓ shared function ↓↓↓
        static int getIntParam()
        {
            return int.Parse(Console.ReadLine());
        }
        static int[] getIntParams()
        {
            return Console.ReadLine().Split(' ').Select(i => int.Parse(i)).ToArray();
        }
        static long getLongParam()
        {
            return long.Parse(Console.ReadLine());
        }
        static long[] getLongParams()
        {
            return Console.ReadLine().Split(' ').Select(i => long.Parse(i)).ToArray();
        }
        static string getStringParam()
        {
            return Console.ReadLine();
        }
        static string[] getStringParams()
        {
            return Console.ReadLine().Split(' ');
        }
        static decimal getDecimalParam()
        {
            return decimal.Parse(Console.ReadLine());
        }
        static decimal[] getDecimalParams()
        {
            return Console.ReadLine().Split(' ').Select(i => decimal.Parse(i)).ToArray();
        }

    }
}


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

int main(){
	int n,UN[202][3],a,b,c;
	int d[3][102]={0};
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d%d%d",&UN[i][0],&UN[i][1],&UN[i][2]);
		d[0][UN[i][0]]++;
		d[1][UN[i][1]]++;
		d[2][UN[i][2]]++;
	}
	for(int i=0;i<n;i++){
		int ans=0;
		if(d[0][UN[i][0]]==1)ans+=UN[i][0];
		if(d[1][UN[i][1]]==1)ans+=UN[i][1];
		if(d[2][UN[i][2]]==1)ans+=UN[i][2];
		printf("%d\n",ans);
	}
}

Measured memory_bytes: 1176
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().split_whitespace().map(|e| e.parse().ok().unwrap()).collect()
}

// fn read<T: std::str::FromStr>() -> T {
//     let mut s = String::new();
//     std::io::stdin().read_line(&mut s).ok();
//     s.trim().parse().ok().unwrap()
// }

fn main(){
    loop {
        let v:Vec<i32> = read_vec();
        if v[0] == -1 && v[1] == -1 && v[2] == -1 {
            break;
        }
        /*
        中間試験、期末試験のいずれかを欠席した場合成績は F。
        中間試験と期末試験の合計点数が 80 以上ならば成績は A 。
        中間試験と期末試験の合計点数が 65 以上 80 未満ならば成績は B。
        中間試験と期末試験の合計点数が 50 以上 65 未満ならば成績は C。
        中間試験と期末試験の合計点数が 30 以上 50 未満ならば成績は D。 ただし、再試験の点数が 50 以上ならば成績は C。
        中間試験と期末試験の合計点数が 30 未満ならば成績は F。
         */

        if v[0] + v[1] < 30 || v[0] == -1 || v[1] == -1 {
            println!("F");
        }else if v[0] + v[1] >= 80 {
            println!("A");
        } else if 65 <= v[0] + v[1] && v[0] + v[1] < 80 {
            println!("B");
        } else if 50 <= v[0] + v[1] && v[0] + v[1] < 65 {
            println!("C");
        } else if 30 <= v[0] + v[1] && v[0] + v[1] < 50 {
            if v[2] >= 50 { println!("C"); } 
            else { println!("D"); }
        }
    }
}


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

int main() {
  int x,y; cin>>x>>y;
  for(int i=0;i<=100;i++){
      for(int j=0;j<=100;j++){
          if(i+j==x && 2*i+4*j==y){cout<<"Yes"; return 0;}
      }
  }
  cout<<"No";
}


Measured memory_bytes: 3640
#include<bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n ; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x) // decltypeで型取得、引数があればva_argsのところに入れる
using namespace std;
typedef long long ll; // long longをllでかけるようにした
const int INF = 1e9;

int main(void){
    int a, b; cin >> a >> b;
    if(a > b){
        REP(i, a) cout << b;
    }
    else REP(i, b) cout << a;
    cout << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <stack>
#include <istream>
#include <map>
#include <set>
#include <queue>
#define rep0(i,n) for (int i = 0; i <= (n); ++i)
#define rep1(i,n) for (int i = 1; i <= (n); ++i)
#define REP(a,b) for (int i = a; i <= (b); ++i) 
#define mREP(a,b) for (int i = a; i == (b); --i)
typedef long long ll;
using namespace std;
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	/*
	　　　　　 　 _... ..._
	　　　　　 , ´⌒ ⌒ヽ
	　　　 　 ﾉｨ（ノﾉ）ﾉ）ﾉ）
	　　　 　 （（q|.ﾟ ヮﾟﾉ|ﾊ
	　　　　　　△ﾑ,8ﾑﾌつ
	　　　　　/(ﾝ_/＿ヽ＼
	　　　　 ～～i_ﾌ_ﾌ～´
	*/
	int n;
	int ans = 0;
	scanf("%d", &n);
	priority_queue<int>A;
	rep0(i, n - 1) {
		int m;
		scanf("%d", &m);
		A.push(m);
	}
	rep0(j, n - 1) {
		if (j % 2 != 0) {
			ans += A.top() * -1;
			A.pop();
		}
		else {
			ans += A.top();
			A.pop();
		}
	}
	printf("%d", ans);
	return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
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 rep2(i,a,b) for(ll i=(a);i<(b);i++)
#define all(a)  (a).begin(),(a).end()
#define pb emplace_back
#define INF (1e9+1)
//#define INF (1LL<<59)

int main(){
    int n,x;
    cin>>n>>x;
    if(n==2&&x==2){
        cout<<"Yes"<<endl<<1<<endl<<2<<endl<<3<<endl;
        return 0;
    }
    if(x==1 || x==2*n-1){
        cout<<"No"<<endl;
        return 0;
    }
    cout<<"Yes"<<endl;
    
    vector<bool> used(2*n,false);
    if(x==2){
        for(int i=x-1;i<=x+2;i++)used[i] = true;
        int p = 1;
        rep(i,n-2){
            while(used[p])p++;
            cout<<p<<endl;
            used[p] = true;
        }
        cout<<x+1<<endl;
        cout<<x<<endl;
        cout<<x-1<<endl;
        cout<<x+2<<endl;
        rep(i,n-3){
            while(used[p])p++;
            cout<<p<<endl;
            used[p] = true;
        }
    }else{
        for(int i=x-2;i<=x+1;i++)used[i] = true;
        int p = 1;
        rep(i,n-2){
            while(used[p])p++;
            cout<<p<<endl;
            used[p] = true;
        }
        cout<<x-1<<endl;
        cout<<x<<endl;
        cout<<x+1<<endl;
        cout<<x-2<<endl;
        rep(i,n-3){
            while(used[p])p++;
            cout<<p<<endl;
            used[p] = true;
        }
    }
}

Measured memory_bytes: 1536
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
    ll n,x;
    cin>>n>>x;
    ll dp[n+1][x+1],v[n+1],w[n+1];
    ll i,j;
    for(i=1;i<=n;i++)
    cin>>w[i]>>v[i];
    
    
    for(i=0;i<=n;i++){
        for(j=0;j<=x;j++)
        {
            if(j==0||i==0)
            dp[i][j]=0;
            
            else{
            if(w[i]<=j){
                dp[i][j]= max(v[i]+dp[i-1][j-w[i]],dp[i-1][j]);
            }
            else
            dp[i][j]=dp[i-1][j];
        }
        }
    }
    cout<<dp[n][x]<<endl;
    
    return 0;
}

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

int main() {
	int N;
	string S;
	cin >> N >> S;
	int change = 0, min = N;
	for (int i = 0;i < N;i ++) if (S[i] == 'E') change ++;
	for (int i = 0;i < N;i ++) {
		if (S[i] == 'W') change ++;
		if (change - 1 < min) min = change - 1;
		if (S[i] == 'E') change --;
	}
	cout << min;
	return 0;
}
/*
31536000のコメント解説欄
ここテンプレで用意してるから、A問題とかだとこの先空欄の危険あり

順に数えていけるだけで解けるよ
*/

Measured memory_bytes: 900
# けつから順に当てはまるものを探していく
s = list(input())
h = list(input())

index = len(s)
omega = []
notFound = False
while index - len(h) >= 0 and not notFound:
    obj = s[index - len(h):index]
    #この文字列がhと生合成を持っているかを確認する
    for i in range(len(obj)):
        if not obj[i] == "?":
            if not obj[i] == h[i]:
                notFound = True

    if not notFound:
        omega.append(s[0:index-len(h)] + h + s[index:])

    notFound = False
    index -= 1

    

if omega == []:
    print('UNRESTORABLE')
else:
    ans = sorted(omega)[0]
    for i in range(len(ans)):
        if ans[i] == "?":
            ans[i] = "a"
    print("".join(ans))

Measured memory_bytes: 3064
#include <iostream>

using namespace std;

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

    int cnt = 0;
    for (int i = 1; i <= n; i += 2) {
        int c = 0;
        for (int j = 1; j <= i; j++) {
            if (i % j == 0) {
                c++;
            }
        }

        if (c == 8) {
            cnt++;
        }
    }

    cout << cnt << endl;
}


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

#define    ll     long long int

int grid[4][4];
int a[4];
int b[4];

int main( )
{
    int i, j, k;
    int p, q, r;
    int x, y, z;
    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            cin >> grid[i][j];
        }
    }
    for (i = 0; i <= 100; i++) {
        for (j = 0; j <= 100; j++) {
            for (k = 0; k <= 100; k++) {
                b[0] = grid[0][0] - i;
                b[1] = grid[1][1] - j;
                b[2] = grid[2][2] - k;
                a[0] = i;
                a[1] = j;
                a[2] = k;
                int fl = 1;
                for (x = 0; fl && x < 3; x++) {
                    for (y = 0; fl && y < 3; y++) {
                        if (a[x] + b[y] != grid[x][y]) {
                            fl = 0;
                            break;
                        }
                    }
                }
                if (fl) {
                    cout << "Yes\n";
                    return 0;
                }
            }
        }
    }
    cout << "No\n";
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <math.h>
#include <map>
#include <vector>

using namespace std;



int main()
{
	double a, b, c, d, e, f;
	double x, y;
	
	while (cin >> a >> b >> c >> d >> e >> f)
	{
		x = (b*f - e*c) / (b*d - a*e);
		y = (a*f - c*d) / (a*e - b*d);
		if (x == 0) x = 0;
		if (y == 0) y = 0;
		printf("%.3f %.3f\n", x, y);
	}
	
	return 0;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  
  int c4 = 0;
  int c2 = 0;
  for (int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if (x % 4 == 0) c4 += 1;
    else if (x % 2 == 0) c2 += 1;
  }
  int L = c4 * 2 + c2;
  if (L >= N || (c2 == 0 && L == N-1 && N % 2 != 0)) cout << "Yes" << endl;
  else cout << "No" << endl;
}

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

int main() {
  int N, K, i, r, a = 0;
  cin >> N >> K;
  
  for (int j = 0;j < N;j++){
    cin >> i;
    r = (K - i) * 2;
    i *= 2;
    
    if (i < r) a += i;
    else a += r;
  }
  cout << a << endl;
}


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

int main() {
    int X, N;
    cin >> X >> N;
    vector<int> vec(N);
    for (int i = 0; i < N; i++) {
      cin >> vec.at(i);
    }

    int index = 0;
    int res;
    while(true) {
        bool minusFlag = true;
        bool plusFlag = true;
        for(int i = 0; i < N; i++) {
            if(vec.at(i) == (X - index)) {
                minusFlag = false;
            }
            if(vec.at(i) == (X + index)) {
                plusFlag = false;
            }
            if(!minusFlag && !plusFlag) {

            }
        }
        if(minusFlag) {
            res = X - index;
            break;
        }
        if(plusFlag) {
            res = X + index;
            break;
        }
        index++;
    }
    cout << res << endl;
}

Measured memory_bytes: 3612
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<long long> vll;
typedef vector<pair<int,int>> vp;
typedef pair<int,int> pii;
#define INF (int)(1e9)
#define MAXX 1.1529215e+18
#define inf 999999
#define EPS (1e-7)
#define rep(i,n) for(int i=0; i<(int)(n);i++)
#define REP(i,n) for(int i=1;i<=(int)(n);i++)
#define FOR(i,k,n) for(int i=(k);i<(int)(n);i++)
#define ALL(a)  a.begin(),a.end()
#define RALL(a) a.begin(),a.end(),greater<int>()
#define ROT(a) a.begin(),a.begin()+1,a.end()
#define RROT(a) a.begin(),a.end()-1,a.end()
#define PB push_back
#define MP make_pair
#define PI acos(-1.0)
#define sz(a) a.size()
const ll MOD = 1e9+7;
const int MAX = 10000000;
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }
//cout << 'H' << endl;
/*--------------------------------------------*/

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

    string s;
    cin >> s;
    ll K;
    cin >> K;

    ll N = sz(s);

    int a;
    
    rep(i,N){
        a = 26 - (s[i] - 'a');
        if(s[i] == 'a')continue;
        if(K >= a){
            K -= a;
            s[i] = 'a';
        }
    }

    if(K){
        s[N-1] = char((s[N-1] - 'a' + K)%26 + 'a');
    }

    cout << s << endl;

}


Measured memory_bytes: 592
#include <iostream>
#define lli long long int

using namespace std;

int main()
{
	lli z, s, t;
	
	while(cin >> z && z){
		t = z*z*z;
		s = 0;
		for(int i = 1; i < z; i++){
			for(int j = 1; j < z; j++){
				if(i*i*i+j*j*j <= t && s < i*i*i+j*j*j){
					s = i*i*i+j*j*j;
				} else if(s > t){
					break;
				}
			}
		}
		
		cout << (t - s) << endl;
	}
	
	return 0;
}

Measured memory_bytes: 860
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <cassert>
#include <random>

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

using namespace std;
typedef long long int ll;
const ll INF = 10000000000;
const double PI = acos(-1);
const ll mod = 1000000007;

bool is_ok(int l1, int l2, int l3) {
    if (l1 == l2 || l2 == l3 || l3 == l1) {
        return false;
    }
    vector<int> l = { l1, l2, l3 };
    sort(l.begin(), l.end());
    if (l[2] >= l[0] + l[1]) {
        return false;
    }
    return true;
}

int main()
{
    int n;
    cin >> n;
    vector<int> l(n);
    rep(i, n) cin >> l[i];

    int ans = 0;
    rep(i, n) {
        for (int j = i + 1; j < n; j++) {
            for (int k = j + 1; k < n; k++) {
                if (is_ok(l[i], l[j], l[k])) ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3640
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <math.h>
#include <complex>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <bitset>
#include <functional>
#include <assert.h>
#define int long
#define ll long long
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define eb emplace_back
#define all(x) x.begin(),x.end()
#define SZ(x) x.size()
#define fi first
#define se second
const int INF = 1e9;
using namespace std;

template<class t> using vc=vector<t>;
template<class t> using vvc=vc<vc<t>>;

using pi=pair<int,int>;
using vi=vc<int>;

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

int dx[]={0,0,1,-1};
int dy[]={1,-1,0,0};

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

    int ans=0;
    int H,W; cin >> H >> W;
    char c[H][W];
    rep(i,H) rep(j,W) cin >> c[i][j];
    rep(i,H){
        rep(j,W){
            if(c[i][j]=='#') continue;
            queue<pi> q;
            q.emplace(i,j);
            int dis[H][W];
            rep(k,H) rep(l,W) dis[k][l]=-1;
            dis[i][j]=0;
            int maxd=0;
            while(!q.empty()){
                auto pos=q.front(); q.pop();
                rep(k,4){
                    if(pos.fi+dx[k]>=0 && pos.fi+dx[k]<H && pos.se+dy[k] >=0 && pos.se+dy[k]<W && c[pos.fi+dx[k]][pos.se+dy[k]]!='#' && dis[pos.fi+dx[k]][pos.se+dy[k]]==-1){
                        q.emplace(pos.fi+dx[k],pos.se+dy[k]);
                        dis[pos.fi+dx[k]][pos.se+dy[k]]=dis[pos.fi][pos.se]+1;
                        chmax(maxd,dis[pos.fi][pos.se]+1);
                    }
                }
            }
            chmax(ans,maxd);
        }
    }
    cout << ans << endl;
}

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

typedef pair<int, int> P;

int main() {
    while(true) {
        int n_ticket, n_city, n_path, from, to;
        cin >> n_ticket >> n_city >> n_path >> from >> to;
        if (n_ticket + n_city + n_path + from + to == 0)
            break;
        from--, to--;

        vector<int> ticket(n_ticket);
        for (int i = 0; i < n_ticket; i++)
            cin >> ticket[i];
        vector<vector<P>> path(n_city);
        for (int i = 0; i < n_path; i++) {
            int x, y, z; cin >> x >> y >> z;
            x--, y--;
            path[x].push_back({y, z});
            path[y].push_back({x, z});
        }

        map<P, float> done;
        queue<P> Q;
        P start = {from, 0};
        Q.push(start);
        done[start] = 0.0;
        for (int i = 0; i < (1 << n_ticket); i++) {
            start = {from, i};
            done[start] = 0.0;
        }

        while (Q.size()) {
            P q = Q.front();
            Q.pop();
            int pos = q.first;
            int ticket_state = q.second;
            float acc_cost = done[q];

            if (pos == to) continue;
            // cout << pos << " " << ticket_state << endl;
            for (auto p : path[pos]) {
                int next_pos = p.first;
                float cost = p.second;
                for (int i = 0; i < n_ticket; i++) {
                    if (ticket_state & (1 << i)) continue;

                    P next_state = {next_pos, (ticket_state | (1 << i))};
                    float next_cost = cost / ticket[i] + acc_cost;
                    if (!done.count(next_state) || done[next_state] > next_cost) {
                        done[next_state] = next_cost;
                        Q.push(next_state);
                        // cout << "\t" << next_pos << " " << i << " : " << done[next_state] << endl;
                    }
                }
            }
        }

        float ans = 1e9;
        bool has_answer = false;
        for (int ticket_state = 0; ticket_state < (1 << n_ticket); ticket_state++) {
            P state = {to, ticket_state};
            if (done.count(state)) {
                has_answer = true;
                ans = min(ans, done[state]);
            }
        }
        if (has_answer)
            printf("%.3lf\n", ans);
        else
            cout << "Impossible" << endl;
    }
}



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

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod= int(1e9)+7;
using P = pair<ll,ll>;
using ld=long double;

int main(){
    map<ll,int>mp;
    ll n;cin >>n;
    for (int i = 0; i < n; ++i) {
        ll a;cin >>a;
        if(mp[a]==1)mp[a]--;
        else mp[a]++;
    }
    ll ans=0;
    for(auto e:mp){
        ans+=e.second;
    }
    cout <<ans <<endl;
    return 0;
}

Measured memory_bytes: 6528
#include <iostream>
#include <cstdio>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <cstring>
#include <functional>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i)
#define rep1(i,n) for(int i=1;i<=(n);++i)
#define all(c) (c).begin(),(c).end()
#define fs first
#define sc second
#define pb push_back
#define show(x) cout << #x << " " << x << endl
typedef pair<int,int> P;
bool dp[1001][1001];
int main(){
	while(true){
		int N;
		cin>>N;
		if(N==0) break;
		vector<P> vc;
		rep(i,N){
			int r,w;
			cin>>r>>w;
			vc.pb(P(r,w));
		}
		sort(all(vc));
		int elsum=0,mx=vc[N-1].fs;
		rep(i,N-1) elsum+=vc[i].fs;
		if(elsum>=mx){
			int ans=0;
			rep(i,N) ans+=vc[i].fs+vc[i].sc;
			cout<<ans<<endl;
		}else{
			int ans=mx*2;
			int left=mx-elsum;
			rep(i,N) rep(j,left+1) dp[i][j]=0;
			dp[0][0]=true;
			rep(i,N-1){
				rep(j,left+1){
					if(!dp[i][j]) continue;
					dp[i+1][j]=1;
					if(j+vc[i].sc<=left) dp[i+1][j+vc[i].sc]=1;
				}
			}
			int can=0;
			rep(i,left+1) if(dp[N-1][i]) can=i;
			int wsum=0;
			rep(i,N) wsum+=vc[i].sc;
			cout<<ans+wsum-can<<endl;
		}
	}
}

Measured memory_bytes: 2192
<?php

fscanf(STDIN, '%d', $count);
$max=1-10**9;
for($i=0;$i<$count;$i++) {
  fscanf(STDIN, '%d',$n);
  if($i===0){
    $min=$n;
  }else {
    if($n-$min>$max){
      $max=$n-$min;
    }
  }
  if($min>$n) {
    $min=$n;
  }
}
echo $max . PHP_EOL;


Measured memory_bytes: 9220
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll nCr(int n, int r){
    if(2 * r > n) r = n - r;
    ll mul(1), div(1);
    for(int i(1);i<=r;++i){
        mul *= n + 1 - i;
        div *= i;
    }
    return mul/div;
}

int main(){
    int N;
    cin >> N;
    vector<pair<int, int>> G;
    ll M;
    if(N % 2 == 0){
        for(int i(1);i<=N/2;++i) G.push_back(make_pair(i, N+1-i));
        M = N/2 * (N/2-1) * 2;
    }else{
        for(int i(1);i<=(N-1)/2;++i){
            G.push_back(make_pair(i, N-i));
        }
        G.push_back(make_pair(0, N));
        if(G.size() >= 1)
        M =  (N+1)/2 * ((N+1)/2 - 1) * 2 - (N - 1);
    }
    cout << M << endl;
    for(int i(0);i<G.size();++i){
        for(int j(i+1);j<G.size();++j){
            if(G[i].first != 0 && G[j].first != 0 ) cout << G[i].first << ' ' << G[j].first << endl;
            if(G[i].first != 0 && G[j].second != 0 ) cout << G[i].first << ' ' << G[j].second << endl;
            if(G[i].second != 0 && G[j].first != 0 ) cout << G[i].second << ' ' << G[j].first << endl;
            if(G[i].second != 0 && G[j].second != 0 ) cout << G[i].second << ' ' << G[j].second << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define int long long
#define uint unsigned int
#define pii pair<int, int>
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORR(i,a,b) for(int i=(a);i>=(b);--i)
#define REP(i,n) FOR(i,0,n)
#define REPR(i,n) FORR(i,n,0)
#define ALL(c) ((c).begin(),(c).end())
#define PB(a) push_back(a)
#define EMPB(...) emplace_back(__VA_ARGS__)
#define EMP(...) emplace(__VA_ARGS__)
#define MP(a,b) make_pair(a,b)
#define MT(...) make_tuple(__VA_ARGS__)
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
#define MINF(a) memset(a,0x3f,sizeof(a))
#define PW(n) (1LL<<(n))
using namespace std;
template <class T> inline bool CHMIN(T& a, T b) { if(a>b) { a=b; return 1; } return 0; }
template <class T> inline bool CHMAX(T& a, T b) { if(a<b) { a=b; return 1; } return 0; }
template <class T> inline void SORT(T& a) { sort(ALL(a)); }
template <class T> inline void REV(T& a) { reverse(ALL(a)); }
template <class T> inline void UNI(T& a) { SORT(a); a.erase(unique(ALL(a)),a.end()); }
template <class S, class T> ostream& operator<<(ostream& os, const pair<S,T> a) {
  return os << a.first << ' ' << a.second;
}
const int MOD = 1000000007;
const int INF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-10;
/* ---------------------------------------------------------------------------------------------------- */

int n,m,t,p;
int pre[50][50];
int nxt[50][50];

void fold(int c, int& n, int& m) {
  if (2*c <= n) {
    for (int i = 0; c+i < n; ++i) {
      REP(j,m) nxt[i][j] = pre[c+i][j];
    }
    for (int i = 0; c-1-i >= 0; ++i) {
      REP(j,m) nxt[i][j] += pre[c-1-i][j];
    }
    n = n-c;
  } else {
    for (int i = 0; c-1-i >= 0; ++i) {
      REP(j,m) nxt[i][j] = pre[c-1-i][j];
    }
    for (int i = 0; c+i < n; ++i) {
      REP(j,m) nxt[i][j] += pre[c+i][j];
    }
    n = c;
  }
}

void rotate(int d) {
  int tmp[50][50];
  if (d > 0) {
    REP(i,m) REP(j,n) tmp[i][j] = pre[n-1-j][i];
    REP(i,m) REP(j,n) pre[i][j] = tmp[i][j];
  } else {
    REP(i,n) REP(j,m) tmp[i][j] = pre[j][n-1-i];
    REP(i,n) REP(j,m) pre[i][j] = tmp[i][j];
  }
}

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  while (cin >> n >> m >> t >> p, n) {
    REP(i,n) REP(j,m) pre[i][j] = 1;
    REP(_,t) {
      ZERO(nxt);
      int d,c;
      cin >> d >> c;
      if (d == 1) {
        fold(c,n,m);
        swap(pre,nxt);
      } else {
        rotate(1);
        fold(c,m,n);
        swap(pre,nxt);
        rotate(-1);
      }
    }

    REP(_,p) {
      int x,y;
      cin >> x >> y;
      cout << pre[x][y] << endl;
    }
  }

  return 0;
}


Measured memory_bytes: 3156
import math
n = int(input())
    
for i in range(n) :
    x1, y1, x2, y2, x3, y3 = map(float, input().split())
    py = ((x3-x1)*(x1**2 + y1**2 - x2**2 - y2**2) - (x2-x1)*(x1**2 + y1**2 - x3**2 - y3**2)) / (2*(x3-x1)*(y1-y2) - 2*(x2-x1)*(y1-y3))
    if x1 == x2 :
        px = (2*(y1-y3)*py - x1**2 - y1**2 + x3**2 + y3**2) / (2*(x3 - x1))
    else :
        px = (2*(y1-y2)*py - x1**2 - y1**2 + x2**2 + y2**2) / (2*(x2 - x1))
    r = math.sqrt((px - x1)**2 + (py -y1)**2)

    print('{:.3f}'.format(px), '{:.3f}'.format(py), '{:.3f}'.format(r))


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

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

dp={}
dp_up={}
V_max=0

for i in range(1,N+1):
    w,v=map(int,input().split())

    V_max+=v

    for j in range(v,V_max+1):
        if j==v:
            if j in dp:
                dp_up[j]=min(dp[j],w)
                j_max=j
            else:
                dp_up[j]=w
                j_max=j
        else:
            if j-v in dp and dp[j-v]+w<=W:
                if j in dp:
                    dp_up[j]=min(dp[j],dp[j-v]+w)
                    j_max=j
                else:
                    dp_up[j]=dp[j-v]+w
                    j_max=j
    dp=dp_up.copy()
print(max(dp))    

Measured memory_bytes: 99832
#include<iostream>
#include<vector>
#include<utility>
#include<map>
#include<deque>
#include<numeric>
#include<algorithm>
#include<unordered_map>
#include<cmath>

using namespace std;
using ll = long long int;
constexpr int INF = 1 << 29;
constexpr ll MOD = ll(1e9 + 7);

ll n, a, b;

int main(void) {
	cin >> n >> a >> b;
	ll ret = n/(a + b)*a + min(a, n % (a + b));
	cout << ret << endl;
	return 0;
}

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

int main() {

    int n;

    cin >> n;

    vector<int> v(n);
    list<int> l;

    for(int i = 0; i < n; ++i) {
        int num;
        cin >> num;
        l.push_front(num);
    }

    vector<int> ans;
    for(int i = 0; i < n; ++i) {
        /*
        for(auto itr = l.begin(); itr != l.end(); itr++)
            cout << *itr << " ";
        cout << endl;
        */
        int count = l.size();
        for(auto itr = l.begin(); itr != l.end(); itr++) {
            if(count == *itr) {
                ans.push_back(count);
                l.erase(itr);
                break;
            }
            count--;
        }

    }


    if(ans.size() == n) {
        for(int i = n - 1; i >= 0; --i)
            cout << ans[i] << endl;

    }
    else {
        cout << -1 << endl;
    }

    return 0;

}

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

using namespace std;

#define INF (1 << 30)
#define EPS 1e-10
#define MOD 1000000007

#define rep(i, n) FOR(i, 0, n)
#define FOR(i, x, n) for (int i = (x); i < (n); ++i)
#define all(v) (v).begin(), (v).end()

using ll = long long;

template<class T> void chmax(T& a, T b){ if (a < b) { a = b; } }
template<class T> void chmin(T& a, T b){ if (a > b) { a = b; } }

int main() {
    char c;
    cin >> c;
    cout << (c >= 'a' ? 'a' : 'A') << endl;

    return 0;
}

Measured memory_bytes: 3596
#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <iomanip>
using namespace std;
typedef pair<int,int> P;
typedef long long ll;


#define rep(i,n) for (int i=0; i<(n); i++)
const ll INF =2*1e9+2;
const ll BIG = 1e9+7;
const double PI = 3.141592653589793;

int main(){
    int h, w, k;
    cin >> h >> w >> k;
    vector<vector<int>> c(h, vector<int>(w));
    vector<vector<int>> c_T(w, vector<int>(h));
    int ans = 0;
    int sum = 0;
    rep(i,h){
        rep(j,w){
            char c_ij;
            cin >> c_ij;
            if(c_ij=='#') {
                c[i][j] = 1;
                c_T[j][i] = 1;
                sum++;
            }
        }
    }
    for(int row = 0; row<(1<<h); row++){
        for(int col = 0; col<(1<<w); col++){
            vector<int> rows;
            for (int i = 0; i < h; ++i) {
                if (row & (1<<i)) { // i が bit に入るかどうか
                    rows.push_back(i);
                }
            }
            vector<int> cols;
            for (int i = 0; i < w; ++i) {
                if (col & (1<<i)) { // i が bit に入るかどうか
                    cols.push_back(i);
                }
            }
            int sub_sum = 0;
            for(int i: rows){
                sub_sum += accumulate(c[i].begin(), c[i].end(), 0);
            }
            for(int j: cols){
                sub_sum += accumulate(c_T[j].begin(), c_T[j].end(), 0);
            }
            for(int i: rows){
                for(int j: cols){
                    sub_sum -= c[i][j];
                }
            }
            if((sum-sub_sum)==k) ans++;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3624
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using vd=vector<double>;
using vvd=vector<vd>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using vs=vector<string>;
#define rep(i,n) range(i,0,n)
#define range(i,a,n) for(int i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define LINF    	((ll)1ll<60)
#define INF     	((int)1<<30)
#define EPS     	(1e-9)
#define MOD     	(1000000007)
#define fcout(a)	cout<<setprecision(a)<<fixed
#define fs 			first
#define sc			second
#define PI			3.141592653589793

template<class S,class T>ostream& operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s; a=0;} return os;}
/*
 * 自明
 */

constexpr ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
constexpr ll lcm(ll a,ll b){return a*b/gcd(a,b);}


int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	
	int n,m;
	cin>>n>>m;
    vector<pii> v(m);
    rep(i,m)cin>>v[i].fs>>v[i].sc;
    sort(all(v),[](pii a, pii b){return a.sc<b.sc;});
    int ans=0, f=0;
    rep(i,m){
        if(v[i].fs<f)continue;
        f=v[i].sc;
        ans++;
    }
    cout<<ans<<endl;
	return 0;
}


Measured memory_bytes: 1152
import java.util.*;
import java.io.*;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		int n = scanner.nextInt();
		int[] A = new int[n];
		
		for (int i = 0; i < n; i ++) {
			A[i] = scanner.nextInt();
		}
		
		scanner.close();
		
		int q = partition(A, 0, n - 1);
		
		for (int i = 0; i < n - 1; i ++) {
			if (i == q) {
				System.out.print("[" + A[i] + "] ");
			} else {
				System.out.print(A[i] + " ");
			}
		}
		if (n - 1 == q) {
			System.out.println("[" + A[n - 1] + "]");
		} else {
			System.out.println(A[n - 1]);
		}
	}
	
	public static int partition (int[] A, int p, int r) {
		int x = A[r];
		int i = p - 1;
		
		for (int j = p; j < r; j++) {
			if (A[j] <= x) {
				i ++;
				int temp = A[i];
				A[i] = A[j];
				A[j] = temp;
			}
		}

		int temp = A[++ i];
		A[i] = A[r];
		A[r] = temp;

		return i;
	}
} 



Measured memory_bytes: 71556
from collections import deque

N = int(input())
Edge = [[]for _ in range(N)]
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    Edge[a].append(b)
    Edge[b].append(a)


def bfs(G, root):
    n = len(G)
    Parents = [-1] * n
    Depth = [0] * n
    dq = deque([root])
    dq2 = deque()
    visited = [0] * n
    while dq:
        u = dq.pop()
        for v in G[u]:
            if visited[v]:
                continue
            else:
                dq2 += [v]
                Depth[v] = Depth[u] + 1
                Parents[v] = u
        dq.extend(dq2)
        dq2 = deque()
        visited[u] = 1
    return Depth, Parents


Depth, Parents = bfs(Edge, 0)
cnt = (Depth[-1] - 1)//2
now = N-1
while cnt:
    cnt -= 1
    now = Parents[now]
Edge[Parents[now]].remove(now)

Q = deque()
Q.append(0)
Done = [0] * N
Done[0] = 1
ans = 1
while Q:
    now = Q.popleft()
    for nex in Edge[now]:
        if Done[nex]:
            continue
        else:
            Q.append(nex)
            Done[nex] = 1
            ans += 1

if ans > N // 2:
    print('Fennec')
else:
    print('Snuke')


Measured memory_bytes: 85020
#include <cstdio>
#include <iostream>
#include <cmath>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <memory.h>
#include <iomanip>
#include <bitset>
#include <list>
#include <stack>
 
using namespace std;
 
#define mod 1000000007
 
int main()
{
	int a, b;
	cin >> a >> b;
	cout << (a + b) % 24 << endl;
}

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

namespace _0151
{
    public class Program

    {

        const int STL = 1;
        public static int[] di = new int[] { 0, -1, -1, -1 };
        public static int[] dj = new int[] { -1, -1, 0, 1 };

        public static void Main(string[] args)
        {
            while (true)
            {
                int n = RInt();
                if (n == 0) break;

                int[,] map = new int[n + STL * 2, n + STL * 2];
                int[,,] dp = new int[n + STL * 2, n + STL * 2, 4];

                for (int i = 0 + STL ; i <= n ; i++)
                {
                    string line = RSt();
                    for (int j = 0 + STL ; j <= n ; j++)
                    {
                        map[i, j] = int.Parse(line[j - 1].ToString());
                    }
                }

                for (int i = 0 + STL ; i <= n ; i++)
                {
                    for (int j = 0 + STL ; j <= n ; j++)
                    {
                        for (int k = 0 ; k < 4 ; k++)
                        {
                            if (map[i, j] == 0) dp[i, j, k] = 0;
                            else dp[i, j, k] = dp[i + di[k], j + dj[k], k] + 1;
                        }
                    }
                }
                Console.WriteLine(dp.Cast<int>().Max());
            }
        }

        static string RSt() { return Console.ReadLine(); }
        static int RInt() { return int.Parse(Console.ReadLine().Trim()); }
        static long RLong() { return long.Parse(Console.ReadLine().Trim()); }
        static double RDouble() { return double.Parse(Console.ReadLine()); }
        static string[] RStAr(char sep = ' ') { return Console.ReadLine().Trim().Split(sep); }
        static int[] RIntAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Trim().Split(sep), e => int.Parse(e)); }
        static long[] RLongAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Trim().Split(sep), e => long.Parse(e)); }
        static double[] RDoubleAr(char sep = ' ') { return Array.ConvertAll(Console.ReadLine().Trim().Split(sep), e => double.Parse(e)); }
        static string WAr<T>(IEnumerable<T> array, string sep = " ") { return string.Join(sep, array.Select(x => x.ToString()).ToArray()); }
    }

}



Measured memory_bytes: 24568
#include<stdio.h>

int main(){
    int n,l;
    scanf("%d %d",&n,&l);
    if(l>=0){
        printf("%d\n",(2*l+n)*(n-1)/2);
    }else if(l+n-1<=0){
        printf("%d\n",(2*l+n-2)*(n-1)/2);
    }else{
        printf("%d\n",(2*l+n-1)*n/2);
    }
    return 0;
}

 

Measured memory_bytes: 128
#include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <deque>
#include <iostream>
#include <limits.h>
#include<functional>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <stack>
#include <deque>
#include <tuple>

#define LL long long
#define pii pair<int,int>
#define pLL pair<LL,LL>
#define mp make_pair
#define mt make_tuple
#define pq priority_queue<LL>
#define pqg priority_queue<int,vector<int>,greater<int>>
#define pb push_back
#define vec vector<int>
#define vecll vector<LL>
#define vecpii vector<pii>
#define vecpLL vector<pLL>
#define vecbL vector<bool>
#define endl "\n"
#define REP(e,v) for(auto e:v)
#define rep(i, a, n) for(LL i = a; i < n;i++)



using namespace std;


struct edge {//枝
    LL to,cost;
} ;


struct vertex{//頂点(枝付き)
	LL key;
    vector<edge> edges;
};

typedef vector<vertex> graph;//グラフ(枝付き頂点の集合として扱う)

edge edge_init(LL t,LL cos){//枝の初期化
	edge e = {.to = t, .cost = cos};

	return e;

}

vertex vertex_init(LL key,vector<edge> E){//頂点の初期化
	vertex v;

	v.key = key;

	rep(i, 0, E.size()){
				v.edges.push_back(E[i]);
	}

	return v;

}

vector<vertex> graph_init(LL V, LL key[],vector<edge> E[]){//グラフの初期化

	graph G;

	rep(i,0,V){
		vertex v = vertex_init(key[i],E[i]);
		G.push_back(v);
	}

	return G;

}

void dijkstra(LL s,LL V,vector<vertex> G, LL d[]) {

    priority_queue<pLL, vector<pLL>, greater<pLL> > que;

    fill(d,d+V,LLONG_MAX);
    d[s] = 0;
    que.push(pLL(0, s));

    while (!que.empty()) {
        pLL p = que.top();
        que.pop();
        LL v = p.second;
        if (d[v] < p.first) continue;

        rep(i,0, G[v].edges.size()) {
            edge e = G[v].edges[i];
            if (d[e.to] > d[v] + e.cost) {
                d[e.to] = d[v] + e.cost;
                que.push(pLL(d[e.to], e.to));
            }
        }
    }

    return;
}



int inp(){
	int x;
	scanf("%d",&x);
	return x;
}
LL linp(){
	LL x;
	scanf("%lld",&x);
	return x;
}

void outp(int i){
	printf("%d",i);
}

void loutp(LL i){
	printf("%lld",i);
}

LL mod(LL i){
	return i % 1000000007;
}


LL gcd(LL i,LL j){
	if(j != 0)
		return gcd(j, i % j);
	else
		return i;

}

LL A(){
	string S;

	cin >> S;

	if(S[2] == S[3] && S[4] == S[5])
		cout << "Yes";
	else
		cout << "No";

	return 0;
}

LL B(){

	LL X;

	cin >> X;

	cout << (X/500) * 1000 + ((X % 500)/5)*5;

	return 0;
}

LL C(){

	return 0;
}

LL D(){
	 LL V, X, Y;

	 cin >> V >> X >> Y;



	 LL key[V] = {0};
	 vector<edge> E[V];


	 rep(i,0,V-1){
		E[i].push_back(edge_init(i+1,1));
		E[i+1].push_back(edge_init(i,1));
	 }

	 E[X-1].push_back(edge_init(Y-1,1));
	 E[Y-1].push_back(edge_init(X-1,1));

	 graph G = graph_init(V,key,E);

	 LL d[V][V];//d[i][j]はiからjへの最短距離が入る
	 LL count[V] = {0};

	 rep(i,0,V){
		 dijkstra(i,V,G,d[i]);

	 }

	 rep(i,0,V){
		 rep(j,i+1,V){
			  count[d[i][j]]++;

		 }
	 }

	 rep(i,1,V){
		 cout << count[i] << endl;
	 }

	return 0;
}

LL E(){

	return 0;
}

signed main() {
	//A();
	B();
	//C();
	//D();
	//E();
	return 0;
}


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

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define erep(i, n) for (ll i = 1; i <= (ll)(n); ++i)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } }

int main() {

    ll N, W;
    cin >> N >> W;
    vector<ll> w(N), v(N);
    rep(i, N) cin >> w[i] >> v[i];

    // dpテーブル
    vector<vector<ll>> dp(110, vector<ll>(201010));

    // dpテーブルはmin関数で作っていくので、INFで初期化しておく
    // なぜかLONG_MAXだと正しく計算されなかったのでINT_MAXで(vの最高はたかだか、10e3なのでok)
    rep(i, N+1) rep(val, N*1010+1) dp[i][val] = INT_MAX;
    // 全てINT_MAXで入っているとテーブルの構築ができないので、最初のとこだけ0初期化しておく
    dp[0][0] = 0;

    rep(i, N) rep(val, N * 1010 + 1) {
        chmin(dp[i + 1][val], dp[i][val]);
        chmin(dp[i + 1][val + v[i]], dp[i][val] + w[i]);
    }

    ll ans = 0;
    rep(val, N * 1010 + 1) if (dp[N][val] <= W) chmax(ans, val);
    cout << ans << endl;


//    rep(i, N) rep(val, 10000) {
//            if(dp[i+1][val] != INT_MAX && dp[i+1][val] != 0) {
//                cout << dp[i+1][val] << " : " << val << endl;
//            }
//    }

    return 0;
}


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

#define rep(i,n) for (int i = 0; i< (n); ++i)
#define vvec(m,n) vector<vector<int>> (int(m), vector<int>(n))
#define ALL(a) (a).begin(), (a).end()

using ll = long long;

int main(){
    int A, B;
    cin >> A >> B;
    cout << A*B - A - B + 1 << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define optimizar_io ios_base::sync_with_stdio(false);cin.tie(0);

bool esPalindromo(string s) {
	for (int i = 0, j = s.size() - 1; i <= j; i++, j--) {
		if (s[i] != s[j])
			return false;
	}
	return true;
}

int main() {
	optimizar_io
	string s;
	int n;

	cin >> s;
	n = s.size();
	int pos1 = ((n - 1) / 2);
	int pos2 = (n + 3) / 2;
	//cout << s.substr(0, n - pos1 - 1) << "\n";
	//cout << s.substr(pos2 - 1, n - pos2 + 1) << "\n";
	if (esPalindromo(s) and esPalindromo(s.substr(0, n - pos1 - 1)) and esPalindromo(s.substr(pos2 - 1, n - pos2 + 1)))
		cout << "Yes\n";
	else
		cout << "No\n"; 
	return 0;
}

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

#define rep(i,s,t) for(int i = s; i <= t; ++ i)
#define reep(i,s,t) for(int i = s; i >= t; -- i)
const int MaxN = 100050;
typedef long long LL;

int n, t, ans;
LL mx;
int w[MaxN], suf[MaxN];

int main()
{
	scanf("%d%d",&n,&t);
	rep(i,1,n) scanf("%d",&w[i]);
	reep(i,n,1) suf[i] = max(suf[i+1],w[i]);
	rep(i,1,n)
		if(suf[i+1] > w[i]) 
			mx = max(mx, (LL)(suf[i+1]-w[i])*(LL)t/2LL);
	rep(i,1,n)
		if((LL)(suf[i+1]-w[i])*(LL)t/2LL == mx)
			++ ans;
	printf("%d",ans);
}

Measured memory_bytes: 1024
#define _USE_MATH_DEFINES
 
#include <bits/stdc++.h> 
 
using namespace std;
 
#define len(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define endl '\n'
#define x first
#define y second	
#define int long long
#define double long double       
#define vec vector            
#define pb push_back
#define pii pair<int, int>
#define pdd pair<double, double> 
#define beg begin
#define print(x) cout << x << endl;
 
#ifdef LOCAL
    #define dbg(x) cerr << #x << " = " << x << endl;
#else 
    #define dbg(x)
#endif 

template<class T> ostream& operator<<(ostream &str, vector<T> &a) {
    for (auto &i : a) {
        str << i << " ";
    }
    return str;
}
 
void solve(); signed main() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin);                                       
    freopen("output.txt", "w", stdout);
#endif
    srand(time(0));
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cout.setf(ios::fixed); cout.precision(2);
    solve();    
    return 0;
}
 
const int INF = 1e18, MAXN = 1e6, MOD = 998244353, BASE = 37;	
int t[4 * MAXN];

void upd(int v, int tl, int tr, int id, int val) {
	if (id < tl || id > tr) {
		return;
	}
	if (tl == tr) {
		t[v] = min(t[v], val);
		return;
	}
	int tm = (tl + tr) / 2;
	upd(v * 2, tl, tm, id, val);
	upd(v * 2 + 1, tm + 1, tr, id, val);
	t[v] = min(t[v * 2], t[v * 2 + 1]);
}

int gt(int v, int tl, int tr, int l, int r) {
	if (tr < l || tl > r) {
		return INF;
	}
	if (l <= tl && tr <= r) {
		return t[v];
	}
	int tm = (tl + tr) / 2;
	int ans1 = gt(v * 2, tl, tm, l, r);
	int ans2 = gt(v * 2 + 1, tm + 1, tr, l, r);
	return min(ans1, ans2);
}

void solve() {
	for (int i = 0; i < 4 * MAXN; ++i) {
		t[i] = INF;
	}

	int n, m;
	cin >> n >> m;
	vec<vec<int>> sg(m, vec<int>(3));
	for (int i = 0; i < m; ++i) {
		cin >> sg[i][1] >> sg[i][0] >> sg[i][2];
	}
	sort(all(sg));

	upd(1, 1, n, 1, 0);

	for (auto it : sg) {
		int l = it[1], r = it[0], c = it[2];
		int cur = gt(1, 1, n, l, r) + c;
		upd(1, 1, n, r, cur);
	}	

	int ans = gt(1, 1, n, n, n);
	if (ans >= INF) {
		print(-1);
	}
	else {
		print(ans);
	}
}

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

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

	int n = 0;
	scanf("%d", &n);
	// printf("%d\n", n);
	vector<vector<int>> S(26, vector<int>(n, 0));
	char c;
	int line = 0;
	scanf("%c", &c);
	string s;
	for (int i = 0; i < n; i++) {
		cin >> s;
		int j = 0;
		while (true) {
			// printf("[%c]", s[j]);
			if (s[j] < 'a' || s[j] > 'z') {
				// printf("\n");
				if (line >= n) {
					goto finish;
				} else {
					break;
				}
			} else {
				// printf("%c", c);
				S[s[j] - 'a'][i]++;
			}
			j++;
		}
	}
finish:
	for (int i = 0; i < 26; i++) {
		// printf("%c %d\n", 'a' + i, *min_element(S[i].begin(), S[i].end()));
		for (int j = 0; j < *min_element(S[i].begin(), S[i].end()); j++) {
			printf("%c", 'a' + i);
		}
	}

	return 0;
}

Measured memory_bytes: 256
x,n = map(int,input().split())
p = list(map(int,input().split()))

l = x
while True:
    if l in p:
        l += 1
    else:
        break

r = x
while True:
    if r in p:
        r -= 1
    else:
        break

if abs(x-l) < abs(x-r):
    print(l)
else:
    print(r)

Measured memory_bytes: 61884
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cassert>

const int maxn = 10005;
const int maxs = 105;

int main()
{
	int H, W;
	int N;
	static int a[maxn];
	static int b[maxs][maxs];
	
	scanf("%d%d", &H, &W);
	scanf("%d", &N);
	for(int i = 1; i <= N; i++) scanf("%d", &a[i]);
	
	int p = 1;
	
	for(int i = 1; i <= H; i++)
	{
		if(i & 1)
		{
			for(int j = 1; j <= W; j++)
			{
				while(a[p] == 0) p++;
				b[i][j] = p, a[p]--;
			}
		}
		else
		{
			for(int j = W; j >= 1; j--)
			{
				while(a[p] == 0) p++;
				b[i][j] = p, a[p]--;
			}
		}
	}
	
	assert(p == N && a[p] == 0);
	
	for(int i = 1; i <= H; i++)
		for(int j = 1; j <= W; j++)
			printf("%d%c", b[i][j], " \n"[j == W]);
	
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
#define P pair<ll, ll>
using namespace std;

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

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

int main(){
  while(true){
    int a, b;
    char op;
    cin >> a >> op >> b;
    if(op == '?') break;
    else if(op == '+') cout << a + b << endl;
    else if(op == '-') cout << a - b << endl;
    else if(op == '*') cout << a * b << endl;
    else if(op == '/') cout << a / b << endl;
  }
}


Measured memory_bytes: 3096
#include <stdio.h>


int main()
{
	int i;
	int n;
	int p[21] = { 0 };
	int count = 0;


	scanf("%d", &n);

	for (i = 1; i <=n; i++)
	{
		scanf("%d", &p[i]);
	}


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

		if (p[i]==0 || p[i + 1]==0 || p[i + 2]==0) {
			break;
		}
		if (p[i] > p[i + 1] && p[i + 1] >p[i + 2]) {
			count++;
		}
		if (p[i] < p[i + 1] && p[i + 1] < p[i + 2])
		{
			count++;
		}
	}


	printf("%d\n", count);

	return 0;


}



Measured memory_bytes: 128