puwaer/puwaer_code_translation huggingface dataset:
AtCoder problem 009

C++ solution:
#include <iostream>

#include <cmath>

#include <vector>

#include <algorithm>

using namespace std;



struct Point {

	double px, py;

};



Point operator-(const Point& a1, const Point& a2) {

	return Point{ a1.px - a2.px, a1.py - a2.py };

}



double getangle(Point G) {

	// 点 G の偏角を求める

	if (G.py >= 0.0) {

		double I = G.px / sqrt(G.px * G.px + G.py * G.py);

		double kaku = acos(I) * 180.0 / 3.14159265358979;

		return kaku;

	}

	else {

		double I = G.px / sqrt(G.px * G.px + G.py * G.py);

		double kaku = acos(I) * 180.0 / 3.14159265358979;

		return 360.0 - kaku;

	}

}



double getangle2(double I1, double I2) {

	// [偏角 I1] - [原点] - [偏角 I2] のなす角度を求める

	// 例えば I1 = 240°、I2 = 30°のとき、求める角度は 150°

	double res = abs(I1 - I2);

	if (res >= 180.0) return 360.0 - res;

	return res;

}



int N;

Point G[2009];



double solve(int pos) {

	// 最初に偏角の昇順にソートする

	vector<double> vec;

	for (int i = 1; i <= N; i++) {

		if (i == pos) continue;

		Point SA = G[i] - G[pos];

		double angle = getangle(SA);

		vec.push_back(angle);

	}

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



	// 点 A を全探索して、最も偏角の大きくなる点 C を二分探索（std::lower_bound）で求める

	double ret = 0.0;

	for (int i = 0; i < vec.size(); i++) {

		double target = vec[i] + 180.0;

		if (target >= 360.0) target -= 360.0;

		int pos1 = lower_bound(vec.begin(), vec.end(), target) - vec.begin();



		// 点 C の候補は高々 2 つに絞れる

		int CandIdx1 = pos1 % vec.size();

		int CandIdx2 = (pos1 + vec.size() - 1) % vec.size();

		double Candidate1 = getangle2(vec[i], vec[CandIdx1]);

		double Candidate2 = getangle2(vec[i], vec[CandIdx2]);

		ret = max({ ret, Candidate1, Candidate2 });

	}

	return ret;

}



// O(N^3) のアルゴリズム（点 A, B, C を全探索）

double solve_Slow() {

	double Answer = 0.0;

	for (int i = 1; i <= N; i++) {

		for (int j = 1; j <= N; j++) {

			for (int k = 1; k <= N; k++) {

				if (i == j || i == k || j == k) continue;

				double I1 = getangle(G[i] - G[j]);

				double I2 = getangle(G[k] - G[j]);

				Answer = max(Answer, getangle2(I1, I2));

			}

		}

	}

	return Answer;

}



// O(N^2 log N) のアルゴリズム

double solve_Fast() {

	// 点 B を全探索

	double Answer = 0.0;

	for (int i = 1; i <= N; i++) {

		double ret = solve(i);

		Answer = max(Answer, ret);

	}

	return Answer;

}



int main() {

	// Step #1. 入力

	cin >> N;

	for (int i = 1; i <= N; i++) {

		cin >> G[i].px >> G[i].py;

	}



	// Step #2. 出力

	double FinalAns = solve_Fast();

	printf("%.12lf\n", FinalAns);

	return 0;

}


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from bisect import bisect_left
from cmath import phase, pi
from typing import List
C = 2 * pi
N = int(input())
P: List[complex] = []
for _ in range(N):
    x, y = map(int, input().split())
    P.append(x + y * 1j)
ans = 0
for i in range(N):
    A = sorted((phase(P[j] - P[i]) + pi) * 360 / C for j in range(N) if i != j)
    now = 0
    for Aj in A:
        k = bisect_left(A, Aj + 180)
        r = A[k % len(A)] - Aj
        if r < 0: r += 360
        if r > 180: r = 360 - r
        l = A[k % len(A) - 1] - Aj
        if l < 0: l += 360
        if l > 180: l = 360 - l
        now = max(now, l, r)
    ans = max(ans, now)
print(ans)
AtCoder problem 009

C++ solution:
#include <iostream>

#include <cmath>

#include <vector>

#include <algorithm>

using namespace std;



struct Point {

	double px, py;

};



Point operator-(const Point& a1, const Point& a2) {

	return Point{ a1.px - a2.px, a1.py - a2.py };

}



double getangle(Point G) {

	// 点 G の偏角を求める

	if (G.py >= 0.0) {

		double I = G.px / sqrt(G.px * G.px + G.py * G.py);

		double kaku = acos(I) * 180.0 / 3.14159265358979;

		return kaku;

	}

	else {

		double I = G.px / sqrt(G.px * G.px + G.py * G.py);

		double kaku = acos(I) * 180.0 / 3.14159265358979;

		return 360.0 - kaku;

	}

}



double getangle2(double I1, double I2) {

	// [偏角 I1] - [原点] - [偏角 I2] のなす角度を求める

	// 例えば I1 = 240°、I2 = 30°のとき、求める角度は 150°

	double res = abs(I1 - I2);

	if (res >= 180.0) return 360.0 - res;

	return res;

}



int N;

Point G[2009];



double solve(int pos) {

	// 最初に偏角の昇順にソートする

	vector<double> vec;

	for (int i = 1; i <= N; i++) {

		if (i == pos) continue;

		Point SA = G[i] - G[pos];

		double angle = getangle(SA);

		vec.push_back(angle);

	}

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



	// 点 A を全探索して、最も偏角の大きくなる点 C を二分探索（std::lower_bound）で求める

	double ret = 0.0;

	for (int i = 0; i < vec.size(); i++) {

		double target = vec[i] + 180.0;

		if (target >= 360.0) target -= 360.0;

		int pos1 = lower_bound(vec.begin(), vec.end(), target) - vec.begin();



		// 点 C の候補は高々 2 つに絞れる

		int CandIdx1 = pos1 % vec.size();

		int CandIdx2 = (pos1 + vec.size() - 1) % vec.size();

		double Candidate1 = getangle2(vec[i], vec[CandIdx1]);

		double Candidate2 = getangle2(vec[i], vec[CandIdx2]);

		ret = max({ ret, Candidate1, Candidate2 });

	}

	return ret;

}



// O(N^3) のアルゴリズム（点 A, B, C を全探索）

double solve_Slow() {

	double Answer = 0.0;

	for (int i = 1; i <= N; i++) {

		for (int j = 1; j <= N; j++) {

			for (int k = 1; k <= N; k++) {

				if (i == j || i == k || j == k) continue;

				double I1 = getangle(G[i] - G[j]);

				double I2 = getangle(G[k] - G[j]);

				Answer = max(Answer, getangle2(I1, I2));

			}

		}

	}

	return Answer;

}



// O(N^2 log N) のアルゴリズム

double solve_Fast() {

	// 点 B を全探索

	double Answer = 0.0;

	for (int i = 1; i <= N; i++) {

		double ret = solve(i);

		Answer = max(Answer, ret);

	}

	return Answer;

}



int main() {

	// Step #1. 入力

	cin >> N;

	for (int i = 1; i <= N; i++) {

		cin >> G[i].px >> G[i].py;

	}



	// Step #2. 出力

	double FinalAns = solve_Fast();

	printf("%.12lf\n", FinalAns);

	return 0;

}






Ruby solution: EPS = 1e-8

n = readline.to_i
z = Array.new(n) do
    x, y = readline.split.map(&:to_i)
    Complex(x, y)
end

ans = 0

for z_i in z
    angles = z.reject { |z_j| z_j == z_i }.map do |z_j|
        (z_j - z_i).angle
    end
    angles.sort!

    max = angles[0]
    max_i = 0
    for min in angles
        while max - min < Math::PI + EPS do
            ans = max - min if ans < max - min
            max_i += 1
            if max_i < angles.size then
                max = angles[max_i]
            else
                max = angles[max_i % angles.size] + 2 * Math::PI
            end
        end
    end
end

puts ans / Math::PI * 180.0
AtCoder problem 009

C++ solution:

Crystal solution: # 0.36.0 以降は Math::TAU があります
TAU = Math::PI * 2
n = read_line.to_i
xy = (1..n).map {
  {Int32, Int32}.from read_line.split.map(&.to_i)
}
puts (0...n).max_of { |i|
  angle = (0...n).select { |j| i != j }.map { |j|
    Math.atan2(xy[j][1] - xy[i][1], xy[j][0] - xy[i][0])
  }
  angles = angle.flat_map { |a| [a, a + Math::PI * 2] }.sort
  angle.max_of { |a|
    index = angles.bsearch_index { |b| b >= a + Math::PI }.not_nil!
    {angles[index - 1], angles[index]}.max_of { |b|
      res = (b - a) % TAU
      res > Math::PI ? Math::PI - res : res
    }
  }
} * 360 / TAU


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from bisect import bisect_left
from cmath import phase, pi
from typing import List
C = 2 * pi
N = int(input())
P: List[complex] = []
for _ in range(N):
    x, y = map(int, input().split())
    P.append(x + y * 1j)
ans = 0
for i in range(N):
    A = sorted((phase(P[j] - P[i]) + pi) * 360 / C for j in range(N) if i != j)
    now = 0
    for Aj in A:
        k = bisect_left(A, Aj + 180)
        r = A[k % len(A)] - Aj
        if r < 0: r += 360
        if r > 180: r = 360 - r
        l = A[k % len(A) - 1] - Aj
        if l < 0: l += 360
        if l > 180: l = 360 - l
        now = max(now, l, r)
    ans = max(ans, now)
print(ans)
AtCoder problem 009

C++ solution:

Crystal solution: # 0.36.0 以降は Math::TAU があります
TAU = Math::PI * 2
n = read_line.to_i
xy = (1..n).map {
  {Int32, Int32}.from read_line.split.map(&.to_i)
}
puts (0...n).max_of { |i|
  angle = (0...n).select { |j| i != j }.map { |j|
    Math.atan2(xy[j][1] - xy[i][1], xy[j][0] - xy[i][0])
  }
  angles = angle.flat_map { |a| [a, a + Math::PI * 2] }.sort
  angle.max_of { |a|
    index = angles.bsearch_index { |b| b >= a + Math::PI }.not_nil!
    {angles[index - 1], angles[index]}.max_of { |b|
      res = (b - a) % TAU
      res > Math::PI ? Math::PI - res : res
    }
  }
} * 360 / TAU






Ruby solution: EPS = 1e-8

n = readline.to_i
z = Array.new(n) do
    x, y = readline.split.map(&:to_i)
    Complex(x, y)
end

ans = 0

for z_i in z
    angles = z.reject { |z_j| z_j == z_i }.map do |z_j|
        (z_j - z_i).angle
    end
    angles.sort!

    max = angles[0]
    max_i = 0
    for min in angles
        while max - min < Math::PI + EPS do
            ans = max - min if ans < max - min
            max_i += 1
            if max_i < angles.size then
                max = angles[max_i]
            else
                max = angles[max_i % angles.size] + 2 * Math::PI
            end
        end
    end
end

puts ans / Math::PI * 180.0
AtCoder problem 009

C++ solution:

Crystal solution: # 0.36.0 以降は Math::TAU があります
TAU = Math::PI * 2
n = read_line.to_i
xy = (1..n).map {
  {Int32, Int32}.from read_line.split.map(&.to_i)
}
puts (0...n).max_of { |i|
  angle = (0...n).select { |j| i != j }.map { |j|
    Math.atan2(xy[j][1] - xy[i][1], xy[j][0] - xy[i][0])
  }
  angles = angle.flat_map { |a| [a, a + Math::PI * 2] }.sort
  angle.max_of { |a|
    index = angles.bsearch_index { |b| b >= a + Math::PI }.not_nil!
    {angles[index - 1], angles[index]}.max_of { |b|
      res = (b - a) % TAU
      res > Math::PI ? Math::PI - res : res
    }
  }
} * 360 / TAU





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var x: []float :: #[n]float
	var y: []float :: #[n]float
	for i(0, n - 1)
		do x[i] :: cui@inputFloat()
		do y[i] :: cui@inputFloat()
	end for
	
	var ans: float :: 0.0
	for i(0, n - 1)
		var rots: []float :: #[n]float
		for j(0, n - 1)
			if(i = j)
				if(i = 0)
					do rots[j] :: lib@invRot(x[j + 1], y[j + 1], x[i], y[i])
				else
					do rots[j] :: lib@invRot(x[j - 1], y[j - 1], x[i], y[i])
				end if
			else
				do rots[j] :: lib@invRot(x[j], y[j], x[i], y[i])
			end if
		end for
		do rots.sort()
		var rots2: []float :: #[2 * n]float
		for j(0, n - 1)
			do rots2[j] :: rots[j]
		end for
		for j(n, 2 * n - 1)
			do rots2[j] :: rots[j - n] + 2.0 * lib@pi
		end for
		for j(0, n - 1)
			var target: float :: rots[j] + lib@pi
			var ok: int :: j
			var ng: int :: j + n
			while((ok - ng).abs() > 1)
				var mid: int :: (ok + ng) / 2
				if(rots2[mid] <= target)
					do ok :: mid
				else
					do ng :: mid
				end if
			end while
			do ans :: [ans, f(rots2[ok] - rots[j])].max()
			do ans :: [ans, f(rots2[ok + 1] - rots[j])].max()
		end for
	end for
	do cui@print("\{lib@toDegree(ans)}\n")
	
	func f(t: float): float
		if(t > lib@pi)
			do t :: 2.0 * lib@pi - t
		end if
		ret t
	end func
end func
AtCoder problem 009

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from bisect import bisect_left
from cmath import phase, pi
from typing import List
C = 2 * pi
N = int(input())
P: List[complex] = []
for _ in range(N):
    x, y = map(int, input().split())
    P.append(x + y * 1j)
ans = 0
for i in range(N):
    A = sorted((phase(P[j] - P[i]) + pi) * 360 / C for j in range(N) if i != j)
    now = 0
    for Aj in A:
        k = bisect_left(A, Aj + 180)
        r = A[k % len(A)] - Aj
        if r < 0: r += 360
        if r > 180: r = 360 - r
        l = A[k % len(A) - 1] - Aj
        if l < 0: l += 360
        if l > 180: l = 360 - l
        now = max(now, l, r)
    ans = max(ans, now)
print(ans)





Ruby solution: EPS = 1e-8

n = readline.to_i
z = Array.new(n) do
    x, y = readline.split.map(&:to_i)
    Complex(x, y)
end

ans = 0

for z_i in z
    angles = z.reject { |z_j| z_j == z_i }.map do |z_j|
        (z_j - z_i).angle
    end
    angles.sort!

    max = angles[0]
    max_i = 0
    for min in angles
        while max - min < Math::PI + EPS do
            ans = max - min if ans < max - min
            max_i += 1
            if max_i < angles.size then
                max = angles[max_i]
            else
                max = angles[max_i % angles.size] + 2 * Math::PI
            end
        end
    end
end

puts ans / Math::PI * 180.0
AtCoder problem 009

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
from bisect import bisect_left
from cmath import phase, pi
from typing import List
C = 2 * pi
N = int(input())
P: List[complex] = []
for _ in range(N):
    x, y = map(int, input().split())
    P.append(x + y * 1j)
ans = 0
for i in range(N):
    A = sorted((phase(P[j] - P[i]) + pi) * 360 / C for j in range(N) if i != j)
    now = 0
    for Aj in A:
        k = bisect_left(A, Aj + 180)
        r = A[k % len(A)] - Aj
        if r < 0: r += 360
        if r > 180: r = 360 - r
        l = A[k % len(A) - 1] - Aj
        if l < 0: l += 360
        if l > 180: l = 360 - l
        now = max(now, l, r)
    ans = max(ans, now)
print(ans)




Kuin solution: func main()
	var n: int :: cui@inputInt()
	var x: []float :: #[n]float
	var y: []float :: #[n]float
	for i(0, n - 1)
		do x[i] :: cui@inputFloat()
		do y[i] :: cui@inputFloat()
	end for
	
	var ans: float :: 0.0
	for i(0, n - 1)
		var rots: []float :: #[n]float
		for j(0, n - 1)
			if(i = j)
				if(i = 0)
					do rots[j] :: lib@invRot(x[j + 1], y[j + 1], x[i], y[i])
				else
					do rots[j] :: lib@invRot(x[j - 1], y[j - 1], x[i], y[i])
				end if
			else
				do rots[j] :: lib@invRot(x[j], y[j], x[i], y[i])
			end if
		end for
		do rots.sort()
		var rots2: []float :: #[2 * n]float
		for j(0, n - 1)
			do rots2[j] :: rots[j]
		end for
		for j(n, 2 * n - 1)
			do rots2[j] :: rots[j - n] + 2.0 * lib@pi
		end for
		for j(0, n - 1)
			var target: float :: rots[j] + lib@pi
			var ok: int :: j
			var ng: int :: j + n
			while((ok - ng).abs() > 1)
				var mid: int :: (ok + ng) / 2
				if(rots2[mid] <= target)
					do ok :: mid
				else
					do ng :: mid
				end if
			end while
			do ans :: [ans, f(rots2[ok] - rots[j])].max()
			do ans :: [ans, f(rots2[ok + 1] - rots[j])].max()
		end for
	end for
	do cui@print("\{lib@toDegree(ans)}\n")
	
	func f(t: float): float
		if(t > lib@pi)
			do t :: 2.0 * lib@pi - t
		end if
		ret t
	end func
end func
AtCoder problem 009

C++ solution:





Kuin solution: func main()
	var n: int :: cui@inputInt()
	var x: []float :: #[n]float
	var y: []float :: #[n]float
	for i(0, n - 1)
		do x[i] :: cui@inputFloat()
		do y[i] :: cui@inputFloat()
	end for
	
	var ans: float :: 0.0
	for i(0, n - 1)
		var rots: []float :: #[n]float
		for j(0, n - 1)
			if(i = j)
				if(i = 0)
					do rots[j] :: lib@invRot(x[j + 1], y[j + 1], x[i], y[i])
				else
					do rots[j] :: lib@invRot(x[j - 1], y[j - 1], x[i], y[i])
				end if
			else
				do rots[j] :: lib@invRot(x[j], y[j], x[i], y[i])
			end if
		end for
		do rots.sort()
		var rots2: []float :: #[2 * n]float
		for j(0, n - 1)
			do rots2[j] :: rots[j]
		end for
		for j(n, 2 * n - 1)
			do rots2[j] :: rots[j - n] + 2.0 * lib@pi
		end for
		for j(0, n - 1)
			var target: float :: rots[j] + lib@pi
			var ok: int :: j
			var ng: int :: j + n
			while((ok - ng).abs() > 1)
				var mid: int :: (ok + ng) / 2
				if(rots2[mid] <= target)
					do ok :: mid
				else
					do ng :: mid
				end if
			end while
			do ans :: [ans, f(rots2[ok] - rots[j])].max()
			do ans :: [ans, f(rots2[ok + 1] - rots[j])].max()
		end for
	end for
	do cui@print("\{lib@toDegree(ans)}\n")
	
	func f(t: float): float
		if(t > lib@pi)
			do t :: 2.0 * lib@pi - t
		end if
		ret t
	end func
end func


Ruby solution: EPS = 1e-8

n = readline.to_i
z = Array.new(n) do
    x, y = readline.split.map(&:to_i)
    Complex(x, y)
end

ans = 0

for z_i in z
    angles = z.reject { |z_j| z_j == z_i }.map do |z_j|
        (z_j - z_i).angle
    end
    angles.sort!

    max = angles[0]
    max_i = 0
    for min in angles
        while max - min < Math::PI + EPS do
            ans = max - min if ans < max - min
            max_i += 1
            if max_i < angles.size then
                max = angles[max_i]
            else
                max = angles[max_i % angles.size] + 2 * Math::PI
            end
        end
    end
end

puts ans / Math::PI * 180.0
AtCoder problem 010

C++ solution:
#include <iostream>

using namespace std;



// 入力

int N;

int C[100009], P[100009];

int Q;

int L[100009], R[100009];



// 累積和

int Sum1[100009];

int Sum2[100009];



int main() {

	// Step #1. 入力

	cin >> N;

	for (int i = 1; i <= N; i++) cin >> C[i] >> P[i];

	cin >> Q;

	for (int i = 1; i <= Q; i++) cin >> L[i] >> R[i];



	// Step #2. 1 組・2 組それぞれの累積和を取る

	for (int i = 1; i <= N; i++) {

		Sum1[i] = Sum1[i - 1];

		Sum2[i] = Sum2[i - 1];

		if (C[i] == 1) Sum1[i] += P[i];

		if (C[i] == 2) Sum2[i] += P[i];

	}



	// Step #3. クエリに答える

	for (int i = 1; i <= Q; i++) {

		int Answer1 = Sum1[R[i]] - Sum1[L[i] - 1];

		int Answer2 = Sum2[R[i]] - Sum2[L[i] - 1];

		cout << Answer1 << " " << Answer2 << endl;

	}

	return 0;

}

Crystal solution: n = read_line.to_i
sums = {0, 1}.map { [0] * n.succ }
n.times do |i|
  c, p = read_line.split.map(&.to_i)
  sums.each { |sum| sum[i + 1] += sum[i] }
  sums[c - 1][i + 1] += p
end
read_line.to_i.times do
  l, r = read_line.split.map(&.to_i)
  puts sums.join(' ') { |sum| sum[r] - sum[l - 1] }
end
AtCoder problem 010

C++ solution:
#include <iostream>

using namespace std;



// 入力

int N;

int C[100009], P[100009];

int Q;

int L[100009], R[100009];



// 累積和

int Sum1[100009];

int Sum2[100009];



int main() {

	// Step #1. 入力

	cin >> N;

	for (int i = 1; i <= N; i++) cin >> C[i] >> P[i];

	cin >> Q;

	for (int i = 1; i <= Q; i++) cin >> L[i] >> R[i];



	// Step #2. 1 組・2 組それぞれの累積和を取る

	for (int i = 1; i <= N; i++) {

		Sum1[i] = Sum1[i - 1];

		Sum2[i] = Sum2[i - 1];

		if (C[i] == 1) Sum1[i] += P[i];

		if (C[i] == 2) Sum2[i] += P[i];

	}



	// Step #3. クエリに答える

	for (int i = 1; i <= Q; i++) {

		int Answer1 = Sum1[R[i]] - Sum1[L[i] - 1];

		int Answer2 = Sum2[R[i]] - Sum2[L[i] - 1];

		cout << Answer1 << " " << Answer2 << endl;

	}

	return 0;

}


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
N = int(input())
S = [[0] * (N + 1) for _ in range(2)]
for i in range(N):
    c, p = map(int, input().split())
    S[c - 1][i + 1] = p
for i in range(N):
    for j in range(2):
        S[j][i + 1] += S[j][i]
Q = int(input())
LR = [tuple(map(int, input().split())) for _ in range(Q)]
for l, r in LR:
    print(S[0][r] - S[0][l - 1], S[1][r] - S[1][l - 1])
AtCoder problem 010

C++ solution:
#include <iostream>

using namespace std;



// 入力

int N;

int C[100009], P[100009];

int Q;

int L[100009], R[100009];



// 累積和

int Sum1[100009];

int Sum2[100009];



int main() {

	// Step #1. 入力

	cin >> N;

	for (int i = 1; i <= N; i++) cin >> C[i] >> P[i];

	cin >> Q;

	for (int i = 1; i <= Q; i++) cin >> L[i] >> R[i];



	// Step #2. 1 組・2 組それぞれの累積和を取る

	for (int i = 1; i <= N; i++) {

		Sum1[i] = Sum1[i - 1];

		Sum2[i] = Sum2[i - 1];

		if (C[i] == 1) Sum1[i] += P[i];

		if (C[i] == 2) Sum2[i] += P[i];

	}



	// Step #3. クエリに答える

	for (int i = 1; i <= Q; i++) {

		int Answer1 = Sum1[R[i]] - Sum1[L[i] - 1];

		int Answer2 = Sum2[R[i]] - Sum2[L[i] - 1];

		cout << Answer1 << " " << Answer2 << endl;

	}

	return 0;

}





Kuin solution: func main()
	var n: int :: cui@inputInt()
	
	var cum1: []int :: #[n + 1]int
	var cum2: []int :: #[n + 1]int
	for i(0, n - 1)
		var c: int :: cui@inputInt()
		var p: int :: cui@inputInt()
		do cum1[i + 1] :: cum1[i]
		do cum2[i + 1] :: cum2[i]
		if(c = 1)
			do cum1[i + 1] :+ p
		else
			do cum2[i + 1] :+ p
		end if
	end for
	
	var q: int :: cui@inputInt()
	for(1, q)
		var l: int :: cui@inputInt() - 1
		var r: int :: cui@inputInt()
		do cui@print("\{cum1[r] - cum1[l]} \{cum2[r] - cum2[l]}\n")
	end for
end func
AtCoder problem 010

C++ solution:

Crystal solution: n = read_line.to_i
sums = {0, 1}.map { [0] * n.succ }
n.times do |i|
  c, p = read_line.split.map(&.to_i)
  sums.each { |sum| sum[i + 1] += sum[i] }
  sums[c - 1][i + 1] += p
end
read_line.to_i.times do
  l, r = read_line.split.map(&.to_i)
  puts sums.join(' ') { |sum| sum[r] - sum[l - 1] }
end


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
N = int(input())
S = [[0] * (N + 1) for _ in range(2)]
for i in range(N):
    c, p = map(int, input().split())
    S[c - 1][i + 1] = p
for i in range(N):
    for j in range(2):
        S[j][i + 1] += S[j][i]
Q = int(input())
LR = [tuple(map(int, input().split())) for _ in range(Q)]
for l, r in LR:
    print(S[0][r] - S[0][l - 1], S[1][r] - S[1][l - 1])
AtCoder problem 010

C++ solution:

Crystal solution: n = read_line.to_i
sums = {0, 1}.map { [0] * n.succ }
n.times do |i|
  c, p = read_line.split.map(&.to_i)
  sums.each { |sum| sum[i + 1] += sum[i] }
  sums[c - 1][i + 1] += p
end
read_line.to_i.times do
  l, r = read_line.split.map(&.to_i)
  puts sums.join(' ') { |sum| sum[r] - sum[l - 1] }
end





Kuin solution: func main()
	var n: int :: cui@inputInt()
	
	var cum1: []int :: #[n + 1]int
	var cum2: []int :: #[n + 1]int
	for i(0, n - 1)
		var c: int :: cui@inputInt()
		var p: int :: cui@inputInt()
		do cum1[i + 1] :: cum1[i]
		do cum2[i + 1] :: cum2[i]
		if(c = 1)
			do cum1[i + 1] :+ p
		else
			do cum2[i + 1] :+ p
		end if
	end for
	
	var q: int :: cui@inputInt()
	for(1, q)
		var l: int :: cui@inputInt() - 1
		var r: int :: cui@inputInt()
		do cui@print("\{cum1[r] - cum1[l]} \{cum2[r] - cum2[l]}\n")
	end for
end func
AtCoder problem 010

C++ solution:


Python solution: # でつoO(YOU PLAY WITH THE CARDS YOU'RE DEALT..)
N = int(input())
S = [[0] * (N + 1) for _ in range(2)]
for i in range(N):
    c, p = map(int, input().split())
    S[c - 1][i + 1] = p
for i in range(N):
    for j in range(2):
        S[j][i + 1] += S[j][i]
Q = int(input())
LR = [tuple(map(int, input().split())) for _ in range(Q)]
for l, r in LR:
    print(S[0][r] - S[0][l - 1], S[1][r] - S[1][l - 1])




Kuin solution: func main()
	var n: int :: cui@inputInt()
	
	var cum1: []int :: #[n + 1]int
	var cum2: []int :: #[n + 1]int
	for i(0, n - 1)
		var c: int :: cui@inputInt()
		var p: int :: cui@inputInt()
		do cum1[i + 1] :: cum1[i]
		do cum2[i + 1] :: cum2[i]
		if(c = 1)
			do cum1[i + 1] :+ p
		else
			do cum2[i + 1] :+ p
		end if
	end for
	
	var q: int :: cui@inputInt()
	for(1, q)
		var l: int :: cui@inputInt() - 1
		var r: int :: cui@inputInt()
		do cui@print("\{cum1[r] - cum1[l]} \{cum2[r] - cum2[l]}\n")
	end for
end func
AtCoder problem 011

C++ solution:
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>
using ll = long long;
using namespace std;
template<class T, class U>
T &chmax(T &a, const U &b){ if(a < b) a = b; return a; }

//43[ms]
int main(){
  int n;
  cin >> n;
  vector<tuple<int,int,int>> v(n);
  for(int i = 0; i < n; i++){
    int d,c,s;
    cin >> d >> c >> s;
    v[i] = {d,c,s};
  }
  sort(v.begin(), v.end());
  vector<ll> cur(5001), next(5001);
  for(int i = 0; i < n; i++){
    int d,c,s;
    tie(d,c,s) = v[i];
    for(int j = 0; j <= 5000; j++){
      next[j] = cur[j];
      if(j > d || j < c) continue;
      chmax(next[j], cur[j - c] + s);
    }
    swap(cur, next);
  }
  ll ans = 0;
  for(int i = 0; i <= 5000; i++) chmax(ans, cur[i]);
  cout << ans << "\n";
}

Crystal solution: n = read_line.to_i
dcs = (1..n).map {
  read_line.split.try { |(d, c, s)| {d.to_i, c.to_i, s.to_i64} }
}.sort
day = dcs.max_of { |(d, c, s)| d }

puts dcs.each_with_object([0i64] * day.succ) { |(d, c, s), dp|
  (c..d).reverse_each do |i|
    dp[i] = {dp[i], dp[i - c] + s}.max
  end
  (0...day).each do |i|
    dp[i + 1] = {dp[i + 1], dp[i]}.max
  end
}.max