exec_outcome
stringclasses 1
value | code_uid
stringlengths 32
32
| file_name
stringclasses 111
values | prob_desc_created_at
stringlengths 10
10
| prob_desc_description
stringlengths 63
3.8k
| prob_desc_memory_limit
stringclasses 18
values | source_code
stringlengths 117
65.5k
| lang_cluster
stringclasses 1
value | prob_desc_sample_inputs
stringlengths 2
802
| prob_desc_time_limit
stringclasses 27
values | prob_desc_sample_outputs
stringlengths 2
796
| prob_desc_notes
stringlengths 4
3k
⌀ | lang
stringclasses 5
values | prob_desc_input_from
stringclasses 3
values | tags
sequencelengths 0
11
| src_uid
stringlengths 32
32
| prob_desc_input_spec
stringlengths 28
2.37k
⌀ | difficulty
int64 -1
3.5k
⌀ | prob_desc_output_spec
stringlengths 17
1.47k
⌀ | prob_desc_output_to
stringclasses 3
values | hidden_unit_tests
stringclasses 1
value |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | 7baf91380ab85a645c05ec9f6e7265cb | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
public class solution {
public static void main (String[] args){
int t=sc.nextInt();
while(t--!=0) {
int n=sc.nextInt();long b=sc.nextLong();long x=sc.nextLong();long y=sc.nextLong();
long a[]=new long[n+1];long sum=0;
if(x>=y) {
for(int i=1;i<n+1;i++) {
if(a[i-1]+x<=b) {
a[i]=a[i-1]+x;sum+=a[i];
}else {
a[i]=a[i-1]-y;sum+=a[i];
}
}
}else {
for(int i=1;i<n+1;i++) {
if(a[i-1]+x<=b) {
a[i]=a[i-1]+x;sum+=a[i];
}else {
a[i]=a[i-1]-y;sum+=a[i];
}
}
}
out.println(sum);
}
////////////////////////////////////////////////////////////////////
out.flush();out.close();
}//*END OF MAIN METHOD*
static final Random random = new Random();
static void sort(int arr[]) {
int n = arr.length;
for(int i = 0; i < n; i++) {
int j = random.nextInt(n),temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long[] readArrayL(int n) {
long a[]=new long[n];
for(int i=0;i<n;i++) a[i]=nextLong();
return a;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));//PRINTLN METHOD
static FastScanner sc = new FastScanner();//FASTSCANNER OBJECT
}//*END OF MAIN CLASS*
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 42b26ca08d815890162fa46298db9caf | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.io.*;
public class B1657 {
public static void main(String[] args) throws IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(r.readLine());
for(int i = 0; i < T; i++) {
StringTokenizer st = new StringTokenizer(r.readLine());
int n=Integer.parseInt(st.nextToken());
int b=Integer.parseInt(st.nextToken());
int x=Integer.parseInt(st.nextToken());
int y=Integer.parseInt(st.nextToken());
long ans=0;
int[] arr = new int[n+1];
for(int j = 1; j < n+1; j++) {
int numX= arr[j-1]+x; int numY=arr[j-1]-y;
if(numX>numY && numX<=b){
arr[j]=numX;
ans+= numX;
} else {
arr[j]=numY;
ans+= numY;
}
}
System.out.println(ans);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 9217a71fa10029b790e117756f9443a5 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args)throws IOException {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t-- > 0) {
int n = in.nextInt(),B = in.nextInt(),x = in.nextInt(),y = in.nextInt();
int a = 0;
long sum = 0;
for (int i = 0; i < n; i++) {
if(a + x <= B) a+=x;
else a-=y;
sum+=a;
}
System.out.println(sum);
}
}
public static void sort(int[] arr){
ArrayList<Integer> ls = new ArrayList<>();
for(int x : arr){
ls.add(x);
}
Collections.sort(ls);
for(int i = 0;i < arr.length;i++){
arr[i] = ls.get(i);
}
}
public static int binSearch(int[] arr,int a){
int l = 0;
int r = arr.length-1;
int mid = 0;
while (l <= r){
if(arr[mid] > a){
r = mid - 1;
}
else{
l = mid + 1;
}
mid = (r+l)/2;
}
return l;
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 19926e3b67a1817f2befd17a3ef67702 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class Solution{
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-->0){
int n = scn.nextInt();
int b = scn.nextInt();
int x = scn.nextInt();
int y = scn.nextInt();
long[] arr = new long[n+1];
arr[0]=0;
long sum=0;
for(int i=1;i<n+1;i++){
if(arr[i-1]+x>b)
arr[i]=arr[i-1]-y;
else
arr[i]=arr[i-1]+x;
sum+=arr[i];
}
System.out.println(sum);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 03e5e0eb02008ca1e30d4d29f479ef31 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
import java.util.StringTokenizer;
/*
cF B
*/
public class Main {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int T=fs.nextInt();
for (int tt=0; tt<T; tt++) {
int n=fs.nextInt();
int B=fs.nextInt();
int x=fs.nextInt();
int y=fs.nextInt();
long sum = 0;
long []a = new long[n+1];
a[0] = 0;
for(int i= 1;i <1+ n; i++){
if(a[i-1] + x <= B){
a[i] = a[i-1] + x;
}else{
a[i] = a[i-1] - y;
}
sum += a[i];
}
System.out.println(sum);
}
}
static final Random random=new Random();
static final int mod=1_000_000_007;
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long add(long a, long b) {
return (a+b)%mod;
}
static long sub(long a, long b) {
return ((a-b)%mod+mod)%mod;
}
static long mul(long a, long b) {
return (a*b)%mod;
}
static long exp(long base, long exp) {
if (exp==0) return 1;
long half=exp(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static long[] factorials=new long[2_000_001];
static long[] invFactorials=new long[2_000_001];
static void precompFacts() {
factorials[0]=invFactorials[0]=1;
for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i);
invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2);
for (int i=invFactorials.length-2; i>=0; i--)
invFactorials[i]=mul(invFactorials[i+1], i+1);
}
static long nCk(int n, int k) {
return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k]));
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | bf28f498515eef784f469a4bb712b4e2 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class GFG {
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
//int n, B, x, y;
int n=sc.nextInt();
int B=sc.nextInt();
int x=sc.nextInt();
int y=sc.nextInt();
//System.out.println("B: "+B);
//scanf("%d %d %d %d", &n, &B, &x, &y);
int a=0;
long sum=0;
int i;
for(i=0;i<n;i++)
{
a=a+x;
//System.out.print(a+" "+i+" ");
if(a<=B)
{ //sum+=a;
//System.out.print("if");
}
else{
//System.out.print("else");
a-=y;
a-=x;
//sum+=a;
}
sum+=a;
//System.out.println(a+" ");
}
System.out.println(sum);
}
//System.out.println("GfG!");
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 99cf66964779aa643a032981c86b08a5 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.Scanner;
public class template
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
int l = sc.nextInt();
long max = sc.nextLong();
long x = sc.nextLong();
long y = sc.nextLong();
output(l, max, x, y);
}
}
private static void output(int l, long max, long x, long y) {
long prev = 0, sum = 0;
for (int i = 0; i < l; i++) {
long cur = prev + x;
if (cur > max) cur = prev - y;
sum += cur;
prev = cur;
}
System.out.println(sum);
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 8fa0a74ad559ce42a4509a1583772bb9 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class Cf {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args ) {
int t = sc.nextInt();
while(t-- > 0)
solve();
}
public static void solve() {
int n = sc.nextInt();
long b = sc.nextInt();
long x = sc.nextInt();
long y = sc.nextInt();
long [] arr = new long [n + 1];
arr[0] = 0;
// 1 + 2 + 3 + 4 + 0 = 10
for (int a = 1; a < arr.length; a++) {
if (arr[a - 1] + x <= b) {
arr[a] = arr[a - 1] + x;
} else {
arr[a] = arr[a - 1] - y;
}
}
long sum = 0;
for (int a = 0; a < arr.length; a++) {
sum += arr[a];
}
System.out.println(sum);
}
}
// 0, 1, 2,
/*
n^2 = a
*/ | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | f395fd4c52aa4594a5a4fb9fccbdbcf0 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class XYsequence {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
int n = in.nextInt();
int b = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
int[] data = new int[n+1];
data[0] = 0;
long answ = 0;
for (int j = 1; j < n+1; j++) {
if (data[j-1] + x <= b) {
data[j] = data[j-1] + x;
} else {
data[j] = data[j-1] - y;
}
}
for (long e: data
) {
answ += e;
}
System.out.println(answ);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 2135d9ad073cf6571580721243f77b7f | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.BigInteger;
public class quetion2template {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
static class FastWriter {
private final BufferedWriter bw;
public FastWriter() {
this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
}
public void print(Object object) throws IOException {
bw.append("" + object);
}
public void println(Object object) throws IOException {
print(object);
bw.append("\n");
}
public void close() throws IOException {
bw.close();
}
}
public static void main(String[] args) {
try {
FastReader in = new FastReader();
FastWriter out = new FastWriter();
// int testCases=in.nextInt();
int testCases = in.nextInt();
while (testCases-- > 0) {
int n=in.nextInt();
long B=in.nextLong();
long x=in.nextLong();
long y=in.nextLong();
long a[]=new long[n+1];
a[0]=0;
long sum=0;
for(int i=1;i<n+1;i++) {
if(a[i-1]+x<=B) {
a[i]=a[i-1]+x;
sum+=a[i];
}
else {
a[i]=a[i-1]-y;
sum+=a[i];
}
}
out.println(sum);
}
out.close();
} catch (Exception e) {
return;
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 21a08cf57bcba6b16c09a0e59d5042d6 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.*;
import java.util.InputMismatchException;
import java.io.IOException;
public class B {
public static void main(String[] args) throws IOException {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int t = in.readInt();
for (int o = 1; o <= t; o++) {
int n = in.readInt();
int b = in.readInt();
int x = in.readInt();
int y = in.readInt();
int sum = 0;
long max_sum = 0;
for (int i = 1; i <= n; i++) {
if (x + sum <= b) {
sum = sum + x;
} else {
sum = sum - y;
}
max_sum += sum;
}
out.printLine(max_sum);
}
out.flush();
out.close();
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c)) c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.readInt();
return array;
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 8c02c65bc2e8662c7964cc3dc02ea8be | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader s = new FastReader();
int t = s.nextInt();
while (t-- > 0) {
long n = s.nextLong();
long B = s.nextLong();
long x = s.nextLong();
long y = s.nextLong();
long current = 0;
ArrayList<Long> result = new ArrayList<>();
result.add(current);
while (n-- > 0) {
if (current + x <= B) {
result.add(current + x);
current = current + x;
} else {
result.add(current - y);
current = current - y;
}
}
long sum = 0;
for (int i = 0; i < result.size(); i++) {
sum += result.get(i);
}
System.out.println(sum);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | b7392eda5494954e37ad883b16856c89 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class XYSequence {
public static void main(String[] args)throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while (t-->0){
String[] inp=br.readLine().split(" ");
int n=Integer.parseInt(inp[0]);
int b=Integer.parseInt(inp[1]);
int x=Integer.parseInt(inp[2]);
int y=Integer.parseInt(inp[3]);
System.out.println(calc(0,0,0,n,b,x,y));
}
}
private static long calc(int i, long sum, int prev, int n, int b, int x, int y) {
if(i==n) return sum;
if(prev+x<=b) return calc(i+1,sum+prev+x,prev+x,n,b,x,y);
else return calc(i+1,sum+prev-y,prev-y,n,b,x,y);
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 9040f6fdda4f0c074e20bff34f3da813 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class XY_Sequence
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int[] n = new int[t]; int[] B = new int[t]; int[] x = new int[t]; int[] y = new int[t]; int[] a = new int[t];
long[] num = new long[t];
for(int i = 0; i < t; i++)
{
n[i] = sc.nextInt();
B[i] = sc.nextInt();
x[i] = sc.nextInt();
y[i] = sc.nextInt();
}
for(int i = 0; i < t; i++)
{
for(int j = 0; j < n[i]; j++)
{
if(a[i]+x[i]<=B[i])
{
a[i] += x[i];
}
else
{
a[i] -= y[i];
}
num[i] += a[i];
}
}
for(int i = 0; i < t; i++)
{
System.out.println(num[i]);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 802e250aae5477dcdba86357ab594b5c | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class Solution {
static Scanner sc = new Scanner(System.in);
public static void solver() {
int n = sc.nextInt(), B = sc.nextInt(), x = sc.nextInt(), y = sc.nextInt();
long sum = 0;
int[] a = new int[n+1];
a[0] = 0;
for(int i = 1; i <= n; i++){
if((x+a[i-1])> B){
a[i] = a[i-1]-y;
}
else
a[i] = a[i-1]+x;
sum += a[i];
}
System.out.println(sum);
}
public static void main(String[] args) {
int t = sc.nextInt();
while (t-- > 0) {
solver();
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 596579b652666fa780af11a1bdc43efa | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class Main {
// static List<List<Integer>> list;
// static int findKMaxElement(int[] arr, int k) {
// PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
// int count = 1;
// int n = arr.length;
// int i = 0;
// for (; i < k; i++) {
// pq.add(arr[i]);
// }
// pq.remove(arr[0]);
// int j = 0;
// for (; i < n; i++) {
// pq.add(arr[i]);
// if(arr[j] > pq.peek()) {
// count++;
// }
// j++;
// pq.remove(arr[i - k + 1]);
// }
// for(; j < n - 1; j++) {
// if(arr[j] > pq.peek()) {
// count++;
// }
// pq.remove(arr[j + 1]);
// }
// return count;
// }
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0) {
int n = sc.nextInt();
int B = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
long sum = 0;
long currElement = 0;
for(int i = 0; i <= n; i++) {
//System.out.print(currElement + " ");
if(currElement + x <= B) {
sum += currElement;
currElement += x;
}else {
sum += currElement;
currElement -= y;
}
}
System.out.println(sum);
}
// int n = sc.nextInt();
// int k = sc.nextInt();
// int[] arr = new int[n];
// for(int i = 0; i < n; i++) {
// arr[i] = sc.nextInt();
// }
// System.out.print(findKMaxElement(arr, k));
// System.out.print(allPathsSourceTarget(new int[][]{{1,2},{3},{3},{}}));
}
// static public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
// list = new ArrayList<>();
// method(graph, 0, graph.length, new ArrayList<>());
// return list;
// }
// static void method(int[][] graph, int s, int e, List<Integer> t) {
// if(s == e) {
// list.add(t);
// }
// t.add(s);
// for(int i = 0; i < graph[s].length; i++) {
// method(graph, graph[s][i], e, t);
// }
// }
}
// class Solution {
// public boolean canJump(int[] nums) {
// int n = nums.length;
// int max = 0;
// for(int i = 0; i < n; i++) {
// if(max >= n - 1) {
// return true;
// }
// if(max < i) {
// return false;
// }
// max = Math.max(max, i + nums[i]);
// }
// return true;
// }
// }
/*
l1 4, 10, 15, 24, 26
l2 0, 9, 12, 20
l3 5, 18, 22, 30
max(4,0,5)=5 (4,9,5)=9 (10,9,5)=10 (10,9,18)=18 (10,12,18)=18 (15,12,18)=18 (15,20,18)=20 (24,20,18)=24 (24,20,22)=24
min(4,0,5)=0 (4,9,5)=4 (10,9,5)=5 (10,9,18)=9 (10,12,18)=10 (15,12,18)=12 (15,20,18)=15 (24,20,18)=18 (24,20,22)=20
after that one arr will be finise
so ans is max = 24 and min = 20
*/
// public class Main
// {
// public static void main(String[] args) {
// System.out.println(Arrays.toString(smallRange(new int[][]{{4,10,15,24,26}, {0,9,12,20}, {5,18,22,30}})));
// }
// static int[] smallRange(int[][] arr) {
// int n = arr.length;
// PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[2] - b[2]);
// // find max element for every array
// int max = Integer.MIN_VALUE;
// for(int i = 0; i < n; i++) {
// pq.offer(new int[]{i, 0, arr[i][0]});
// max = Math.max(max, arr[i][0]);
// }
// int s = -1, e = -1, r = Integer.MAX_VALUE;
// while(pq.size() == n) {
// int[] curr = pq.poll();
// // check max - curr[2](min) < range
// if(max - curr[2] < r) {
// r = max - curr[2];
// s = curr[2];
// e = max;
// }
// // check if we are able to inceament the index of min value row
// if(curr[1] + 1 < arr[curr[0]].length) {
// curr[1] = curr[1] + 1;
// curr[2] = arr[curr[0]][curr[1]];
// pq.offer(curr);
// // check curr value is greater than max or not
// if(curr[2] > max) {
// max = curr[2];
// }
// }
// }
// return new int[]{s, e};
// }
// } | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 51d898abd145c68d606f63a68f14b93c | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
import java.lang.Math;
import java.util.Arrays;
import java.util.Scanner;
public class JavaApplication26 {
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- != 0) {
int n = sc.nextInt();
int B = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
long a = 0;
long ans = 0;
for(int i = 1; i <= n; i++){
if(a + x <= B){
a += x;
}else{
a -= y;
}
ans += a;
}
System.out.println(ans);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | e17944b12ccd59816a4096cc2c5885cd | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | /* Author:Farhan Shaikh */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Pupil
{
static FastReader sc = new FastReader();
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
int t=sc.nextInt();
while(t>0){
long n=sc.nextLong();
long B=sc.nextLong();
long x=sc.nextLong();
long y=sc.nextLong();
long sum=0;
long arr[]=new long[(int)n+1];
for(int i=1;i<n+1;i++)
{
if(sum+x<=B)
{
sum+=x;
arr[i]=sum;
}
else {
sum-=y;
arr[i]=sum;
}
// System.out.println(sum);
}
long sum1=0;
for(int i=0;i<n+1;i++)
{
// System.out.print(arr[i]+" ");
sum1+=arr[i];
}
System.out.println(sum1);
t--;
}
}
// FAST I/O
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static boolean two(int n)//power of two
{
if((n&(n-1))==0)
{
return true;
}
else{
return false;
}
}
public static boolean isPrime(long n){
for (int i = 2; i * i <= n; ++i) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static int digit(int n)
{
int n1=(int)Math.floor((int)Math.log10(n)) + 1;
return n1;
}
public static long gcd(long a,long b) {
if(b==0)
return a;
return gcd(b,a%b);
}
public static long highestPowerof2(long x)
{
// check for the set bits
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
// Then we remove all but the top bit by xor'ing the
// string of 1's with that string of 1's shifted one to
// the left, and we end up with just the one top bit
// followed by 0's.
return x ^ (x >> 1);
}
public static void yes()
{
System.out.println("YES");
return;
}
public static void no()
{
System.out.println("NO");
return ;
}
public static void al(long arr[],int n)
{
for(int i=0;i<n;i++)
{
arr[i]=sc.nextLong();
}
return ;
}
public static void ai(int arr[],int n)
{
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
return ;
}
}
//CAAL THE BELOW FUNCTION IF PARING PRIORITY IS NEEDED //
// PriorityQueue<pair> pq = new PriorityQueue<>(); **********declare the syntax in the main function******
// pq.add(1,2)///////
// class pair implements Comparable<pair> {
// int value, index;
// pair(int v, int i) { index = i; value = v; }
// @Override
// public int compareTo(pair o) { return o.value - value; }
// }
// User defined Pair class
// class Pair {
// int x;
// int y;
// // Constructor
// public Pair(int x, int y)
// {
// this.x = x;
// this.y = y;
// }
// }
// Arrays.sort(arr, new Comparator<Pair>() {
// @Override public int compare(Pair p1, Pair p2)
// {
// return p1.x - p2.x;
// }
// });
// class Pair {
// int height, id;
//
// public Pair(int i, int s) {
// this.height = s;
// this.id = i;
// }
// }
//Arrays.sort(trips, (a, b) -> Integer.compare(a[1], b[1]));
// ArrayList<ArrayList<Integer>> connections = new ArrayList<ArrayList<Integer>>();
// for(int i = 0; i<n;i++)
// connections.add(new ArrayList<Integer>()); | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | d47cc535c3b858842d8066ca86255d91 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main {
public static void swap (int [] arr , int i , int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void main(String[] args) throws IOException {
OutputStreamWriter osr = new OutputStreamWriter(System.out);
PrintWriter o = new PrintWriter(osr);
FastReader fr = new FastReader();
int t = fr.nextInt();
while (t-- != 0)
{
int n = fr.nextInt() , b = fr.nextInt() , x = fr.nextInt() , y = fr.nextInt() , ai = 0;
long s = 0;
for (int i = 0 ; i < n ; i++)
{
if((ai + x ) > b)
ai -= y;
else
ai += x;
s += ai;
}
o.println(s);
}
o.close();
}
}
class FastReader {
// Attributes :
BufferedReader br;
StringTokenizer st;
// Constructor :
public FastReader() {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
}
// Operations :
// #01 :
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
// #02 :
public String nextLine() throws IOException {
return br.readLine();
}
// #03 :
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
// #04 :
public long nextLong() throws IOException {
return Long.parseLong(next());
}
// #05 :
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
// #06 :
public int [] intArray (int size) throws IOException{
int [] arr = new int[size];
for (int i = 0 ; i < size; i++)
arr[i] = nextInt();
return arr;
}
// #07 :
public char [] charArray() throws IOException {
return nextLine().toCharArray();
}
}
class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
static class Compare implements Comparator<Pair> {
@Override
public int compare(Pair o1, Pair o2) {
return (o1.y - o2.y);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 69d30fa9bf3b327ca16e0c2cae269938 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.io.*;
public class Codeforces{
public static void main(String[] args) throws IOException{
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = sc.nextInt();
while (t-- > 0)
{
int n = sc.nextInt() + 1;
int b = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
int[] a = new int[n];
long sum = 0;
for (int i = 1; i < n; i++)
{
if (a[i-1] + x <= b)
a[i] = a[i-1] + x;
else
a[i] = a[i-1] - y;
sum += a[i];
}
pw.println(sum);
}
pw.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 5f8378635ce3a1bd34955af68618ae14 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
new Thread(null, () -> new Main().run(), "1", 1 << 23).start();
}
private void run() {
FastReader scan = new FastReader();
PrintWriter out = new PrintWriter(System.out);
Solution solve = new Solution();
int t = scan.nextInt();
// int t = 1;
for (int qq = 0; qq < t; qq++) {
solve.solve(scan, out);
//out.println();
}
out.close();
}
}
class Solution {
/*
* think and coding
*/ double EPS = 0.000_0001;
public void solve(FastReader scan, PrintWriter out) {
long n = scan.nextInt(), b = scan.nextInt(), x = scan.nextInt(), y = scan.nextInt();
long sum = 0, ans = 0;
for (int i = 0; i < n; i++) {
if (sum + x <= b) {
sum += x;
} else {
sum -= y;
}
ans += sum;
}
out.println(ans);
}
static class Pair implements Comparable<Pair> {
int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public Pair(Pair p) {
this.a = p.a;
this.b = p.b;
}
@Override
public int compareTo(Pair p) {
return Integer.compare(a, p.a);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair pair = (Pair) o;
return a == pair.a && b == pair.b;
}
@Override
public int hashCode() {
return Objects.hash(a, b);
}
@Override
public String toString() {
return "Pair{" + "a=" + a + ", b=" + b + '}';
}
}
}
class FastReader {
private final BufferedReader br;
private StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | de2ce329801990bc0b30109a4e6c0ee1 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0)
{
int n = sc.nextInt();
int B = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
int[]nums = new int[n+1];
nums[0] = 0;
long sum = 0;
for (int i=1; i<n+1; i++)
{
if (nums[i-1] + x <= B)
{
nums[i] = nums[i-1] + x;
}
else if (nums[i-1] + x > B)
{
nums[i] = nums[i-1] - y;
}
sum = sum + (long)nums[i];
}
System.out.println(sum);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 38514f37816dbae898a6c047849fc30e | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.io.*;
import java.util.*;
public final class Main {
//int 2e9 - long 9e18
static PrintWriter out = new PrintWriter(System.out);
static FastReader in = new FastReader();
static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)};
static int mod = (int) (1e9 + 7);
static int mod2 = 998244353;
public static void main(String[] args) {
int tt = i();
while (tt-- > 0) {
solve();
}
out.flush();
}
public static void solve() {
int n = i();
int b = i();
int x = i();
int y = i();
long ans = 0;
long cur = 0;
for (int i = 0; i < n; i++) {
if (cur + x <= b) {
cur += x;
} else {
cur -= y;
}
ans += cur;
}
out.println(ans);
}
// (10,5) = 2 ,(11,5) = 3
static long upperDiv(long a, long b) {
return (a / b) + ((a % b == 0) ? 0 : 1);
}
static long sum(int[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static int[] preint(int[] a) {
int[] pre = new int[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static long[] pre(int[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static long[] post(int[] a) {
long[] post = new long[a.length + 1];
post[0] = 0;
for (int i = 0; i < a.length; i++) {
post[i + 1] = post[i] + a[a.length - 1 - i];
}
return post;
}
static long[] pre(long[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static void print(char A[]) {
for (char c : A) {
out.print(c);
}
out.println();
}
static void print(boolean A[]) {
for (boolean c : A) {
out.print(c + " ");
}
out.println();
}
static void print(int A[]) {
for (int c : A) {
out.print(c + " ");
}
out.println();
}
static void print(long A[]) {
for (long i : A) {
out.print(i + " ");
}
out.println();
}
static void print(List<Integer> A) {
for (int a : A) {
out.print(a + " ");
}
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static String c() {
return in.next();
}
static int[][] inputWithIdx(int N) {
int A[][] = new int[N][2];
for (int i = 0; i < N; i++) {
A[i] = new int[]{i, in.nextInt()};
}
return A;
}
static int[] input(int N) {
int A[] = new int[N];
for (int i = 0; i < N; i++) {
A[i] = in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[] = new long[N];
for (int i = 0; i < A.length; i++) {
A[i] = in.nextLong();
}
return A;
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long GCD(long a, long b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long LCM(int a, int b) {
return (long) a / GCD(a, b) * b;
}
static long LCM(long a, long b) {
return a / GCD(a, b) * b;
}
// find highest i which satisfy a[i]<=x
static int lowerbound(int[] a, int x) {
int l = 0;
int r = a.length - 1;
while (l < r) {
int m = (l + r + 1) / 2;
if (a[m] <= x) {
l = m;
} else {
r = m - 1;
}
}
return l;
}
static void shuffle(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
}
static void shuffleAndSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int[] temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr, comparator);
}
static void shuffleAndSort(long[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
long temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static boolean isPerfectSquare(double number) {
double sqrt = Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static void swap(int A[], int a, int b) {
int t = A[a];
A[a] = A[b];
A[b] = t;
}
static void swap(char A[], int a, int b) {
char t = A[a];
A[a] = A[b];
A[b] = t;
}
static long pow(long a, long b, int mod) {
long pow = 1;
long x = a;
while (b != 0) {
if ((b & 1) != 0) {
pow = (pow * x) % mod;
}
x = (x * x) % mod;
b /= 2;
}
return pow;
}
static long pow(long a, long b) {
long pow = 1;
long x = a;
while (b != 0) {
if ((b & 1) != 0) {
pow *= x;
}
x = x * x;
b /= 2;
}
return pow;
}
static long modInverse(long x, int mod) {
return pow(x, mod - 2, mod);
}
static boolean isPrime(long N) {
if (N <= 1) {
return false;
}
if (N <= 3) {
return true;
}
if (N % 2 == 0 || N % 3 == 0) {
return false;
}
for (int i = 5; i * i <= N; i = i + 6) {
if (N % i == 0 || N % (i + 2) == 0) {
return false;
}
}
return true;
}
public static String reverse(String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
public static void reverse(int[] arr) {
for (int i = 0; i < arr.length / 2; i++) {
int tmp = arr[i];
arr[arr.length - 1 - i] = tmp;
arr[i] = arr[arr.length - 1 - i];
}
}
public static String repeat(char ch, int repeat) {
if (repeat <= 0) {
return "";
}
final char[] buf = new char[repeat];
for (int i = repeat - 1; i >= 0; i--) {
buf[i] = ch;
}
return new String(buf);
}
public static int[] manacher(String s) {
char[] chars = s.toCharArray();
int n = s.length();
int[] d1 = new int[n];
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1);
while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) {
k++;
}
d1[i] = k--;
if (i + k > r) {
l = i - k;
r = i + k;
}
}
return d1;
}
public static int[] kmp(String s) {
int n = s.length();
int[] res = new int[n];
for (int i = 1; i < n; ++i) {
int j = res[i - 1];
while (j > 0 && s.charAt(i) != s.charAt(j)) {
j = res[j - 1];
}
if (s.charAt(i) == s.charAt(j)) {
++j;
}
res[i] = j;
}
return res;
}
}
class Pair {
int i;
int j;
Pair(int i, int j) {
this.i = i;
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pair pair = (Pair) o;
return i == pair.i && j == pair.j;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
class ThreePair {
int i;
int j;
int k;
ThreePair(int i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ThreePair pair = (ThreePair) o;
return i == pair.i && j == pair.j && k == pair.k;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
class Node {
int val;
public Node(int val) {
this.val = val;
}
}
class ST {
int n;
Node[] st;
ST(int n) {
this.n = n;
st = new Node[4 * Integer.highestOneBit(n)];
}
void build(Node[] nodes) {
build(0, 0, n - 1, nodes);
}
private void build(int id, int l, int r, Node[] nodes) {
if (l == r) {
st[id] = nodes[l];
return;
}
int mid = (l + r) >> 1;
build((id << 1) + 1, l, mid, nodes);
build((id << 1) + 2, mid + 1, r, nodes);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
void update(int i, Node node) {
update(0, 0, n - 1, i, node);
}
private void update(int id, int l, int r, int i, Node node) {
if (i < l || r < i) {
return;
}
if (l == r) {
st[id] = node;
return;
}
int mid = (l + r) >> 1;
update((id << 1) + 1, l, mid, i, node);
update((id << 1) + 2, mid + 1, r, i, node);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
Node get(int x, int y) {
return get(0, 0, n - 1, x, y);
}
private Node get(int id, int l, int r, int x, int y) {
if (x > r || y < l) {
return new Node(0);
}
if (x <= l && r <= y) {
return st[id];
}
int mid = (l + r) >> 1;
return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y));
}
Node comb(Node a, Node b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
return new Node(GCD(a.val, b.val));
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 15c122fd19fb3e2cfb39a774339d36dc | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.io.*;
public class MyCpClass{
public static void main(String []args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int T = Integer.parseInt(br.readLine().trim());
while(T-- > 0){
String []ip = br.readLine().trim().split(" ");
int n = Integer.parseInt(ip[0]);
long B = Integer.parseInt(ip[1]);
int x = Integer.parseInt(ip[2]);
int y = Integer.parseInt(ip[3]);
long sum = 0, a = 0;
while(n-- > 0){
if(a+x <= B)
a += x;
else
a -= y;
sum += a;
}
sb.append(sum + "\n");
}
System.out.println(sb);
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | f309daa14073fee21d7c10be41394b18 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.util.*;
import java.io.*;
public class B {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void swap(long[] a, int i, int j) {
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
FastReader t = new FastReader();
PrintWriter o = new PrintWriter(System.out);
int test = t.nextInt();
while (test-- > 0) {
long max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
int n = t.nextInt();
long B = t.nextLong();
long x = t.nextLong();
long y = t.nextLong();
long[] a = new long[n+1];
long pr = 0;
long c = 0;
for (int i = 0; i <= n; ++i) {
a[i] = 0;
if (i == 0)
{
}
else
{
if (a[i-1]+x<=B)
a[i] = a[i-1]+x;
else
a[i] = a[i-1]-y;
}
c += a[i];
}
o.println(c);
}
o.flush();
o.close();
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 4c97794aaabb1798487e69e4cc4ab042 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int b = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
long sum = 0;
for (int i = 0, s = 0; i < n; i++) {
s += x;
if (s > b) {
s -= x;
s -= y;
}
sum += s;
}
System.out.println(sum);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 776ae54840a0a0bb96c8fc27f11bfd81 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.io.*;
public class codeforces1657B {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCases = Integer.parseInt(br.readLine());
for (int rep = 0; rep<numCases;rep++)
{
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int B = Integer.parseInt(st.nextToken());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
long sum = 0;
int holder = 0;
for (int i = 1; i<=n;i++)
{
if (x+holder<=B)
{
holder+=x;
sum+=holder;
}
else
{
holder-=y;
sum+=holder;
}
}
System.out.println(sum);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 8134a5c22924df4936d57cf22b870d0b | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[]data;
int t, n, B, x, y;
t = Integer.valueOf(br.readLine());
while((t--)!=0){
data = br.readLine().split(" ");
n = Integer.valueOf(data[0]);
B = Integer.valueOf(data[1]);
x = Integer.parseInt(data[2]);
y = Integer.parseInt(data[3]);
long sumA = 0;
int a = 0;
for(int i = 1 ; i <= n ; ++i){
if(a + x <= B){
a += x;
}else{
a-=y;
}
sumA +=a;
}
System.out.println(sumA);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | db14551dad53291279239133f1bf3b58 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Main {
static Reader rd = new Reader();
public static void main(String[] args) {
int tt = rd.nextInt();
while (tt-- > 0) {
new Solution().solve();
}
}
static class Solution {
void solve() {
int n = rd.nextInt(), b = rd.nextInt(), x = rd.nextInt(), y = rd.nextInt();
int[] a = new int[n + 1];
a[0] = 0;
long sum = 0;
for (int i = 1; i <= n; i++) {
if (a[i - 1] + x <= b) {
a[i] = a[i - 1] + x;
} else {
a[i] = a[i - 1] - y;
}
sum += a[i];
}
System.out.println(sum);
}
}
static class Reader {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer tokenizer = new StringTokenizer("");
private String innerNextLine() {
try {
return reader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String nextLine = innerNextLine();
if (nextLine == null) {
return false;
}
tokenizer = new StringTokenizer(nextLine);
}
return true;
}
public String nextLine() {
tokenizer = new StringTokenizer("");
return innerNextLine();
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.valueOf(next());
}
public long nextLong() {
return Long.valueOf(next());
}
public double nextDouble() {
return Double.valueOf(next());
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | e516076ffad1b3e4b89c2a088a297be3 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main
{
public static void main(String args[]) throws java.lang.Exception
{
FastScanner input = new FastScanner();
int tc = input.nextInt();
work:
while (tc-- > 0) {
int n = input.nextInt();
long b = input.nextLong();
long x = input.nextLong();
long y = input.nextLong();
long a[] = new long[n];
long ans =0;
long now = 0;
for (int i = 0; i <n; i++) {
if(now+x<=b)
{
now+=x;
}
else
{
now-=y;
}
ans+=now;
}
System.out.println(ans);
}
}
static class FastScanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next()
{
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() throws IOException
{
return br.readLine();
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 070be80514ec4b469b4bcdecabc5ee46 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.io.*;import java.math.*;
public class Main
{
public static void main(String ... args)
{
//import com.sun.java_cup.internal.runtime.Scanner;
Scanner sc=new Scanner(System.in);
int tt=sc.nextInt();
int n,B,x,y;
while((tt--)>0)
{
BigInteger ans=new BigInteger("0");
long an=0;
n=sc.nextInt();B=sc.nextInt();x=sc.nextInt();y=sc.nextInt();
//fscanf(stdin,"%d%d%d%d",&n,&B,&x,&y);
// ans=0;
int []a=new int[n+1];//memset(a,0,sizeof(a));
a[0]=0;
//printf("%d %d %d %d\n",n,B,x,y);
for(int i=1;i<n+1;i++)
{
a[i]+=(((a[i-1]+x)>B)?(a[i-1]-y):(a[i-1]+x));
}
//import java.math.BigInteger;
for(int i=1;i<n+1;i++)
{
//System.out.print(a[i]+" ");
an+=a[i];
//ans.add(new BigInteger(String.valueOf(a[i])));
}
System.out.println(an);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 9fb6227f28b29c370c95f40528352bb8 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.Scanner;
public class B1657 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t=0; t<T; t++) {
int N = in.nextInt();
int B = in.nextInt();
int X = in.nextInt();
int Y = in.nextInt();
int a = 0;
long sum = 0;
for (int n=1; n<=N; n++) {
int nextA = a+X;
if (nextA > B) {
nextA = a-Y;
}
sum += nextA;
a = nextA;
}
System.out.println(sum);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 94f3a90dba5fd2ec2059608392004fa2 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.lang.Math;
import java.util.*;
import java.util.Arrays;
public class B{
public static void main(String[] args){
int t, n, b, x, y;
int [] a;
FastReader in = new FastReader();
t = in.nextInt();
while(t-- > 0){
long sum = 0;
n = in.nextInt();
a = new int[n + 1];
a[0] = 0;
b = in.nextInt();
x = in.nextInt();
y = in.nextInt();
for(int i = 1; i < a.length;i++){
if(a[i - 1] + x <= b){
a[i] = a[i - 1] + x;
}else{
a[i] = a[i - 1] - y;
}
}
for(int j = 0; j < a.length;j++){
sum += a[j];
}
System.out.println(sum);
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 8ba6bbd5762e755f5ccccd0eea191e81 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.*;
import java.util.*;
public class _1657bb {
FastScanner scn;
PrintWriter w;
PrintStream fs;
int MOD = 1000000007;
int MAX = 200005;
long mul(long x, long y) {long res = x * y; return (res >= MOD ? res % MOD : res);}
long power(long x, long y) {if (y < 0) return 1; long res = 1; x %= MOD; while (y!=0) {if ((y & 1)==1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;}
void ruffleSort(int[] a) {int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {int oi=r.nextInt(n), temp=a[i];a[i]=a[oi];a[oi]=temp;}Arrays.sort(a);}
void reverseSort(int[] arr){List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++){list.add(arr[i]);}Collections.sort(list, Collections.reverseOrder());for (int i = 0; i < arr.length; i++){arr[i] = list.get(i);}}
boolean LOCAL;
void debug(Object... o){if(LOCAL)System.err.println(Arrays.deepToString(o));}
//SPEED IS NOT THE CRITERIA, CODE SHOULD BE A NO BRAINER, CMP KILLS, MOCIM
void solve(){
int t=scn.nextInt();
while(t-->0)
{
long n=scn.nextLong(),b=scn.nextLong(),x=scn.nextLong(),y=scn.nextLong();
long prev = 0,sum = 0;
for(int i=0;i<n;i++){
long newprev = prev+x;
if(newprev>b){
newprev = prev-y;
}
sum+=newprev;
prev=newprev;
}
w.println(sum);
}
}
void run() {
try {
long ct = System.currentTimeMillis();
scn = new FastScanner(new File("input.txt"));
w = new PrintWriter(new File("output.txt"));
fs=new PrintStream("error.txt");
System.setErr(fs);
LOCAL=true;
solve();
w.close();
System.err.println(System.currentTimeMillis() - ct);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
scn = new FastScanner(System.in);
w = new PrintWriter(System.out);
LOCAL=false;
solve();
w.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
long[] nextLongArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
int lowerBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid-1 >= 0 && arr[mid-1] == arr[mid]){ei = mid-1;}else{return mid;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; }
int upperBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid+1 < n && arr[mid+1] == arr[mid]){si = mid+1;}else{return mid + 1;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; }
int upperBound(ArrayList<Integer> list, int x){int n = list.size(), si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(list.get(mid) == x){if(mid+1 < n && list.get(mid+1) == list.get(mid)){si = mid+1;}else{return mid + 1;}}else if(list.get(mid) > x){ei = mid - 1; }else{si = mid+1;}}return si; }
void swap(int[] arr, int i, int j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;}
long nextPowerOf2(long v){if (v == 0) return 1;v--;v |= v >> 1;v |= v >> 2;v |= v >> 4;v |= v >> 8;v |= v >> 16;v |= v >> 32;v++;return v;}
int gcd(int a, int b) {if(a == 0){return b;}return gcd(b%a, a);} // TC- O(logmax(a,b))
boolean nextPermutation(int[] arr) {if(arr == null || arr.length <= 1){return false;}int last = arr.length-2;while(last >= 0){if(arr[last] < arr[last+1]){break;}last--;}if (last < 0){return false;}if(last >= 0){int nextGreater = arr.length-1;for(int i=arr.length-1; i>last; i--){if(arr[i] > arr[last]){nextGreater = i;break;}}swap(arr, last, nextGreater);}int i = last + 1, j = arr.length - 1;while(i < j){swap(arr, i++, j--);}return true;}
public static void main(String[] args) {
new _1657bb().runIO();
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | b5e69e8406c2eb7b99fb7f30a773e4c1 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
/* Template created by Jabra Ram - @hack41 */
public class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
int t = Integer.parseInt(s);
while(t-->0){
// int n = Integer.parseInt(br.readLine());
String [] str = br.readLine().split(" ");
// String [] array = br.readLine().split(" ");
int a = Integer.parseInt(str[0]);
int b = Integer.parseInt(str[1]);
int x = Integer.parseInt(str[2]);
int y = Integer.parseInt(str[3]);
// int b = Integer.parseInt(str[2]);
// int [] arr1 = new int[n];
// int [] arr2 = new int[n];
// for(int i=0;i<n;i++){
// arr1[i] = Integer.parseInt(str[i]);
// arr2[i] = Integer.parseInt(array[i]);
// }
long sum=0, total=0;
for(int i=0;i<a;i++){
if(sum+x<=b){
sum += x;
}
else{
sum -= y;
}
total += sum;
}
System.out.println(total);
}
}
public static int gcd(int a, int b){
if(b>a){
return gcd(b,a);
}
if(b==0)
return a;
return gcd(b, a%b);
}
public static long factMod(int n, int mod){
long ans = 1;
for(long i=1;i<=n/2;i++){
ans = ans*i*i%mod;
ans = ans%mod;
}
return ans;
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 3f9390db3ef6a9f3c04c1641b3bcce8e | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import javax.swing.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
import java.util.stream.Stream;
// Please name your class Main
public class Main {
static FastScanner fs=new FastScanner();
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
public String next() {
while (!st.hasMoreElements())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int Int() {
return Integer.parseInt(next());
}
long Long() {
return Long.parseLong(next());
}
String Str(){
return next();
}
}
public static void main (String[] args) throws java.lang.Exception {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//reading /writing file
//Scanner in=new Scanner(System.in);
//Scanner in=new Scanner(new File("input.txt"));
//PrintWriter pr=new PrintWriter("output.txt")
int T=1;
for(int t=0;t<T;t++){
Solution sol1=new Solution(out,fs);
sol1.solution(T,t);
}
out.flush();
}
public static int[] Arr(int n){
int A[]=new int[n];
for(int i=0;i<n;i++)A[i]=Int();
return A;
}
public static int Int(){
return fs.Int();
}
public static long Long(){
return fs.Long();
}
public static String Str(){
return fs.Str();
}
}
class Solution {
PrintWriter out;
int INF = Integer.MAX_VALUE;
int NINF = Integer.MIN_VALUE;
int MOD = 998244353;
int mod = 1000000007;
Main.FastScanner fs;
public Solution(PrintWriter out, Main.FastScanner fs) {
this.out = out;
this.fs = fs;
}
public void add(Map<Integer, Integer> f, int key) {
Integer cnt = f.get(key);
if(cnt == null) {
f.put(key, 1);
} else {
f.put(key, cnt + 1);
}
}
public void del(Map<Integer, Integer> f, int key) {
Integer cnt = f.get(key);
if(cnt == 1) {
f.remove(key);
} else {
f.put(key, cnt - 1);
}
}
public void msg(String s) {
System.out.println(s);
}
public void solution(int all, int testcase) {
int t = 1;
t = fs.Int();
for(int i = 0; i < t; i++) {
solve();
}
}
public void solve() {
int n =fs.Int();
int B = fs.Int();
int x = fs.Int();
int y = fs.Int();
long res = 0;
long cur = 0;
for(int i = 0; i < n; i++) {
if(cur + x <= B) {
cur += x;
} else {
cur -= y;
}
res += cur;
}
out.println(res);
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 58f518f40c31c6d647cf0f8133ad0dbe | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
// int a = 1;
int t;
t = in.nextInt();
//t = 1;
while (t > 0) {
// out.print("Case #"+(a++)+": ");
solver.call(in,out);
t--;
}
out.close();
}
static class TaskA {
public void call(InputReader in, PrintWriter out) {
int n, b, x, y;
n = in.nextInt();
b = in.nextInt();
x = in.nextInt();
y = in.nextInt();
long ans =0, a = 0;
for (int i = 1; i <= n; i++) {
if(a + x <=b){
a+=x;
}
else{
a-=y;
}
ans +=a;
}
out.println(ans);
}
}
static int gcd(int a, int b )
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static class answer implements Comparable<answer>{
int a;
long b, c;
public answer(int a, long b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public answer(int a, long b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
if(this.a == o.a){
return Long.compare(this.b, o.b);
}
return Integer.compare(this.a, o.a);
}
}
static class answer1 implements Comparable<answer1>{
int a, b, c;
public answer1(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public answer1(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer1 o) {
if(this.b == o.b){
return Integer.compare(this.a, o.a);
}
return Integer.compare(this.b, o.b);
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (Long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static final Random random=new Random();
static void shuffleSort(int[] a) {
int n = a.length;
for (int i=0; i<n; i++) {
int oi= random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 523e8f0cfe0c62c7112f16f3f62eac62 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0) {
int n = sc.nextInt();
int B = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
long a=0, sum=0;
for (int i = 0; i < n; i++) {
if(a+x<=B) {
a = a+x;
} else {
a = a-y;
}
sum+=a;
}
System.out.println(sum);
t--;
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | b627ab1061907ba04e8d8fae1472f7d5 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codeforces_1657B
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
int n=sc.nextInt(),B=sc.nextInt(),x=sc.nextInt(),y=sc.nextInt();
long sum=0,temp=0;
long[] arr=new long[n+1];
arr[0]=0;
for(int j=1;j<=n;j++){
if(arr[j-1]+x<=(long)B){
arr[j]=arr[j-1]+x;
}
else if(arr[j-1]-y<=(long)B){
arr[j]=arr[j-1]-y;
}
sum+=arr[j];
}
// for(int j=0;j<=n;j++){
// System.out.println(arr[j]);
// }
System.out.println(sum);
}
// your code goes here
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 92d8add6db93e7d0e1a6a888e3007194 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | // package faltu;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.util.Map.Entry;
public class Main {
public static int upperBound(long[] arr, long m, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(arr[mid]<=m) l=mid+1;
else r=mid-1;
}
return l;
}
public static int lowerBound(long[] a, long m, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(a[mid]<m) l=mid+1;
else r=mid-1;
}
return l;
}
public static long getClosest(long val1, long val2,long target)
{
if (target - val1 >= val2 - target)
return val2;
else
return val1;
}
static void ruffleSort(long[] a) {
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
long oi=r.nextInt(n), temp=a[i];
a[i]=a[(int)oi];
a[(int)oi]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(int[] a){
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n), temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
Arrays.sort(a);
}
int ceilIndex(int input[], int T[], int end, int s){
int start = 0;
int middle;
int len = end;
while(start <= end){
middle = (start + end)/2;
if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){
return middle+1;
}else if(input[T[middle]] < s){
start = middle+1;
}else{
end = middle-1;
}
}
return -1;
}
public static int findIndex(long arr[], long t)
{
if (arr == null) {
return -1;
}
int len = arr.length;
int i = 0;
while (i < len) {
if (arr[i] == t) {
return i;
}
else {
i = i + 1;
}
}
return -1;
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a,long b)
{
return (a / gcd(a, b)) * b;
}
public static int[] swap(int a[], int left, int right)
{
int temp = a[left];
a[left] = a[right];
a[right] = temp;
return a;
}
public static void swap(long x,long max1)
{
long temp=x;
x=max1;
max1=temp;
}
public static int[] reverse(int a[], int left, int right)
{
// Reverse the sub-array
while (left < right) {
int temp = a[left];
a[left++] = a[right];
a[right--] = temp;
}
return a;
}
static int lowerLimitBinarySearch(ArrayList<Integer> A,int B) {
int n =A.size();
int first = 0,second = n;
while(first <second) {
int mid = first + (second-first)/2;
if(A.get(mid) > B) {
second = mid;
}else {
first = mid+1;
}
}
if(first < n && A.get(first) < B) {
first++;
}
return first; //1 index
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// *******----segement tree implement---*****
// -------------START--------------------------
void buildTree (int[] arr,int[] tree,int start,int end,int treeNode)
{
if(start==end)
{
tree[treeNode]=arr[start];
return;
}
buildTree(arr,tree,start,end,2*treeNode);
buildTree(arr,tree,start,end,2*treeNode+1);
tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1];
}
void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value)
{
if(start==end)
{
arr[idx]=value;
tree[treeNode]=value;
return;
}
int mid=(start+end)/2;
if(idx>mid)
{
updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value);
}
else
{
updateTree(arr,tree,start,mid,2*treeNode,idx,value);
}
tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1];
}
// disjoint set implementation --start
static void makeSet(int n)
{
parent=new int[n];
rank=new int[n];
for(int i=0;i<n;i++)
{
parent[i]=i;
rank[i]=0;
}
}
static void union(int u,int v)
{
u=findpar(u);
v=findpar(v);
if(rank[u]<rank[v])parent[u]=v;
else if(rank[v]<rank[u])parent[v]=u;
else
{
parent[v]=u;
rank[u]++;
}
}
private static int findpar(int node)
{
if(node==parent[node])return node;
return parent[node]=findpar(parent[node]);
}
static int parent[];
static int rank[];
// *************end
static void presumbit(int[][]prebitsum) {
for(int i=1;i<=200000;i++) {
int z=i;
int j=0;
while(z>0) {
if((z&1)==1) {
prebitsum[i][j]+=(prebitsum[i-1][j]+1);
}else {
prebitsum[i][j]=prebitsum[i-1][j];
}
z=z>>1;
j++;
}
}
}
public static int[] sort(int[] arr) {
ArrayList<Integer> al = new ArrayList<>();
for(int i=0;i<arr.length;i++) al.add(arr[i]);
Collections.sort(al);
for(int i=0;i<arr.length;i++) arr[i]=al.get(i);
return arr;
}
static ArrayList<String>powof2s;
static void powof2S() {
long i=1;
while(i<(long)2e18) {
powof2s.add(String.valueOf(i));
i*=2;
}
}
static boolean coprime(int a, long l){
return (gcd(a, l) == 1);
}
static Long MOD=(long) (1e9+7);
static int prebitsum[][];
static ArrayList<Integer>arr;
static boolean[] vis;
static ArrayList<ArrayList<Integer>>adj;
public static void main(String[] args) throws IOException
{
// sieve();
// prebitsum=new int[200001][18];
// presumbit(prebitsum);
// powof2S();
FastReader s = new FastReader();
long tt = s.nextLong();
while(tt-->0) {
int n=s.nextInt();
long b=s.nextLong();
long x=s.nextLong();
long y=s.nextLong();
long prev=0,sum=0;
while(n-->0) {
if(prev+x>b) {
prev=prev-y;
sum+=prev;
}
else if(prev+x<=b) {
prev=prev+x;
sum+=prev;
}
}
System.out.println(sum);
}
}
static void DFSUtil(int v, boolean[] visited)
{
visited[v] = true;
Iterator<Integer> it = adj.get(v).iterator();
while (it.hasNext()) {
int n = it.next();
if (!visited[n])
DFSUtil(n, visited);
}
}
static long DFS(int n)
{
boolean[] visited = new boolean[n+1];
long cnt=0;
for (int i = 1; i <= n; i++) {
if (!visited[i]) {
DFSUtil(i, visited);
cnt++;
}
}
return cnt;
}
public static String revStr(String str){
String input = str;
StringBuilder input1 = new StringBuilder();
input1.append(input);
input1.reverse();
return input1.toString();
}
public static String sortString(String inputString){
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static long myPow(long n, long i){
if(i==0) return 1;
if(i%2==0) return (myPow(n,i/2)%MOD * myPow(n,i/2)%MOD)%MOD;
return (n%MOD* myPow(n,i-i)%MOD)%MOD;
}
static void palindromeSubStrs(String str) {
HashSet<String>set=new HashSet<>();
char[]a =str.toCharArray();
int n=str.length();
int[][]dp=new int[n][n];
for(int g=0;g<n;g++){
for(int i=0,j=g;j<n;j++,i++){
if(!set.contains(str.substring(i,i+1))&&g==0) {
dp[i][j]=1;
set.add(str.substring(i,i+1));
}
else {
if(!set.contains(str.substring(i,j+1))&&isPalindrome(str,i,j)) {
dp[i][j]=1;
set.add(str.substring(i,j+1));
}
}
}
}
int ans=0;
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
System.out.print(dp[i][j]+" ");
if(dp[i][j]==1)ans++;
}
System.out.println();
}
System.out.println(ans);
}
static boolean isPalindrome(String str,int i,int j)
{
while (i < j) {
if (str.charAt(i) != str.charAt(j))
return false;
i++;
j--;
}
return true;
}
static boolean sign(long num) {
return num>0;
}
static boolean isSquare(long x){
if(x==1)return true;
long y=(long) Math.sqrt(x);
return y*y==x;
}
static long power1(long a,long b) {
if(b == 0){
return 1;
}
long ans = power(a,b/2);
ans *= ans;
if(b % 2!=0){
ans *= a;
}
return ans;
}
static void swap(StringBuilder sb,int l,int r)
{
char temp = sb.charAt(l);
sb.setCharAt(l,sb.charAt(r));
sb.setCharAt(r,temp);
}
// function to reverse the string between index l and r
static void reverse(StringBuilder sb,int l,int r)
{
while(l < r)
{
swap(sb,l,r);
l++;
r--;
}
}
// function to search a character lying between index l and r
// which is closest greater (just greater) than val
// and return it's index
static int binarySearch(StringBuilder sb,int l,int r,char val)
{
int index = -1;
while (l <= r)
{
int mid = (l+r)/2;
if (sb.charAt(mid) <= val)
{
r = mid - 1;
}
else
{
l = mid + 1;
if (index == -1 || sb.charAt(index) >= sb.charAt(mid))
index = mid;
}
}
return index;
}
// this function generates next permutation (if there exists any such permutation) from the given string
// and returns True
// Else returns false
static boolean nextPermutation(StringBuilder sb)
{
int len = sb.length();
int i = len-2;
while (i >= 0 && sb.charAt(i) >= sb.charAt(i+1))
i--;
if (i < 0)
return false;
else
{
int index = binarySearch(sb,i+1,len-1,sb.charAt(i));
swap(sb,i,index);
reverse(sb,i+1,len-1);
return true;
}
}
private static int lps(int m ,int n,String s1,String s2,int[][]mat)
{
for(int i=1;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
if(s1.charAt(i-1)==s2.charAt(j-1))mat[i][j]=1+mat[i-1][j-1];
else mat[i][j]=Math.max(mat[i-1][j],mat[i][j-1]);
}
}
return mat[m][n];
}
static int lcs(String X, String Y, int m, int n)
{
int[][] L = new int[m+1][n+1];
// Following steps build L[m+1][n+1] in bottom up fashion. Note
// that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1]
for (int i=0; i<=m; i++)
{
for (int j=0; j<=n; j++)
{
if (i == 0 || j == 0)
L[i][j] = 0;
else if (X.charAt(i-1) == Y.charAt(j-1))
L[i][j] = L[i-1][j-1] + 1;
else
L[i][j] = Math.max(L[i-1][j], L[i][j-1]);
}
}
return L[m][n];
// Following code is used to print LCS
// int index = L[m][n];
// int temp = index;
//
// // Create a character array to store the lcs string
// char[] lcs = new char[index+1];
// lcs[index] = '\u0000'; // Set the terminating character
//
// // Start from the right-most-bottom-most corner and
// // one by one store characters in lcs[]
// int i = m;
// int j = n;
// while (i > 0 && j > 0)
// {
// // If current character in X[] and Y are same, then
// // current character is part of LCS
// if (X.charAt(i-1) == Y.charAt(j-1))
// {
// // Put current character in result
// lcs[index-1] = X.charAt(i-1);
//
// // reduce values of i, j and index
// i--;
// j--;
// index--;
// }
//
// // If not same, then find the larger of two and
// // go in the direction of larger value
// else if (L[i-1][j] > L[i][j-1])
// i--;
// else
// j--;
// }
// return String.valueOf(lcs);
// Print the lcs
// System.out.print("LCS of "+X+" and "+Y+" is ");
// for(int k=0;k<=temp;k++)
// System.out.print(lcs[k]);
}
static long lis(long[] aa2, int n)
{
long lis[] = new long[n];
int i, j;
long max = 0;
for (i = 0; i < n; i++)
lis[i] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (aa2[i] >= aa2[j] && lis[i] <= lis[j] + 1)
lis[i] = lis[j] + 1;
for (i = 0; i < n; i++)
if (max < lis[i])
max = lis[i];
return max;
}
static boolean isPalindrome(String str)
{
int i = 0, j = str.length() - 1;
while (i < j) {
if (str.charAt(i) != str.charAt(j))
return false;
i++;
j--;
}
return true;
}
static boolean issafe(int i, int j, int r,int c, char ch)
{
if (i < 0 || j < 0 || i >= r || j >= c|| ch!= '1')return false;
else return true;
}
static long power(long a, long b)
{
a %=MOD;
long out = 1;
while (b > 0) {
if((b&1)!=0)out = out * a % MOD;
a = a * a % MOD;
b >>= 1;
a*=a;
}
return out;
}
static long[] sieve;
public static void sieve()
{
int nnn=(int) 1e6+1;
long nn=(int) 1e6;
sieve=new long[(int) nnn];
int[] freq=new int[(int) nnn];
sieve[0]=0;
sieve[1]=1;
for(int i=2;i<=nn;i++)
{
sieve[i]=1;
freq[i]=1;
}
for(int i=2;i*i<=nn;i++)
{
if(sieve[i]==1)
{
for(int j=i*i;j<=nn;j+=i)
{
if(sieve[j]==1)
{
sieve[j]=0;
}
}
}
}
}
}
class decrease implements Comparator<Long> {
// Used for sorting in ascending order of
// roll number
public int compare(long a, long b)
{
return (int) (b - a);
}
@Override
public int compare(Long o1, Long o2) {
// TODO Auto-generated method stub
return (int) (o2-o1);
}
}
class pair{
long x;
long y;
long c;
char ch;
public pair(long x,long y) {
this.x=x;
this.y=y;
}
public pair(long x,char ch) {
this.x=x;
this.ch=ch;
}
public pair(long x,long y,long c)
{
this.x=x;
this.y=y;
this.c=c;
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | c8a3ae6d91aa2f167b1fea1542aa6e07 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(){
try {br = new BufferedReader(
new FileReader("input.txt"));
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);}
catch(Exception e){ br = new BufferedReader(new InputStreamReader(System.in));
}
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {st = new StringTokenizer(br.readLine());}
catch (IOException e) {
e.printStackTrace();}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble() {return Double.parseDouble(next()); }
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
long b,x,y;
int n;
int num =sc.nextInt();
for(int i =0;i<num;i++){
n=sc.nextInt();
b=sc.nextLong();
x=sc.nextLong();
y=sc.nextLong();
solve(n,b,x,y);
}
}
static void solve(int n,long b,long x,long y){
Long a1,a2;
long prev=0;
long sum=0;
for(int i=1;i<=n;i++){
a1=prev+x;
a2=prev-y;
if(a1>a2 && a1<=b){
sum+=a1;
prev=a1;
}
else if(a2<=b){
sum+=a2;
prev=a2;
}
}
System.out.println(sum);
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | cea7610482d3dcfb346209bbb83ff050 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
public class XYSequence {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t= sc.nextInt();
while (t-->0) {
int n = sc.nextInt();
long b = sc.nextLong();
long x = sc.nextLong();
long y = sc.nextLong();
long a[]=new long[n+1];
Arrays.fill(a,0);
a[0]=0;
for (int i = 1; i <n+1 ; i++) {
long r=a[i-1]+x;
if(r<=b){
a[i]=r;
r=0;
}
else {
a[i]=a[i-1]-y;
r=0;
}
}
long sum=0;
for (int i = 0; i < n+1; i++) {
// System.out.print(a[i]+" ");
sum+=a[i];
}
System.out.println(sum);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 179c05cb4d1110120f14006e50d2958b | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt();
in.nextLine();
while (t-- > 0) {
long n = in.nextLong();
long B = in.nextLong();
long x = in.nextLong();
long y = in.nextLong();
in.nextLine();
long total = 0, current = 0;
for (int i = 0; i < n; i++) {
if (current + x <= B) {
current += x;
} else {
current -= y;
}
total += current;
}
out.println(total);
}
out.close();
in.close();
}
private static Integer[] readArray(int n, Scanner in) {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++) a[i] = in.nextInt();
in.nextLine();
return a;
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 5382103c46ed943a796d5355463624f9 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.util.Scanner;
public class XY_Sequence {
public static long sum(int n,int B,int x,int y){
long s = 0;
long arr[] = new long[n];
for (int i = 0; i < n; i++) {
if(s + x <= B){
s += x;
}
else {
s -= y;
}
arr[i] = s;
}
long c = 0;
for (int i = 0; i < arr.length; i++) {
c += arr[i];
}
return c;
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for (int i = 0; i < t; i++) {
int n = input.nextInt();
int B = input.nextInt();
int x = input.nextInt();
int y = input.nextInt();
System.out.println(sum(n,B,x,y));
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 2bdc149b965b9334303e1352e906f836 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.TreeSet;
import java.util.TreeMap;
import java.util.PriorityQueue;
import java.util.Collections;
import java.util.Stack;
import java.math.BigInteger;
import java.util.LinkedList;
import java.util.Iterator;
public class First {
public static void main(String[] args) {
FastScanner fs = new FastScanner();
int T = fs.nextInt();
for (int tt = 0; tt < T; tt++) {
solve(fs);
}
}
static void solve(FastScanner fs)
{
int n=fs.nextInt(), B=fs.nextInt(), x=fs.nextInt(), y=fs.nextInt();
long[] arr=new long[n+1];
arr[0]=0;
long sum=0;
for(int i=1;i<=n;++i)
{
if(arr[i-1]+x <= (long)B)
{
arr[i]=arr[i-1]+x;
}
else
{
arr[i]=arr[i-1]-y;
}
sum+=arr[i];
}
pn(sum);
}
static long MOD=(long)(1e9+7);
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static void pn(Object o) { System.out.println(o); }
static void p(Object o) { System.out.print(o); }
static void flush() { System.out.flush(); }
static void debugInt(int[] arr)
{
for(int i=0;i<arr.length;++i)
System.out.print(arr[i]+" ");
System.out.println();
}
static void debugIntInt(int[][] arr)
{
for(int i=0;i<arr.length;++i)
{
for(int j=0;j<arr[0].length;++j)
System.out.print(arr[i][j]+" ");
System.out.println();
}
System.out.println();
}
static void debugLong(long[] arr)
{
for(int i=0;i<arr.length;++i)
System.out.print(arr[i]+" ");
System.out.println();
}
static void debugLongLong(long[][] arr)
{
for(int i=0;i<arr.length;++i)
{
for(int j=0;j<arr[0].length;++j)
System.out.print(arr[i][j]+" ");
System.out.println();
}
System.out.println();
}
static long[] takeLong(int n, FastScanner fs)
{
long[] arr=new long[n];
for(int i=0;i<n;++i)
arr[i]=fs.nextLong();
return arr;
}
static long[][] takeLongLong(int m, int n, FastScanner fs)
{
long[][] arr=new long[m][n];
for(int i=0;i<m;++i)
for(int j=0;j<n;++j)
arr[i][j]=fs.nextLong();
return arr;
}
static int[] takeInt(int n, FastScanner fs)
{
int[] arr=new int[n];
for(int i=0;i<n;++i)
arr[i]=fs.nextInt();
return arr;
}
static int[][] takeIntInt(int m, int n, FastScanner fs)
{
int[][] arr=new int[m][n];
for(int i=0;i<m;++i)
for(int j=0;j<n;++j)
arr[i][j]=fs.nextInt();
return arr;
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static int getDecimalFromBinary(String s)
{
long no=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='1')
no++;
no=no<<1;
}
return (int)(no>>1);
}
static String getBinaryFromDecimal(int x)
{
StringBuilder sb=new StringBuilder();
for(int i=0;i<31;++i)
{
if((x&1)==0)
sb.append('0');
else
sb.append('1');
x=x>>1;
}
return sb.reverse().toString();
}
static boolean[] sieve()
{
int size=(int)(1e5+5);
boolean[] isPrime=new boolean[size];
Arrays.fill(isPrime, true);
for(int i=2;i<size;++i)
for(int j=2*i;isPrime[i]==true && j<size;j+=i)
isPrime[j]=false;
return isPrime;
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
class Pair
{
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 3ddf20d8bcf69bbc536e85885edb79d8 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.util.Scanner;
public class codevita {
public static long solve(int n,int B,int x,int y) {
long sum=0;
int arr[]= new int[n+1];
for(int i=1; i<=n; i++) {
if(arr[i-1]+x<=B) {
arr[i]=arr[i-1]+x;
}else {
arr[i]=arr[i-1]-y;
}
}
for(int i=0; i<=n; i++) {
// System.out.println(arr[i]);
sum+=arr[i];
}
return sum;
}
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
int t=s.nextInt();
while(t-->0) {
int n= s.nextInt();
int B= s.nextInt();
int x= s.nextInt();
int y= s.nextInt();
long a=solve(n,B,x,y);
System.out.println(a);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | da3d159e855cd8bcd7f1c550a72992ff | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class Main {
private static int T;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
T = in.nextInt();
while(T-- > 0) {
int n = in.nextInt();
int b = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
long sum = 0;
long ans = 0;
for(int i = 1; i <= n; i++) {
if(sum + x > b) {
sum -= y;
ans += sum;
} else {
sum += x;
ans += sum;
}
}
System.out.println(ans);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | d222253d1cdbdba11b62eb1aaaf75267 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int t = scanner.nextInt();
for (int j = 0; j <t ; j++) {
Integer n=scanner.nextInt();
Integer b=scanner.nextInt();
Integer x=scanner.nextInt();
Integer y=scanner.nextInt();
Long sum=0l;
Integer prev = 0;
for (int i = 1; i <= n; i++) {
if (prev+x<=b){
prev=prev+x;
sum+=prev;
}else {
prev=prev-y;
sum+=prev;
}
}
System.out.println(sum);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | da8447b00b654104b62adf35f6958fbc | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.lang.Math;
public class test {
public static void main(String[] args) {
Scanner inputul = new Scanner(System.in);
int numbers = inputul.nextInt();
for (int i = 0; i < numbers; i++) {
int n,B,x,y;
n = inputul.nextInt();
B = inputul.nextInt();
x = inputul.nextInt();
y = inputul.nextInt();
int array[] = new int[n+1];
array[0] = 0;
for (int j = 1; j <= n; j++) {
if (array[j-1] + x <= B){
array[j] = array[j-1] + x;
}
else array[j] = array[j-1]-y;
}
long sum = 0;
for (int j = 0; j <= n; j++) {
sum += array[j];
}
System.out.println(sum);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 521df4d1b203bd343d7e6f21cb1f483a | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
try {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
long b=sc.nextInt();
long x=sc.nextInt();
long y=sc.nextInt();
long arr[]=new long[n+1];
arr[0]=0;
long res=0;
for(int i=1;i<=n;i++)
{
long sum=arr[i-1];
if(sum+x > b)
{
sum-=y;
arr[i]=sum;
}
else{
arr[i]=sum+x;
}
res+=arr[i];
}
System.out.println(res);
}
} catch(Exception e) {
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 7e97e1426892dedecb82086902998114 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String args[]) {
//System.out.println("Hello World!");
Scanner in = new Scanner(System.in);
int t = in.nextInt();;
while(t-- > 0){
int n = in.nextInt();
int b = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
long sum = 0;
int arr[] = new int[n + 1];
arr[0] = 0;
for(int i = 1; i <= n; i++){
if(b >= arr[i - 1] + x){
arr[i] = arr[i - 1] + x;
}else{
arr[i] = arr[i - 1] - y;
}
}
for(int i = 0;i <= n; i++){
sum += arr[i];
}
System.out.println(sum);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 44e4491248aea2eae44a02d0b4db4726 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class ahmed {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
long r = 0;
while (t-->0) {
r = 0;
Long[] l = new Long[4];
for (int i = 0;i<4;i++) {
l[i] = sc.nextLong();
}
Long[] v = new Long[(int) (l[0]+1)];
v[0] = (long) 0;
for(int i = 1;i<l[0]+1;i++) {
if (v[i-1] + l[2] <=l[1])
v[i] = v[i-1] + l[2];
else {
v[i] = v[i-1] - l[3];
}
}
for (int i = 0;i<l[0]+1;i++) {
r+=v[i];
}System.out.println(r);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 36cecee1e81736a39b25f7068ccf7e5e | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.io.IOException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner in=new Scanner(System.in);
int t=in.nextInt();
for (int i=0;i<t;i++)
{
int n=in.nextInt();
int b=in.nextInt();
int x=in.nextInt();
int y=in.nextInt();
long sum=0l;
int an=0;
for (int j=0;j<n;j++)
{
if (an+x<=b)
{
an=an+x;
}
else
{
an=an-y;
}
sum+=an;
}
System.out.println(sum);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 7463ab415264a99c189f9705560d40d5 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class Main{
static void solve(int n, int b, int x, int y) {
int cur = 0;
long res = 0;
for (int i = 0; i < n; i++) {
if (cur + x <= b) {
cur += x;
} else {
cur -= y;
}
res += cur;
}
System.out.println(res);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int TT = sc.nextInt();
while (TT-- > 0) {
solve(sc.nextInt(), sc.nextInt(),sc.nextInt(), sc.nextInt());
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | f05519ee0c0a059d478781b61f10a74f | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.util.*;
import java.util.stream.Collectors;
import java.io.*;
import java.math.*;
public class ER125_B{
public static FastScanner sc;
public static PrintWriter pw;
public static StringBuilder sb;
public static int MOD= 1000000007;
public static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
public static void printList(List<Long> al) {
System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" ")));
}
public static void printList(Deque<Long> al) {
System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" ")));
}
public static void printArr(long[] arr) {
System.out.println(Arrays.toString(arr).trim().replace("[", "").replace("]","").replace(","," "));
}
public static long gcd(long a, long b) {
if(b==0) return a;
return gcd(b,a%b);
}
public static long lcm(long a, long b) {
return((a*b)/gcd(a,b));
}
public static void decreasingOrder(ArrayList<Long> al) {
Comparator<Long> c = Collections.reverseOrder();
Collections.sort(al,c);
}
public static boolean[] sieveOfEratosthenes(int n) {
boolean isPrime[] = new boolean[n+1];
Arrays.fill(isPrime, true);
isPrime[0]=false;
isPrime[1]=false;
for(int i=2;i*i<n;i++) {
for(int j=2*i;j<n;j+=i) {
isPrime[j]=false;
}
}
return isPrime;
}
public static long nCr(long N, long R) {
double result=1;
for(int i=1;i<=R;i++) result=((result*(N-R+i))/i);
return (long) result;
}
public static long fastPow(long a, long b, int n) {
long result=1;
while(b>0) {
if((b&1)==1)
result=(result*a %n)%n;
a=(a%n * a%n)%n;
b>>=1;
}
return result;
}
public static int BinarySearch(long[] arr, long key) {
int low=0;
int high=arr.length-1;
while(low<=high) {
int mid=(low+high)/2;
if(arr[mid]==key)
return mid;
else if(arr[mid]>key)
high=mid-1;
else
low=mid+1;
}
return low; //High=low-1
}
public static void solve(int t) {
int n=sc.nextInt();
long b=sc.nextLong();
long x=sc.nextLong();
long y=sc.nextLong();
long temp=0;
long ans=0;
ans+=temp;
for(int i=1;i<=n;i++) {
if((temp+x)<=b) {
temp+=x;
}
else temp-=y;
ans+=temp;
// System.out.println(temp);
}
System.out.println(ans);
}
public static void main(String[] args) {
sc = new FastScanner();
pw = new PrintWriter(new OutputStreamWriter(System.out));
sb= new StringBuilder("");
int t=sc.nextInt();
for(int i=1;i<=t;i++) solve(i);
System.out.println(sb);
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | ab769f8cd1779da24f04ca83606cd9e6 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.util.Scanner;
public class XYSequence {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
long n = scanner.nextLong(), b = scanner.nextLong(), x = scanner.nextLong(), y = scanner.nextLong();
long sum = 0;
long max = 0;
for (int j = 0; j < n; j++) {
long min = Long.min(max + x, max - y);
max = Long.max(max + x, max - y);
sum += max <= b ? max : min;
max = max <= b ? max : min;
}
System.out.println(sum);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | b339b4a2705806d7a334f8956c6e1ee3 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
import java.util.logging.Logger;
/**
* Accomplished using the EduTools plugin by JetBrains https://plugins.jetbrains.com/plugin/10081-edutools
*
* To modify the template, go to Preferences -> Editor -> File and Code Templates -> Other
*/
public class Main {
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
public static class Pair<K extends Comparable<K>,V extends Comparable<V>> implements Comparable<Pair<K,V>> {
private K p1;
private V p2;
public Pair(K p1, V p2) {
this.p1 = p1;
this.p2 = p2;
}
public K getP1() {
return p1;
}
public void setP1(K p1) {
this.p1 = p1;
}
public V getP2() {
return p2;
}
public void setP2(V p2) {
this.p2 = p2;
}
@Override
public int compareTo(Pair<K, V> o) {
if(p1.compareTo(o.getP1()) == 0) {
return p2.compareTo(o.getP2());
}
return p1.compareTo(o.getP1());
}
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Long t = in.nextLong();
while(t-- > 0) {
long n = in.nextLong();
long B = in.nextLong();
long x = in.nextLong();
long y = in.nextLong();
long curr = 0;
long sum = 0;
for(int i = 1;i <= n;i++) {
if(curr + x > B) {
curr = curr - y;
} else {
curr = curr + x;
}
sum += curr;
}
out.write(sum + "\n");
}
out.close();
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 490c7d2d61186662d8646b2988873752 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author cpp
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
BXYSequence solver = new BXYSequence();
solver.solve(1, in, out);
out.close();
}
static class BXYSequence {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt();
int b = in.nextInt();
int x = in.nextInt();
int y = in.nextInt();
long ans = 0L;
int curr = 0;
for (int i = 1; i <= n; i++) {
if (curr + x <= b) {
ans += curr + x;
curr += x;
} else {
ans += curr - y;
curr -= y;
}
}
out.println(ans);
}
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 1d70210d179b9deb26be6b864e0ba044 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.lang.Math;
public class IntegerMoves {
static long maxSum ( long n, long B, long x, long y ) {
int i;
long last = 0;
long totalSum = 0;
for ( i = 0; i < n; i++ ) {
if (B<last+x)
last=last-y;
else
last=last+x;
totalSum = totalSum+last;
}
return totalSum;
}
public static void main(String[]f) {
Scanner s=new Scanner(System.in);
int t = s.nextInt();
int i;
for ( i = 0; i < t; i ++) {
int n = s.nextInt();
long B = s.nextInt();
long x = s.nextInt();
long y = s.nextInt();
System.out.println( maxSum(n,B,x,y));
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 1f8658ba6cb50692b20b7dfba202eba1 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
public class c731{
public static void main(String[] args) throws IOException {
BufferedWriter out = new BufferedWriter(
new OutputStreamWriter(System.out));
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
PrintWriter pt = new PrintWriter(System.out);
FastReader sc = new FastReader();
int t = sc.nextInt();
for(int o = 0; o<t;o++){
int n = sc.nextInt();
int b = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
int l = 0;
long sum = 0;
for(int i = 0 ; i<n;i++ ) {
if(l + x <= b) {
l += x;
sum += l;
// System.out.println(l + " " + sum);
}else {
l -= y;
sum += l;
}
}
System.out.println(sum);
}
}
//}
//------------------------------------------------------------------------------------------------------------------------------------------------
public static int cnt_set(long x) {
long v = 1l;
int c =0;
int f = 0;
while(v<=x) {
if((v&x)!=0) {
c++;
}
v = v<<1;
}
return c;
}
public static int lis(int[] arr,int[] dp) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
dp[0]= 1;
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>x) {
al.add(arr[i]);
}else {
int v = lower_bound(al, 0, al.size(), arr[i]);
// System.out.println(v);
al.set(v, arr[i]);
}
dp[i] = al.size();
}
//return al.size();
return al.size();
}
public static int lis2(int[] arr,int[] dp) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(-arr[n-1]);
dp[n-1] = 1;
// System.out.println(al);
for(int i = n-2 ; i>=0;i--) {
int x = al.get(al.size()-1);
// System.out.println(-arr[i] + " " + i + " " + x);
if((-arr[i])>x) {
al.add(-arr[i]);
}else {
int v = lower_bound(al, 0, al.size(), -arr[i]);
// System.out.println(v);
al.set(v, -arr[i]);
}
dp[i] = al.size();
}
//return al.size();
return al.size();
}
static int cntDivisors(int n){
int cnt = 0;
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
if (n/i == i)
cnt++;
else
cnt+=2;
}
}
return cnt;
}
public static long power(long x, long y, long p){
long res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0){
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
public static long ncr(long[] fac, int n , int r , long m) {
if(r>n) {
return 0;
}
return fac[n]*(modInverse(fac[r], m))%m *(modInverse(fac[n-r], m))%m;
}
public static int lower_bound(ArrayList<Integer> arr,int lo , int hi, int k)
{
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (arr.get(mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==arr.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> arr,int lo , int hi, int k)
{
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (arr.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==arr.size())
{
return -1;
}
return s;
}
// -----------------------------------------------------------------------------------------------------------------------------------------------
public static long gcd(long a, long b){
if (a == 0)
return b;
return gcd(b % a, a);
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
public static long modInverse(long a, long m){
long m0 = m;
long y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
// q is quotient
long q = a / m;
long t = m;
// m is remainder now, process
// same as Euclid's algo
m = a % m;
a = t;
t = y;
// Update x and y
y = x - q * y;
x = t;
}
// Make x positive
if (x < 0)
x += m0;
return x;
}
//_________________________________________________________________________________________________________________________________________________________________
// private static int[] parent;
// private static int[] size;
public static int find(int[] parent, int u) {
while(u != parent[u]) {
parent[u] = parent[parent[u]];
u = parent[u];
}
return u;
}
private static void union(int[] parent,int[] size,int u, int v) {
int rootU = find(parent,u);
int rootV = find(parent,v);
if(rootU == rootV) {
return;
}
if(size[rootU] < size[rootV]) {
parent[rootU] = rootV;
size[rootV] += size[rootU];
} else {
parent[rootV] = rootU;
size[rootU] += size[rootV];
}
}
//-----------------------------------------------------------------------------------------------------------------------------------
//segment tree
//for finding minimum in range
public static void build(boolean [] seg,boolean []arr,int idx, int lo , int hi) {
if(lo == hi) {
seg[idx] = arr[lo];
return;
}
int mid = (lo + hi)/2;
build(seg,arr,2*idx+1, lo, mid);
build(seg,arr,idx*2+2, mid +1, hi);
// seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]);
// seg[idx] = seg[idx*2+1]+ seg[idx*2+2];
// seg[idx] = Math.min(seg[idx*2+1], seg[idx*2+2]);
seg[idx] = seg[idx*2+1] && seg[idx*2+2];
}
//for finding minimum in range
public static boolean query(boolean[]seg,int idx , int lo , int hi , int l , int r) {
if(lo>=l && hi<=r) {
return seg[idx];
}
if(hi<l || lo>r) {
return true;
}
int mid = (lo + hi)/2;
boolean left = query(seg,idx*2 +1, lo, mid, l, r);
boolean right = query(seg,idx*2 + 2, mid + 1, hi, l, r);
// return Math.min(left, right);
//return gcd(left, right);
// return Math.min(left, right);
return left && right;
}
// // for sum
//
//public static void update(int[]seg,int idx, int lo , int hi , int node , int val) {
// if(lo == hi) {
// seg[idx] += val;
// }else {
//int mid = (lo + hi )/2;
//if(node<=mid && node>=lo) {
// update(seg, idx * 2 +1, lo, mid, node, val);
//}else {
// update(seg, idx*2 + 2, mid + 1, hi, node, val);
//}
//seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2];
//
//}
//}
//---------------------------------------------------------------------------------------------------------------------------------------
//
//static void shuffleArray(long[] ar)
//{
// // If running on Java 6 or older, use `new Random()` on RHS here
// Random rnd = ThreadLocalRandom.current();
// for (int i = ar.length - 1; i > 0; i--)
// {
// int index = rnd.nextInt(i + 1);
// // Simple swap
// int a = ar[index];
// ar[index] = ar[i];
// ar[i] = a;
// }
//}
// static void shuffleArray(coup[] ar)
// {
// // If running on Java 6 or older, use `new Random()` on RHS here
// Random rnd = ThreadLocalRandom.current();
// for (int i = ar.length - 1; i > 0; i--)
// {
// int index = rnd.nextInt(i + 1);
// // Simple swap
// coup a = ar[index];
// ar[index] = ar[i];
// ar[i] = a;
// }
// }
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------
class coup{
int a;
int b;
public coup(int a , int b) {
this.a = a;
this.b = b;
}
}
class trip{
int a , b, c;
public trip(int a , int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 7d00a0d60802862ddb4e97136a425b4f | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t--!=0){
String[] str = br.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int B = Integer.parseInt(str[1]);
int x = Integer.parseInt(str[2]);
int y = Integer.parseInt(str[3]);
long[] arr = new long[n+1];
arr[0] = 0;
long sum = 0;
for(int i=1;i<=n;i++){
if(arr[i-1]+x<=B){
arr[i] = arr[i-1]+x;
}else{
arr[i] = arr[i-1]-y;
}
sum+=arr[i];
}
System.out.println(sum);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 0ae4c789174012c24ee5cf999c5d503d | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
import java.util.regex.Matcher;
public class Main {
static final int INF = 0x3f3f3f3f;
static final long LNF = 0x3f3f3f3f3f3f3f3fL;
static int[]arr;static int n;static int b;static int x,y;
public static void main(String[] args) throws IOException {
initReader();
int t=nextInt();
while (t--!=0){
n=nextInt();
b=nextInt();
x=nextInt();
y=nextInt();
arr=new int[n+1];
solve(1);
long sum=0;
for(int i=1;i<=n;i++){
sum+=arr[i];
}
pw.println(sum);
}
pw.close();
}
static void solve(int i){
if(i==n+1)return;
if(arr[i-1]+x<=b){
arr[i]=arr[i-1]+x;
}else {
arr[i]=arr[i-1]-y;
}
solve(i+1);
}
/***************************************************************************************/
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter pw;
public static void initReader() throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = new StringTokenizer("");
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
// 从文件读写
// reader = new BufferedReader(new FileReader("test.in"));
// tokenizer = new StringTokenizer("");
// pw = new PrintWriter(new BufferedWriter(new FileWriter("test1.out")));
}
public static boolean hasNext() {
try {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (Exception e) {
return false;
}
return true;
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() {
try {
return reader.readLine();
} catch (Exception e) {
return null;
}
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static char nextChar() throws IOException {
return next().charAt(0);
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 64daad6017a217b14769c0c3df42f459 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.*;
import java.util.*;
public class XYSequence {
public static PrintWriter out;
public static void main(String[] args)throws IOException{
Scanner sc=new Scanner();
out=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int b=sc.nextInt();
int x=sc.nextInt();
int y=sc.nextInt();
long start=0;
long ans=0;
for(int i=0;i<n;i++) {
if(start+x<=b) {
start+=x;
}else {
start-=y;
}
ans+=start;
}
out.println(ans);
}
out.close();
}
public static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 53eb8391014c66fbb975dc58e61c8267 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.util.Scanner;
public class XYSequence {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
int t = in.nextInt();
int i=0;
StringBuilder sc=new StringBuilder();
while(i<t) {
int n=in.nextInt();
long b=in.nextLong();
long x=in.nextLong();
long y=in.nextLong();
long[] arr = new long[n+1];
arr[0]=0;
long sum=0;
for (int j = 1; j < arr.length; j++) {
if(arr[j-1]+x<=b) {
arr[j]=arr[j-1]+x;
}else {
arr[j]=arr[j-1]-y;
}
sum+=arr[j];
}
sc.append(sum+"\n");
i++;
}
System.out.println(sc);
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | b7917a644be281b283abb2d1ab2eb93c | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | /*
* akshaygupta26
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class B {
static long mod = (long) (1e9 + 7);
public static void main(String[] args) {
FastReader sc = new FastReader();
StringBuilder ans = new StringBuilder();
int test = sc.nextInt();
while (test-- > 0) {
long n = sc.nextInt(), B = sc.nextInt(), x = sc.nextInt(), y = sc.nextInt();
long sum = 0;
long prv = 0;
for (int i = 1; i <= n; i++) {
if (prv + x <= B) {
prv += x;
sum += prv;
} else {
prv -= y;
sum += prv;
}
}
ans.append(sum + "\n");
}
System.out.print(ans);
}
static long add(long a, long b) {
return (a + b) % mod;
}
static long mult(long a, long b) {
return (a * b) % mod;
}
static long _gcd(long a, long b) {
if (b == 0)
return a;
return _gcd(b, a % b);
}
static final Random random = new Random();
static void ruffleSort(int[] a) {
int n = a.length;// shuffle, then sort
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | bca88751e5d78f02865910dc41875632 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | // package JAVA;
// import java.util.ArrayList;
// import java.util.Arrays;
// import java.util.List;
import java.util.Scanner;
// import org.omg.CORBA.INV_OBJREF;
// import javax.swing.text.Highlighter.Highlight;
public class Main {
// static int[] f = new int[10];
public static void main(String[] args) {
solve();
}
// public static void getCount(int num){
// // Arrays.fill(f,0);
// int i = 0;
// while(i < 5 && num !=0){
// f[i++] = num % 10;
// num /= 10;
// }
// }
//解决问题函数。
// static int[][] dir = new int[][]{{1,-1},{-1,1}};
// static int ans = 0;
// static int[] nums = new int[2000005];
final static double Gamma = 0.577215649;//欧拉常数
final static int N = 10005;//常数N
// static int[][] nums = new int[N][N];
//马跳跃一步的跳法。
static int[][] dir = new int[][]{{1,2},{1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
public static void solve(){
Scanner input = new Scanner(System.in);
int n = input.nextInt();
while((n--)!=0){
int m =input.nextInt();
int B = input.nextInt();
int x =input.nextInt();
int y = input.nextInt();
long sum = 0;
long s = 0;
for(int i = 1;i<=m;i++){
if(s+x<=B){
s+=x;
}else{
s-=y;
}
sum += s;
}
System.out.println(sum);
}
input.close();
}
public static void cnt(int num,int[] nums){
while(num != 0){
nums[num%10] = 1;
num /= 10;
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 199f4d510e6e41e03e0f5835bcd3fec8 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static AReader scan = new AReader();
static void slove() {
int n = scan.nextInt();
int b = scan.nextInt();
int x = scan.nextInt();
int y = scan.nextInt();
long s = 0;
long last = 0;
for(int i = 1;i<=n;i++){
if(last + x <=b){
last +=x;
}else{
last -=y;
}
s +=last;
}
System.out.println(s);
}
public static void main(String[] args) {
int T = scan.nextInt();
while (T-- > 0) {
slove();
}
}
}
class AReader {
private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private StringTokenizer tokenizer = new StringTokenizer("");
private String innerNextLine() {
try {
return reader.readLine();
} catch (IOException ex) {
return null;
}
}
public boolean hasNext() {
while (!tokenizer.hasMoreTokens()) {
String nextLine = innerNextLine();
if (nextLine == null) {
return false;
}
tokenizer = new StringTokenizer(nextLine);
}
return true;
}
public String nextLine() {
tokenizer = new StringTokenizer("");
return innerNextLine();
}
public String next() {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
class Pair {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | b9ee4c23891947c24ec4495cf9df9a70 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import javax.management.Query;
import java.io.*;
public class Main {
// static int n,k;
//// static String t,s;
// static long[]memo;
// static int n;
static String s;
static HashMap<Long,Integer>hm;
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while (t-->0) {
int n=sc.nextInt();
int b=sc.nextInt();
int x=sc.nextInt();
int y=sc.nextInt();
long sum=0;
int prev=0;
while (n-->0){
if(prev+x<=b){
sum+=prev+x;
prev+=x;
}
else{
sum+=(prev-y);
prev-=y;
}
}
pw.println(sum);
}
pw.close();
}
static LinkedList getfact(int n){
LinkedList<Integer>ll=new LinkedList<>();
LinkedList<Integer>ll2=new LinkedList<>();
for (int i = 1; i <= Math.sqrt(n); i++) {
if(n%i==0) {
ll.add(i);
if(i!=n/i)
ll2.addLast(n/i);
}
}
while (!ll2.isEmpty()){
ll.add(ll2.removeLast());
}
return ll;
}
static void rev(int n){
String s1=s.substring(0,n);
s=s.substring(n);
for (int i = 0; i <n ; i++) {
s=s1.charAt(i)+s;
}
}
static class SegmentTree { // 1-based DS, OOP
int N; //the number of elements in the array as a power of 2 (i.e. after padding)
long[] array, sTree;
Long[]lazy;
SegmentTree(long[] in)
{
array = in; N = in.length - 1;
sTree = new long[N<<1]; //no. of nodes = 2*N - 1, we add one to cross out index zero
lazy = new Long[N<<1];
build(1,1,N);
}
void build(int node, int b, int e) // O(n)
{
if(b == e)
sTree[node] = array[b];
else
{
int mid = b + e >> 1;
build(node<<1,b,mid);
build(node<<1|1,mid+1,e);
sTree[node] = sTree[node<<1]+sTree[node<<1|1];
}
}
void update_point(int index, int val) // O(log n)
{
index += N - 1;
sTree[index] += val;
while(index>1)
{
index >>= 1;
sTree[index] = sTree[index<<1] + sTree[index<<1|1];
}
}
void update_range(int i, int j, int val) // O(log n)
{
update_range(1,1,N,i,j,val);
}
void update_range(int node, int b, int e, int i, int j, int val)
{
if(i > e || j < b)
return;
if(b >= i && e <= j)
{
sTree[node] = (e-b+1)*val;
lazy[node] = val*1l;
}
else
{
int mid = b + e >> 1;
propagate(node, b, mid, e);
update_range(node<<1,b,mid,i,j,val);
update_range(node<<1|1,mid+1,e,i,j,val);
sTree[node] = sTree[node<<1] + sTree[node<<1|1];
}
}
void propagate(int node, int b, int mid, int e)
{
if(lazy[node]!=null) {
lazy[node << 1] = lazy[node];
lazy[node << 1 | 1] = lazy[node];
sTree[node << 1] = (mid - b + 1) * lazy[node];
sTree[node << 1 | 1] = (e - mid) * lazy[node];
}
lazy[node] = null;
}
long query(int i, int j)
{
return query(1,1,N,i,j);
}
long query(int node, int b, int e, int i, int j) // O(log n)
{
if(i>e || j <b)
return 0;
if(b>= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
propagate(node, b, mid, e);
long q1 = query(node<<1,b,mid,i,j);
long q2 = query(node<<1|1,mid+1,e,i,j);
return q1 + q2;
}
}
// public static long dp(int idx) {
// if (idx >= n)
// return Long.MAX_VALUE/2;
// return Math.min(dp(idx+1),memo[idx]+dp(idx+k));
// }
// if(num==k)
// return dp(0,idx+1);
// if(memo[num][idx]!=-1)
// return memo[num][idx];
// long ret=0;
// if(num==0) {
// if(s.charAt(idx)=='a')
// ret= dp(1,idx+1);
// else if(s.charAt(idx)=='?') {
// ret=Math.max(1+dp(1,idx+1),dp(0,idx+1) );
// }
// }
// else {
// if(num%2==0) {
// if(s.charAt(idx)=='a')
// ret=dp(num+1,idx+1);
// else if(s.charAt(idx)=='?')
// ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1));
// }
// else {
// if(s.charAt(idx)=='b')
// ret=dp(num+1,idx+1);
// else if(s.charAt(idx)=='?')
// ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1));
// }
// }
// }
static void putorrem(long x){
if(hm.getOrDefault(x,0)==1){
hm.remove(x);
}
else
hm.put(x,hm.getOrDefault(x,0)-1);
}
public static int max4(int a,int b, int c,int d) {
int [] s= {a,b,c,d};
Arrays.sort(s);
return s[3];
}
public static double logbase2(int k) {
return( (Math.log(k)+0.0)/Math.log(2));
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | be27daa86231a6dfd987bf53b396dfa2 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
import javax.sql.rowset.spi.SyncResolver;
import java.io.*;
import java.nio.channels.NonReadableChannelException;
import java.text.DateFormatSymbols;
public class CpTemp {
static int a[];
static long count=0;
static long row = 1l;
static long col = 1l;
static FastScanner fs = null;
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
outer:
while (t-- > 0) {
int n = fs.nextInt();
int b = fs.nextInt();
int x = fs.nextInt();
int y = fs.nextInt();
ArrayList<Long> al = new ArrayList<>();
al.add(0l);
long max = 0l;
long a = 0;
while(n-->0){
if(a+x<=b){
a += x;
al.add(a);
}else{
a -= y;
al.add(a);
}
max += a;
}
out.println(max);
}
out.close();
}
static class Pair {
int x;
int y;
int id;
Pair(int x, int y , int id) {
this.x = x;
this.y = y;
this.id = id;
}
/*// public int compareTo(Pair o) {
return this.x - o.x;
}*/
}
static long power(long x, long y, long p) {
if (y == 0)
return 1;
if (x == 0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y) {
if (y == 0)
return 1;
if (x == 0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readlongArray(int n){
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static boolean prime[];
static void sieveOfEratosthenes(int n) {
prime = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static long nCk(int n, int k) {
long res = 1;
for (int i = n - k + 1; i <= n; ++i)
res *= i;
for (int i = 2; i <= k; ++i)
res /= i;
return res;
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | f89e8e78ba6613e6bb1b553cd9e7e8df | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class XYSequence {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()){
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
int t = sc.nextInt();
while(t-- != 0) {
int n = sc.nextInt();
int B = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
long sum = 0;
long tmp = 0;
for (int i = 1; i <= n; i++) {
if (tmp + x <= B) {
tmp += x;
} else {
tmp -= y;
}
sum += tmp;
}
System.out.println(sum);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 1b723eeed4655805eae601bd84066378 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.lang.*;
public class sumdigit{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
long B=sc.nextLong();
long x=sc.nextLong();
long y=sc.nextLong();
long sum=0;
long pre=0;
while(n-->0){
if(pre+x<=B){
pre+=x;
sum+=pre;
}
else{
pre-=y;
sum+=pre;
}
}
System.out.println(sum);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | e78992e06a6b39227e97e04e3d167bae | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class CodeforcesQuestions {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0)
{
int n=s.nextInt();
int b=s.nextInt();
int x=s.nextInt();
int y=s.nextInt();
long ans=0;
int arr[]=new int [n+1];
arr[0]=0;
for(int i=1;i<=n;i++)
{
int t1=arr[i-1]+x;
int t2=arr[i-1]-y;
int t3=Math.max(t1, t2);
if(t3<=b)
{
ans+=t3;
arr[i]=t3;
}
else
{
ans+=Math.min(t1, t2);
arr[i]=Math.min(t1, t2);
}
}
System.out.println(ans);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 65edbed2734226ff3c53e03ca717ea62 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.*;
public class Main {
public static void main(String[] args){
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
solve(in, out);
out.close();
}
static String reverse(String s) {
return (new StringBuilder(s)).reverse().toString();
}
static void sieveOfEratosthenes(int n, int factors[], ArrayList<Integer> ar)
{
factors[1]=1;
int p;
for(p = 2; p*p <=n; p++)
{
if(factors[p] == 0)
{
ar.add(p);
factors[p]=p;
for(int i = p*p; i <= n; i += p)
if(factors[i]==0)
factors[i] = p;
}
}
for(;p<=n;p++){
if(factors[p] == 0)
{
factors[p] = p;
ar.add(p);
}
}
}
static void sort(int ar[]) {
int n = ar.length;
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static void sort1(long ar[]) {
int n = ar.length;
ArrayList<Long> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static long ncr(long n, long r, long mod) {
if (r == 0)
return 1;
long val = ncr(n - 1, r - 1, mod);
val = (n * val) % mod;
val = (val * modInverse(r, mod)) % mod;
return val;
}
static int findMax(int a[], int n, int vis[], int i, int d){
if(i>=n)
return 0;
if(vis[i]==1)
return findMax(a, n, vis, i+1, d);
int max = 0;
for(int j=i+1;j<n;j++){
if(Math.abs(a[i]-a[j])>d||vis[j]==1)
continue;
vis[j] = 1;
max = Math.max(max, 1 + findMax(a, n, vis, i+1, d));
vis[j] = 0;
}
return max;
}
static void findSub(ArrayList<ArrayList<Integer>> ar, int n, ArrayList<Integer> a, int i){
if(i==n){
ArrayList<Integer> b = new ArrayList<Integer>();
for(int y:a){
b.add(y);
}
ar.add(b);
return;
}
for(int j=0;j<n;j++){
if(j==i)
continue;
a.set(i,j);
findSub(ar, n, a, i+1);
}
}
// *-------------------code starts here--------------------------------------------------*
static HashMap<Long,Integer>map;
public static void solve(InputReader sc, PrintWriter pw){
long mod=(long)1e9+7;
int t=sc.nextInt();
// int t=1;
L : while(--t>=0){
int n=sc.nextInt();
int b=sc.nextInt();
int x=sc.nextInt(),y=sc.nextInt();
int a[]=new int[n+1];
a[0]=0;
for(int i=1;i<=n;i++){
if((a[i-1]+x)<=b){
a[i]=a[i-1]+x;
}
else{
a[i]=a[i-1]-y;
}
}
long sum=0;
for(int i:a){
sum+=i;
}
pw.println(sum);
}
}
// *-------------------code ends here-----------------------------------------------------*
static void assignAnc(ArrayList<Integer> ar[],int sz[], int pa[] ,int curr, int par){
sz[curr] = 1;
pa[curr] = par;
for(int v:ar[curr]){
if(par==v)
continue;
assignAnc(ar, sz, pa, v, curr);
sz[curr] += sz[v];
}
}
static int findLCA(int a, int b, int par[][], int depth[]){
if(depth[a]>depth[b]){
a = a^b;
b = a^b;
a = a^b;
}
int diff = depth[b] - depth[a];
for(int i=19;i>=0;i--){
if((diff&(1<<i))>0){
b = par[b][i];
}
}
if(a==b)
return a;
for(int i=19;i>=0;i--){
if(par[b][i]!=par[a][i]){
b = par[b][i];
a = par[a][i];
}
}
return par[a][0];
}
static void formArrayForBinaryLifting(int n, int par[][]){
for(int j=1;j<20;j++){
for(int i=0;i<n;i++){
if(par[i][j-1]==-1)
continue;
par[i][j] = par[par[i][j-1]][j-1];
}
}
}
static long lcm(int a, int b){
return a*b/gcd(a,b);
}
static class Pair1 {
long a;
long b;
Pair1(long a, long b) {
this.a = a;
this.b = b;
}
}
static class Pair implements Comparable<Pair> {
int a;
int b;
// int c;
Pair(int a, int b) {
this.a = a;
this.b = b;
// this.c = c;
}
public int compareTo(Pair p) {
return Integer.compare(a,p.a);
}
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long fast_pow(long base, long n, long M) {
if (n == 0)
return 1;
if (n == 1)
return base % M;
long halfn = fast_pow(base, n / 2, M);
if (n % 2 == 0)
return (halfn * halfn) % M;
else
return (((halfn * halfn) % M) * base) % M;
}
static long modInverse(long n, long M) {
return fast_pow(n, M - 2, M);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 9992768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
public long[] readarray(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 2d662a4b893ca55e65009d71f24bf11b | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.Math;
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
public class Main {
static PrintWriter pw;
static Scanner sc;
static StringBuilder ans;
static long mod = 1000000000+7;
static void pn(final Object arg) { pw.print(arg); pw.flush(); }
/*-------------- for input in an value ---------------------*/
static int ni() { return sc.nextInt(); }
static long nl() { return sc.nextLong(); }
static double nd() { return sc.nextDouble(); }
static String ns() { return sc.next(); }
static String nLine() { return sc.nextLine(); }
static void ap(int arg) { ans.append(arg); }
static void ap(long arg) { ans.append(arg); }
static void ap(String arg) { ans.append(arg); }
static void ap(StringBuilder arg) { ans.append(arg); }
static void apn() { ans.append("\n"); }
static void apn(int arg) { ans.append(arg+"\n"); }
static void apn(long arg) { ans.append(arg+"\n"); }
static void apn(String arg) { ans.append(arg+"\n"); }
static void apn(StringBuilder arg) { ans.append(arg+"\n"); }
static void yes() { ap("Yes\n"); }
static void no() { ap("No\n"); }
/* for Dubuggin */
static void printArr(int ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printArr(long ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printArr(String ar[], int st , int end) { for(int i = st; i<=end; i++ ) ap(ar[i]+ "\n"); ap("\n"); }
static void printIntegerList(List<Integer> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
static void printLongList(List<Long> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
static void printStringList(List<String> ar, int st , int end) { for(int i = st; i<=end; i++ ) ap(ar.get(i)+ " "); ap("\n"); }
/*-------------- for input in an array ---------------------*/
static void readArray(int arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ni();
}
static void readArray(long arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nl();
}
static void readArray(String arr[]){
for(int i=0; i<arr.length; i++)arr[i] = ns();
}
static void readArray(double arr[]){
for(int i=0; i<arr.length; i++)arr[i] = nd();
}
/*-------------- File vs Input ---------------------*/
static void runFile() throws Exception {
sc = new Scanner(new FileReader("input.txt"));
pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
}
static void runIo() throws Exception {
pw =new PrintWriter(System.out);
sc = new Scanner(System.in);
}
static int log2(int n) { return (int)(Math.log(n) / Math.log(2)); }
static boolean isPowerOfTwo (long x) { return x!=0 && ((x&(x-1)) == 0);}
static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); }
static long nCr(long n, long r) { // Combinations
if (n < r)
return 0;
if (r > n - r) { // because nCr(n, r) == nCr(n, n - r)
r = n - r;
}
long ans = 1L;
for (long i = 0; i < r; i++) {
ans *= (n - i);
ans /= (i + 1);
}
return ans;
}
static int countDigit(long n){return (int)Math.floor(Math.log10(n) + 1);}
static boolean isPrime(long n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (long i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean sv[] = new boolean[10002];
static void seive() {
//true -> not prime
// false->prime
sv[0] = sv[1] = true;
sv[2] = false;
for(int i = 0; i< sv.length; i++) {
if( !sv[i] && (long)i*(long)i < sv.length ) {
for ( int j = i*i; j<sv.length ; j += i ) {
sv[j] = true;
}
}
}
}
static long kadensAlgo(long ar[]) {
int n = ar.length;
long pre = ar[0];
long ans = ar[0];
for(int i = 1; i<n; i++) {
pre = Math.max(pre + ar[i], ar[i]);
ans = Math.max(pre, ans);
}
return ans;
}
static long binpow( long a, long b) {
long res = 1;
while (b > 0) {
if ( (b & 1) > 0){
res = (res * a);
}
a = (a * a);
b >>= 1;
}
return res;
}
static long factorial(long n) {
long res = 1, i;
for (i = 2; i <= n; i++){
res = ((res%mod) * (i%mod))%mod;
}
return res;
}
static int getCountPrime(int n) {
int ans = 0;
while (n%2==0) {
ans ++;
n /= 2;
}
for (int i = 3; i *i<= n; i+= 2) {
while (n%i == 0) {
ans ++;
n /= i;
}
}
if(n > 1 )
ans++;
return ans;
}
static void sort(int[] arr) {
/* because Arrays.sort() uses quicksort which is dumb
Collections.sort() uses merge sort */
ArrayList<Integer> ls = new ArrayList<Integer>();
for(int x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static void sort(long[] arr) {
/* because Arrays.sort() uses quicksort which is dumb
Collections.sort() uses merge sort */
ArrayList<Long> ls = new ArrayList<Long>();
for(Long x: arr)
ls.add(x);
Collections.sort(ls);
for(int i=0; i < arr.length; i++)
arr[i] = ls.get(i);
}
static int lowerBound(ArrayList<Integer> ar, int k, int l, int r) {
while (l <= r) {
int m = (l + r) >> 1 ;
if (ar.get(m) >= k) {
r = m - 1 ;
} else {
l = m + 1 ;
}
}
return l ;
}
static int upperBound(long ar[], long k, int l, int r) {
while (l <= r) {
int m = (l + r) >> 1 ;
if (ar[m] > k) {
r = m - 1 ;
} else {
l = m + 1 ;
}
}
return l ;
}
static int lowerBound(int ar[], int k, int l, int r) {
while (l <= r) {
int m = (l + r) >> 1 ;
if (ar[m] >= k) {
r = m - 1 ;
} else {
l = m + 1 ;
}
}
return l ;
}
static void findDivisor(int n, ArrayList<Integer> al) {
for(int i = 1; i*i <= n; i++){
if( n % i == 0){
if(n/i==i){
al.add(i);
}
else{
al.add(n/i);
al.add(i);
}
}
}
}
static class Pair{
int a;
int b;
Pair( int a, int b ){
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Pair)) return false;
Pair pair = (Pair) o;
return a == pair.a && b == pair.b;
}
@Override
public int hashCode() {
int result = a;
result = 31 * result + b;
return result;
}
}
public static void main(String[] args) throws Exception {
// runFile();
runIo();
int t;
t = 1;
t = sc.nextInt();
ans = new StringBuilder();
while( t-- > 0 ) {
solve();
}
pn(ans+"");
}
public static void solve ( ) {
long n = nl();
long b = nl();
long x = nl();
long y = nl();
long sum = 0;
long pre = 0;
for(int i = 0; i<=n; i++) {
if( i > 0 ) {
if( pre + x <= b ) {
pre = pre + x;
sum += pre ;
}
else {
pre = pre-y;
sum += pre ;
}
}
// apn(pre);
}
apn(sum);
}
static int find(int i, int ar[], int d ) {
if( i >= ar.length ) return 0;
int op1 = 0, op2 = 0;
op1 = find(i+d-1, ar, d-1);
op2 = find(i+d, ar, d);
return ar[i] + max( max(op1, op2), find(i+d+1, ar, d+1) );
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | d095efa166d149286a04a9e426449e6d | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | /*Radhe-Krishna*/
import javax.print.CancelablePrintJob;
import javax.xml.stream.events.Characters;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLType;
import java.util.*;
public class Main {
static int mod=1000000007;
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader sc = new FastReader();
// PrintWriter out = new PrintWriter(System.out);
// Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
int n=sc.nextInt();
int B=sc.nextInt();
int x=sc.nextInt();
int y=sc.nextInt();
int val=0;
int[] arr=new int[n+1];
arr[0]=0;
long sum=0;
for(int j=1;j<=n;j++){
val+=x;
if(val>B){
arr[j]=arr[j-1]-y;
}else{
arr[j]=arr[j-1]+x;
}
val=arr[j];
sum+=arr[j];
}
System.out.println(sum);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 357e0b9c7fa110dca06b26ed0e0a4c71 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class Main {
static Scanner sc = new Scanner(System.in) ;
public static void main(String[] args) {
int N = sc.nextInt();
while (N-- != 0) {
int n = sc.nextInt() ;
int b = sc.nextInt() ;
int x = sc.nextInt() ;
int y = sc.nextInt() ;
List<Integer> list = new ArrayList<>() ;
list.add(0) ;
for(int i = 0 ; i < n ; i ++){
int val = list.get(i) + x ;
if (val > b) {
val -= (x + y);
}
list.add(val) ;
}
long sum = 0 ;
for(int i : list){
sum += i ;
}
System.out.println(sum);
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 88fc45e0be05a11b7fa572337343574e | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int b = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
long sum = 0;
long c = 0;
for(int i=0;i<n;i++) {
if(c+x<=b) {
c+=x;
}
else c-=y;
sum+=c;
}
pw.println(sum);
}
pw.flush();
}
static class SegmentTree {
int[] arr, sTree, lazy;
int N;
public SegmentTree(int[] in) {
arr = in;
N = in.length - 1;
sTree = new int[2 * N];
lazy = new int[2 * N];
build(1, 1, N);
}
public void build(int Node, int left, int right) { // O(n)
if (left == right)
sTree[Node] = arr[left];
else {
int r = 2 * Node + 1;
int l = 2 * Node;
int mid = (left + right) / 2;
build(l, left, mid);
build(r, mid + 1, right);
sTree[Node] = sTree[l] + sTree[r];
}
}
public int query(int i, int j) {
return query(1, 1, N, i, j);
}
public int query(int Node, int left, int right, int i, int j) {
if (right < i || left > j)
return 0;
if (right <= j && i <= left) {
return sTree[Node];
}
int mid = (left + right) / 2;
int l = 2 * Node;
int r = 2 * Node + 1;
return query(l, left, mid, i, j) + query(r, mid + 1, right, i, j);
}
public void updatePoint(int idx, int value) {
int node = idx + N - 1;
arr[idx] = value;
sTree[node] = value;
while (node > 1) {
node /= 2;
int l = 2 * node;
int r = 2 * node + 1;
sTree[node] = sTree[l] + sTree[r];
}
}
public void updateRange(int i, int j, int val) {
updateRange(1, 1, N, i, j, val);
}
public void updateRange(int Node, int left, int right, int i, int j, int val) {
if (i > right || left > j)
return;
if (right <= j && i <= left) {
sTree[Node] += val * (right - left + 1);
lazy[Node] += val;
} else {
int l = 2 * Node;
int r = 2 * Node + 1;
int mid = (left + right) / 2;
propagate(Node, left, right);
updateRange(l, left, mid, i, j, val);
updateRange(r, mid + 1, right, mid, j, val);
sTree[Node] = sTree[l] + sTree[r];
}
}
public void propagate(int node, int left, int right) {
int l = 2 * node;
int r = 2 * node + 1;
int mid = (left + right) / 2;
lazy[l] = lazy[node];
lazy[r] = lazy[node];
sTree[l] += lazy[node] * (mid - left + 1);
sTree[r] += lazy[node] * (right - mid);
lazy[node] = 0;
}
}
public static void sort(int[] in) {
shuffle(in);
Arrays.sort(in);
}
public static void shuffle(int[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
int tmp = in[i];
in[i] = in[idx];
in[idx] = tmp;
}
}
static class Pair implements Comparable<Pair> {
int x;
int y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
if (o.y != y)
return (o.y) - (y);
return o.x - x;
}
public String toString() {
return x + " " + y;
}
}
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
public static void display(char[][] a) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 998fc938f3758b66fc70da705fa01666 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | //CP- MASS_2701
import java.util.*;
import java.io.*;
public class B_XY_Sequence {
static FastReader in=new FastReader();
static final Random random=new Random();
static long mod=1000000007L;
static HashMap<String,Integer>map=new HashMap<>();
public static void main(String args[]) throws IOException {
int t=in.nextInt();
int cse=1;
loop:
while(t-->0)
{
StringBuilder res=new StringBuilder();
//res.append("Hello"+"\n");
int n=in.nextInt();
long b=in.nextLong();
long x=in.nextLong();
long y=in.nextLong();
long sum=0;
long[] a=new long[n];
if(x>b)
{
a[0]=-1*y;
}
else{
a[0]=x;
}
sum=a[0];
for(int i=1;i<n;i++)
{
if((a[i-1]+x)<=b)
{
a[i]=a[i-1]+x;
}
else{
a[i]=a[i-1]-y;
}
sum+=a[i];
}
println(sum);
}
}
static int max(int a, int b)
{
if(a<b)
return b;
return a;
}
static void ruffleSort(int[] a) {
int n=a.length;
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static < E > void print(E res)
{
System.out.print(res);
}
static < E > void println(E res)
{
System.out.println(res);
}
static int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static int abs(int a)
{
if(a<0)
return -1*a;
return a;
}
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1)
return false;
if (n <= 3)
return true;
// This is checked so that we can skip
// middle five numbers in below loop
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
int [] readintarray(int n) {
int res [] = new int [n];
for(int i = 0; i<n; i++)res[i] = nextInt();
return res;
}
long [] readlongarray(int n) {
long res [] = new long [n];
for(int i = 0; i<n; i++)res[i] = nextLong();
return res;
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | c30b38587daed692d4723cd40916aa8f | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.awt.*;
import java.util.*;
import java.io.*;
public class Codeforces {
static FScanner sc = new FScanner();
static PrintWriter out = new PrintWriter(System.out);
static final Random random = new Random();
static long mod = 1000000007L;
static HashMap<String, Integer> map = new HashMap<>();
static boolean[] sieve = new boolean[1000000];
static double[] fib = new double[1000000];
public static void main(String args[]) throws IOException {
int T = sc.nextInt();
while (T-- > 0) {
int n=sc.nextInt();
int b=sc.nextInt();
int x=sc.nextInt();
int y=sc.nextInt();
long[] a=new long[n];
long s=0;
long ans=0;
for(int i=0;i<n;i++)
{
if(s+x<=b)
a[i]=s+x;
else
a[i]=s-y;
s=a[i];
ans+=s;
}
out.println(ans);
}
out.close();
}
// TemplateCode
static void fib() {
fib[0] = 0;
fib[1] = 1;
for (int i = 2; i < fib.length; i++)
fib[i] = fib[i - 1] + fib[i - 2];
}
static void primeSieve() {
for (int i = 0; i < sieve.length; i++)
sieve[i] = true;
for (int i = 2; i * i <= sieve.length; i++) {
if (sieve[i]) {
for (int j = i * i; j < sieve.length; j += i) {
sieve[j] = false;
}
}
}
}
static int max(int a, int b) {
if (a < b)
return b;
return a;
}
static int min(int a, int b) {
if (a < b)
return a;
return b;
}
static void ruffleSort(int[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int oi = random.nextInt(n), temp = a[oi];
a[oi] = a[i];
a[i] = temp;
}
Arrays.sort(a);
}
static <E> void print(E res) {
System.out.println(res);
}
static int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
static int abs(int a) {
if (a < 0)
return -1 * a;
return a;
}
static class FScanner {
BufferedReader br;
StringTokenizer st;
public FScanner() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readintarray(int n) {
int res[] = new int[n];
for (int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
long[] readlongarray(int n) {
long res[] = new long[n];
for (int i = 0; i < n; i++) res[i] = nextLong();
return res;
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | a92d9fbe773b3bdf708169fcd5bea823 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) throws Exception {
FastIO io = new FastIO();
int test=io.nextInt();
while(test>0)
{
int n=io.nextInt();
long b=io.nextLong();
long x=io.nextLong();
long y=io.nextLong();
long arr[]=new long[n+1];
arr[0]=0;
for(int i=1;i<=n;i++)
{
if(arr[i-1]+x<=b)arr[i]=arr[i-1]+x;
else arr[i]=arr[i-1]-y;
}
long res=0;
for(long t:arr)res+=t;
io.println(res);
test--;
}
io.close();
}
}
class FastIO extends PrintWriter {
private InputStream stream;
private byte[] buf = new byte[1<<16];
private int curChar, numChars;
// standard input
public FastIO() { this(System.in,System.out); }
public FastIO(InputStream i, OutputStream o) {
super(o);
stream = i;
}
// file input
public FastIO(String i, String o) throws IOException {
super(new FileWriter(o));
stream = new FileInputStream(i);
}
// throws InputMismatchException() if previously detected end of file
private int nextByte() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars == -1) return -1; // end of file
}
return buf[curChar++];
}
public String nextLine() {
int c; do { c = nextByte(); } while (c <= '\n');
StringBuilder res = new StringBuilder();
do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n');
return res.toString();
}
public String next() {
int c; do { c = nextByte(); } while (c <= ' ');
StringBuilder res = new StringBuilder();
do { res.appendCodePoint(c); c = nextByte(); } while (c > ' ');
return res.toString();
}
public int nextInt() {
int c; do { c = nextByte(); } while (c <= ' ');
int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); }
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10*res+c-'0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public long nextLong() {
int c; do { c = nextByte(); } while (c <= ' ');
long sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); }
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = 10*res+c-'0';
c = nextByte();
} while (c > ' ');
return res * sgn;
}
public double nextDouble() { return Double.parseDouble(next()); }
} | Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | e79ca35c8efbcbef789cd74691d68b57 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | //import java.io.IOException;
import java.io.*;
import java.util.*;
public class XYSequence {
static InputReader inputReader=new InputReader(System.in);
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static List<LinkedHashSet<Integer>>answer=new ArrayList<>();
static void solve() throws IOException
{
long n=inputReader.nextLong();
long B=inputReader.nextLong();
long x=inputReader.nextLong();
long y=inputReader.nextLong();
long prev=0;
long sum=0;
int ind=0;
while (ind<n)
{
long curr=prev+x;
if (curr>B)
{
curr=prev-y;
}
sum+=curr;
prev=curr;
ind++;
}
out.println(sum);
}
static PrintWriter out=new PrintWriter((System.out));
static void SortDec(long arr[])
{
List<Long>list=new ArrayList<>();
for(long ele:arr) {
list.add(ele);
}
Collections.sort(list,Collections.reverseOrder());
for (int i=0;i<list.size();i++)
{
arr[i]=list.get(i);
}
}
static void Sort(long arr[])
{
List<Long>list=new ArrayList<>();
for(long ele:arr) {
list.add(ele);
}
Collections.sort(list);
for (int i=0;i<list.size();i++)
{
arr[i]=list.get(i);
}
}
public static void main(String args[])throws IOException
{
int t=inputReader.nextInt();
while (t-->0) {
solve();
}
long s = System.currentTimeMillis();
// out.println(System.currentTimeMillis()-s+"ms");
out.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c))
c = snext();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | ed28758cb4cbefb825684c9282e4e706 | train_110.jsonl | 1647960300 | You are given four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$. You should build a sequence $$$a_0, a_1, a_2, \dots, a_n$$$ where $$$a_0 = 0$$$ and for each $$$i \ge 1$$$ you can choose: either $$$a_i = a_{i - 1} + x$$$ or $$$a_i = a_{i - 1} - y$$$. Your goal is to build such a sequence $$$a$$$ that $$$a_i \le B$$$ for all $$$i$$$ and $$$\sum\limits_{i=0}^{n}{a_i}$$$ is maximum possible. | 256 megabytes | import java.util.*;
public class code {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long t = sc.nextLong();
while (t-- > 0) {
int n=sc.nextInt();
int B=sc.nextInt();
int x=sc.nextInt();
int y= sc.nextInt();
ArrayList<Integer> a=new ArrayList<>();
a.add(0);
for (int i = 1; i <=n; i++) {
int val=a.get(i-1)+x;
if(val<=B) a.add(val);
else a.add(a.get(i-1)-y);
}
long sum =0;
for (Integer e: a
) {
sum+=e;
}
System.out.println(sum);
}
}
}
| Java | ["3\n5 100 1 30\n7 1000000000 1000000000 1000000000\n4 1 7 3"] | 2 seconds | ["15\n4000000000\n-10"] | NoteIn the first test case, the optimal sequence $$$a$$$ is $$$[0, 1, 2, 3, 4, 5]$$$.In the second test case, the optimal sequence $$$a$$$ is $$$[0, 10^9, 0, 10^9, 0, 10^9, 0, 10^9]$$$.In the third test case, the optimal sequence $$$a$$$ is $$$[0, -3, -6, 1, -2]$$$. | Java 8 | standard input | [
"greedy"
] | 2c921093abf2c5963f5f0e96cd430456 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Next $$$t$$$ cases follow. The first and only line of each test case contains four integers $$$n$$$, $$$B$$$, $$$x$$$ and $$$y$$$ ($$$1 \le n \le 2 \cdot 10^5$$$; $$$1 \le B, x, y \le 10^9$$$). It's guaranteed that the total sum of $$$n$$$ doesn't exceed $$$2 \cdot 10^5$$$. | 800 | For each test case, print one integer — the maximum possible $$$\sum\limits_{i=0}^{n}{a_i}$$$. | standard output | |
PASSED | 42b8338409c4a5808afe7aa250d6f0d7 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | // package edu_125;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class DD{
public static void main(String[] args){
FastReader sc = new FastReader();
int t=1;
while(t-->0){
int n=sc.nextInt();
int C=sc.nextInt();
Pair a[]=new Pair[n];
long dp[]=new long[C+1];
for(int i=0;i<n;i++) {
int c=sc.nextInt();
long d=sc.nextLong();
long h=sc.nextLong();
long hd=(long)(h*d);
a[i]=new Pair(c,hd);
dp[c]=Math.max(dp[c], hd);
}
for(int cost=1;cost<=C;cost++) {
for(int newCost=cost;newCost<=C;newCost+=cost) {
dp[newCost]=Math.max(dp[newCost], dp[cost]*(newCost/cost));
}
}
for(int i=1;i<=C;i++)
dp[i]=(long)Math.max(dp[i-1], dp[i]);
// print(dp);
//next input monster
int m=sc.nextInt();
long ans[]= new long[m];int i=0;
while(m-->0) {
long b=sc.nextLong()*sc.nextLong();
// System.out.println(b);
ans[i++]=b_search(dp, b);
}
print(ans);
}
}
static int b_search(long dp[],long x) {
int l=0;int r=dp.length;
int up=-1;
while(l<r-1) {
int mid=(r+l)/2;
// System.out.println(mid);
if(dp[mid]>x) {
up=mid;
// System.out.println(mid+" ---");
r=mid-1;
}else l=mid;
}
if(up==-1)return -1;
return dp[r]>x?r:up;
}
static class sq {
int c;int d;int h;
sq(int c,int d,int h){
this.c=c;
this.d=d;
this.h=h;
}
}
static class Pair{
long c;long hd;
Pair(long c,long hd){
this.c=c;
this.hd=hd;
}
}
static class mons{
int d;long h;
mons(int d,long h){
this.d=d;
this.h=h;
}
}
static void sort(int a[]) {
ArrayList<Integer> aa= new ArrayList<>();
for(int i:a)aa.add(i);
Collections.sort(aa);
int j=0;
for(int i:aa)a[j++]=i;
}
static ArrayList<Integer> primeFac(int n){
ArrayList<Integer>ans = new ArrayList<Integer>();
int lp[]=new int [n+1];
Arrays.fill(lp, 0); //0-prime
for(int i=2;i<=n;i++) {
if(lp[i]==0) {
for(int j=i;j<=n;j+=i) {
if(lp[j]==0) lp[j]=i;
}
}
}
int fac=n;
while(fac>1) {
ans.add(lp[fac]);
fac=fac/lp[fac];
}
print(ans);
return ans;
}
static ArrayList<Long> prime_in_given_range(long l,long r){
ArrayList<Long> ans= new ArrayList<>();
int n=(int)Math.sqrt(r)+1;
int prime[]=sieve_of_Eratosthenes(n);
long res[]=new long [(int)(r-l)+1];
for(int i=0;i<=r-l;i++) {
res[i]=i+l;
}
for(int i=0;i<prime.length;i++) {
if(prime[i]==1) {
System.out.println(2);
for(int j=Math.max((int)i*i, (int)(l+i-1)/i*i);j<=r;j+=i) {
res[j-(int)l]=0;
}
}
}
for(long i:res) if(i!=0)ans.add(i);
return ans;
}
static int [] sieve_of_Eratosthenes(int n) {
int prime[]=new int [n];
Arrays.fill(prime, 1); // 1-prime | 0-not prime
prime[0]=prime[1]=0;
for(int i=2;i<n;i++) {
if(prime[i]==1) {
for(int j=i*i;j<n;j+=i) {
prime[j]=0;
}
}
}
return prime;
}
static long binpow(long a,long b) {
long res=1;
if(b==0)return a;
if(a==0)return 1;
while(b>0) {
if((b&1)==1) {
res*=a;
}
a*=a;
b>>=1;
}
return res;
}
static void print(int a[]) {
// System.out.println(a.length);
for(int i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long a[]) {
// System.out.println(a.length);
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(ArrayList<Integer> a) {
System.out.println(a.size());
for(long i:a) {
System.out.print(i+" ");
}
System.out.println();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
long [] fastArray(int n,long x) {
long a[]=new long [n];
for(int i=0;i<n;i++) {
a[i]=nextLong();
}
return a;
}
int [] fastArray(int n) {
int a[]=new int [n];
for(int i=0;i<n;i++) {
a[i]=nextInt();
}
return a;
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | b4ca523397e96f55fc103d163168ff88 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes |
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] tokens = in.readLine().split("\\s+");
int n = Integer.parseInt(tokens[0]);
int C = Integer.parseInt(tokens[1]);
//H*D < h*d*numUnits
//4, 3
//7, 15
//8, 6
//9, 4
//10, 12
//12, 9
//14, 30
//14, 30
//k*cost, k*h*d
Map<Integer, Long> unitValues = new HashMap<>();
for (int i = 0; i < n; i++) {
tokens = in.readLine().split("\\s+");
int cost = Integer.parseInt(tokens[0]);
long value = Long.parseLong(tokens[1]) * Long.parseLong(tokens[2]);
if (value > unitValues.getOrDefault(cost, 0L)) {
unitValues.put(cost, value);
}
}
long[] values = new long[C + 1];
for (int unitCost : unitValues.keySet()) {
for (int numUnits = 1; numUnits * unitCost <= C; numUnits++) {
int cost = numUnits * unitCost;
long value = numUnits * unitValues.get(unitCost);
values[cost] = Math.max(values[cost], value);
}
}
for (int i = 1; i <= C; i++) {
values[i] = Math.max(values[i], values[i-1]);
}
int m = Integer.parseInt(in.readLine());
for (int i = 0; i < m; i++) {
tokens = in.readLine().split("\\s+");
long damage = Long.parseLong(tokens[0]);
long health = Long.parseLong(tokens[1]);
sb.append(search(1, C, damage * health, values)).append(" ");
}
System.out.println(sb);
}
private static long search(int begin, int end, long val, long[] values) {
if (end < begin) return -1;
int mid = begin + (end - begin) / 2;
if (values[mid] > val) {
long left = search(begin, mid - 1, val, values);
return left > -1 ? left : mid;
}
return search(mid + 1, end, val, values);
}
}
| Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | d3e54d52f888738988e99402765c2882 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.Random;
import java.io.FileWriter;
import java.io.PrintWriter;
/*
Solution Created: 20:55:54 22/03/2022
Custom Competitive programming helper.
*/
public class Main {
public static void solve() throws IOException {
int n = in.nextInt(), c = in.nextInt();
long[] dp = new long[c+1];
long[] co = new long[c+1];
for(int i = 0; i<n; i++) {
int cost = in.nextInt();
co[cost] = Math.max(co[cost] , in.nextLong()*in.nextLong());
}
for(int i = 1; i<=c; i++) {
if(co[i] == 0) continue;
for(int j = 1; i*j<=c; j++) {
dp[i*j] = Math.max(dp[i*j], j*co[i]);
}
}
for(int i = 1; i<=c; i++) dp[i] = Math.max(dp[i], dp[i-1]);
int m = in.nextInt();
for(int i = 0; i<m; i++) {
long mcoef = in.nextLong()*in.nextLong();
int l = 0, r = c, an = -1;
while(l<=r) {
int md = (l+r)/2;
if(dp[md]>mcoef) {
an = md;
r = md-1;
}else l = md+1;
}
out.print(an+" ");
}
out.println();
}
public static void main(String[] args) throws IOException {
in = new Reader();
out = new Writer();
int t = 1;
while(t-->0) solve();
out.exit();
}
static Reader in; static Writer out;
static class Reader {
static BufferedReader br;
static StringTokenizer st;
public Reader() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(String f){
try {
this.br = new BufferedReader(new FileReader(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public double[] nd(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextDouble();
return a;
}
public long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
public char[] nca() {
return next().toCharArray();
}
public String[] ns(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) a[i] = next();
return a;
}
public int nextInt() {
ensureNext();
return Integer.parseInt(st.nextToken());
}
public double nextDouble() {
ensureNext();
return Double.parseDouble(st.nextToken());
}
public Long nextLong() {
ensureNext();
return Long.parseLong(st.nextToken());
}
public String next() {
ensureNext();
return st.nextToken();
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private void ensureNext() {
if (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static class Util{
private static Random random = new Random();
static long[] fact;
public static void initFactorial(int n, long mod) {
fact = new long[n+1];
fact[0] = 1;
for (int i = 1; i < n+1; i++) fact[i] = (fact[i - 1] * i) % mod;
}
public static long modInverse(long a, long MOD) {
long[] gcdE = gcdExtended(a, MOD);
if (gcdE[0] != 1) return -1; // Inverted doesn't exist
long x = gcdE[1];
return (x % MOD + MOD) % MOD;
}
public static long[] gcdExtended(long p, long q) {
if (q == 0) return new long[] { p, 1, 0 };
long[] vals = gcdExtended(q, p % q);
long tmp = vals[2];
vals[2] = vals[1] - (p / q) * vals[2];
vals[1] = tmp;
return vals;
}
public static long nCr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[r], MOD) % MOD * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nCr(int n, int r) {
return (fact[n]/fact[r])/fact[n-r];
}
public static long nPr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nPr(int n, int r) {
return fact[n]/fact[n-r];
}
public static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean[] getSieve(int n) {
boolean[] isPrime = new boolean[n+1];
for (int i = 2; i <= n; i++) isPrime[i] = true;
for (int i = 2; i*i <= n; i++) if (isPrime[i])
for (int j = i; i*j <= n; j++) isPrime[i*j] = false;
return isPrime;
}
static long pow(long x, long pow, long mod){
long res = 1;
x = x % mod;
if (x == 0) return 0;
while (pow > 0){
if ((pow & 1) != 0) res = (res * x) % mod;
pow >>= 1;
x = (x * x) % mod;
}
return res;
}
public static int gcd(int a, int b) {
int tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static long gcd(long a, long b) {
long tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static int random(int min, int max) {
return random.nextInt(max-min+1)+min;
}
public static void dbg(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static void reverse(int[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
int tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(int[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(long[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
long tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(long[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(float[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
float tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(float[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(double[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
double tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(double[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(char[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
char tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(char[] s) {
reverse(s, 0, s.length-1);
}
public static <T> void reverse(T[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
T tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static <T> void reverse(T[] s) {
reverse(s, 0, s.length-1);
}
public static void shuffle(int[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
int t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(long[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
long t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(float[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
float t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(double[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
double t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(char[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
char t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static <T> void shuffle(T[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
T t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void sortArray(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(float[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(double[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(char[] a) {
shuffle(a);
Arrays.sort(a);
}
public static <T extends Comparable<T>> void sortArray(T[] a) {
Arrays.sort(a);
}
public static int[][] rotate90(int[][] a){
int n = a.length, m = a[0].length;
int[][] ans = new int[m][n];
for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) ans[m-j-1][i] = a[i][j];
return ans;
}
public static char[][] rotate90(char[][] a){
int n = a.length, m = a[0].length;
char[][] ans = new char[m][n];
for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) ans[m-j-1][i] = a[i][j];
return ans;
}
}
static class Writer {
private PrintWriter pw;
public Writer(){
pw = new PrintWriter(System.out);
}
public Writer(String f){
try {
pw = new PrintWriter(new FileWriter(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public void yesNo(boolean condition) {
println(condition?"YES":"NO");
}
public void printArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void printArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void print(Object o) {
pw.print(o.toString());
}
public void println(Object o) {
pw.println(o.toString());
}
public void println() {
pw.println();
}
public void flush() {
pw.flush();
}
public void exit() {
pw.close();
}
}
}
| Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | 68b5bf02f3c87e058fc8dcb9cdaa7bc4 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution extends PrintWriter {
Solution() { super(System.out, true); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
Solution o = new Solution(); o.main(); o.flush();
}
static final long INF = 0x3f3f3f3f3f3f3f3fL;
void main() {
int n = sc.nextInt();
int c = sc.nextInt();
long[] dh = new long[c + 1];
while (n-- > 0) {
int a = sc.nextInt();
int d = sc.nextInt();
int h = sc.nextInt();
dh[a] = Math.max(dh[a], (long) d * h);
}
for (int a = c - 1; a > 0; a--) {
long x = dh[a];
if (x == 0)
continue;
for (int k = 2, b; (b = k * a) <= c; k++)
dh[b] = Math.max(dh[b], k * x);
}
for (int a = 2; a <= c; a++)
dh[a] = Math.max(dh[a], dh[a - 1]);
int m = sc.nextInt();
while (m-- > 0) {
int d = sc.nextInt();
long h = sc.nextLong();
long x = d * h;
int ans;
if (x >= dh[c])
ans = -1;
else {
int lower = 0, upper = c;
while (upper - lower > 1) {
int a = (lower + upper) / 2;
if (x < dh[a])
upper = a;
else
lower = a;
}
ans = upper;
}
print(ans + " ");
}
println();
}
}
| Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | c7ac1430d13ae963784d0637e5a93c4d | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.util.*;
import java.io.*;
public class D {
static FastScanner fs = new FastScanner();
static PrintWriter pw = new PrintWriter(System.out);
static StringBuilder sb = new StringBuilder("");
public static void main(String[] args) {
int n = fs.nextInt();
int C = fs.nextInt();
long[] f = new long[C+1];
long[] g = new long[C+1];
for (int i = 0; i < n; i++) {
int c = fs.nextInt();
long d = fs.nextLong();
long h = fs.nextLong();
f[c] = Math.max(f[c], d * h);
}
for (int i = 1; i <= C; i++) {
for (int j = 1; i * j <= C; j++) {
g[i * j] = Math.max(g[i * j], f[i] * j);
}
}
for (int i = 1; i <= C; i++) g[i] = Math.max(g[i], g[i-1]);
int q = fs.nextInt();
while(q-->0) {
long D = fs.nextLong();
long H = fs.nextLong();
if (g[C] <= D * H) sb.append("-1 ");
else {
int lo = 0, hi = C;
while (hi - lo > 1) {
int mid = (lo + hi) >> 1;
if (g[mid] > D * H) hi = mid;
else lo = mid;
}
sb.append(hi + " ");
}
}
pw.print(sb.toString());
pw.close();
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {while (!st.hasMoreTokens()) try {st = new StringTokenizer(br.readLine());} catch (IOException e) {}return st.nextToken();}
int nextInt() {return Integer.parseInt(next());}
long nextLong() {return Long.parseLong(next());}
double nextDouble() {return Double.parseDouble(next());}
}
} | Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | c8d86dd3d816baf6097ca8a0f52d7490 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.security.cert.X509CRL;
import java.util.*;
import java.lang.*;
import java.util.stream.Collector;
import java.util.stream.Collectors;
@SuppressWarnings("unused")
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static String OUTPUT = "";
//global
private final static long BASE = 998244353L;
private final static int ALPHABET = (int)('z') - (int)('a') + 1;
//private final static int BASE = 1000000007;
private final static int INF_I = (1<<31)-1;
private final static long INF_L = (1l<<63)-1;
private final static int MAXN = 100100;
private final static int MAXK = 31;
static void solve() {
int ntest = 1;
for (int test=0;test<ntest;test++) {
int N = readInt(), C = readInt();
long[] maxCost = new long[C+1];
boolean[] been = new boolean[C+1];
for (int i=0;i<N;i++) {
int m = readInt();
int a = readInt();
int h = readInt();
maxCost[m] = Math.max(maxCost[m], 1l*a*h);
been[m]=true;
}
for (int i=1;i<=C;i++) {
if (!been[i]) continue;
for (int k=1;i*k<=C;k++) {
maxCost[i*k] = Math.max(maxCost[i*k], maxCost[i] * 1l*k);
}
}
for (int i=1;i<=C;i++) maxCost[i] = Math.max(maxCost[i], maxCost[i-1]);
//for (int i=1;i<=C;i++) out.println(i + " " +maxCost[i]);
int M = readInt();
for (int i=0;i<M;i++) {
int A = readInt();
long H = readLong();
int lo=0, hi=C;
int ans=-1;
while (lo <= hi) {
int mid = (lo+hi)/2;
if (maxCost[mid] > 1l*A*H) {
ans = mid;
hi = mid-1;
} else {
lo = mid+1;
}
}
out.print(ans + " ");
}
}
}
public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
if (INPUT=="") {
is = System.in;
} else {
File file = new File(INPUT);
is = new FileInputStream(file);
}
if (OUTPUT == "") out = new PrintWriter(System.out);
else out = new PrintWriter(OUTPUT);
solve();
out.flush();
long G = System.currentTimeMillis();
}
private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> {
private T x;
private T y;
public Point(T x, T y) {
this.x = x;
this.y = y;
}
public T getX() {return x;}
public T getY() {return y;}
@Override
public int compareTo(Point<T> o) {
int cmp = x.compareTo(o.getX());
if (cmp==0) return y.compareTo(o.getY());
return cmp;
}
}
private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> {
public ClassComparator() {}
@Override
public int compare(T a, T b) {
return a.compareTo(b);
}
}
private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> {
public ListComparator() {}
@Override
public int compare(List<T> o1, List<T> o2) {
for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) {
int c = o1.get(i).compareTo(o2.get(i));
if (c != 0) {
return c;
}
}
return Integer.compare(o1.size(), o2.size());
}
}
private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
// private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double readDouble() { return Double.parseDouble(readString()); }
private static char readChar() { return (char)skip(); }
private static String readString()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] readChar(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] readTable(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = readChar(m);
return map;
}
private static int[] readIntArray(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = readInt();
return a;
}
private static long[] readLongArray(int n) {
long[] a = new long[n];
for (int i=0;i<n;i++) a[i] = readLong();
return a;
}
private static int readInt()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static long readLong()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
} | Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | 9b0855de1010b119f5148a990c18100b | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class TaskD {
public static void main(String[] args) {
final Scanner in = new Scanner(System.in);
new TaskD().solve(in);
}
private void solve(final Scanner in) {
final int n = in.nextInt();
final int C = in.nextInt();
final long[] bestPowerAtCost = new long[C + 1];
Arrays.fill(bestPowerAtCost, 0L);
for (int i = 1; i <= n; i++) {
final int getCost = in.nextInt();
bestPowerAtCost[getCost] = Math.max(bestPowerAtCost[getCost], in.nextLong() * in.nextLong());
}
for (int i = 1; i <= C; i++) {
for (int j = i + i; j <= C; j += i) {
bestPowerAtCost[j] = Math.max(bestPowerAtCost[j], (j / i) * bestPowerAtCost[i]);
}
}
for (int i = 1; i <= C; i++) {
bestPowerAtCost[i] = Math.max(bestPowerAtCost[i], bestPowerAtCost[i - 1]);
}
final int m = in.nextInt();
for (int i = 1; i <= m; i++) {
final long enemyPower = in.nextLong() * in.nextLong();
System.out.print(binarySearch(bestPowerAtCost, enemyPower) + " ");
}
System.out.println();
}
private long binarySearch(final long[] bestPowerAtCost, final long enemyPower) {
int res = -1;
int l = 1;
int r = bestPowerAtCost.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (bestPowerAtCost[mid] > enemyPower) {
r = mid - 1;
res = mid;
} else {
l = mid + 1;
}
}
return res;
}
}
| Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | 715ccc2e4cb18208e66eaa22f413c0b0 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class TaskD {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
new TaskD().solve(in);
}
private void solve(Scanner in) {
int n = in.nextInt();
final int C = in.nextInt();
long[] bestPowerAtCost = new long[C + 1];
Arrays.fill(bestPowerAtCost, 0L);
for (int i = 1; i <= n; i++) {
int getCost = in.nextInt();
bestPowerAtCost[getCost] = Math.max(bestPowerAtCost[getCost], in.nextLong() * in.nextLong());
}
for (int i = 1; i <= C; i++) {
for (int j = i + i; j <= C; j += i) {
bestPowerAtCost[j] = Math.max(bestPowerAtCost[j], (j / i) * bestPowerAtCost[i]);
}
}
for (int i = 1; i <= C; i++) {
bestPowerAtCost[i] = Math.max(bestPowerAtCost[i], bestPowerAtCost[i - 1]);
}
int m = in.nextInt();
for (int i = 1; i <= m; i++) {
long enemyPower = in.nextLong() * in.nextLong();
System.out.print(binarySearch(bestPowerAtCost, enemyPower) + " ");
}
System.out.println();
}
private long binarySearch(long[] bestPowerAtCost, long enemyPower) {
int res = -1;
int l = 1;
int r = bestPowerAtCost.length - 1;
while (l <= r) {
int mid = l + (r - l) / 2;
if (bestPowerAtCost[mid] > enemyPower) {
r = mid - 1;
res = mid;
} else {
l = mid + 1;
}
}
return res;
}
}
| Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | 14fe6af96374b43be2e0e7333b47b84a | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.io.*;
import java.lang.Math;
import java.math.BigInteger;
import java.nio.*;
import java.util.*;
public final class code {
static int tot;
static class sortCond implements Comparator<Pair<Integer, Pair<Long, Long>>> {
@Override
public int compare(
Pair<Integer, Pair<Long, Long>> p1,
Pair<Integer, Pair<Long, Long>> p2
) {
if (p1.b.b <= p2.b.b) {
return -1;
} else {
return 1;
}
}
}
static class sortCond1
implements Comparator<Pair<Integer, Pair<Long, Long>>> {
@Override
public int compare(
Pair<Integer, Pair<Long, Long>> p1,
Pair<Integer, Pair<Long, Long>> p2
) {
if (p1.b.a <= p2.b.a) {
return -1;
} else {
return 1;
}
}
}
static class Pair<f, s> {
f a;
s b;
Pair(f a, s b) {
this.a = a;
this.b = b;
}
}
interface modOperations {
int mod(int a, int b, int mod);
}
static int findBinaryExponentian(int a, int pow, int mod) {
if (pow == 1) {
return a;
} else if (pow == 0) {
return 1;
} else {
int retVal = findBinaryExponentian(a, (int) pow / 2, mod);
return modMul.mod(
modMul.mod(retVal, retVal, mod),
(pow % 2 == 0) ? 1 : a,
mod
);
}
}
static int findPow(int a, int b, int mod) {
if (b == 1) {
return a % mod;
} else if (b == 0) {
return 1;
} else {
int res = findPow(a, (int) b / 2, mod);
return modMul.mod(res, modMul.mod(res, (b % 2 == 1 ? a : 1), mod), mod);
}
}
static int bleft(long ele, ArrayList<Long> sortedArr) {
int l = 0;
int h = sortedArr.size() - 1;
int ans = -1;
while (l <= h) {
int mid = l + (int) (h - l) / 2;
if (sortedArr.get(mid) < ele) {
l = mid + 1;
} else if (sortedArr.get(mid) >= ele) {
ans = mid;
h = mid - 1;
}
}
return ans;
}
static long gcd(long a, long b) {
long div = b;
long rem = a % b;
while (rem != 0) {
long temp = rem;
rem = div % rem;
div = temp;
}
return div;
}
static int log(int no) {
int i = 0;
if (no == 0) {
return 0;
}
while ((1 << i) <= no) {
i += 1;
}
return (1 << (i - 1)) == no ? i - 1 : i;
}
static modOperations modAdd = (int a, int b, int mod) -> {
return (a % mod + b % mod) % mod;
};
static modOperations modSub = (int a, int b, int mod) -> {
return (a % mod - b % mod + mod) % mod;
};
static modOperations modMul = (int a, int b, int mod) -> {
return (int) (((1l * a % mod * b % mod)) % mod);
};
static modOperations modDiv = (int a, int b, int mod) -> {
return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod);
};
static HashSet<Integer> primeList(int MAXI) {
int[] prime = new int[MAXI + 1];
HashSet<Integer> obj = new HashSet<>();
for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) {
if (prime[i] == 0) {
obj.add(i);
for (int j = i * i; j <= MAXI; j += i) {
prime[j] = 1;
}
}
}
for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) {
if (prime[i] == 0) {
obj.add(i);
}
}
return obj;
}
static int[] factorialList(int MAXI, int mod) {
int[] factorial = new int[MAXI + 1];
factorial[0] = 0 % mod;
factorial[1] = 1 % mod;
for (int i = 2; i < MAXI + 1; i++) {
factorial[i] = modMul.mod(factorial[i - 1], i, mod);
}
return factorial;
}
static void put(HashMap<Integer, Integer> cnt, int key) {
if (cnt.containsKey(key)) {
cnt.replace(key, cnt.get(key) + 1);
} else {
cnt.put(key, 1);
}
}
static long arrSum(ArrayList<Long> arr) {
long tot = 0;
for (int i = 0; i < arr.size(); i++) {
tot += arr.get(i);
}
return tot;
}
static int ord(char b) {
return (int) b - (int) 'a';
}
static int optimSearch(int[] cnt, int lower_bound, int pow, int n) {
int l = lower_bound + 1;
int h = n;
int ans = 0;
while (l <= h) {
int mid = l + (h - l) / 2;
if (cnt[mid] - cnt[lower_bound] == pow) {
return mid;
} else if (cnt[mid] - cnt[lower_bound] < pow) {
ans = mid;
l = mid + 1;
} else {
h = mid - 1;
}
}
return ans;
}
static int factorList(
HashMap<Integer, Integer> maps,
int no,
int maxK,
int req
) {
int i = 1;
while (i * i <= no) {
if (no % i == 0) {
if (i != no / i) {
put(maps, no / i);
}
put(maps, i);
if (maps.get(i) == req) {
maxK = Math.max(maxK, i);
}
if (maps.get(no / i) == req) {
maxK = Math.max(maxK, no / i);
}
}
i++;
}
return maxK;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n, c, q, i, j, a, b, c1;
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
long cost[] = new long[c + 1];
for (i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
c1 = Integer.parseInt(st.nextToken());
cost[a] = Math.max(cost[a], 1l * b * c1);
}
for (i = c - 1; i > 0; i--) {
if (cost[i] == 0) {
continue;
}
for (j = 2; j * i < c + 1; j++) {
cost[i * j] = Math.max(cost[i * j], 1l * j * cost[i]);
}
}
for (j = 2; j < c + 1; j++) {
cost[j] = Math.max(cost[j], cost[j - 1]);
}
q = Integer.parseInt(br.readLine());
while (q-- > 0) {
st = new StringTokenizer(br.readLine());
a = Integer.parseInt(st.nextToken());
long b1 = Long.parseLong(st.nextToken());
if (1l * a * b1 >= cost[c]) {
bw.write("-1 ");
} else {
int l = 0;
int h = c;
int ans = -1;
while (l <= h) {
int mid = l + (h - l) / 2;
if (cost[mid] > 1l * a * b1) {
ans = mid;
h = mid - 1;
} else {
l = mid + 1;
}
}
bw.write(Integer.toString(ans) + " ");
}
}
bw.write("\n");
bw.flush();
}
}
| Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | 011c7f3affeeee5bcc92e2fbe142d006 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.io.*;
import java.lang.Math;
import java.math.BigInteger;
import java.nio.*;
import java.util.*;
public final class code {
static int tot;
static class sortCond implements Comparator<Pair<Integer, Pair<Long, Long>>> {
@Override
public int compare(
Pair<Integer, Pair<Long, Long>> p1,
Pair<Integer, Pair<Long, Long>> p2
) {
if (p1.b.b <= p2.b.b) {
return -1;
} else {
return 1;
}
}
}
static class sortCond1
implements Comparator<Pair<Integer, Pair<Long, Long>>> {
@Override
public int compare(
Pair<Integer, Pair<Long, Long>> p1,
Pair<Integer, Pair<Long, Long>> p2
) {
if (p1.b.a <= p2.b.a) {
return -1;
} else {
return 1;
}
}
}
static class Pair<f, s> {
f a;
s b;
Pair(f a, s b) {
this.a = a;
this.b = b;
}
}
interface modOperations {
int mod(int a, int b, int mod);
}
static int findBinaryExponentian(int a, int pow, int mod) {
if (pow == 1) {
return a;
} else if (pow == 0) {
return 1;
} else {
int retVal = findBinaryExponentian(a, (int) pow / 2, mod);
return modMul.mod(
modMul.mod(retVal, retVal, mod),
(pow % 2 == 0) ? 1 : a,
mod
);
}
}
static int findPow(int a, int b, int mod) {
if (b == 1) {
return a % mod;
} else if (b == 0) {
return 1;
} else {
int res = findPow(a, (int) b / 2, mod);
return modMul.mod(res, modMul.mod(res, (b % 2 == 1 ? a : 1), mod), mod);
}
}
static int bleft(long ele, ArrayList<Long> sortedArr) {
int l = 0;
int h = sortedArr.size() - 1;
int ans = -1;
while (l <= h) {
int mid = l + (int) (h - l) / 2;
if (sortedArr.get(mid) < ele) {
l = mid + 1;
} else if (sortedArr.get(mid) >= ele) {
ans = mid;
h = mid - 1;
}
}
return ans;
}
static long gcd(long a, long b) {
long div = b;
long rem = a % b;
while (rem != 0) {
long temp = rem;
rem = div % rem;
div = temp;
}
return div;
}
static int log(int no) {
int i = 0;
if (no == 0) {
return 0;
}
while ((1 << i) <= no) {
i += 1;
}
return (1 << (i - 1)) == no ? i - 1 : i;
}
static modOperations modAdd = (int a, int b, int mod) -> {
return (a % mod + b % mod) % mod;
};
static modOperations modSub = (int a, int b, int mod) -> {
return (a % mod - b % mod + mod) % mod;
};
static modOperations modMul = (int a, int b, int mod) -> {
return (int) (((1l * a % mod * b % mod)) % mod);
};
static modOperations modDiv = (int a, int b, int mod) -> {
return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod);
};
static HashSet<Integer> primeList(int MAXI) {
int[] prime = new int[MAXI + 1];
HashSet<Integer> obj = new HashSet<>();
for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) {
if (prime[i] == 0) {
obj.add(i);
for (int j = i * i; j <= MAXI; j += i) {
prime[j] = 1;
}
}
}
for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) {
if (prime[i] == 0) {
obj.add(i);
}
}
return obj;
}
static int[] factorialList(int MAXI, int mod) {
int[] factorial = new int[MAXI + 1];
factorial[0] = 0 % mod;
factorial[1] = 1 % mod;
for (int i = 2; i < MAXI + 1; i++) {
factorial[i] = modMul.mod(factorial[i - 1], i, mod);
}
return factorial;
}
static void put(HashMap<Integer, Integer> cnt, int key) {
if (cnt.containsKey(key)) {
cnt.replace(key, cnt.get(key) + 1);
} else {
cnt.put(key, 1);
}
}
static long arrSum(ArrayList<Long> arr) {
long tot = 0;
for (int i = 0; i < arr.size(); i++) {
tot += arr.get(i);
}
return tot;
}
static int ord(char b) {
return (int) b - (int) 'a';
}
static int optimSearch(int[] cnt, int lower_bound, int pow, int n) {
int l = lower_bound + 1;
int h = n;
int ans = 0;
while (l <= h) {
int mid = l + (h - l) / 2;
if (cnt[mid] - cnt[lower_bound] == pow) {
return mid;
} else if (cnt[mid] - cnt[lower_bound] < pow) {
ans = mid;
l = mid + 1;
} else {
h = mid - 1;
}
}
return ans;
}
static int factorList(
HashMap<Integer, Integer> maps,
int no,
int maxK,
int req
) {
int i = 1;
while (i * i <= no) {
if (no % i == 0) {
if (i != no / i) {
put(maps, no / i);
}
put(maps, i);
if (maps.get(i) == req) {
maxK = Math.max(maxK, i);
}
if (maps.get(no / i) == req) {
maxK = Math.max(maxK, no / i);
}
}
i++;
}
return maxK;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n, c, i, j, a, b, c1, q;
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
c = Integer.parseInt(st.nextToken());
long cost[] = new long[c + 1];
for (i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
a = Integer.parseInt(st.nextToken());
b = Integer.parseInt(st.nextToken());
c1 = Integer.parseInt(st.nextToken());
cost[a] = Math.max(cost[a], 1l * b * c1);
}
for (i = c - 1; i > 0; i--) {
if (cost[i] == 0) {
continue;
}
for (j = 2; j * i < c + 1; j++) {
//System.out.println(i + " " + j) ;
cost[j * i] = Math.max(cost[j * i], 1l * j * cost[i]);
}
}
for (i = 2; i < c + 1; i++) {
cost[i] = Math.max(cost[i], cost[i - 1]);
}
//System.out.println(cost[c]);
q = Integer.parseInt(br.readLine());
for (i = 0; i < q; i++) {
st = new StringTokenizer(br.readLine());
a = Integer.parseInt(st.nextToken());
long b1 = Long.parseLong(st.nextToken());
if (1l * a * b1 > cost[c]) {
bw.write("-1 ");
} else {
int l = 0;
int h = c;
int ans = -1;
while (l <= h) {
int mid = l + (h - l) / 2;
if (cost[mid] > 1l * a * b1) {
ans = mid;
h = mid - 1;
} else {
l = mid + 1;
}
}
bw.write(Integer.toString(ans) + " ");
}
}
bw.write("\n");
bw.flush();
}
}
| Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | 3790cf3816ae17f690023f94b5da31c2 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class C1657D {
static class Point {
long a;
int b;
public Point(long a, int b) {
this.a = a;
this.b = b;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int C = Integer.parseInt(st.nextToken());
long[] a = new long[C+1];
for (int i=0; i < n; i++){
st = new StringTokenizer(br.readLine());
int c = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
int h = Integer.parseInt(st.nextToken());
a[c] = Math.max(a[c], (long)d*h);
}
long[] max = new long[C+1];
for (int i=1; i <= C; i++){
for (int j=1; j <= C/i; j++){
max[i*j] = Math.max(max[i*j], a[i]*j);
}
}
for (int i=1; i <= C; i++){
max[i] = Math.max(max[i], max[i-1]);
}
int m = Integer.parseInt(br.readLine());
List<Point> l = new ArrayList<>();
for (int i=0; i <= C; i++){
l.add(new Point (max[i], i));
}
for (int i=0; i < m; i++){
st = new StringTokenizer(br.readLine());
long D = Long.parseLong(st.nextToken());
long H = Long.parseLong(st.nextToken());
long tot = D*H;
int ans = Collections.binarySearch(l, new Point(tot+1, -1), new Comparator<Point>() {
@Override
public int compare(Point point, Point t1) {
if (point.a == t1.a){
return point.b-t1.b;
}
return Long.compare(point.a, t1.a);
}
});
if (ans < 0){
ans = -(ans+1);
}
if (ans > C){
ans = -1;
}
if (i == 0){
System.out.print(ans);
}
else {
System.out.print(" " + ans);
}
}
}
}
| Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | 244819039f0825c220c758ea4167c062 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.io.*;
import java.util.*;
public class Solution {
static int mod=(int)1e9+7;
static double epsilon = 0.000001d;
static long h[],add[];
public static void main(String[] args) {
Copied io = new Copied(System.in, System.out);
// int k=1;
int t = 1;
// t = io.nextInt();
for (int i = 0; i < t; i++) {
// io.print("Case #" + k + ": ");
solve(io);
// k++;
}
io.close();
}
public static void solve(Copied io) {
int n=io.nextInt(),c=io.nextInt();
long maxhd[]=new long[c+1];
for(int i=0;i<n;i++){
int cost=io.nextInt(),d=io.nextInt(),h=io.nextInt();
long a=(long)h*d;
maxhd[cost]=max(maxhd[cost],a);
}
for(int i=1;i<=c;i++){
long cur=max(maxhd[i],maxhd[i-1]);
long k=cur;
for(int j=i;j<=c;j+=i,cur+=k){
maxhd[j]=max(maxhd[j],cur);
}
}
int m=io.nextInt();
StringBuilder sb=new StringBuilder();
for(int i=0;i<m;i++){
long md=io.nextLong(),mh=io.nextLong();
long chk=md*mh;
int lo=0,hi=c,ans=Integer.MAX_VALUE;
while(hi>=lo){
int mid=(hi+lo)/2;
if(maxhd[mid]>chk){
hi=mid-1;
ans=min(ans,mid);
}
else{
lo=mid+1;
}
}
if(ans==Integer.MAX_VALUE){
sb.append(-1+" ");
}
else{
sb.append(ans+" ");
}
}
io.println(sb);
}
static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static void binaryOutput(boolean ans, Copied io){
String a=new String("YES"), b=new String("NO");
if(ans){
io.println(a);
}
else{
io.println(b);
}
}
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void printArr(int[] arr,Copied io) {
for (int x : arr)
io.print(x + " ");
io.println();
}
static void printArr(long[] arr,Copied io) {
for (long x : arr)
io.print(x + " ");
io.println();
}
static int[] listToInt(ArrayList<Integer> a){
int n=a.size();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=a.get(i);
}
return arr;
}
static int mod_mul(int a, int b){ a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;}
static int mod_add(int a, int b){ a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;}
static int mod_sub(int a, int b){ a = a % mod; b = b % mod; return (((a - b) % mod) + mod) % mod;}
}
class stringPres{
StringBuilder s;
int pres;
stringPres(){
s=new StringBuilder();
pres=0;
}
}
// class Pair implements Cloneable, Comparable<Pair>
// {
// int x,y;
// Pair(int a,int b)
// {
// this.x=a;
// this.y=b;
// }
// @Override
// public boolean equals(Object obj)
// {
// if(obj instanceof Pair)
// {
// Pair p=(Pair)obj;
// return p.x==this.x && p.y==this.y;
// }
// return false;
// }
// @Override
// public int hashCode()
// {
// return Math.abs(x)+101*Math.abs(y);
// }
// @Override
// public String toString()
// {
// return "("+x+" "+y+")";
// }
// @Override
// protected Pair clone() throws CloneNotSupportedException {
// return new Pair(this.x,this.y);
// }
// @Override
// public int compareTo(Pair a)
// {
// int t= this.x-a.x;
// if(t!=0)
// return t;
// else
// return this.y-a.y;
// }
// }
// class byY implements Comparator<Pair>{
// // @Override
// public int compare(Pair o1,Pair o2){
// // -1 if want to swap and (1,0) otherwise.
// int t= o1.y-o2.y;
// if(t!=0)
// return t;
// else
// return o1.x-o2.x;
// }
// }
// class byX implements Comparator<Pair>{
// // @Override
// public int compare(Pair o1,Pair o2){
// // -1 if want to swap and (1,0) otherwise.
// int t= o1.x-o2.x;
// if(t!=0)
// return t;
// else
// return o1.y-o2.y;
// }
// }
// class DescendingOrder<T> implements Comparator<T>{
// @Override
// public int compare(Object o1,Object o2){
// // -1 if want to swap and (1,0) otherwise.
// int addingNumber=(Integer) o1,existingNumber=(Integer) o2;
// if(addingNumber>existingNumber) return -1;
// else if(addingNumber<existingNumber) return 1;
// else return 0;
// }
// }
class Copied extends PrintWriter {
public Copied(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Copied(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
} | Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | 29c2b33538218d3b60955d5603f86264 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.io.*;
import java.util.*;
public class Solution {
static int mod=(int)1e9+7;
static double epsilon = 0.000001d;
static long h[],add[];
public static void main(String[] args) {
Copied io = new Copied(System.in, System.out);
// int k=1;
int t = 1;
// t = io.nextInt();
for (int i = 0; i < t; i++) {
// io.print("Case #" + k + ": ");
solve(io);
// k++;
}
io.close();
}
public static void solve(Copied io) {
int n=io.nextInt(),c=io.nextInt();
long maxhd[]=new long[c+1];
// for(int i=1;i<=c;i++){
// maxCost[i]=Integer.M_VALUE;
// }
for(int i=0;i<n;i++){
int cost=io.nextInt(),d=io.nextInt(),h=io.nextInt();
long a=(long)h*d;
maxhd[cost]=max(maxhd[cost],a);
// io.println(a);
}
for(int i=1;i<=c;i++){
long cur=max(maxhd[i],maxhd[i-1]);
long k=cur;
for(int j=i;j<=c;j+=i,cur+=k){
maxhd[j]=max(maxhd[j],cur);
}
}
// long cur=0;
// for(int i=1;i<=c;i++){
// cur=max(cur,maxhd[i]);
// maxhd[i]=cur;
// }
// printArr(maxhd, io);
// int maxInC[]=new int[c+1];
// double curMax=0;
// for(int i=1;i<=c;i++){
// curMax=max(curMax,maxCost[i]);
// maxCost[i]=curMax;
// double k=maxCost[i]*i;
// }
int m=io.nextInt();
StringBuilder sb=new StringBuilder();
for(int i=0;i<m;i++){
long md=io.nextLong(),mh=io.nextLong();
long chk=md*mh;
int lo=0,hi=c,ans=Integer.MAX_VALUE;
while(hi>=lo){
int mid=(hi+lo)/2;
if(maxhd[mid]>chk){
hi=mid-1;
ans=min(ans,mid);
}
else{
lo=mid+1;
}
}
if(ans==Integer.MAX_VALUE){
sb.append(-1+" ");
}
else{
sb.append(ans+" ");
}
}
io.println(sb);
}
static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static void binaryOutput(boolean ans, Copied io){
String a=new String("YES"), b=new String("NO");
if(ans){
io.println(a);
}
else{
io.println(b);
}
}
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void printArr(int[] arr,Copied io) {
for (int x : arr)
io.print(x + " ");
io.println();
}
static void printArr(long[] arr,Copied io) {
for (long x : arr)
io.print(x + " ");
io.println();
}
static int[] listToInt(ArrayList<Integer> a){
int n=a.size();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=a.get(i);
}
return arr;
}
static int mod_mul(int a, int b){ a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;}
static int mod_add(int a, int b){ a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;}
static int mod_sub(int a, int b){ a = a % mod; b = b % mod; return (((a - b) % mod) + mod) % mod;}
}
class stringPres{
StringBuilder s;
int pres;
stringPres(){
s=new StringBuilder();
pres=0;
}
}
// class Pair implements Cloneable, Comparable<Pair>
// {
// int x,y;
// Pair(int a,int b)
// {
// this.x=a;
// this.y=b;
// }
// @Override
// public boolean equals(Object obj)
// {
// if(obj instanceof Pair)
// {
// Pair p=(Pair)obj;
// return p.x==this.x && p.y==this.y;
// }
// return false;
// }
// @Override
// public int hashCode()
// {
// return Math.abs(x)+101*Math.abs(y);
// }
// @Override
// public String toString()
// {
// return "("+x+" "+y+")";
// }
// @Override
// protected Pair clone() throws CloneNotSupportedException {
// return new Pair(this.x,this.y);
// }
// @Override
// public int compareTo(Pair a)
// {
// int t= this.x-a.x;
// if(t!=0)
// return t;
// else
// return this.y-a.y;
// }
// }
// class byY implements Comparator<Pair>{
// // @Override
// public int compare(Pair o1,Pair o2){
// // -1 if want to swap and (1,0) otherwise.
// int t= o1.y-o2.y;
// if(t!=0)
// return t;
// else
// return o1.x-o2.x;
// }
// }
// class byX implements Comparator<Pair>{
// // @Override
// public int compare(Pair o1,Pair o2){
// // -1 if want to swap and (1,0) otherwise.
// int t= o1.x-o2.x;
// if(t!=0)
// return t;
// else
// return o1.y-o2.y;
// }
// }
// class DescendingOrder<T> implements Comparator<T>{
// @Override
// public int compare(Object o1,Object o2){
// // -1 if want to swap and (1,0) otherwise.
// int addingNumber=(Integer) o1,existingNumber=(Integer) o2;
// if(addingNumber>existingNumber) return -1;
// else if(addingNumber<existingNumber) return 1;
// else return 0;
// }
// }
class Copied extends PrintWriter {
public Copied(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Copied(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
} | Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | 5539e0b5e997af71b4e8895b0bb7e56f | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.io.*;
import java.util.*;
public class Solution {
static int mod=(int)1e9+7;
static double epsilon = 0.000001d;
static long h[],add[];
public static void main(String[] args) {
Copied io = new Copied(System.in, System.out);
// int k=1;
int t = 1;
// t = io.nextInt();
for (int i = 0; i < t; i++) {
// io.print("Case #" + k + ": ");
solve(io);
// k++;
}
io.close();
}
public static void solve(Copied io) {
int n=io.nextInt(),c=io.nextInt();
long maxhd[]=new long[c+1];
HashMap<Integer,Long> mp=new HashMap<>();
// for(int i=1;i<=c;i++){
// maxCost[i]=Integer.M_VALUE;
// }
for(int i=0;i<n;i++){
int cost=io.nextInt(),d=io.nextInt(),h=io.nextInt();
long a=(long)h*d;
if(mp.containsKey(cost)){
mp.compute(cost, (k,v)-> v=max(v,a));
}
else{
mp.put(cost,a);
}
// io.println(a);
}
for(int i=1;i<=c;i++){
if(mp.containsKey(i)){
long cur=mp.get(i),k=mp.get(i);
for(int j=i;j<=c;j+=i,cur+=k){
maxhd[j]=max(maxhd[j],cur);
}
}
}
long cur=0;
for(int i=1;i<=c;i++){
cur=max(cur,maxhd[i]);
maxhd[i]=cur;
}
// printArr(maxhd, io);
// int maxInC[]=new int[c+1];
// double curMax=0;
// for(int i=1;i<=c;i++){
// curMax=max(curMax,maxCost[i]);
// maxCost[i]=curMax;
// double k=maxCost[i]*i;
// }
int m=io.nextInt();
StringBuilder sb=new StringBuilder();
for(int i=0;i<m;i++){
long md=io.nextLong(),mh=io.nextLong();
long chk=md*mh;
int lo=0,hi=c,ans=Integer.MAX_VALUE;
while(hi>=lo){
int mid=(hi+lo)/2;
if(maxhd[mid]>chk){
hi=mid-1;
ans=min(ans,mid);
}
else{
lo=mid+1;
}
}
if(ans==Integer.MAX_VALUE){
sb.append(-1+" ");
}
else{
sb.append(ans+" ");
}
}
io.println(sb);
}
static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
static void binaryOutput(boolean ans, Copied io){
String a=new String("YES"), b=new String("NO");
if(ans){
io.println(a);
}
else{
io.println(b);
}
}
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % p;
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
static void sort(int[] a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a)
l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static void printArr(int[] arr,Copied io) {
for (int x : arr)
io.print(x + " ");
io.println();
}
static void printArr(long[] arr,Copied io) {
for (long x : arr)
io.print(x + " ");
io.println();
}
static int[] listToInt(ArrayList<Integer> a){
int n=a.size();
int arr[]=new int[n];
for(int i=0;i<n;i++){
arr[i]=a.get(i);
}
return arr;
}
static int mod_mul(int a, int b){ a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;}
static int mod_add(int a, int b){ a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;}
static int mod_sub(int a, int b){ a = a % mod; b = b % mod; return (((a - b) % mod) + mod) % mod;}
}
class stringPres{
StringBuilder s;
int pres;
stringPres(){
s=new StringBuilder();
pres=0;
}
}
// class Pair implements Cloneable, Comparable<Pair>
// {
// int x,y;
// Pair(int a,int b)
// {
// this.x=a;
// this.y=b;
// }
// @Override
// public boolean equals(Object obj)
// {
// if(obj instanceof Pair)
// {
// Pair p=(Pair)obj;
// return p.x==this.x && p.y==this.y;
// }
// return false;
// }
// @Override
// public int hashCode()
// {
// return Math.abs(x)+101*Math.abs(y);
// }
// @Override
// public String toString()
// {
// return "("+x+" "+y+")";
// }
// @Override
// protected Pair clone() throws CloneNotSupportedException {
// return new Pair(this.x,this.y);
// }
// @Override
// public int compareTo(Pair a)
// {
// int t= this.x-a.x;
// if(t!=0)
// return t;
// else
// return this.y-a.y;
// }
// }
// class byY implements Comparator<Pair>{
// // @Override
// public int compare(Pair o1,Pair o2){
// // -1 if want to swap and (1,0) otherwise.
// int t= o1.y-o2.y;
// if(t!=0)
// return t;
// else
// return o1.x-o2.x;
// }
// }
// class byX implements Comparator<Pair>{
// // @Override
// public int compare(Pair o1,Pair o2){
// // -1 if want to swap and (1,0) otherwise.
// int t= o1.x-o2.x;
// if(t!=0)
// return t;
// else
// return o1.y-o2.y;
// }
// }
// class DescendingOrder<T> implements Comparator<T>{
// @Override
// public int compare(Object o1,Object o2){
// // -1 if want to swap and (1,0) otherwise.
// int addingNumber=(Integer) o1,existingNumber=(Integer) o2;
// if(addingNumber>existingNumber) return -1;
// else if(addingNumber<existingNumber) return 1;
// else return 0;
// }
// }
class Copied extends PrintWriter {
public Copied(InputStream i) {
super(new BufferedOutputStream(System.out));
r = new BufferedReader(new InputStreamReader(i));
}
public Copied(InputStream i, OutputStream o) {
super(new BufferedOutputStream(o));
r = new BufferedReader(new InputStreamReader(i));
}
public boolean hasMoreTokens() {
return peekToken() != null;
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public String next() {
return nextToken();
}
public double[] nextDoubles(int N) {
double[] res = new double[N];
for (int i = 0; i < N; i++) {
res[i] = nextDouble();
}
return res;
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
private BufferedReader r;
private String line;
private StringTokenizer st;
private String token;
private String peekToken() {
if (token == null)
try {
while (st == null || !st.hasMoreTokens()) {
line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
private String nextToken() {
String ans = peekToken();
token = null;
return ans;
}
} | Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | ef3607c15bf21d955d4e4b3226f4d0b2 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.BigDecimal;
import java.math.*;
//
public class Main{
private static int[] input() {
int n=in.nextInt();
int[]arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=in.nextInt();
}
return arr;
}
private static int[]input(int n){
int []arr=new int[n];
for(int i=0;i<n;i++) {
arr[i]=in.nextInt();
}
return arr;
}
private static void printArr(int[][]arr) {
int n=arr.length;int m=arr[0].length;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
print(arr[i][j]+" ");
}
print("\n");
}
}
public static void main(String[] args) {
TaskA solver = new TaskA();
// int t = in.nextInt();
// for (int i = 1; i <= t ; i++) {
// solver.solve(t, in, out);
// }
solver.solve(1, in, out);
out.flush();
out.close();
}
static ArrayList<Integer>[]graph;static int[]arr;
static long[][] dp;
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n= in.nextInt();int C= in.nextInt();
long []bst=new long[C+1];
for(int i=0;i<n;i++) {
int c= in.nextInt();int d= in.nextInt();int h= in.nextInt();
bst[c]=Math.max(bst[c], (long)d*h);
}
for(int c=1;c<=C;c++) {
for(int xc=c;xc<=C;xc+=c) {
bst[xc]=Math.max(bst[xc],bst[c]*(xc/c) );
}
}
for(int c=0;c<C;c++) {
bst[c+1]=Math.max(bst[c+1],bst[c]);
}
int m= in.nextInt();
for(int j=0;j<m;j++) {
int D= in.nextInt();
long H= in.nextLong();
int ind=ub(bst,(long)D*H);
if(ind<0) {
print(-1+" ");
}
else {
print(ind+" ");
}
}
print("\n");
}
}
public static int ub(long[]arr,long target) {
int start=0;
int end=arr.length-1;
int ans=-1;
while(start<=end) {
int mid=(start+end)/2;
if(arr[mid]<=target) {
start=mid+1;
}
else {
ans=mid;
end=mid-1;
}
}
return ans;
}
public static int index(Pair[]p,int x) {
int start=0;int end=p.length-1;
int ans=-1;
while(start<=end) {
int mid=(start+end)/2;
if(p[mid].second<=x) {
start=mid+1;
}
else {
ans=mid;
end=mid-1;
}
}
return ans;
}
public boolean rec(long x,HashMap<Long,Long> h) {
if(h.containsKey(x)) {
long v=h.get(x);
if(v==1) {
h.remove(x);
}
else {
h.put(x,v-1);
}
return true;
}
if(x==0) {
return false;
}
else if(h.containsKey(x/2)&&h.containsKey(ceil(x,2))) {
long v=h.get(x/2);long v2=h.get(ceil(x,2));
if(v==1) {
h.remove(x/2);
}
else {
h.put(x/2,v-1);
}
if(v2==1) {
h.remove(ceil(x,2));
}
else {
h.put(x/2,v-1);
}
return true;
}
else if(h.containsKey(x/2)) {
long v=h.get(x/2);
if(v==1) {
h.remove(x/2);
}
else {
h.put(x/2,v-1);
}
return rec(ceil(x,2),h);
}
else if(h.containsKey(ceil(x,2))) {
long v=h.get(ceil(x,2));
if(v==1) {
h.remove(ceil(x,2));
}
else {
h.put(ceil(x,2),v-1);
}
return true;
}
else {
return rec(x/2,h)&rec(ceil(x,2),h);
}
}
public static int querry(int i,int j) {
System.out. println("? "+i+" "+j);System.out.flush();
int x=in.nextInt();
return x;
}
public static void printAns(int[]arr) {
System.out.print("! ");
for(int i=0;i<arr.length;i++) {
System .out.print(arr[i]+" ");
}
System .out.print("\n");System.out.flush();
}
public static int fun(int x,int a) {
return x/a+x%a;
}
public static int findMin(int arr[], int n)
{
// Compute total sum of elements
int sumTotal = 0;
for (int i = 0; i < n; i++)
sumTotal += arr[i];
// Compute result using recursive function
return findMinRec(arr, n, 0, sumTotal);
}
public static int findMinRec(int arr[], int i,
int sumCalculated,
int sumTotal)
{
// If we have reached last element.
// Sum of one subset is sumCalculated,
// sum of other subset is sumTotal-
// sumCalculated. Return absolute
// difference of two sums.
if (i == 0)
return Math.abs((sumTotal - sumCalculated)
- sumCalculated);
// For every item arr[i], we have two choices
// (1) We do not include it first set
// (2) We include it in first set
// We return minimum of two choices
return Math.min(
findMinRec(arr, i - 1,
sumCalculated + arr[i - 1],
sumTotal),
findMinRec(arr, i - 1, sumCalculated,
sumTotal));
}
static ArrayList<Long> Divisors(long n){
ArrayList<Long>a=new ArrayList<>();
a.add(1L);a.add(n);
for (long i=2; i<=Math.sqrt(n); i++){
if(n%i==0) {
a.add(i);a.add(n/i);
}
}
return a;
}
static long setBitNumber(long n)
{
if (n == 0)
return 0;
int msb = 0;
n = n / 2;
while (n != 0) {
n = n / 2;
msb++;
}
return (1 << msb);
}
private static int opp(int x) {
if(x==1||x==8||x==0) {return x;}
else if(x==2) {return 5;}
{return 2;}
}
public static int getFirstSetBitPos(int n)
{
return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1;
}
public static long rec(int i,int j,int[]arr) {
if(arr.length==1) {return 0;}
if(dp[i][j]!=0) {return dp[i][j];}
else if(j-i==1) {dp[i][j]=arr[j]-arr[i];return dp[i][j];}
else {
dp[i][j]=Math.min(rec(i+1,j,arr),rec(i,j-1,arr))+arr[j]-arr[i];
return dp[i][j];
}
}
private static boolean isPalindrome(String s) {
StringBuilder sb=new StringBuilder(s);
if(sb.reverse().toString().equals(s))
return true;
return false;
}
private static void dfs(int root,Pair[]p,int parent,int[][]dp) {
int ans1=0;int ans2=0;
for(int i:graph[root]) {
if(i!=parent) {
dfs(i,p,root,dp);
}
}
for(int i:graph[root]) {
if(i!=parent) {
int val1=Math.abs(p[root].first-p[i].first)+dp[i][0];
int val2=Math.abs(p[root].first-p[i].second)+dp[i][1];
ans1+=Math.max(val1, val2);
}
}
for(int i:graph[root]) {
if(i!=parent) {
int val1=Math.abs(p[root].second-p[i].first)+dp[i][0];
int val2=Math.abs(p[root].second-p[i].second)+dp[i][1];
ans2+=Math.max(val1, val2);
}
}
dp[root][0]=ans1;dp[root][1]=ans2;
}
private static void printArr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
print(arr[i] + " ");
}
print("\n");
}
private static String rev(String s) {
String st="";
for(int i=s.length()-1;i>=0;i--) {
st+=s.charAt(i);
}
return st;
}
private static int[] rev(int[]arr) {
int[]a=arr.clone();
for(int i=0;i<arr.length;i++) {
a[i]=arr[arr.length-i-1];
}
return arr;
}
// for (Map.Entry<Integer,ArrayList<Integer>> entry : hm.entrySet()) {
// ArrayList<Integer>a=entry.getValue();
//
// }
static long ceil(long a,long b) {
return (a/b + Math.min(a%b, 1));
}
static long pow(long b, long e) {
long ans = 1;
while (e > 0) {
if (e % 2 == 1)
ans = ans * b % mod;
e >>= 1;
b = b * b % mod;
}
return ans;
}
static void sortDiff(Pair arr[])
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{ if(p1.first==p2.first) {
return (p1.second-p1.first)-(p2.second-p1.first);
}
return (p1.second-p1.first)-(p2.second-p2.first);
}
});
}
static long[] fac;
static long mod = (long) 1000000007;
static void initFac(long n) {
fac = new long[(int)n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++) {
fac[i] = (fac[i - 1] * i) % mod;
}
}
static long nck(int n, int k) {
if (n < k)
return 0;
long den = inv((int) (fac[k] * fac[n - k] % mod));
return fac[n] * den % mod;
}
static long inv(long x) {
return pow(x, mod - 2);
}
static void sort(int[] a) {
ArrayList<Integer> q = new ArrayList<>();
for (int i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
static void sort(long[] a) {
ArrayList<Long> q = new ArrayList<>();
for (long i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
public static boolean contains(int[][]arr,int d,int rcnum,int rc) {
if(rc==0) {
for(int j=0;j<arr.length;j++) {
if(arr[rcnum][j]==d) {
return true;
}
}
}
else {
for(int j=0;j<arr.length;j++) {
if(arr[j][rcnum]==d) {
return true;
}
}
}
return false;
}
public static int bfsSize(int source,LinkedList<Integer>[]a,boolean[]visited) {
Queue<Integer>q=new LinkedList<>();
q.add(source);
visited[source]=true;
int distance=0;
while(!q.isEmpty()) {
int curr=q.poll();
distance++;
for(int neighbour:a[curr]) {
if(!visited[neighbour]) {
visited[neighbour]=true;
q.add(neighbour);
}
}
}
return distance;
}
public static Set<Integer>factors(int n){
Set<Integer>ans=new HashSet<>();
ans.add(1);
for(int i=2;i*i<=n;i++) {
if(n%i==0) {
ans.add(i);
ans.add(n/i);
}
}
return ans;
}
public static int bfsSp(int source,int destination,ArrayList<Integer>[]a) {
boolean[]visited=new boolean[a.length];
int[]parent=new int[a.length];
Queue<Integer>q=new LinkedList<>();
int distance=0;
q.add(source);
visited[source]=true;
parent[source]=-1;
while(!q.isEmpty()) {
int curr=q.poll();
if(curr==destination) {
break;
}
for(int neighbour:a[curr]) {
if(neighbour!=-1&&!visited[neighbour]) {
visited[neighbour]=true;
q.add(neighbour);
parent[neighbour]=curr;
}
}
}
int cur=destination;
while(parent[cur]!=-1) {
distance++;
cur=parent[cur];
}
return distance;
}
static int bs(int size,int[]arr) {
int x = -1;
for (int b = size/2; b >= 1; b /= 2) {
while (!ok(arr));
}
int k = x+1;
return k;
}
static boolean ok(int[]x) {
return false;
}
public static int solve1(ArrayList<Integer> A) {
long[]arr =new long[A.size()+1];
int n=A.size();
for(int i=1;i<=A.size();i++) {
arr[i]=((i%2)*((n-i+1)%2))%2;
arr[i]%=2;
}
int ans=0;
for(int i=0;i<A.size();i++) {
if(arr[i+1]==1) {
ans^=A.get(i);
}
}
return ans;
}
public static String printBinary(long a) {
String str="";
for(int i=31;i>=0;i--) {
if((a&(1<<i))!=0) {
str+=1;
}
if((a&(1<<i))==0 && !str.isEmpty()) {
str+=0;
}
}
return str;
}
public static String reverse(long a) {
long rev=0;
String str="";
int x=(int)(Math.log(a)/Math.log(2))+1;
for(int i=0;i<32;i++) {
rev<<=1;
if((a&(1<<i))!=0) {
rev|=1;
str+=1;
}
else {
str+=0;
}
}
return str;
}
////////////////////////////////////////////////////////
static void sortF(Pair arr[])
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{ if(p1.first==p2.first) {
return p1.second-p2.second;
}
return p1.first - p2.first;
}
});
}
static void sortS(Pair arr[])
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{ if(p1.second==p2.second) {
return p1.first-p2.first;
}
return p1.second - p2.second;
}
});
}
static class Pair implements Comparable<Pair>
{
int first ;int second ;
public Pair(int x, int y)
{
this.first = x ;this.second = y ;
}
@Override
public boolean equals(Object obj)
{
if(obj == this)return true ;
if(obj == null)return false ;
if(this.getClass() != obj.getClass()) return false ;
Pair other = (Pair)(obj) ;
if(this.first != other.first)return false ;
if(this.second != other.second)return false ;
return true ;
}
@Override
public int hashCode()
{
return this.first^this.second ;
}
@Override
public String toString() {
String ans = "" ;ans += this.first ; ans += " "; ans += this.second ;
return ans ;
}
@Override
public int compareTo(Main.Pair o) {
{ if(this.first==o.first) {
return this.second-o.second;
}
return this.first - o.first;
}
}
}
//////////////////////////////////////////////////////////////////////////
static int nD(long num) {
String s=String.valueOf(num);
return s.length();
}
static int CommonDigits(int x,int y) {
String s=String.valueOf(x);
String s2=String.valueOf(y);
return 0;
}
static int lcs(String str1, String str2, int m, int n)
{
int L[][] = new int[m + 1][n + 1];
int i, j;
// Following steps build L[m+1][n+1] in
// bottom up fashion. Note that L[i][j]
// contains length of LCS of str1[0..i-1]
// and str2[0..j-1]
for (i = 0; i <= m; i++) {
for (j = 0; j <= n; j++) {
if (i == 0 || j == 0)
L[i][j] = 0;
else if (str1.charAt(i - 1)
== str2.charAt(j - 1))
L[i][j] = L[i - 1][j - 1] + 1;
else
L[i][j] = Math.max(L[i - 1][j],
L[i][j - 1]);
}
}
// L[m][n] contains length of LCS
// for X[0..n-1] and Y[0..m-1]
return L[m][n];
}
/////////////////////////////////
boolean IsPowerOfTwo(int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}
////////////////////////////////
static long power(long a,long b,long m ) {
long ans=1;
while(b>0) {
if(b%2==1) {
ans=((ans%m)*(a%m))%m; b--;
}
else {
a=(a*a)%m;b/=2;
}
}
return ans%m;
}
///////////////////////////////
public static boolean repeatedSubString(String string) {
return ((string + string).indexOf(string, 1) != string.length());
}
static int search(char[]c,int start,int end,char x) {
for(int i=start;i<end;i++) {
if(c[i]==x) {return i;}
}
return -2;
}
////////////////////////////////
static int gcd(int a, int b) {
while (b != 0) {
int t = b;
b = a % b;
a = t;
}
return a;
}
static long fac(long a)
{
if(a== 0L || a==1L)return 1L ;
return a*fac(a-1L) ;
}
static ArrayList al() {
ArrayList<Integer>a =new ArrayList<>();
return a;
}
static HashSet h() {
return new HashSet<Integer>();
}
static void debug(long[][]a) {
int n= a.length;
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[0].length;j++) {
out.print(a[i][j]+" ");
}
out.print("\n");
}
}
static void debug(int[]a) {
out.println(Arrays.toString(a));
}
static void debug(ArrayList<Integer>a) {
out.println(a.toString());
}
static boolean[]seive(int n){
boolean[]b=new boolean[n+1];
for (int i = 2; i <= n; i++)
b[i] = true;
for(int i=2;i*i<=n;i++) {
if(b[i]) {
for(int j=i*i;j<=n;j+=i) {
b[j]=false;
}
}
}
return b;
}
static int longestIncreasingSubseq(int[]arr) {
int[]sizes=new int[arr.length];
Arrays.fill(sizes, 1);
int max=1;
for(int i=1;i<arr.length;i++) {
for(int j=0;j<i;j++) {
if(arr[j]<arr[i]) {
sizes[i]=Math.max(sizes[i],sizes[j]+1);
max=Math.max(max, sizes[i]);
}
}
}
return max;
}
public static ArrayList primeFactors(long n)
{
ArrayList<Long>h= new ArrayList<>();
// Print the number of 2s that divide n
if(n%2 ==0) {h.add(2L);}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (long i = 3; i <= Math.sqrt(n); i+= 2)
{
if(n%i==0) {h.add(i);}
}
if (n > 2)
h.add(n);
return h;
}
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
public static void superSet(int[]a,ArrayList<String>al,int i,String s) {
if(i==a.length) {
al.add(s);
return;
}
superSet(a,al,i+1,s);
superSet(a,al,i+1,s+a[i]+" ");
}
public static long[] makeArr() {
long size=in.nextInt();
long []arr=new long[(int)size];
for(int i=0;i<size;i++) {
arr[i]=in.nextInt();
}
return arr;
}
public static long[] arr(int n) {
long []arr=new long[n+1];
for(int i=1;i<n+1;i++) {
arr[i]=in.nextLong();
}
return arr;
}
public static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void println(long x) {
out.println(x);
}
static void print(long c) {
out.print(c);
}
static void print(int c) {
out.print(c);
}
static void println(int x) {
out.println(x);
}
static void print(String s) {
out.print(s);
}
static void println(String s) {
out.println(s);
}
public static void merge(long arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long[n1];
long R[] = new long[n2];
//Copy data to temp arrays
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
public static void reverse(int[] array)
{
int n = array.length;
for (int i = 0; i < n / 2; i++) {
int temp = array[i];
array[i] = array[n - i - 1];
array[n - i - 1] = temp;
}
}
public static long nCr(int n,int k)
{
long ans=1L;
k=k>n-k?n-k:k;
for( int j=1;j<=k;j++,n--)
{
if(n%j==0)
{
ans*=n/j;
}else
if(ans%j==0)
{
ans=ans/j*n;
}else
{
ans=(ans*n)/j;
}
}
return ans;
}
static int searchMax(int index,long[]inp) {
while(index+1<inp.length &&inp[index+1]>inp[index]) {
index+=1;
}
return index;
}
static int searchMin(int index,long[]inp) {
while(index+1<inp.length &&inp[index+1]<inp[index]) {
index+=1;
}
return index;
}
public class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
static class Pairr implements Comparable<Pairr>{
private int index;
private int cumsum;
private ArrayList<Integer>indices;
public Pairr(int index,int cumsum) {
this.index=index;
this.cumsum=cumsum;
indices=new ArrayList<Integer>();
}
public int compareTo(Pairr other) {
return Integer.compare(cumsum, other.cumsum);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
} | Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | fc4b17d42b3cf33de5399081080be758 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.io.*;
import java.util.*;
/**
* A simple template for competitive programming problems.
*/
public class Solution {
//InputReader in = new InputReader("input.txt");
final InputReader in = new InputReader(System.in);
final PrintWriter out = new PrintWriter(System.out);
static final int mod = 1000000007;
Map<Integer, Long> uns;
void solve() {
int n = in.nextInt(); int C = in.nextInt();
uns = new TreeMap<>();
for(int i=0; i<n; i++) {
int c = in.nextInt();
long d = in.nextLong();
long h = in.nextLong();
long cur = uns.getOrDefault(c, 0L);
if(d*h > cur) {
uns.put(c, d*h);
}
}
long[] bests = new long[C+1];
for(int c : uns.keySet()) {
long x = uns.get(c);
int lim = C/c;
for(int k=1; k<=lim; k++) {
bests[c*k] = Math.max(bests[c*k], x*k);
}
}
long maxSoFar = bests[0];
for(int i=0; i<=C; i++) {
if(bests[i]>maxSoFar) {
maxSoFar = bests[i];
}
bests[i] = maxSoFar;
}
int m = in.nextInt();
while(m-->0) {
long target = in.nextLong()*in.nextLong();
int lo = 0;
int hi = C+1;
while(lo<hi) {
int mi = (lo+hi)/2;
if(bests[mi]>target) {
hi = mi;
} else {
lo = mi+1;
}
}
out.print((lo==C+1 ? -1 : lo) + " ");
}
out.println();
}
private long bestWithCostK(int mi) {
long best = 0;
for(int c : uns.keySet()) {
long x = uns.get(c);
long cand = mi/c*x;
if(cand>best) {
best = cand;
}
}
return best;
}
public static void main(final String[] args) throws FileNotFoundException {
final Solution s = new Solution();
final Long t1 = System.currentTimeMillis();
s.solve();
System.err.println(System.currentTimeMillis() - t1 + " ms");
s.out.close();
}
public Solution() throws FileNotFoundException {
}
private static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
Random r = new Random();
InputReader(final InputStream stream) {
this.stream = stream;
}
InputReader(final String fileName) {
InputStream stream = null;
try {
stream = new FileInputStream(fileName);
} catch (final FileNotFoundException e) {
e.printStackTrace();
}
this.stream = stream;
}
int[] nextArray(final int n) {
final int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
int[][] nextMatrix(final int n, final int m) {
final int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
matrix[i][j] = nextInt();
return matrix;
}
String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
final StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
final StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
int[] randomArray(int n, int low, int up) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = low + r.nextInt(up - low + 1);
}
return arr;
}
double nextDouble() {
return Double.parseDouble(nextString());
}
private int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (final IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
private boolean isSpaceChar(final int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(final int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
} | Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | f6a5e87846bf83b60facab9c9ba58f9d | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF1657D extends PrintWriter {
CF1657D() { super(System.out, true); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1657D o = new CF1657D(); o.main(); o.flush();
}
static final long INF = 0x3f3f3f3f3f3f3f3fL;
void main() {
int n = sc.nextInt();
int c = sc.nextInt();
long[] dh = new long[c + 1];
while (n-- > 0) {
int a = sc.nextInt();
int d = sc.nextInt();
int h = sc.nextInt();
dh[a] = Math.max(dh[a], (long) d * h);
}
for (int a = c - 1; a > 0; a--) {
long x = dh[a];
if (x == 0)
continue;
for (int k = 2, b; (b = k * a) <= c; k++)
dh[b] = Math.max(dh[b], k * x);
}
for (int a = 2; a <= c; a++)
dh[a] = Math.max(dh[a], dh[a - 1]);
int m = sc.nextInt();
while (m-- > 0) {
int d = sc.nextInt();
long h = sc.nextLong();
long x = d * h;
int ans;
if (x >= dh[c])
ans = -1;
else {
int lower = 0, upper = c;
while (upper - lower > 1) {
int a = (lower + upper) / 2;
if (x < dh[a])
upper = a;
else
lower = a;
}
ans = upper;
}
print(ans + " ");
}
println();
}
}
| Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output | |
PASSED | 82d4000af7b24c70c6b73d66704fbd97 | train_110.jsonl | 1647960300 | Monocarp is playing a strategy game. In the game, he recruits a squad to fight monsters. Before each battle, Monocarp has $$$C$$$ coins to spend on his squad.Before each battle starts, his squad is empty. Monocarp chooses one type of units and recruits no more units of that type than he can recruit with $$$C$$$ coins.There are $$$n$$$ types of units. Every unit type has three parameters: $$$c_i$$$ — the cost of recruiting one unit of the $$$i$$$-th type; $$$d_i$$$ — the damage that one unit of the $$$i$$$-th type deals in a second; $$$h_i$$$ — the amount of health of one unit of the $$$i$$$-th type. Monocarp has to face $$$m$$$ monsters. Every monster has two parameters: $$$D_j$$$ — the damage that the $$$j$$$-th monster deals in a second; $$$H_j$$$ — the amount of health the $$$j$$$-th monster has. Monocarp has to fight only the $$$j$$$-th monster during the $$$j$$$-th battle. He wants all his recruited units to stay alive. Both Monocarp's squad and the monster attack continuously (not once per second) and at the same time. Thus, Monocarp wins the battle if and only if his squad kills the monster strictly faster than the monster kills one of his units. The time is compared with no rounding.For each monster, Monocarp wants to know the minimum amount of coins he has to spend to kill that monster. If this amount is greater than $$$C$$$, then report that it's impossible to kill that monster. | 256 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = 1;
for (int t = 1; t <= test; t++) {
solve();
}
out.close();
}
private static void solve() {
int totalUnits = sc.nextInt();
int maxCoinsToSpend = sc.nextInt();
/*
Let hs -> the squad health, ds -> damage squad causes per second
and, hm -> the monster health, hs -> damage monster causes per second
Time needed to kill the monster = hm / ds
Time needed to kill the squad = hs / dm
To kill the monster, hm / ds < hs / dm or hm * dm < hs * ds
So we would spend our coins on squad such that its hs * ds is maximum.
*/
long[] dp = new long[maxCoinsToSpend + 1]; // dp[i] is the max hs * ds we can get by spending i coins.
for (int i = 0; i < totalUnits; i++) {
int cost = sc.nextInt();
long health = sc.nextLong();
long damage = sc.nextLong();
// buy ith type once
dp[cost] = Math.max(dp[cost], health * damage);
}
for (int i = 1; i <= maxCoinsToSpend; i++) {
for (int j = 1; i * j <= maxCoinsToSpend; j++) {
dp[i * j] = Math.max(dp[i * j], j * dp[i]);
}
}
for (int i = 1; i <= maxCoinsToSpend; i++) {
dp[i] = Math.max(dp[i], dp[i - 1]);
}
int m = sc.nextInt();
for (int i = 0; i < m; i++) {
long health = sc.nextLong();
long damage = sc.nextLong();
out.print(binarySearch(dp, maxCoinsToSpend, health * damage) + " ");
}
out.println();
}
private static int binarySearch(long[] dp, int n, long time) {
int low = 0, high = n;
int minCoins = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (dp[mid] > time) {
minCoins = mid;
high = mid - 1;
}else {
low = mid + 1;
}
}
return minCoins;
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
} | Java | ["3 10\n3 4 6\n5 5 5\n10 3 4\n3\n8 3\n5 4\n10 15", "5 15\n14 10 3\n9 2 2\n10 4 3\n7 3 5\n4 3 1\n6\n11 2\n1 1\n4 7\n2 1\n1 14\n3 3", "5 13\n13 1 9\n6 4 5\n12 18 4\n9 13 2\n5 4 5\n2\n16 3\n6 2"] | 4 seconds | ["5 3 -1", "14 4 14 4 7 7", "12 5"] | NoteConsider the first monster of the first example.Monocarp can't recruit one unit of the first type, because it will take both him and the monster $$$0.75$$$ seconds to kill each other. He can recruit two units for the cost of $$$6$$$ coins and kill the monster in $$$0.375$$$ second.Monocarp can recruit one unit of the second type, because he kills the monster in $$$0.6$$$ seconds, and the monster kills him in $$$0.625$$$ seconds. The unit is faster. Thus, $$$5$$$ coins is enough.Monocarp will need at least three units of the third type to kill the first monster, that will cost $$$30$$$ coins.Monocarp will spend the least coins if he chooses the second type of units and recruits one unit of that type. | Java 11 | standard input | [
"binary search",
"brute force",
"greedy",
"math",
"sortings"
] | 476c05915a1536cd989c4681c61c9deb | The first line contains two integers $$$n$$$ and $$$C$$$ ($$$1 \le n \le 3 \cdot 10^5$$$; $$$1 \le C \le 10^6$$$) — the number of types of units and the amount of coins Monocarp has before each battle. The $$$i$$$-th of the next $$$n$$$ lines contains three integers $$$c_i, d_i$$$ and $$$h_i$$$ ($$$1 \le c_i \le C$$$; $$$1 \le d_i, h_i \le 10^6$$$). The next line contains a single integer $$$m$$$ ($$$1 \le m \le 3 \cdot 10^5$$$) — the number of monsters that Monocarp has to face. The $$$j$$$-th of the next $$$m$$$ lines contains two integers $$$D_j$$$ and $$$H_j$$$ ($$$1 \le D_j \le 10^6$$$; $$$1 \le H_j \le 10^{12}$$$). | 2,000 | Print $$$m$$$ integers. For each monster, print the minimum amount of coins Monocarp has to spend to kill that monster. If this amount is greater than $$$C$$$, then print $$$-1$$$. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.