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
94e4ad88984ab39912c2e46a250902e6
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 void main(String[] args) { Scanner sc = new Scanner(System.in); int t =sc.nextInt(); while(t--!=0) { long c = 0; int n= sc.nextInt(); int b = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); long ans = 0; while(n--!=0) { if (c + x <= b) c += x; else c -= y; ans += c; } 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 11
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
f3666feb3e9bfe175b4cf08b972d43b8
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) 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 ans = 0; long a = 0; for(int i=0; i<n+1; i++) { ans = ans + a; if(a+x>b) { a = a - y; } else { a = a + x; } } 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 11
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
6b585856cb4402830b12fd7940d3ec00
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 { static Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws Exception { int tc = scanner.nextInt(); while (tc > 0) { int n = scanner.nextInt(); int limit = scanner.nextInt(); int toAdd = scanner.nextInt(); int toSub = scanner.nextInt(); System.out.println(solve(n, limit, toAdd, toSub)); tc --; } } static long solve(int n,int limit,int toAdd,int toSub) { long sum = 0; int lastElement = 0; for (int i=1; i<=n; i++) { if (lastElement + toAdd <= limit) lastElement += toAdd; else lastElement -= toSub; sum += lastElement; } return 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 11
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
17f90143dac039fa3810606b832b2faa
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(),B=in.nextInt(),x=in.nextInt(),y=in.nextInt(); long cur=0,res=0; while(n-->0){ if(cur+x<=B) cur+=x; else cur-=y; res+=cur; } System.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 11
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
00a0e301ae22caecc13dc39e87050bf2
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.lang.*; import java.util.*; public class exam { 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 arr[]=new int[n+1]; arr[0]=0; long sum=0; int b[]=new int[n+1]; for(int i=1;i<=n;i++) { int s=arr[i-1]+x; int q=arr[i-1]-y; if(s<=B || q<=B) { if(s>q && s<=B) { arr[i]=arr[i-1]+x; sum=sum+arr[i]; } else{ arr[i]=arr[i-1]-y;; sum=sum+arr[i]; } } else{ if(s>q) { arr[i]=arr[i-1]+x; i--; } else{ arr[i]=arr[i-1]-y; 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 11
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
74d856e464e50f0ce9faa39fdeb5013e
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 Solution{ 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(); } } static int[] string_to_array(String[] arr){ int[] ans=new int[arr.length]; for(int i=0;i<arr.length;i++){ ans[i]=Integer.parseInt(arr[i]); } return ans; } public static double distance(int x1,int x2){ return Math.sqrt(x1*x1+x2*x2); } public static void main(String[] args) { try { FastReader in=new FastReader(); FastWriter out = new FastWriter(); int testCases=in.nextInt(); List<String>answer=new ArrayList<>(); while(testCases-- > 0){ int[] inp=string_to_array(in.nextLine().split(" ")); int n,b,x,y; n=inp[0]; b=inp[1]; x=inp[2]; y=inp[3]; int[] a=new int[n+1]; a[0]=0; for(int i=1;i<n+1;i++){ boolean aa=a[i-1]+x<=b; boolean bb=a[i-1]-y<=b; if(aa && bb){ a[i]=Math.max(a[i-1]+x,a[i-1]-y); continue; }else if(aa){ a[i]=a[i-1]+x; continue; }else if(bb){ a[i]=a[i-1]-y; continue; } } double sum=0; for(int i=0;i<n+1;i++){ //out.println(a[i]); sum+=a[i]; } //out.println(sum); System.out.printf("%.0f\n",sum); //answer.add(Integer.parseInt(sum)); } for(String s:answer){ out.println(s); } 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 11
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
3c094a96194950eca6433f39bc80d8e5
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 Problem1657B { public static Scanner scanner = new Scanner(System.in); public static void main(String[] args) { int tc = scanner.nextInt(); while (tc-->0){ int n = scanner.nextInt(); int num = scanner.nextInt(); int x = scanner.nextInt(); int y = scanner.nextInt(); long sum =0; int cnt =0; while (n-->0){ if (cnt+x<=num){ cnt+=x; }else { cnt-=y; } sum+=cnt; } 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 11
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
5dc94851ab977659f78c6920e6e6e48a
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.math.BigInteger; import java.util.Scanner; 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(); BigInteger B = new BigInteger(sc.next()), x = new BigInteger(sc.next()), y = new BigInteger(sc.next()); BigInteger sum = new BigInteger("0"); BigInteger last = new BigInteger("0"); for (int i = 0; i < n; ++i) { last = last.add(x); if (last.compareTo(B) == 1) { last = last.subtract(x); last = last.subtract(y); } sum = sum.add(last); } System.out.println(sum); } sc.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 11
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
14d49da911fe16c7feed007ab504de0c
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 com.company; import java.io.DataInputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Random; public class CodeforcesEducationalRounds_125_B { @SuppressWarnings("FieldCanBeLocal") private static Reader in; private static PrintWriter out; private static int MinValue = 0; private static void solve() throws IOException{ //Your Code Goes Here; long n = in.nextLong(), m = in.nextLong(), o = in.nextLong(), p = in.nextLong(); long total = 0; long[] arr = new long[(int) n + 1]; arr[0] = MinValue; for(long i=1;i<=n;i++){ if(arr[(int)i-1] + o <= m){ arr[(int) i] = arr[(int) (i - 1)] + o; } else{ arr[(int) i] = arr[(int) (i - 1)] - p; } } for(int i=0;i<=n;i++){ total += arr[(int)i]; } System.out.print(total + "\n"); } public static void main(String[] args) throws IOException { //FastScaner fs = new FastScaner();//There is a class below .. uncomment that class to instantiate an object from that class; //int t = fs.nextInt();//uncomment this line when you're using the fastscaner class; in = new Reader(); out = new PrintWriter(new OutputStreamWriter(System.out)); int T = in.nextInt(); while(T-->0){ solve(); } out.flush(); in.close(); out.close(); } static final double INF = 1e18; static final Random random = new Random(); static final int mod = 1_000_000_007; //Taken From "Second Thread" static void ruffleSort(int[] a){ int n = a.length;//shuffles, then sort; for(int i=0; i<n; i++){ int oi = random.nextInt(n), temp = a[oi]; a[oi] = a[i]; a[i] = temp; } sort(a); } public static boolean primeFinder(int n){ int m = 0; int flag = 0; m = n / 2; if(n == 0 ||n == 1){ return false; } else{ for(int i=2;i<=m;i++){ if(n % i == 0){ return false; } } return true; } } private static boolean[] sieveOfEratosthenes(long n) { boolean prime[] = new boolean[(int)n + 1]; for (int i = 0; i <= n; i++) { prime[i] = true; } for (long p = 2; p * p <= n; p++) { if (prime[(int)p] == true) { for (long i = p * p; i <= n; i += p) prime[(int)i] = false; } } return prime; } private static long add(long a, long b){ return (a + b) % mod; } private static int gcd(int a, int b) { if (a == 0 || b == 0) return 0; while (b != 0) { int tmp; tmp = a % b; a = b; b = tmp; } return a; } private static long lcm(long a, long b){ return (a / gcd((int) a, (int) b) * b); } private 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); } public static int[][] prefixSum( int n , int m , int[][] arr){ int[][] prefixSum = new int[n+1][m+1]; for( int i = 1 ;i <= n ;i++) { for( int j = 1 ; j<= m ; j++) { int toadd = 0; if( arr[i-1][j-1] == 1) { toadd = 1; } prefixSum[i][j] = toadd + prefixSum[i][j-1] + prefixSum[i-1][j] - prefixSum[i-1][j-1]; } } return prefixSum; } private static int sumOfArrayElements(int[] arr){ int sum = 0; // Initialize sum ; //Iterate through all elements and add them to sum; for(int i=0;i<arr.length;i++){ sum += arr[i]; } return sum; } //call this method when you want to read an integer array; private static int[] readArray(int len) throws IOException{ int[] a = new int[len]; for(int i=0;i<len;i++)a[i] = in.nextInt(); return a; } //call this method when you want to read an Long array; private static long[] readLongArray(int len) throws IOException{ long[] a = new long[len]; for(int i=0;i<len;i++) a[i] = in.nextLong(); return a; } //call this method to print the integer array; private static void printArray(int[] array){ for(int now : array) out.print(now);out.print(' ');out.close(); } //call this method to print the long array; private static void printLongArray(long[] array){ for(long now:array) out.print(now); out.print(' '); out.close(); } /*Another way of Reading input...collected from a online blog from here => https://codeforces.com/contest/1625/submission/144334744;*/ static class Reader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; Reader() {//Constructor; din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String readLine() throws IOException {//To take user input for String values; final byte[] buf = new byte[1024]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { break; } buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextSign() throws IOException {//For taking the signs like plus or minus ... byte c = read(); while ('+' != c && '-' != c) { c = read(); } return '+' == c ? 0 : 1; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } public int skip() throws IOException { int b; // noinspection ALL while ((b = read()) != -1 && isSpaceChar(b)) { ; } return b; } public char nc() throws IOException { return (char) skip(); } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) { return -ret; } return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') { c = read(); } final boolean neg = c == '-'; if (neg) { c = read(); } do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) { return -ret; } return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) { buffer[0] = -1; } } private byte read() throws IOException { if (bufferPointer == bytesRead) { fillBuffer(); } return buffer[bufferPointer++]; } public void close() throws IOException { din.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 11
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
f1662fdaf9260b8c8cb9c81408ca0d2b
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.math.*; public class Solution{ public static final Scanner in= new Scanner(System.in); public static long Solve(int n, Long B, Long x, Long y){ long array[] = new long[n+1]; array[0]=0; for(int i=1; i<=n; i++){ if(array[i-1]+x<=B) array[i] = array[i-1]+x; else array[i] = array[i-1]-y; } long sum = 0; for(int i=0; i<=n; i++) sum += array[i]; return sum; } public static void main(String[] args){ int cases=in.nextInt(); int n; long B, x, y; for(int i=0; i<cases; i++){ n = in.nextInt(); B = in.nextLong(); x = in.nextLong(); y = in.nextLong(); System.out.println(Solve(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 11
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
32349a7a469e864ce848eafc11b95fa0
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.math.*; public class Solution{ public static final Scanner in= new Scanner(System.in); public static long Solve(int n, Long B, Long x, Long y){ long array[] = new long[n+1]; array[0]=0; for(int i=1; i<=n; i++){ if(array[i-1]+x<=B) array[i] = array[i-1]+x; else array[i] = array[i-1]-y; } long sum = 0; for(int i=0; i<=n; i++) sum += array[i]; return sum; } public static void main(String[] args){ int cases=in.nextInt(); int n; long B, x, y; for(int i=0; i<cases; i++){ n = in.nextInt(); B = in.nextLong(); x = in.nextLong(); y= in.nextLong(); System.out.println(Solve(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 11
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
23df45448f9c7d01f142e63f10b83f75
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 xysequence{ public static void main(String[] args) throws Exception{ 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(); int[] a=new int[n+1]; long sigma=0; for(int i=1;i<a.length;i++){ int optionA=Integer.MIN_VALUE; int optionB=Integer.MIN_VALUE; if(a[i-1]+x<=b) optionA=a[i-1]+x; if(a[i-1]-y<=b) optionB=a[i-1]-y; // System.out.println("option a is: "+optionA); // System.out.println("option b is: "+optionB); // System.out.println("we will add to sigma: "+Math.max(optionA,optionB)); sigma+=Math.max(optionA,optionB); // System.out.println("Sigma updated to "+sigma); // System.out.println(); a[i]=Math.max(optionA,optionB); } // System.out.println(); // System.out.println(); System.out.println(sigma); } } }
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 11
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
e3bbbbed12a1b0e73dfd7a09a8a1b396
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
// Java is like Alzheimer's, it starts off slow, but eventually, your memory is gone. import java.io.*; import java.util.*; public class Aqueous { static MyScanner sc = new MyScanner(); static PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) { int t =sc.nextInt(); while(t-->0) { int n = sc.nextInt(); int b = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); long num = 0; long sum = 0; for(int i=1; i<=n; i++) { //pw.print(num+" "); if(num+x>b) { sum = sum + (num-y); num = num-y; } else { sum = sum + (num+x); num += x; } } //pw.println(); pw.println(sum); } pw.close(); } static void ruffleSort(int a[]) { int n = a.length; Random r = new Random(); for (int i = 0; i < n; i++) { int oi = r.nextInt(n); int temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } static void ruffleSort(long a[]) { int n = a.length; Random r = new Random(); for (int i = 0; i < n; i++) { int oi = r.nextInt(n); long temp = a[oi]; a[oi] = a[i]; a[i] = temp; } Arrays.sort(a); } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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 11
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
32417108abb2651a2123dce91ac62681
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.StringTokenizer; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.lang.reflect.Array; import java.io.IOException; import java.io.BufferedReader; import java.io.BufferedWriter; import java.math.BigInteger; import java.util.Arrays; import java.util.Collections; import java.util.ArrayList; public class p2 { BufferedReader br; StringTokenizer st; BufferedWriter bw; public static void main(String[] args)throws Exception { new p2().run(); } void run()throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); bw=new BufferedWriter(new OutputStreamWriter(System.out)); solve(); } void solve() throws IOException { short t=ns(); while(t-->0) { int n=ni(),b=ni(),x=ni(),y=ni(); int a[]=new int[n+1]; for(int i=0;++i<n+1;) { 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=-1;++i<n+1;) sum+=a[i]; System.out.println(sum); } } public int gcd(int a, int b) { if(a==0) return b; else return gcd(b%a,a); } int[] nai(int n) { int a[]=new int[n]; for(int i=-1;++i<n;)a[i]=ni(); return a;} long[] nal(int n) { long a[]=new long[n]; for(int i=-1;++i<n;)a[i]=nl(); return a;} char[] nac() {char c[]=nextLine().toCharArray(); return c;} char [][] nmc(int n) {char c[][]=new char[n][]; for(int i=-1;++i<n;)c[i]=nac(); return c;} int[][] nmi(int r, int c) {int a[][]=new int[r][c]; for(int i=-1;++i<r;)a[i]=nai(c); return a;} String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } byte nb() { return Byte.parseByte(next()); } short ns() { return Short.parseShort(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public int[] primeNO(long limit) { int size=(int)Math.sqrt(limit)+1; int size1=size/2; boolean composite[]=new boolean[size1]; int zz=(int)Math.sqrt(size-1); for(int i=3;i<=zz;) { for(int j=(i*i)/2;j<size1;j+=i) composite[j]=true; for(i+=2;composite[i/2];i+=2); } int prime[]=new int[3401]; prime[0]=2; int p=1; for(int i=1;i<size1;i++) { if(!composite[i]) prime[p++]=2*i+1; } //////////////////////////////////////////////////////////////////////////////////////////////////////// // System.out.println(p); //////////////////////////////////////////////////////////////////////////////////////////////////////// prime=Arrays.copyOf(prime, p); return prime; } public static class queue { public static class node { node next; int val; public node(int val) { this.val=val; } } node head,tail; public void add(int val) { node a=new node(val); if(head==null) { head=tail=a; return; } tail.next=a; tail=a; } public int remove() { int a=head.val; head=head.next; if(head==null) tail=null; return a; } } public static class stack { public static class node { node next; int val; public node(int val) { this.val=val; } } node head; public void add(int val) { node a=new node(val); a.next=head; head=a; } public int remove() { int a=head.val; head=head.next; return a; } } public static class data { int a,b; public data(int a, int b) { this.a=a; this.b=b; } } public data[] dataArray(int n) { data d[]=new data[n]; for(int i=-1;++i<n;) d[i]=new data(ni(), ni()); return d; } }
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 11
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
b1cf5fb043f108ffd4d82674f9d0c9f9
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; /** * B_XY_Sequence */ public class B_XY_Sequence { 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 Obj = new FastReader(); int T = Obj.nextInt(); for (int i = 0; i < T; i++) { Long N, B, X, Y; N = Obj.nextLong(); B = Obj.nextLong(); X = Obj.nextLong(); Y = Obj.nextLong(); Long Sum = (long)0; Long Current_Sum = (long)0; for (int j = 1; j <= N; j++) { if (Current_Sum + X <= B) Current_Sum += X; else { Current_Sum -= Y; } Sum += Current_Sum; } 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 11
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
402692638dfd25cba0e61a45fc590783
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 codeforces { 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(); long sum=0; int arr[] = new int[n]; arr[0] = 0; int a=0; for (int k = 1; k <= n; k++) { if (a + x <= B) { a+=x; } else { a-=y; } sum+=a; } 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 11
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
29ae9591f086d85aaa683580fb27f933
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 Xyz { public static void main(String args[]) { long n,B,a,b,t,i,s=0,j,m=0; Scanner x=new Scanner(System.in); t=x.nextInt(); while(t>0) { t--; n=x.nextLong(); B=x.nextLong(); a=x.nextLong(); b=x.nextLong(); if(B>=a*n) { for(i=1;i<=n;i++) { s=s+a; m=m+s; } System.out.println(m); s=0; m=0; } else { for(j=1;j<=n;j++) { if(s+a<=B) s=s+a; else s=s-b; m=m+s; } System.out.println(m); s=0; m=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 11
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
5efca75e978abeabddaeb1a6d70afa3c
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) { // write your code here Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++) { // HashMap<Integer,Integer> m=new HashMap<>(); int n=sc.nextInt(); int b=sc.nextInt(); int x=sc.nextInt(); int y=sc.nextInt(); long sum=0; int ans=0; int[] a=new int[n+1]; a[0]=0; for(int j=1;j<=n;j++) { if(((a[j-1]+x)>(a[j-1]-y)) && (a[j-1]+x)<=b) { ans=(a[j-1]+x); //System.out.println(ans); } else { ans=(a[j-1]-y); //System.out.println(ans); } a[j]=ans; /* if(sum+x>sum-y ) { // System.out.println("True"); if(sum+x<=b) { sum+=ans+x; System.out.println(sum); ans=sum; } else { System.out.println("False"); sum=0; System.out.println(sum); } } /* else if(sum+x<sum-y ) { && sum-y<=b sum+=sum-y; } else { } */ } for(int j=0;j<=n;j++) { sum+=a[j]; } System.out.println(sum); //System.out.println(ans); /* if(a==0 && b==0) { System.out.println("0"); } else if(a==0 || b==0) { System.out.println("1"); } else { double x=Math.sqrt((a-0)*(a-0)+(b-0)*(b-0)); if (x == (int)x) { System.out.println("1"); } else { System.out.println("2"); } } } */ } } long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, 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 11
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
791a544c7454dafd8e21b070734c7c3e
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 B_XY_Sequence { static Scanner t = new Scanner(System.in); public static void kalingaisop() { int n = t.nextInt(); Long B = t.nextLong(); Long x = t.nextLong(); Long y = t.nextLong(); long gg[] = new long[n + 1]; gg[0] = 0; for (int kk = 1; kk <= n; kk++) { if (gg[kk - 1] + x <= B) gg[kk] = gg[kk - 1] + x; else gg[kk] = gg[kk - 1] - y; } long sum = 0; for (int r = 1; r <= n; r++) { sum += gg[r]; } System.out.println(sum); } public static void main(String[] args) throws java.lang.Exception { long tcs = t.nextLong(); while (tcs-- > 0) { kalingaisop(); } } }
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 11
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
6d43417c5aed513d521a359ee951ebb2
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 Solution { static long res(int n, int b, int x, int y){ long prev = 0; long curr ; long sum = 0; for(int i = 0; i<n; i++){ if(prev + x <= b){ curr = prev + x; }else{ curr = prev - y; } sum += curr; prev = curr; } return sum; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine().trim()); while(t-- != 0){ String[] line = br.readLine().trim().split(" "); int n = Integer.parseInt(line[0]); int b = Integer.parseInt(line[1]); int x = Integer.parseInt(line[2]); int y = Integer.parseInt(line[3]); long out = res(n, b, x, y); System.out.println(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 11
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
0a4fa6cb61fe7c6b9283ce45f4fac532
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 xyseqmar22 { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); for(int i = 0 ; i < t ; i++){ int n = scn.nextInt(); int B = scn.nextInt(); int x = scn.nextInt(); int y = scn.nextInt(); PossibleSequence(n,B,x,y); } } public static void PossibleSequence(int n , int B , int x , int y){ int[] arr = new int[n+1]; arr[0] = 0; long sum = 0; for(int i = 1 ; i < arr.length ; i++){ if(arr[i-1] + x > B){ arr[i] = arr[i-1] - y; } else{ arr[i] = arr[i-1] + x ; } } for(int i = 0 ; i < arr.length ; 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 11
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
51e7309fc13cb17c6102a7b418d3c9ec
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 C { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); PrintWriter pw = 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 sum = 0; int cur = 0; for (int i = 1; i <= n; i++) { if (cur + x <= b) { sum += cur + x; cur += x; } else { sum += cur - y; cur -= y; } } pw.println(sum); } pw.close(); } // --------------------sTufF ---------------------- 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(); } } }
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 11
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
5ae498aaaf00803cf099c6a84899acf1
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 Dd { 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 ans = 0L; Long pre = 0L; for(int i =1 ; i<=n ; i++){ long inc = Math.max(pre+x, pre-y); long dec = Math.min(pre+x, pre-y); if(inc <= B){ ans+=inc ; pre = inc; } else{ ans+=dec; pre= dec; } } System.out.println(ans); } sc.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 11
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
74202cd8cfc6e114974d19d8170d3807
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 sc = new Scanner(System.in); for (int t = sc.nextInt(); t > 0; t--) { int n = sc.nextInt(); int B = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); long sum = 0; int next = 0; for (int i = 1; i <= n; i++) { if (next + x > B) { next -= y; } else { next += x; } sum += next; } 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 11
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
0a1a790652f1c3a2a48a02ed6c97b13b
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 tp { static long max_sum(int n,long B,long x,long y) { long sum=0; long a=0; int i=0; while(i<=n) { sum+=a; i++; if(a+x>B) { a=a-y; } else { a=a+x; } } return sum; } public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); int[] n=new int[t]; long[] B=new long[t]; long[] x=new long[t]; long[] y=new long[t]; for(int i=0;i<t;i++) { n[i]=sc.nextInt(); B[i]=sc.nextLong(); x[i]=sc.nextLong(); y[i]=sc.nextLong(); } for(int i=0;i<t;i++) { System.out.println(max_sum(n[i],B[i],x[i],y[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 11
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
3306b70eed76144f7da842865dca3595
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 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; } } 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; int a = 0; for (int i = 0; i < n; i++) { if (a + x <= B) { a = a + x; sum = sum + a; } else { a = a - y; sum = sum + a; } } 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 11
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
d7c84a85df3fff8e44a947347d94cea0
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.io.PrintWriter; import java.util.Collections; import java.util.ArrayList; public class B { static final FastReader sc = new FastReader(); static final PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { int t = sc.nextInt(); while (t-- > 0) { int n = sc.nextInt(); int b = sc.nextInt(); int x = sc.nextInt(); int 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] - y; else a[i] = a[i - 1] + x; } long s = 0; for (int i = 1; i <= n; i++) { s += 1L * a[i]; } out.println(s); } out.close(); } static int[] sort(int[] arr) { ArrayList<Integer> a = new ArrayList<>(); for (int i : arr) { a.add(i); } Collections.sort(a); for (int i = 0; i < a.size(); i++) { arr[i] = a.get(i); } return arr; } 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 11
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
66983823f1debd7403cc1d60305eb73b
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.Map.Entry; import java.math.*; public class Simple{ final static int MAX = 10000003; static long mod = 998244353 ; static int[] prime = new int[MAX]; 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 n) { long res = 1; int i=0; for (i = 0; i < n; i++) { res = ((res*a[i])/gcd(res, a[i])); } return res; } public static class Pair{ long x; long y; public Pair(long x,long y){ this.x = x; this.y = y; } } static Pair reduceFraction(long x, long y) { long d; d = gcd(x, y); x = x / d; y = y / d; return new Pair(x, y); } public static class Tuple{ long node; Pair weight; public Tuple(long x,long y,long z){ this.node = x; this.weight = new Pair(y, z); } } // Function to return a^n public static long power(long a, long n) { if(n == 0) return 1; long p = power(a, n / 2) % mod; p = (p * p) % mod; if((n & 1) > 0) p = (p * a) % mod; return p; } // Function to find the smallest prime // factors of numbers upto MAX public static void sieve() { prime[0] = 1; prime[1] = 1; for(int i = 2; i < MAX; i++) { if(prime[i] == 0) { for(int j = i * 2; j < MAX; j += i) { if(prime[j] == 0) { prime[j] = i; } } prime[i] = i; } } } public static long lcmModuloM(long[] arr, int n) { HashMap<Integer, Integer> maxMap = new HashMap<>(); for(int i = 0; i < n; i++) { HashMap<Integer, Integer> temp = new HashMap<>(); long num = arr[i]; // Temp stores mapping of prime // factor to its power for the // current element while(num > 1) { // Factor is the smallest prime // factor of num int factor = prime[(int)num]; if(temp.containsKey(factor)) temp.put(factor, temp.get(factor) + 1); else temp.put(factor, 1); // Factor is the smallest prime // factor of num num = num / factor; } for(Map.Entry<Integer, Integer> m : temp.entrySet()) { if(maxMap.containsKey(m.getKey())) { int maxPower = Math.max(m.getValue(), maxMap.get( m.getKey())); maxMap.put(m.getKey(), maxPower); } else { maxMap.put(m.getKey(),m.getValue()); } } } long ans = 1; for(Map.Entry<Integer, Integer> m : maxMap.entrySet()) { // LCM is product of primes to their // highest powers modulo M ans = (ans * power(m.getKey(), m.getValue()) % mod); } return ans; } public static void main(String args[]){ //System.out.println("Hello Java"); Scanner s = new Scanner(System.in); int t = s.nextInt(); for(int t1 = 1;t1<=t;t1++){ int n = s.nextInt(); long b = s.nextInt(); long x = s.nextInt(); long y = s.nextInt(); // if(x<y){ // long temp = x; // x= y; // y= temp; // } long ans =0; long temp =0; for(int i=0;i<n;i++){ if(temp+x<=b){ temp+=x; } else{ temp-=y; } ans+=temp; } // cx + by // long lo = 0; // long hi = n; // long ans = Long.MIN_VALUE; // while(lo<=hi){ // long mid = (lo+hi)/2; // long max = mid*x + (n-mid)*y; // long temp =0; // long sum =0; // if(max<=b){ // for(int i=0;i<(n-mid);i++){ // temp+=y; // sum+=temp; // } // for(int i=0;i<mid;i++){ // temp+=x; // sum+=temp; // } // ans = Math.max(ans,sum); // lo = mid+1; // } // else{ // hi = mid-1; // } // } 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 11
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
48b3e6c5c64c90c816538d8e7ea227f4
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 practice_359 { 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 g =0; long sum=0; for(int i=0;i<n;i++){ if(g+x<=b){ int a =g+x; sum+=a; g=a; } else{ int c=g-y; sum+=c; g=c; } } 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 11
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
fa93e07b48152bc4a5c76adeee633e7e
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.Arrays; public class Sets { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); int numberSets = Integer.parseInt(br.readLine()); for (int nos = 0; nos < numberSets; nos++) { int [] data = Arrays.stream(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); int size = data[0]; int B = data [1]; int x = data [2]; int y = data [3]; int a = 0; long sum = 0; for (int i = 0; i < size; i++) { if (a + x > B) { a = a-y; } else { a = a+x; } sum+=a; } 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 11
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
76e66b030d3bdd7b4b5400c37b9af921
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 discrete { public static void main(String[] args) { Scanner input= new Scanner(System.in); int t,n,B,x,y; t=input.nextInt(); int ai=0; long sum=0; int aXi=0; int aYi=0; for(int i=0;i<t;i++) { sum=0; ai=0; n=input.nextInt(); B=input.nextInt(); x=input.nextInt(); y=input.nextInt(); for(int j=1;j<=n;j++) { if(ai<=B) { aXi=ai+x; aYi=ai-y; if(aXi<=B) ai=aXi; else ai=aYi; } sum+=ai; } 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 11
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
b7abc38544dfa47fc6c840b58c02bfca
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 code { /* public static void print(int[] arr){ int n = arr.length; for(int i = 0;i<n;i++){ System.out.print(arr[i]+" "); } }*/ 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(); int[] arr = new int[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]+x; sum+=arr[i]; }else{ arr[i] = arr[i-1 ]-y; sum+=arr[i]; } } //print(arr); 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 11
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
98afd816c7fef8424eb7512280bc739e
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 discrete { public static void main(String[] args) { Scanner input= new Scanner(System.in); int t,n,B,x,y; t=input.nextInt(); int ai=0; long sum=0; int aXi=0; int aYi=0; for(int i=0;i<t;i++) { sum=0; ai=0; n=input.nextInt(); B=input.nextInt(); x=input.nextInt(); y=input.nextInt(); for(int j=1;j<=n;j++) { if(ai<=B) { aXi=ai+x; aYi=ai-y; if(aXi<=B) ai=aXi; else ai=aYi; } sum+=ai; } 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 11
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
45fa19ec7be1c2914c462ed269fdb4a5
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) { // TODO Auto-generated method stub Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- > 0) { long n = sc.nextLong(); long B = sc.nextLong(); long x = sc.nextLong(); long y = sc.nextLong(); ArrayList<Integer> a= new ArrayList(); long sum=0; a.add(0); for(int i=1;i<=n;i++) { int temp=(int) (a.get(i-1)+x); if(temp<=B) { a.add(temp); }else { long temp2=(long) a.get(i-1)-y; a.add((int) temp2); } sum+=(long) a.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 11
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
3b54115c868f04697aebd99c9942e29d
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
//Code By KB. import java.beans.Visibility; import java.io.*; import java.lang.annotation.Target; import java.lang.reflect.Array; import java.nio.channels.AsynchronousCloseException; import java.security.KeyStore.Entry; import java.util.*; import java.util.logging.*; import java.io.*; import java.util.logging.*; import java.util.regex.*; import javax.management.ValueExp; import javax.print.DocFlavor.INPUT_STREAM; import javax.swing.plaf.basic.BasicBorders.SplitPaneBorder; import javax.swing.text.AbstractDocument.LeafElement; import javax.xml.validation.Validator; public class Codeforces { public static void main(String[] args) { FlashFastReader in = new FlashFastReader(System.in); try(PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));) { new Codeforces().solution(in, out); } catch (Exception e) { System.out.println(e.getStackTrace()); } } public void solution(FlashFastReader in, PrintWriter out) { try { int t = in.nextInt(); while (t-->0) { int n =in.nextInt(); long B =in.nextLong(); long x = in.nextLong(); long y = in.nextLong(); long a []= new long[n+1]; for (int i = 1; i <=n; i++) { if((a[i-1]+x)>B){ a[i]=a[i-1]-y; } else { a[i] = a[i-1]+x; } } long s = 0; for (int i = 0; i < a.length; i++) { s+=a[i]; } out.println(s); } } catch (Exception exception) { out.println(exception); } } public int countSquares(int[][] matrix) { int dp [][] = new int [matrix.length+1][matrix[0].length+1]; for (int i = 1; i <=matrix.length; i++) { for (int j = 1; j <=matrix[0].length; j++) { if (matrix[i][j]==1) { if (i>=1&&j>=1) { dp[i][j] =1+ Math.min(dp[i-1][j], Math.min(dp[i-1][j-1], dp[i][j-1]) ); } } x+=dp[i][j]; } } return x; } HashMap<Integer, Integer> map = new HashMap<>(); public int totalNQueens(int n) { if (n==1) { return 1; } if (n==2||n==3) { return 0; } dfsNqueens( map , 0 , n ); return x; } public void dfsNqueens( HashMap<Integer, Integer>map , int col , int n ) { if (col == n) { x++; return; } for (int i = 0; i < n; i++) { map.put(col, i); boolean f = true; for (int j = 0; j < n; j++) { if (map.containsKey(j)) { if (map.get(j)==i||Math.abs(col - j)==Math.abs(map.get(j)-i) ) { f = false; } } } if (f) { dfsNqueens(map, col+1, n); } } } public int[] minOperations(String boxes) { int a [] = new int[boxes.length()]; for (int i = 0; i < boxes.length(); i++) { StringBuilder sb = new StringBuilder(boxes); int x = 0; for (int j = 0; j < sb.length(); j++) { if (sb.indexOf("1")<0) { break; } x+=Math.abs(i - sb.indexOf("1")); sb.setCharAt(sb.indexOf("1"), '0'); } a[i] = x; } return a; } public List<List<Integer>> subsets(int[] nums) { List<Integer> curr = new ArrayList<>(); subsetsGen(0, nums , curr); return list; } public void subsetsGen(int j ,int[] n , List<Integer>curr) { list.add(curr); for (int i = j; i < n.length; i++) { curr.add(n[i]); subsetsGen(j+1, n, curr); curr.remove(curr.size()-1); } } boolean visited[]= new boolean[1000]; public int findCircleNum(int[][] isConnected) { for (int i = 0; i < isConnected.length; i++) { if (visited[i]!=true) { x++; dfsFindProvinces( i , isConnected ); } } return x; } public void dfsFindProvinces( int i ,int [][]isConnected ) { visited[i]=true; for (int j = 0; j < isConnected.length; j++) { if (isConnected[i][j]==1&&visited[j]!=true) { dfsFindProvinces(j, isConnected); } } } public int[] canSeePersonsCount(int[] heights) { int ans [] = new int[heights.length]; Stack<Integer> stck = new Stack<>(); for (int i = 0; i < heights.length; i++) { while(!stck.isEmpty()&&heights[stck.peek()]<heights[i]) { ans[stck.pop()]++; } if (!stck.isEmpty()) { ans[stck.peek()]++; } stck.push(i); } return ans; } public int pathSum(TreeNode root, int targetSum) { List<Integer> path = new ArrayList<>(); dsfPathSum(root, targetSum , 0 ,path); pathSum(root.left, targetSum); pathSum(root.right, targetSum); for (List<Integer> p : list) { for (int i = 0; i < p.size() ; i++) { System.out.print(p.get(i)+" "); } System.out.println(); } return list.size(); } public void dsfPathSum(TreeNode root , int t , long sum , List<Integer>path ) { if (root==null) { return; } sum+=root.val ; path.add(root.val); if (sum==t) { list.add(path); } dsfPathSum(root.left, t, sum , path); dsfPathSum(root.right, t, sum , path); } public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode l = new ListNode(); int a = 0; int b =0; l1 = reverseList(l1); l2 = reverseList(l2); while (l1!=null) { a=a*10+l1.val; l1=l1.next; } while (l2!=null) { b=b*10+l2.val; l2=l2.next; } a=a+b; ListNode head = null; while (a!=0) { int r = a%10; if(l==null) { head= new ListNode(r); l=head; }else { l.next = new ListNode(r); l= l.next; } a/=10; } return head; } public ListNode reverseList(ListNode head) { if(head==null || head.next==null) return head; ListNode nextNode=head.next; ListNode newHead=reverseList(nextNode); nextNode.next=head; head.next=null; return newHead; } int x=0; public int uniquePathsIII(int[][] grid) { int zeros = 0; int sx=0; int sy =0; for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if (grid[i][j]==0) { zeros++; } else if (grid[i][j]==1) { sx=i; sy=j; } } } dfsUniquePaths(zeros , sx , sy , grid); return x; } public void dfsUniquePaths(int zeros ,int i ,int j , int g[][]) { if (i<0||i>=g.length||j>=g[0].length||j<0||g[i][j]<0) { System.out.println(i+" "+j); return; } if (g[i][j]==2) { if(zeros==0) x++; return; } g[i][j]=-2; zeros--; dfsUniquePaths(zeros, i+1, j, g); dfsUniquePaths(zeros, i-1, j, g); dfsUniquePaths(zeros, i, j+1, g); dfsUniquePaths(zeros, i, j-1, g); g[i][j]=0; zeros++; } public TreeNode addOneRow(TreeNode root, int val, int depth) { if (depth==1) { TreeNode t = new TreeNode(val); t.left=root; return t; } dfsAddOneRow(root , 1 , val , depth); return root; } public void dfsAddOneRow(TreeNode root , int currD , int val , int depth) { if (root==null) { return ; } if (currD==depth) { TreeNode l = root.left; root.left = new TreeNode(val); root.left.left = l; TreeNode r = root.right; root.right = new TreeNode(val); root.right.right = r; } currD++; dfsAddOneRow(root.left, currD, val, depth); dfsAddOneRow(root.right, currD, val, depth); } public List<List<Integer>> threeSum(int[] nums) { int n = nums.length; int sum =0; HashSet<List<Integer>> h = new HashSet<List<Integer>>(); for (int i = 0; i < nums.length; i++) { sum=nums[i]; for (int j = i+1; j < nums.length; j++) { sum+=nums[j]; int k =j; n = nums.length; while (k<n) { int mid = (k+n)/2; if ((sum+nums[mid])==0) { HashSet<Integer> m = new HashSet<>(); m.add(nums[i]); m.add(nums[j]); m.add(nums[mid]); if (m.size()==3) { List<Integer>x = new ArrayList<>(); x.add(nums[i]); x.add(nums[j]); x.add( nums[mid]); h.add(x); } } else if ((sum+nums[mid])>0) { n=mid; } else { k=mid; } } } } return list; } public int uniquePaths(int m, int n) { int dp[][]= new int [m][n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if(i==0&&j==0) { dp[i][j]=1; } else if (i==0) { dp[i][j]=dp[i][j]+dp[i][j-1]; } else if (j==0) { dp[i][j]=dp[i][j]+dp[i-1][j]; } else { dp[i][j]+=dp[i][j-1]+dp[i-1][j]; } } } return dp[m-1][n-1]; } public int maxProfit(int[] prices) { int p = -1; return p ; } public int trap(int[] height) { int n = height.length; int left[] = new int [n]; int right[] = new int [n]; left[0] = height[0]; for (int i = 1; i <n; i++) { left[i] = Math.max(height[i], left[i-1]); } right[n-1]=height[n-1]; for (int i = n-2; i>=0 ; i--) { right[i] = Math.max(height[i], right[i+1]); } for (int i = 1; i < right.length-1; i++) { int s =Math.min(left[i] , right[i])-height[i]; if (s>0) { x+=s; } } return x; } public int maxProduct(int[] nums) { int maxSoFar = Integer.MIN_VALUE; int maxTillEnd = 0; for (int i = 0; i < nums.length; i++) { maxTillEnd*=nums[i]; if (maxSoFar<maxTillEnd) { maxSoFar=maxTillEnd; } if (maxTillEnd<=0) { maxTillEnd=1; } } return maxSoFar; } public int maximumScore(int[] nums, int[] multipliers) { int n = multipliers.length; int dp[] = new int[n+1]; for (int i = 1; i < multipliers.length; i++) { for (int j = 1; j < nums.length; j++) { dp[i] = Math.max(dp[j-1], multipliers[i-1]*nums[i-1]); } } return dp[n]; } public int islandPerimeter(int[][] grid) { for (int i = 0; i < grid.length; i++) { for (int j = 0; j < grid[0].length; j++) { if(grid[i][j]==1){ dfsIslandPerimeter(i , j , grid); } } } return x; } public void dfsIslandPerimeter(int i , int j , int[][] grid) { if(i<0||i>=grid.length||j<0||j>grid[0].length) { x++; return; } if(grid[i][j]==0) { x++; return; } if(grid[i][j]==1) { return; } dfsIslandPerimeter(i+1, j, grid); dfsIslandPerimeter(i-1, j, grid); dfsIslandPerimeter(i, j+1, grid); dfsIslandPerimeter(i, j-1, grid); } public int[] findOriginalArray(int[] changed) { int n =changed.length; int m = n/2; int a[] = new int[m]; if (n<2) { return new int[]{}; } if (n%2!=0) { return new int[]{}; } TreeMap<Integer , Integer> map = new TreeMap<>(); for (int i = 0; i < changed.length; i++) { map.put(changed[i], map.getOrDefault(changed[i] ,0)+1); } int j =0; for (Map.Entry<Integer , Integer> e : map.entrySet()) { if (map.containsKey(e.getKey()*2)&&map.get(e.getKey()*2)>=1&&map.get(e.getKey())>=1) { map.put(e.getKey(), map.get(e.getKey()) -1); if(j==n-1) break; a[j++] = e.getKey(); } } return a; } public int jump(int[] nums) { int n =nums.length; int jumps =1; int i =0; int j =0; while (i<n) { int max =0; for (j=i+1 ;j<=i+nums[i];j++) { max = Math.max(max , nums[j]+j); } jumps++; i=max; } return jumps; } static void sort(int[] arr) { if (arr.length < 2) return; int mid = arr.length / 2; int[] left_half = new int[mid]; int[] right_half = new int[arr.length - mid]; // copying the elements of array into left_half for (int i = 0; i < mid; i++) { left_half[i] = arr[i]; } // copying the elements of array into right_half for (int i = mid; i < arr.length; i++) { right_half[i - mid] = arr[i]; } sort(left_half); sort(right_half); merge(arr, left_half, right_half); } static void merge(int[] arr, int[] left_half, int[] right_half) { int i = 0, j = 0, k = 0; while (i < left_half.length && j < right_half.length) { if (left_half[i] < right_half[j]) { arr[k++] = left_half[i++]; } else { arr[k++] = right_half[j++]; } } while (i < left_half.length) { arr[k++] = left_half[i++]; } while (j < right_half.length) { arr[k++] = right_half[j++]; } } public int minCostClimbingStairs(int[] cost) { int n = cost.length; int dp[] = new int[n+1]; dp[1] = cost[0]; dp[2] = cost[1]; for (int i = 3; i <=n; i++) { dp[i]=cost[i-1]+Math.min(dp[i-1], dp[i-2]); } return dp[n]; } TreeNode newAns =null; public TreeNode removeLeafNodes(TreeNode root, int target) { newAns = root; dfsRemoveNode(newAns , target); return newAns; } public void dfsRemoveNode(TreeNode root , int t) { if (root==null) { return; } if (root.val==t) { if (root.left!=null) { root=root.left; }else if (root.right!=null){ root=root.right; } else { root=null; } } dfsRemoveNode(root.left, t); dfsRemoveNode(root.right, t); } public int maxProfit(int k, int[] prices) { int n = prices.length; if (n <= 1) return 0; if (k >= n/2) { return stocks(prices, n); } int[][] dp = new int[k+1][n]; for (int i = 1; i <=k; i++) { int c= dp[i-1][0] - prices[0]; for (int j = 1; j < n; j++) { dp[i][j] = Math.max(dp[i][j-1], prices[j] + c); c = Math.max(c, dp[i-1][j] - prices[j]); } } return dp[k][n-1]; } public int stocks(int [] prices , int n ) { int maxPro = 0; for (int i = 1; i < n; i++) { if (prices[i] > prices[i-1]) maxPro += prices[i] - prices[i-1]; } return maxPro; } public List<List<Integer>> combine(int n, int k) { List<Integer> x = new ArrayList<>(); combinations( n , k , 1 ,x); return list; } public void combinations(int n , int k ,int startPos , List<Integer> x) { if (k==x.size()) { list.add(new ArrayList<>(x)); //x=new ArrayList<>(); return; } for (int i = startPos; i <=n; i++) { x.add(i); combinations(n, k, i+1, x); x.remove(x.size()-1); } } List<List<Integer>> list = new ArrayList<>(); public List<List<Integer>> permute(int[] a) { List<Integer> x = new ArrayList<>(); permutations(a , 0 ,a.length ,x ); return list ; } public void permutations(int a[] , int startPos , int l , List<Integer>x) { if (l==x.size()) { list.add(new ArrayList<>(x)); //x=new ArrayList<>(); } for (int i = 0; i <=l; i++) { if (!x.contains(a[i])) { x.add(a[i]); permutations(a, i, l, x); x.remove(x.size()-1); } } } List<String> str = new ArrayList<>(); public List<String> letterCasePermutation(String s) { casePermutations(s , 0 , s.length() , "" ); return str; } public void casePermutations(String s ,int i , int l , String curr) { if (curr.length()==l) { str.add(curr); return; } char b = s.charAt(i); curr+=b; casePermutations(s, i+1, l, curr); curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString(); if (Character.isAlphabetic(b)&&Character.isLowerCase(b)) { curr+=Character.toUpperCase(b); casePermutations(s, i+1, l, curr); curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString(); } else if (Character.isAlphabetic(b)) { curr+=Character.toLowerCase(b); casePermutations(s, i+1, l, curr); curr = new StringBuilder(curr).deleteCharAt(curr.length()-1).toString(); } } TreeNode ans= null; public TreeNode lcaDeepestLeaves(TreeNode root) { dfslcaDeepestLeaves(root , 0 ); return ans; } public void dfslcaDeepestLeaves(TreeNode root , int level) { if (root==null) { return; } if (depth(root.left)==depth(root.right)) { ans= root; return; } else if (depth(root.left)>depth(root.right)){ dfslcaDeepestLeaves(root.left, level+1); } else { dfslcaDeepestLeaves(root.right, level+1); } } public int depth(TreeNode root) { if (root==null) { return 0; } return 1+Math.max(depth(root.left), depth(root.right)); } TreeMap<Integer , Integer> m = new TreeMap<>(); public int maxLevelSum(TreeNode root) { int maxlevel =0; int mx = Integer.MIN_VALUE; dfsMaxLevelSum(root , 0); for (Map.Entry<Integer,Integer> e : m.entrySet()) { if (e.getValue()>mx) { mx=e.getValue(); maxlevel=e.getKey()+1; } } return maxlevel; } public void dfsMaxLevelSum(TreeNode root , int currLevel) { if (root==null) { return; } if (!m.containsKey(currLevel)) { m.put(currLevel, root.val); } else { m.put(currLevel, m.get(currLevel)+root.val); } dfsMaxLevelSum(root.left, currLevel+1); dfsMaxLevelSum(root.right, currLevel+1); } int teampPerf = 0; int teampSpeed = 0; public int maxPerformance(int n, int[] speed, int[] efficiency, int k) { int [][] map = new int[efficiency.length][2]; for (int i = 0; i < efficiency.length; i++) { map[i][0] = efficiency[i]; map[i][1] = speed[i]; } Arrays.sort(map , (e1 , e2 )-> (e2[0] - e1[0])); PriorityQueue<Integer> pq = new PriorityQueue<>(); calmax(map , speed , efficiency , pq , k); return teampPerf ; } public void calmax(int [][]map , int s[] , int e[] , PriorityQueue<Integer>pq , int k) { for (int i = 0 ; i<n ; i++) { if (pq.size()==k) { int lowestSpeed =pq.remove(); teampSpeed-=lowestSpeed; } pq.add(map[i][1]); teampSpeed+=map[i][1]; teampPerf=Math.max(teampPerf, teampSpeed*map[i][1]); } } int maxRob = 0; public int rob(int[] nums) { int one = 0; int two = 0; for (int i = 0; i < nums.length; i++) { if (i%2==0) { one+=nums[i]; } else { two+=nums[i]; } } return Math.max(one, two); } public int numberOfWeakCharacters(int[][] properties) { int c =0; Arrays.sort(properties, (a, b) -> (b[0] == a[0]) ? (a[1] - b[1]) : b[0] - a[0]); PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder()); pq.offer(0); for (int i = 0; i < properties.length; i++) { if (properties[i][1]<pq.peek()) { c++; } pq.offer(properties[i][1]); } return c; } public boolean canFinish(int numCourses, int[][] prerequisites) { boolean f = true ; TreeMap <Integer,Integer> map = new TreeMap<>(); for (int i = 0; i < prerequisites.length; i++) { if (map.get(prerequisites[i][1])!=null) { return false; } map.put(prerequisites[i][0], prerequisites[i][1]); } return f; } int n =0; public int dfsB(int i , int j , char [][]b , char [][]res) { if (i<0||i>=b.length|j<0||j>=b[0].length||res[i][j]=='.') { return 0; } if (res[i][j]=='X') { return 1+dfsB(i+1, j, b, res)+dfsB(i, j+1, b, res); } return 0; } List<Integer> l = new ArrayList<>(); public List<Integer> rightSideView(TreeNode root) { return dfsRightSideView(root); } public List<Integer> dfsRightSideView(TreeNode root) { if (root!=null) { l.add(root.val); } if (root==null) { return l; } if (root.right==null) { l.add(root.right.val); } dfsRightSideView(root.right); return l; } public void dfsSumNumber(TreeNode root ,int sum , int l ) { l=l*10 +root.val; if (root.left!=null) { dfsSumNumber(root.left,sum , l); } if (root.right!=null) { dfsSumNumber(root.right , sum, l); } if (root.left==null && root.right==null) { sum+=l; } // r=l*10+root.val; l-=root.val; l/=10; } //BFS SOLN public int[][] updateMatrix(int[][] mat) { int m =mat.length; int n = mat[0].length; Queue<int[]> q = new LinkedList<>(); for (int i = 0; i < mat.length; i++) { for (int j = 0; j < mat[0].length; j++) { if (mat[i][j]==0) { q.offer(new int[] { i , j}); } else { mat[i][j] = Integer.MAX_VALUE; } } } int dir[][] = {{1 , 0} , {-1 , 0} , {0 , 1} , { 0 , -1}}; while (!q.isEmpty()) { int zeroPos[] = q.poll(); for (int[] d : dir) { int r = zeroPos[0]+d[0]; int c = zeroPos[1]+d[1]; if (r<0||r>=mat.length || c<0||c>=mat[0].length||mat[r][c]<=mat[zeroPos[0]][zeroPos[1]]+1) { continue; } q.add(new int[]{r,c}); mat[r][c] = mat[zeroPos[0]][zeroPos[1]]+1; } } return mat; } // DFS SOLN // public int[][] updateMatrix(int[][] mat) { // int res [][] = new int[mat.length][mat[0].length]; // for (int i = 0; i < mat.length; i++) { // for (int j = 0; j < mat.length; j++) { // if (mat[i][j]==0) { // Dis0(i, j, mat , res, 0); // } // } // } // return res; // } // public void Dis0(int i , int j , int [][]mat, int res[][] , int dist) { // if (i<0||i>=mat.length||j>=mat[0].length||j<0) { // return ; // } // if (dist==0 ||mat[i][j]==1&&(res[i][j]==0||res[i][j]>dist)) { // res[i][j]= dist; // Dis0(i+1, j, mat, res, dist+1); // Dis0(i-1, j, mat, res, dist+1); // Dis0(i, j+1, mat, res, dist+1); // Dis0(i, j-1, mat, res, dist+1); // } // // return dist; // } public int maxDepth(TreeNode root) { if (root==null) { return 0; } return maxD(root , 0); } public int maxD(TreeNode root , int d ) { if (root==null) { return d; } return Math.max(maxD(root.left, d+1), maxD(root.right, d+1)); } public int reverse(int x) { int sign=x>0?1:-1; //x=Math.abs(x); long s= 0; int r= 0; int n=x; while (n!=0) { r=n%10; s=s*10+r; n/=10; if (s>Integer.MAX_VALUE||s<Integer.MIN_VALUE) { return 0; } } return (int)s*sign; } public boolean checkInclusion(String s1, String s2) { boolean f = false ; if (s1.length()>s2.length()) { return false; } for (int i = 0; i < s2.length(); i++) { HashMap<Character ,Integer> c1 = new HashMap<>(); HashMap<Character ,Integer> c2 = new HashMap<>(); for (int j = 0; j < s1.length(); j++) { char ch2 = s2.charAt(i+j); char ch1 = s1.charAt(j); // if (c1.get(key)) { // } } } return f; } 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; } } public ListNode middleNode(ListNode head) { ListNode mid = head; ListNode last = head; int k =0; while (last.next!=null&&last.next.next!=null) { k++; mid= mid.next; last = last.next.next; } if (getLen(head)%2==0) { return mid.next; } return mid; } public int getLen(ListNode mid) { int l = 0; ListNode p = mid; while (p!=null) { l++; p=p.next; } return l; } public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } } public List<List<Integer>> verticalTraversal(TreeNode root) { TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map = new TreeMap<>(); dfs(root, 0, 0, map); List<List<Integer>> list = new ArrayList<>(); for (TreeMap<Integer, PriorityQueue<Integer>> ys : map.values()) { list.add(new ArrayList<>()); for (PriorityQueue<Integer> nodes : ys.values()) { while (!nodes.isEmpty()) { // list.get(list.size() - 1).add(nodes.poll()); } } } return list; } private void dfs(TreeNode root, int x, int y, TreeMap<Integer, TreeMap<Integer, PriorityQueue<Integer>>> map) { if (root == null) { return; } if (!map.containsKey(x)) { map.put(x, new TreeMap<>()); } if (!map.get(x).containsKey(y)) { map.get(x).put(y, new PriorityQueue<>()); } map.get(x).get(y).offer(root.val); dfs(root.left, x - 1, y + 1, map); dfs(root.right, x + 1, y + 1, map); } public long pow (int x ,int n ) { long y = 1; if (n==0) { return y; } while (n>0) { if (n%2!=0) { y=y*x; } x*=x; n/=2; } return y; } public long powrec (int x ,int n ) { long y = 1; if (n==0) { return y; } y = powrec(x, n/2); if (n%2==0) { return y*y; } return y*y*x; } public int fib(int n) { if (n==0||n==11) { return n; } return fib(n-1)+fib(n-2); } public long gcd(long a , long b) { if (b%a==0) { return a; } return gcd(b%a,a); } public int maximumElement(int a[]) { int x = Integer.MIN_VALUE; for (int i = 0; i < a.length; i++) { if (a[i]>x) { x=a[i]; } } return x; } public int minimumElement(int a[]) { int x = Integer.MAX_VALUE; for (int i = 0; i < a.length; i++) { if (a[i]<x) { x=a[i]; } } return x; } public boolean [] sieveOfEratosthenes(int n) { boolean 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]) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } // GFG arraylist function for referral public ArrayList create2DArrayList(int n ,int k) { // Creating a 2D ArrayList of Integer type ArrayList<ArrayList<Integer> > x = new ArrayList<ArrayList<Integer> >(); // One space allocated for R0 // Adding 3 to R0 created above x(R0, C0) //x.get(0).add(0, 3); int xr = 0; for (int i = 1; i <= n; i+=2) { if ((i+k)*(i+1)%4==0) { x.add(new ArrayList<Integer>()); x.get(xr).addAll(Arrays.asList(i , i+1)); } else { x.add(new ArrayList<Integer>()); x.get(xr).addAll(Arrays.asList(i+1 , i)); } xr++; } // Creating R1 and adding values // Note: Another way for adding values in 2D // collections // x.add( // new ArrayList<Integer>(Arrays.asList(3, 4, 6))); // // Adding 366 to x(R1, C0) // x.get(1).add(0, 366); // // Adding 576 to x(R1, C4) // x.get(1).add(4, 576); // // Now, adding values to R2 // x.add(2, new ArrayList<>(Arrays.asList(3, 84))); // // Adding values to R3 // x.add(new ArrayList<Integer>( // Arrays.asList(83, 6684, 776))); // // Adding values to R4 // x.add(new ArrayList<>(Arrays.asList(8))); // // Appending values to R4 // x.get(4).addAll(Arrays.asList(9, 10, 11)); // // Appending values to R1, but start appending from // // C3 // x.get(1).addAll(3, Arrays.asList(22, 1000)); // This method will return 2D array return x; } } class FlashFastReader { BufferedReader in; StringTokenizer token; public FlashFastReader(InputStream ins) { in=new BufferedReader(new InputStreamReader(ins)); token=new StringTokenizer(""); } public boolean hasNext() { while (!token.hasMoreTokens()) { try { String s = in.readLine(); if (s == null) return false; token = new StringTokenizer(s); } catch (IOException e) { throw new InputMismatchException(); } } return true; } public String next() { hasNext(); return token.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public int[] nextIntsInputAsArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } public long nextLong() { return Long.parseLong(next()); } public long[] nextLongsInputAsArray(int n) { long [] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public String nextString() { return String.valueOf(next()); } public String[] nextStringsInputAsArray(int n) { String [] a = new String[n]; for (int i = 0; i < n; i++) a[i] = nextString(); 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 11
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
36701b24160c7fbc553314616e1dc570
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(); long x,y,i,n,B,sum,j,prev; double d; for(i=0;i<t;i++){ n=sc.nextInt(); B=sc.nextInt(); x=sc.nextInt(); y=sc.nextInt(); for(j=1,sum=0,prev=0;j<=n;j++){ if(prev+x <= B) prev+=x; else 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 11
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
014df9afef5697ae4da41adc9dddb1b4
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); // System.out.println(sc.nextByte()); xysequence(sc); } private static void xysequence(Scanner scanner){ int testcase=scanner.nextInt(); for(int i=0;i<testcase;i++){ int turn=scanner.nextInt(); long limit=scanner.nextInt(); long add=scanner.nextInt(); long sub=scanner.nextInt(); long val=0; long valsum=0; for(int j=0;j<turn;j++){ if(val+add>limit){ val=val-sub; valsum+=val; }else{ val=val+add; valsum+=val; } } System.out.println(valsum); } }}
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 11
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
9ca6a9e55a9c6e0587f645dba6ee207d
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; import java.util.Arrays; import java.util.HashSet; import java.util.HashMap; import java.util.ArrayList; import java.util.Random; public class Solution{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int test_cases = sc.nextInt(); outer : for(int h=0;h<test_cases;h++){ //scan the input int n = sc.nextInt(); int B = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); long a[] = new long[n+1]; a[0] = 0; //Logic for(int i=1;i<n+1;i++){ if(a[i-1]+x<=B){ a[i] = a[i-1]+x; } else { a[i] = a[i-1]-y; } } long result = sum_array(a); System.out.println(result); } } public static void scan_array(int a[], int n, Scanner sc){ for(int i=0;i<n;i++){ a[i] = sc.nextInt(); } } public static void print_array(long a[]){ for(int i=0;i<a.length;i++){ if(i==a.length-1){ System.out.println(a[i]); return; } System.out.print(a[i]+" "); } } public static long sum_array(long a[]){ long sum = 0; for(int i=0;i<a.length;i++){ sum+=a[i]; } return sum; } public static boolean perfect_square(double num){ boolean isSq = false; double b = Math.sqrt(num); double c = Math.sqrt(num) - Math.floor(b); if(c==0){ isSq=true; } return isSq; } public static boolean ispalindrome(String s){ int i=0, j =s.length()-1; while(i<=j){ if(s.charAt(i)!=s.charAt(j)){ return false; } i++; j--; } return true; } public static int calculate_gcd(int x,int y){ int gcd = 1; if(x%y==0){ return y; } else if(y%x==0){ return x; } else{ for(int i=2;i<=x && i<=y;i++){ if(x%i==0 && y%i==0){ gcd = i; } } return gcd; } } public static long calculate_factorial(long x){ long ans = 1; for(int i=1;i<=x;i++){ ans*=i; } return ans; } public static void swap(int a[], int b[], int index_a, int index_b){ int temp = a[index_a]; a[index_a] = b[index_b]; b[index_b] = temp; } public static long find_maximum(long a[]){ long max = Long.MIN_VALUE; for(int i=0;i<a.length;i++){ max = Math.max(a[i], max); } return max; } public static long find_minimum(long a[]){ long min = Long.MAX_VALUE; for(int i=0;i<a.length;i++){ min = Math.min(a[i], min); } return min; } public static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort Random random = new Random(); for (int i=0; i<n; i++) { int oi=random.nextInt(n); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } public static int sum_of_digits(int n){ int s =0; while(n!=0){ s+=n%10; n/=10; } return s; } }
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 11
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
5a27d4c9f9352dc30914d7e279fb58da
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 XYSequence { 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(); LinkedList<Integer> a = new LinkedList(); a.add(0); long suma = 0; for(int i=1; i<=n; i++){ if((a.get(0) + x) <= B){ suma += a.get(0) + x; a.add(a.get(0) + x); a.removeFirst(); } else{ suma += a.get(0) - y; a.add(a.get(0) - y); a.removeFirst(); } } System.out.println(suma); } } static class Maths { public static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static int lcm(int a, int b) { return a * b / gcd(a, b); } } static class NextPermutation { // Function to swap the data // present in the left and right indices public static int[] swap(int data[], int left, int right) { // Swap the data int temp = data[left]; data[left] = data[right]; data[right] = temp; // Return the updated array return data; } // Function to reverse the sub-array // starting from left to the right // both inclusive public static int[] reverse(int data[], int left, int right) { // Reverse the sub-array while (left < right) { int temp = data[left]; data[left++] = data[right]; data[right--] = temp; } // Return the updated array return data; } // Function to find the next permutation // of the given integer array public static boolean findNextPermutation(int data[]) { // If the given dataset is empty // or contains only one element // next_permutation is not possible if (data.length <= 1) return false; int last = data.length - 2; // find the longest non-increasing suffix // and find the pivot while (last >= 0) { if (data[last] < data[last + 1]) { break; } last--; } // If there is no increasing pair // there is no higher order permutation if (last < 0) return false; int nextGreater = data.length - 1; // Find the rightmost successor to the pivot for (int i = data.length - 1; i > last; i--) { if (data[i] > data[last]) { nextGreater = i; break; } } // Swap the successor and the pivot data = swap(data, nextGreater, last); // Reverse the suffix data = reverse(data, last + 1, data.length - 1); // Return true as the next_permutation is done return true; } } static class UnionFind { // OOP style private ArrayList<Integer> p, rank, setSize; private int numSets; public UnionFind(int N) { p = new ArrayList<Integer>(N); rank = new ArrayList<Integer>(N); setSize = new ArrayList<Integer>(N); numSets = N; for (int i = 0; i < N; i++) { p.add(i); rank.add(0); setSize.add(1); } } public int findSet(int i) { if (p.get(i) == i) return i; else { int ret = findSet(p.get(i)); p.set(i, ret); return ret; } } public Boolean isSameSet(int i, int j) { return findSet(i) == findSet(j); } public void unionSet(int i, int j) { if (!isSameSet(i, j)) { numSets--; int x = findSet(i), y = findSet(j); // rank is used to keep the tree short if (rank.get(x) > rank.get(y)) { p.set(y, x); setSize.set(x, setSize.get(x) + setSize.get(y)); } else { p.set(x, y); setSize.set(y, setSize.get(y) + setSize.get(x)); if (rank.get(x) == rank.get(y)) rank.set(y, rank.get(y) + 1); } } } public int numDisjointSets() { return numSets; } public int sizeOfSet(int i) { return setSize.get(findSet(i)); } } static class Pair<A, B> { A first; B second; // Constructor public Pair(A first, B second) { this.first = first; this.second = second; } } 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; } boolean hasNext() { if (st != null && st.hasMoreTokens()) { return true; } String tmp; try { br.mark(1000); tmp = br.readLine(); if (tmp == null) { return false; } br.reset(); } catch (IOException e) { return false; } return true; } } }
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 11
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
d3898d19d439a78a6242348485b047ce
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
//Utilities import java.io.*; import java.util.*; public class a { static int t; static int n, b, x, y; static long[] a; static long res; public static void main(String[] args) throws IOException { t = in.iscan(); while (t-- > 0) { n = in.iscan(); b = in.iscan(); x = in.iscan(); y = in.iscan(); res = 0; a = new long[n]; for (int i = 0; i < n; i++) { if (i == 0) { if (x <= b) { a[i] = x; } else { a[i] = -y; } } else { if (a[i-1] + x <= b) { a[i] = a[i-1] + x; } else { a[i] = a[i-1] - y; } } res += a[i]; } out.println(res); } out.close(); } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static void sort(int[] a, boolean increasing) { ArrayList<Integer> arr = new ArrayList<Integer>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(long[] a, boolean increasing) { ArrayList<Long> arr = new ArrayList<Long>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(double[] a, boolean increasing) { ArrayList<Double> arr = new ArrayList<Double>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static void updateMap(HashMap<Integer, Integer> map, int key, int v) { if (!map.containsKey(key)) { map.put(key, v); } else { map.put(key, map.get(key) + v); } if (map.get(key) == 0) { map.remove(key); } } public static long gcd (long a, long b) { return b == 0 ? a : gcd (b, a % b); } public static long lcm (long a, long b) { return a * b / gcd (a, b); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static long fast_pow (long b, long x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } // start of permutation and lower/upper bound template public static void nextPermutation(int[] nums) { //find first decreasing digit int mark = -1; for (int i = nums.length - 1; i > 0; i--) { if (nums[i] > nums[i - 1]) { mark = i - 1; break; } } if (mark == -1) { reverse(nums, 0, nums.length - 1); return; } int idx = nums.length-1; for (int i = nums.length-1; i >= mark+1; i--) { if (nums[i] > nums[mark]) { idx = i; break; } } swap(nums, mark, idx); reverse(nums, mark + 1, nums.length - 1); } public static void swap(int[] nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i, j); i++; j--; } } static int lower_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= cmp) high = mid; else low = mid + 1; } return low; } static int upper_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > cmp) high = mid; else low = mid + 1; } return low; } // end of permutation and lower/upper bound template } }
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 11
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
7d0376fc9670f6fe4d94dfeae883e177
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 Solution { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = in.nextInt(); int n, B, x, y; for(int j = 0; j < t; j++) { n = in.nextInt(); B = in.nextInt(); x = in.nextInt(); y = in.nextInt(); long result = 0; int i = 1; long s = 0; while(i<=n) { if(s+x<=B) s+=x; else s-=y; i++; result+=s; } System.out.println(result); } in.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 11
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
d558cbd3080033a54a51d614c1e60322
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 test = sc.nextInt(); for ( int t=0; t<test; t++){ 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; for ( int i=1; i<n+1; i++){ long c=0,d=0; c = arr[i-1]+x; d = arr[i-1]-y; if ( c>=d && c<=B) arr[i] = c; else if ( c>=d && d<=B) arr[i] = d; if ( d>=c && d<=B) arr[i] = d; else if ( d>=c && c<=B) arr[i] = c; } long sum=0; for ( int i=0; i<n+1; i++){ sum = 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 11
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
c664c599eec00bab4ae000b6925c816e
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.lang.*; // import java.math.*; import java.io.*; import java.util.*; public class Main{ public static int mod=(int)(1e9) + 7; public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter ot=new PrintWriter(System.out); public static int[] take_arr(int n){ int a[]=new int[n]; try { String s[]=br.readLine().trim().split(" "); for(int i=0;i<n;i++) a[i]=Integer.parseInt(s[i]); } catch (Exception e) { e.printStackTrace(); } return a; } public static void main (String[] args) throws java.lang.Exception { try{ int t=Integer.parseInt(br.readLine().trim()); int cno=1; while(t-->0){ String s[]=br.readLine().trim().split(" "); int n=Integer.parseInt(s[0]); int b=Integer.parseInt(s[1]); int x=Integer.parseInt(s[2]); int y=Integer.parseInt(s[3]); // int a[]=take_arr(n); // int b[]=take_arr(n); // char ch[]=br.readLine().trim().toCharArray(); // long n=Long.parseLong(s[0]); solve(n,b,x,y); } ot.close(); br.close(); }catch(Exception e){ e.printStackTrace(); return; } } static void solve(int n, int b, int x, int y){ try{ 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]-y; } else{ a[i]=a[i-1]+x; } } long sum=0; for(int xx:a) sum+=xx; ot.println(sum); } catch(Exception e){ e.printStackTrace(); 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 11
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
62bbfa613c163654ca032f0d5eb2244f
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.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 = readInt(); for (int test=0;test<ntest;test++) { int N = readInt(); int B = readInt(); int x = readInt(), y = readInt(); long last = 0; long ans = 0; for (int i=0;i<N;i++) { if (last + x <= B) last += x; else last -= y; ans += last; } out.println(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\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 11
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
b4c992e84c1d3b45aeaa5c5802f0b8b2
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 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(); int x=sc.nextInt(); int y=sc.nextInt(); long arr[]=new long[n+1]; arr[0]=0; long sum=0; for(int i=1;i<n+1;i++) { long b=arr[i-1]+x; if(b<=B) { arr[i]=b; } else { long c=arr[i-1]-y; arr[i]=c; } } for(int i=0;i<n+1;i++) { sum=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 11
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
50af87ebe54e0bcf6e339404341cbe61
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.text.Segment; import java.io.*; import java.lang.reflect.Array; import java.math.BigInteger; import static java.lang.Math.*; import java.util.*; public class Main { static public void main(String[] args){ out = new PrintWriter(new BufferedOutputStream(System.out)); in = new FastReader(); int t =in.nextInt(); while(t>0){ t--; solve(); } out.close(); } static void solve(){ int a = in.nextInt(); long b = in.nextInt(); long x = in.nextInt(); long y = in.nextInt(); long now = 0; long ans =0; for(int i=0;i<a;i++){ if(now+x>b){ now = now - y; ans+=now; }else{ now = now +x; ans+=now; } } out.println(ans); } public static FastReader in; 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; } } static long lcm(long a, long b) { return (a * b) / gcd(a, b); } static long gcd(long a, long b) { return (a % b == 0) ? b : 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 11
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
796b9ff8ecc13232c3dd85430a0cd81c
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 sc = new Scanner(System.in); int t = sc.nextInt(); while (t-- != 0) { long n = sc.nextLong(); long b = sc.nextLong(); long x = sc.nextLong(); long y = sc.nextLong(); int[] arr = new int[(int) (n + 1)]; arr[0] = 0; long sum = 0; for (int i = 1; i < n + 1; i++) { if ((arr[i-1]+x) <= b) { arr[i] = (int) (arr[i - 1] + x); } else arr[i] = (int) (arr[i - 1] - y); } for (int i = 0; i < n+1 ; 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 11
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
e634ee470343b2b19f83b9f4deb4bcaf
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 sc = new Scanner(System.in); int t = sc.nextInt(); for (int j = 0; j < t; j++) { int n = sc.nextInt(); long b= sc.nextLong(); long x= sc.nextLong(); long y= sc.nextLong(); long sum =0; long p =0; long next; for (int i = 1; i <=n ; i++) { if ((p + x) > b){ next = p - y; p = next; sum += p; } else { next = p + x; p = next; sum += p; } } 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 11
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
5cc033df4cf19d89080333a63a3d66c9
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 { Scanner scn = new Scanner(System.in); int t = scn.nextInt(); while(t-- > 0){ long n = scn.nextLong(), B = scn.nextLong(), x = scn.nextLong(), y = scn.nextLong(); long sum = 0, cur = 0, idx = 1; while(idx++ <= n){ if(cur + x <= B){ cur = cur + x; }else{ cur = cur - y; } sum += 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 11
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
6c7d8a2c5f50651d90656cd55dcc5c60
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.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; import java.util.Arrays; public class Main { public static void main(String[] args) throws IOException { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); //solve (in, out); int tests = in.nextInt(); for (int i = 0;i < tests;i++) { solve (in, out); } } 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()); } } // main code ? static void solve (FastScanner in, PrintWriter out) { int n = in.nextInt(), b = in.nextInt(), x = in.nextInt(), y = in.nextInt(); int prev = 0; long sum = 0; for (int i = 0;i < n;++i) { int add = prev + x, sub = prev - y; prev = add > b ? sub : add; sum += prev; } out.println(sum); out.flush(); } }
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 11
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
b823fae0c50867defc18f1c3d9b98c98
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.Arrays; import java.util.StringTokenizer; public class TaskB { public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = 1; t = in.nextInt(); for (int tc = 1; tc <= t; tc++) { // out.printf("Case #%d: ", tc); TaskB s = new TaskB(); s.solve(in, out); } out.flush(); } void solve(FastScanner in, PrintWriter out) { int n = in.nextInt(); long b = in.nextLong(), x = in.nextLong(), y = in.nextLong(); long[] a = new long[n+1]; 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]; } out.println(sum); } 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()); } float nextFloat() { return Float.parseFloat(next()); } 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 11
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
7394ae8a49dd2c2b8de4b51d474f3c1a
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.reflect.Array; import java.util.*; import java.util.stream.IntStream; import java.util.stream.Stream; public class Main { public static void main(String[] args) { in = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); try { int t = in.nextInt(); while(t-- > 0) { solve(); out.println();} // solve(); } finally { out.close(); } return; } public static void solve() { long n = in.nextLong(), B = in.nextLong(), x = in.nextLong(), y = in.nextLong(); long sum = 0; long curr = 0; for(int i = 1; i <= n; i++) { if(curr + x <= B) { curr += x; } else { curr -= y; } sum += curr; } out.print(sum); return; } //-------------- Helper methods------------------- public static int[] fillArray(int n) { int[] array = new int[n]; for(int i = 0; i < n; i++) { array[i] = in.nextInt(); } return array; } public static char[] fillArray() { char[] array = in.next().toCharArray(); return array; } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; public static MyScanner in; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { 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; } } static void shuffleArray(int[] arr){ int n = arr.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = arr[i]; int randomPos = i + rnd.nextInt(n-i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } //-------------------------------------------------------- }
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 11
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
f50dc49d07377ff8548f9a3664573157
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.Math; import java.lang.reflect.Array; import java.util.*; import javax.swing.text.DefaultStyledDocument.ElementSpec; public final class Solution { static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) ); static BufferedWriter bw = new BufferedWriter( new OutputStreamWriter(System.out) ); static StringTokenizer st; /*write your constructor and global variables here*/ static class sortCond implements Comparator<Pair<Integer, Integer>> { @Override public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) { if (p1.a <= p2.a) { return -1; } else { return 1; } } } static class Rec { int a; int b; long c; Rec(int a, int b, long c) { this.a = a; this.b = b; this.c = c; } } 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; while ((1 << i) <= no) { i++; } if ((1 << (i - 1)) == no) { return i - 1; } else { return 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 (int) ((1l * a % mod - 1l * b % mod + 1l * mod) % mod); }; static modOperations modMul = (int a, int b, int mod) -> { return (int) ((1l * (a % mod) * 1l * (b % mod)) % (1l * 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[2] = 1; for (int i = 3; 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 Pair<Long, Integer> ret_ans(ArrayList<Integer> ans) { int size = ans.size(); int mini = 1000000000 + 1; long tit = 0l; for (int i = 0; i < size; i++) { tit += 1l * ans.get(i); mini = Math.min(mini, ans.get(i)); } return new Pair<>(tit - mini, mini); } 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; } static ArrayList<Integer> getKeys(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getKey()); } return vals; } static ArrayList<Integer> getValues(HashMap<Integer, Integer> maps) { ArrayList<Integer> vals = new ArrayList<>(); for (Map.Entry<Integer, Integer> map : maps.entrySet()) { vals.add(map.getValue()); } return vals; } /*write your methods here*/ static int getMax(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) > max) { max = arr.get(i); } } return max; } static int getMin(ArrayList<Integer> arr) { int max = arr.get(0); for (int i = 1; i < arr.size(); i++) { if (arr.get(i) < max) { max = arr.get(i); } } return max; } public static void main(String[] args) throws IOException { int cases = Integer.parseInt(br.readLine()), n, b, x, y, i, j; while (cases-- != 0) { st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); b = Integer.parseInt(st.nextToken()); x = Integer.parseInt(st.nextToken()); y = Integer.parseInt(st.nextToken()); long tot = 0l; long val = 0l; for (i = 0; i < n; i++) { if (val + 1l * x > b) { val = val - 1l * y; } else { val = val + 1l * x; } tot += val; } bw.write(Long.toString(tot) + "\n"); } bw.flush(); } }
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 11
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
3357622040833b856523c46b59bc4231
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 Submit { public static void main(String[] args) { try (InputReader reader = new InputReader()) { int t = reader.nextInt(); while (t-- > 0) { int n = reader.nextInt(); int b = reader.nextInt(); int x = reader.nextInt(); int y = reader.nextInt(); long sum = 0; int o1; int o2; int[] array = new int[n + 1]; for (int i = 1; i <= n; i++) { o1 = array[i-1] + x; o2 = array[i-1] - y; if (o1 <= b) { sum += o1; array[i] = o1; } else { sum += o2; array[i] = o2; } } System.out.println(sum); } } } } class InputReader implements AutoCloseable { private BufferedReader bufferedReader; private StringTokenizer tokenizer; public InputReader() { bufferedReader = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (tokenizer == null || !tokenizer.hasMoreElements()) { try { tokenizer = new StringTokenizer(bufferedReader.readLine()); } catch (IOException ex) { ex.printStackTrace(); } } return tokenizer.nextToken(); } public char nextChar() { char character = ' '; try { character = (char) bufferedReader.read(); } catch (IOException e) { e.printStackTrace(); } return character; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { String line = ""; try { line = bufferedReader.readLine(); } catch (IOException ex) { ex.printStackTrace(); } return line; } @Override public void close() { try { this.bufferedReader.close(); } catch (IOException ex) { ex.printStackTrace(); } } }
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 11
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
71de719b49e5ff34339fe1ad05360819
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 a2 { public static void main(String [] args) { Scanner s=new Scanner(System.in); int k=s.nextInt(); while(k-->0) { int n=s.nextInt(); int b=s.nextInt(); int x=s.nextInt(); int y=s.nextInt(); long sum=0; long f=0; for(int i=0;i<n;i++) { if(f+x<=b) { f=f+x; sum+=f; } else { f=f-y; sum+=f; } } System.out.println(sum+" "); } } public static int gcd(int a,int b) { return b==0?a:gcd(b,a%b); } public static int lcm(int a,int b) { return a*b/gcd(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 11
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
9bbb8ce2e093cecd527edb4a958b2e7f
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.*; public class Sequence { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int t = scan.nextInt(); StringBuilder result = new StringBuilder(); for(int i = 0; i < t; i++) { int n = scan.nextInt(); long B = scan.nextLong(); int x = scan.nextInt(); int y = scan.nextInt(); long sum = 0; int j = 0; long next = 0; while(j < n) { //System.out.println(j + " : " + next + " : " + sum); if(next + x <= B) { next += x; } else { next -= y; } sum += next; j++; } result.append(sum + "\n"); } System.out.println(result); } }
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 11
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
2f944243c05377910c8303debc4e3718
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
/*LoudSilence*/ import java.io.*; import java.net.CookieHandler; import java.util.*; import static java.lang.Math.*; public class Solution { /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static FastScanner s = new FastScanner(); static FastWriter out = new FastWriter(); final static int mod = (int)1e9 + 7; final static int INT_MAX = Integer.MAX_VALUE; final static int INT_MIN = Integer.MIN_VALUE; final static long LONG_MAX = Long.MAX_VALUE; final static long LONG_MIN = Long.MIN_VALUE; final static double DOUBLE_MAX = Double.MAX_VALUE; final static double DOUBLE_MIN = Double.MIN_VALUE; final static float FLOAT_MAX = Float.MAX_VALUE; final static float FLOAT_MIN = Float.MIN_VALUE; /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ static class FastScanner{BufferedReader br;StringTokenizer st; public FastScanner() {if(System.getProperty("ONLINE_JUDGE") == null){try {br = new BufferedReader(new FileReader("E:\\Competitive Coding\\input.txt"));} catch (FileNotFoundException e) {br = new BufferedReader(new InputStreamReader(System.in));}}else{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());} List<Integer> readIntList(int n){List<Integer> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextInt()); return arr;} List<Long> readLongList(int n){List<Long> arr = new ArrayList<>(); for(int i = 0; i < n; i++) arr.add(s.nextLong()); return arr;} int[] readIntArr(int n){int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = s.nextInt(); return arr;} long[] readLongArr(int n){long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = s.nextLong(); return arr;} String nextLine(){String str = "";try{str = br.readLine();}catch (IOException e){e.printStackTrace();}return str;}} static class FastWriter{private BufferedWriter bw;public FastWriter(){if(System.getProperty("ONLINE_JUDGE") == null){try {this.bw = new BufferedWriter(new FileWriter("E:\\Competitive Coding\\output.txt"));} catch (IOException e) {this.bw = new BufferedWriter(new OutputStreamWriter(System.out));}}else{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 debug(int object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void debug(long object[]) throws IOException{bw.append("["); for(int i = 0; i < object.length; i++){if(i != object.length-1){print(object[i]+", ");}else{print(object[i]);}}bw.append("]\n");} public void close() throws IOException{bw.close();}} public static void println(Object str) throws IOException{out.println(""+str);} public static void println(Object str, int nextLine) throws IOException{out.print(""+str);} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static ArrayList<Integer> seive(int n){ArrayList<Integer> list = new ArrayList<>();int arr[] = new int[n+1];for(long i = 2; i <= n; i++) {if(arr[(int)i] == 1) {continue;}else {list.add((int)i);for(long j = i*i; j <= n; j = j + i) {arr[(int)j] = 1;}}}return list;} public static long gcd(long a, long b){if(a > b) {a = (a+b)-(b=a);}if(a == 0L){return b;}return gcd(b%a, a);} public static void swap(int[] arr, int i, int j) {arr[i] = arr[i] ^ arr[j]; arr[j] = arr[j] ^ arr[i]; arr[i] = arr[i] ^ arr[j];} public static boolean isPrime(long n){if(n < 2){return false;}if(n == 2 || n == 3){return true;}if(n%2 == 0 || n%3 == 0){return false;}long sqrtN = (long)Math.sqrt(n)+1;for(long i = 6L; i <= sqrtN; i += 6) {if(n%(i-1) == 0 || n%(i+1) == 0) return false;}return true;} public static long mod_add(long a, long b){ return (a%mod + b%mod)%mod;} public static long mod_sub(long a, long b){ return (a%mod - b%mod + mod)%mod;} public static long mod_mul(long a, long b){ return (a%mod * b%mod)%mod;} public static long modInv(long a, long b){ return expo(a, b-2)%b;} public static long mod_div(long a, long b){return mod_mul(a, modInv(b, mod));} public static long expo (long a, long n){if(n == 0){return 1;}long recAns = expo(mod_mul(a,a), n/2);if(n % 2 == 0){return recAns;}else{return mod_mul(a, recAns);}} /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Pair class public static class Pair<X extends Comparable<X>,Y extends Comparable<Y>> implements Comparable<Pair<X, Y>>{ X first; Y second; public Pair(X first, Y second){ this.first = first; this.second = second; } public String toString(){ return "( " + first+" , "+second+" )"; } @Override public int compareTo(Pair<X, Y> o) { int t = first.compareTo(o.first); if(t == 0) return second.compareTo(o.second); return t; } } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ // Code begins public static void solve() throws IOException { int n = s.nextInt(); int b = s.nextInt(); int x = s.nextInt(); int y = s.nextInt(); long arr[] = new long[n+1]; arr[0] = 0l; 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; } } long sum = 0l; for(long ele : arr){ sum+= ele; } println(sum); } public static double dis(double x, double y, double x1, double y1){ return sqrt(pow((x-x1),2)+pow((y-y1),2)); } /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ public static void main(String[] args) throws IOException { int test = s.nextInt(); for(int t = 1; t <= test; t++) { solve(); } 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 11
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
710d99db3b772b31dccd4d1d92d96bf4
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.*; import static java.lang.Math.*; import static java.util.Arrays.sort; public class Round12 { public static void main(String[] args) { FastReader fastReader = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = fastReader.nextInt(); while (t-- > 0) { int n = fastReader.nextInt(); int b = fastReader.nextInt(); int x = fastReader.nextInt(); int y = fastReader.nextInt(); long sum = 0; long prev = 0; while (n > 0) { if (prev + x <= b) { prev += x; sum += prev; } else { prev -= y; sum += prev; } n--; } out.println(sum); } out.close(); } // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; static Random __r = new Random(); // math util static int minof(int a, int b, int c) { return min(a, min(b, c)); } static int minof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static long minof(long a, long b, long c) { return min(a, min(b, c)); } static long minof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static int maxof(int a, int b, int c) { return max(a, max(b, c)); } static int maxof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static long maxof(long a, long b, long c) { return max(a, max(b, c)); } static long maxof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static int powi(int a, int b) { if (a == 0) return 0; int ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static long powl(long a, int b) { if (a == 0) return 0; long ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static int fli(double d) { return (int) d; } static int cei(double d) { return (int) ceil(d); } static long fll(double d) { return (long) d; } static long cel(double d) { return (long) ceil(d); } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static int[] exgcd(int a, int b) { if (b == 0) return new int[]{1, 0}; int[] y = exgcd(b, a % b); return new int[]{y[1], y[0] - y[1] * (a / b)}; } static long[] exgcd(long a, long b) { if (b == 0) return new long[]{1, 0}; long[] y = exgcd(b, a % b); return new long[]{y[1], y[0] - y[1] * (a / b)}; } static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static long mix(long x) { x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31); } public static boolean[] findPrimes(int limit) { assert limit >= 2; final boolean[] nonPrimes = new boolean[limit]; nonPrimes[0] = true; nonPrimes[1] = true; int sqrt = (int) Math.sqrt(limit); for (int i = 2; i <= sqrt; i++) { if (nonPrimes[i]) continue; for (int j = i; j < limit; j += i) { if (!nonPrimes[j] && i != j) nonPrimes[j] = true; } } return nonPrimes; } // array util static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } 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()); } int[] ria(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = Integer.parseInt(next()); return a; } long nextLong() { return Long.parseLong(next()); } long[] rla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = Long.parseLong(next()); return a; } 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 11
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
38c8bad864547f67ca80f5998a7eb7e0
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.*; public class P03 { 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());} float nextFloat(){return Float.parseFloat(next());} String nextLine(){ String str = ""; try { str = br.readLine(); }catch(IOException e) { e.printStackTrace(); } return str ; } } static class BPair { Integer index;boolean value; public BPair(Integer index, boolean value) {this.value =value;this.index =index;} int getIndex() {return index;} boolean getValue() {return value;} } static class Pair implements Comparable<Pair> { Integer index,value; public Pair(Integer index, Integer value) {this.value = value;this.index = index;} @Override public int compareTo(Pair o){return value - o.value;} int getValue(){ return value;} int getindex(){return index;} } private static long gcd(long l, long m){ if(m==0) {return l;} return gcd(m,l%m); } static void swap(long[] arr, int i, int j){long temp = arr[i];arr[i] = arr[j];arr[j] = temp;} static int partition(long[] arr, int low, int high){ long pivot = arr[high]; int i = (low - 1); for(int j = low; j <= high - 1; j++){ if (arr[j] < pivot){ i++; swap(arr, i, j); } } swap(arr, i + 1, high); return (i + 1); } static void quickSort(long[] arr, int low, int high){ if (low < high){ int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } static long M = 1000000007; static long powe(long a,long b) { long res =1; while(b>0){ if((b&1)!=0) { res=(res*a)&M; } a=(a*a)%M; b>>=1; } return res; } 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; } static boolean [] seive(int n) { boolean a[] = new boolean[n+1]; Arrays.fill(a, true); a[0]=false; a[1]=false; for(int i =2;i<=Math.sqrt(n);i++) { for(int j =2*i;j<=n;j+=i) { a[j]=false; } } return a; } private static void pa(char[] cs){for(int i =0;i<cs.length;i++){System.out.print(cs[i]+" ");}System.out.println();} private static void pa(long[] b){for(int i =0;i<b.length;i++) {System.out.print(b[i]+" ");}System.out.println();} private static void pm(Character[][] 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();}} private static void pm(int [][] a) {for(int i =0;i<a.length;i++) {for(int j=0;j<a[0].length;j++) {System.out.print(a[i][j]+" ");}System.out.println();}} private static void pa(int[] a){for(int i =0;i<a.length;i++){System.out.print(a[i]+" ");}System.out.println();} private static boolean isprime(int n){if (n <= 1) return false;else if (n == 2) return true;else if (n % 2 == 0) return false;for (int i = 3; i <= Math.sqrt(n); i += 2){if (n % i == 0) return false;}return true;} static int lb(long[] b, long a){ int l=-1,r=b.length; while(l+1<r) { int m=(l+r)>>>1; if(b[m]>=a) r=m; else l=m; } return r; } static int ub(long a[], long x){ int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static void rec(String digits,ArrayList<String> ans,String temp,int in ,HashMap<Integer,String>hm,int l){ if(temp.length()==l){ ans.add(temp); return ; } String s = hm.get(digits.charAt(in)-'0'); int n = s.length(); for(int i =0;i<n;i++){ rec(digits,ans,temp+s.charAt(i),in+1,hm,l); } } public static int longestCommonSubsequence(String text1, String text2) { int n = text1.length(); int m = text2.length(); int dp [][]= new int [n+1][m+1]; for(int i =0;i<=n;i++){ for(int j =0;j<=m;j++){ if(i==0||j==0)dp[i][j]=0; else if(text1.charAt(i-1)==text2.charAt(j-1)){ dp[i][j]=1+dp[i-1][j-1]; } else dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1]); } } return dp[n][m]; } static //MAIN FUNCTION -------> ArrayList<String> alpos = new ArrayList<>(); public static void main(String[] args) throws IOException { long start2 = System.currentTimeMillis(); // FastReader fs = new FastReader(); Scanner fs = new Scanner (System.in); int t = fs.nextInt(); // int t =1; while(t-->0) { int n = fs.nextInt(); long B = fs.nextLong(); long x = fs.nextLong(); long y = fs.nextLong(); long sum = 0; long a [] = new long [n+1]; a[0]=0; for(int i =1;i<=n;i++) { if(a[i-1]+x>B) { a[i]=a[i-1]-y; } else a[i]=a[i-1]+x; sum+=a[i]; } System.out.println(sum); } long end2 = System.currentTimeMillis(); // System.out.println("time :"+(end2-start2)); // sc.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 11
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
64c05aa9741b6359f3175069ec8f5630
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 cf1657B { public static void main(String[] args) { FastReader sc = new FastReader(); int t = sc.nextInt(); while(t-->0) { int n = sc.nextInt(); //n+1 numbers in array int b = sc.nextInt(); //each a should not exceed b int x = sc.nextInt(); //add x int y = sc.nextInt(); //minus y long sum = 0; long 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); } } 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 11
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
43ae3182470225ba6559039a612d9297
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.Arrays; import java.util.InputMismatchException; /** * Provide prove of correctness before implementation. Implementation can cost a lot of time. * Anti test that prove that it's wrong. * <p> * Do not confuse i j k g indexes, upTo and length. Do extra methods!!! Write more informative names to simulation * <p> * Will program ever exceed limit? * Try all approaches with prove of correctness if task is not obvious. * If you are given formula/rule: Try to play with it. * Analytic solution/Hardcoded solution/Constructive/Greedy/DP/Math/Brute force/Symmetric data * Number theory * Game theory (optimal play) that consider local and global strategy. */ public class B { private void solveOne() { int n = nextInt(); int B = nextInt(); int x = nextInt(); int y = nextInt(); long[] a = new long[n + 1]; long ans = 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; } ans += a[i]; } System.out.println(ans); } private void solve() { int t = nextInt(); for (int tt = 0; tt < t; tt++) { solveOne(); } } class AssertionRuntimeException extends RuntimeException { AssertionRuntimeException(Object expected, Object actual, Object... input) { super("expected = " + expected + ",\n actual = " + actual + ",\n " + Arrays.deepToString(input)); } } private int nextInt() { return System.in.readInt(); } private long nextLong() { return System.in.readLong(); } private String nextString() { return System.in.readString(); } private int[] nextIntArr(int n) { return System.in.readIntArray(n); } private long[] nextLongArr(int n) { return System.in.readLongArray(n); } public static void main(String[] args) { new B().run(); } static class System { private static FastInputStream in; private static FastPrintStream out; } private void run() { final boolean ONLINE_JUDGE = java.lang.System.getProperty("ONLINE_JUDGE") != null; final boolean USE_IO = ONLINE_JUDGE; if (USE_IO) { System.in = new FastInputStream(java.lang.System.in); System.out = new FastPrintStream(java.lang.System.out); solve(); System.out.flush(); } else { final String nameIn = "input.txt"; final String nameOut = "output.txt"; try { System.in = new FastInputStream(new FileInputStream(nameIn)); System.out = new FastPrintStream(new PrintStream(nameOut)); solve(); System.out.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } } } private static class FastPrintStream { private static final int BUF_SIZE = 8192; private final byte[] buf = new byte[BUF_SIZE]; private final OutputStream out; private int ptr = 0; private FastPrintStream() { this(java.lang.System.out); } public FastPrintStream(OutputStream os) { this.out = os; } public FastPrintStream(String path) { try { this.out = new FileOutputStream(path); } catch (FileNotFoundException e) { throw new RuntimeException("FastWriter"); } } public FastPrintStream print(byte b) { buf[ptr++] = b; if (ptr == BUF_SIZE) innerflush(); return this; } public FastPrintStream print(char c) { return print((byte) c); } public FastPrintStream print(char[] s) { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } return this; } public FastPrintStream print(String s) { s.chars().forEach(c -> { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); }); return this; } //can be optimized public FastPrintStream print0(char[] s) { if (ptr + s.length < BUF_SIZE) { for (char c : s) { buf[ptr++] = (byte) c; } } else { for (char c : s) { buf[ptr++] = (byte) c; if (ptr == BUF_SIZE) innerflush(); } } return this; } //can be optimized public FastPrintStream print0(String s) { if (ptr + s.length() < BUF_SIZE) { for (int i = 0; i < s.length(); i++) { buf[ptr++] = (byte) s.charAt(i); } } else { for (int i = 0; i < s.length(); i++) { buf[ptr++] = (byte) s.charAt(i); if (ptr == BUF_SIZE) innerflush(); } } return this; } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } public FastPrintStream print(int x) { if (x == Integer.MIN_VALUE) { return print((long) x); } if (ptr + 12 >= BUF_SIZE) innerflush(); if (x < 0) { print((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } private static int countDigits(long l) { if (l >= 1000000000000000000L) return 19; if (l >= 100000000000000000L) return 18; if (l >= 10000000000000000L) return 17; if (l >= 1000000000000000L) return 16; if (l >= 100000000000000L) return 15; if (l >= 10000000000000L) return 14; if (l >= 1000000000000L) return 13; if (l >= 100000000000L) return 12; if (l >= 10000000000L) return 11; if (l >= 1000000000L) return 10; if (l >= 100000000L) return 9; if (l >= 10000000L) return 8; if (l >= 1000000L) return 7; if (l >= 100000L) return 6; if (l >= 10000L) return 5; if (l >= 1000L) return 4; if (l >= 100L) return 3; if (l >= 10L) return 2; return 1; } public FastPrintStream print(long x) { if (x == Long.MIN_VALUE) { return print("" + x); } if (ptr + 21 >= BUF_SIZE) innerflush(); if (x < 0) { print((byte) '-'); x = -x; } int d = countDigits(x); for (int i = ptr + d - 1; i >= ptr; i--) { buf[i] = (byte) ('0' + x % 10); x /= 10; } ptr += d; return this; } public FastPrintStream print(double x, int precision) { if (x < 0) { print('-'); x = -x; } x += Math.pow(10, -precision) / 2; // if(x < 0){ x = 0; } print((long) x).print("."); x -= (long) x; for (int i = 0; i < precision; i++) { x *= 10; print((char) ('0' + (int) x)); x -= (int) x; } return this; } public FastPrintStream println(int[] a, int from, int upTo, char separator) { for (int i = from; i < upTo; i++) { print(a[i]); print(separator); } print('\n'); return this; } public FastPrintStream println(int[] a) { return println(a, 0, a.length, ' '); } public FastPrintStream println(long[] a, int from, int upTo, char separator) { for (int i = from; i < upTo; i++) { print(a[i]); print(separator); } print('\n'); return this; } public FastPrintStream println(long[] a) { return println(a, 0, a.length, ' '); } public FastPrintStream println(char c) { return print(c).println(); } public FastPrintStream println(int x) { return print(x).println(); } public FastPrintStream println(long x) { return print(x).println(); } public FastPrintStream println(String x) { return print(x).println(); } public FastPrintStream println(double x, int precision) { return print(x, precision).println(); } public FastPrintStream println() { return print((byte) '\n'); } public FastPrintStream printf(String format, Object... args) { return print(String.format(format, args)); } private void innerflush() { try { out.write(buf, 0, ptr); ptr = 0; } catch (IOException e) { throw new RuntimeException("innerflush"); } } public void flush() { innerflush(); try { out.flush(); } catch (IOException e) { throw new RuntimeException("flush"); } } } private static class FastInputStream { private boolean finished = false; private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public FastInputStream(InputStream stream) { this.stream = stream; } public double[] readDoubleArray(int size) { double[] array = new double[size]; for (int i = 0; i < size; i++) { array[i] = readDouble(); } return array; } public String[] readStringArray(int size) { String[] array = new String[size]; for (int i = 0; i < size; i++) { array[i] = readString(); } return array; } public char[] readCharArray(int size) { char[] array = new char[size]; for (int i = 0; i < size; i++) { array[i] = readCharacter(); } return array; } public char[][] readTable(int rowCount, int columnCount) { char[][] table = new char[rowCount][]; for (int i = 0; i < rowCount; i++) { table[i] = this.readCharArray(columnCount); } return table; } public int[][] readIntTable(int rowCount, int columnCount) { int[][] table = new int[rowCount][]; for (int i = 0; i < rowCount; i++) { table[i] = readIntArray(columnCount); } return table; } public double[][] readDoubleTable(int rowCount, int columnCount) { double[][] table = new double[rowCount][]; for (int i = 0; i < rowCount; i++) { table[i] = this.readDoubleArray(columnCount); } return table; } public long[][] readLongTable(int rowCount, int columnCount) { long[][] table = new long[rowCount][]; for (int i = 0; i < rowCount; i++) { table[i] = readLongArray(columnCount); } return table; } public String[][] readStringTable(int rowCount, int columnCount) { String[][] table = new String[rowCount][]; for (int i = 0; i < rowCount; i++) { table[i] = this.readStringArray(columnCount); } return table; } public String readText() { StringBuilder result = new StringBuilder(); while (true) { int character = read(); if (character == '\r') { continue; } if (character == -1) { break; } result.append((char) character); } return result.toString(); } public void readStringArrays(String[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) { arrays[j][i] = readString(); } } } public long[] readLongArray(int size) { long[] array = new long[size]; for (int i = 0; i < size; i++) { array[i] = readLong(); } return array; } public int[] readIntArray(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) { array[i] = readInt(); } return array; } 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 peek() { if (numChars == -1) { return -1; } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) { return -1; } } return buf[curChar]; } public int peekNonWhitespace() { while (isWhitespace(peek())) { read(); } return peek(); } 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 long readLong() { 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; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { if (Character.isValidCodePoint(c)) { res.appendCodePoint(c); } c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private String readLine0() { StringBuilder buf = new StringBuilder(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') { buf.appendCodePoint(c); } c = read(); } return buf.toString(); } public String readLine() { String s = readLine0(); while (s.trim().length() == 0) { s = readLine0(); } return s; } public String readLine(boolean ignoreEmptyLines) { if (ignoreEmptyLines) { return readLine(); } else { return readLine0(); } } public char readCharacter() { int c = read(); while (isSpaceChar(c)) { c = read(); } return (char) c; } public double readDouble() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') { return res * Math.pow(10, readInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') { return res * Math.pow(10, readInt()); } if (c < '0' || c > '9') { throw new InputMismatchException(); } m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public boolean isExhausted() { int value; while (isSpaceChar(value = peek()) && value != -1) { read(); } return value == -1; } public String next() { return readString(); } public SpaceCharFilter getFilter() { return filter; } public void setFilter(SpaceCharFilter filter) { this.filter = filter; } 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 11
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
367b52adc3a9f7c43ccd1faf05495f89
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 C_1_E { 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(),B=input.nextInt(),x=input.nextInt(),y=input.nextInt(); long sum=0; int a=0; for(int j=1;j<=n;j++){ if(a+x>B){ a-=y; }else{ a+=x; } sum+=a; } 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 11
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
f70d6958da43926913ed938e79edf9af
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.reflect.Array; import java.util.*; public class Main { static Kattio io; static long mod = 998244353, inv2 = 499122177; static { io = new Kattio(); } public static void main(String[] args) { int t = io.nextInt(); for (int i = 0; i < t; i++) { solve(); } io.close(); } private static void solve() { int n = io.nextInt(), d = io.nextInt(), inc = io.nextInt(), dec = io.nextInt(); long curr = 0, prev = 0, ans = 0; for (int i = 0; i < n; i++) { if(curr+inc>d){ curr -= dec; }else { curr+=inc; } ans += curr; } io.println(ans); } static class pair implements Comparable<pair> { private final int x; private final int y; int id; public pair(final int x, final int y) { this.x = x; this.y = y; } @Override public String toString() { return "pair{" + "x=" + x + ", y=" + y + '}'; } @Override public boolean equals(final Object o) { if (this == o) { return true; } if (!(o instanceof pair)) { return false; } final pair pair = (pair) o; if (x != pair.x) { return false; } if (y != pair.y) { return false; } return true; } @Override public int hashCode() { int result = x; result = (int) (31 * result + y); return result; } @Override public int compareTo(pair pair) { return Integer.compare(pair.x, this.x); } } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(new FileWriter(problemName + ".out")); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.readLine()); return st.nextToken(); } catch (Exception e) {} return null; } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(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 11
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
6a719b7f4fb1fbb9819ad919e3569ace
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 int mod = (int) (1e9 + 7); static StringBuilder sb=new StringBuilder(); static void solve() { int n=i(); int b=i(); int x=i(); long ans=0; int y=i(); int[]arr=new int[n+1]; for(int i=1;i<arr.length;i++){ int val=arr[i-1]+x; if(val>b){ arr[i]=arr[i-1]-y; }else{ arr[i]=val; } ans+=arr[i]; } sb.append(ans+"\n"); } public static void main(String[] args) { int test = i(); while (test-- > 0) { solve(); } System.out.println(sb); } // -----> POWER ---> long power(long x, long y) <---- power // -----> LCM ---> long lcm(long x, long y) <---- lcm // -----> GCD ---> long gcd(long x, long y) <---- gcd // -----> NCR ---> long ncr(int n, int r) <---- ncr // -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<-- // -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<- // tempstart static 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 Int() { 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 String() { 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 String(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static InputReader in = new InputReader(System.in); public static int i() { return in.Int(); } public static long l() { String s = in.String(); return Long.parseLong(s); } public static String s() { return in.String(); } }
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 11
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
9b876b2c82cc9b8c309af0bbe98641ff
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.*; public class Practice { 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 prev = 0; long ans=0; for (int i = 0; i < n; i++) { if ( prev+x <= b ) { prev+= x; ans+=prev; } else { prev=prev-y; ans += prev; } //System.out.println(ans); } 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 11
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
3062ec53188682cd19dfda75ddeb7b82
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 { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int t = Integer.parseInt(st.nextToken()); for (int j = 0; j < t; j++) { //System.out.println("j = " + j); 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()); int curr = 0; long sum = 0; for (int i = 0; i < n; i++) { if (curr + x <= b) { curr += x; } else { curr -= y; } //System.out.println("i = " + i + ", curr = " + curr); sum += curr; } 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 11
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
d3e68a08f309310347f97b1dffe8465b
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 X { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(); int B = scanner.nextInt(); int x = scanner.nextInt(); int y = scanner.nextInt(); long sum = 0; int a = 0; for (int i = 1; i<=n; ++i) { if(a + x <= B) { a = a+x; } else { a = a-y; } sum = sum + a; } 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 11
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
05a3ee3f75a65bb680a5feb96efaa8b0
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.security.KeyStore.Entry; import java.util.*; public class code { private static boolean[] vis; private static long[] dist; private static long mod = 1000000000 + 7; public static void merge(int 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 */ int L[] = new int [n1]; int R[] = new int [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 sort(int 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); } } private static long gcd(long n, long l) { if (l == 0) return n; return gcd(l, n % l); } private static int gcd(int n, int l) { if (l == 0) return n; return gcd(l, n % l); } public static void dfs(ArrayList<Integer>[] gr,int v) { vis[v]=true; ArrayList<Integer> arr=gr[v]; for(int i=0;i<arr.size();i++) { if(!vis[arr.get(i)]) { dfs(gr,arr.get(i)); } } } private static class Pairs implements Comparable<Pairs>{ String v;int i;int j; public Pairs(String a,int b,int c){ v=a; i=b; j=c; } @Override public int compareTo(Pairs arg0) { if(!this.v.equals(arg0.v)) return this.v.compareTo(arg0.v); else return arg0.i-this.i; } } private static class Pair implements Comparable<Pair>{ int v,i;Pair j; public Pair(int a,int b){ v=a; i=b; } public Pair(int a,Pair b) { v=a; j=b; } @Override public int compareTo(Pair arg0) { { if(this.i!=arg0.i) return this.i-arg0.i; else return arg0.v-this.v; } } } public static long mmi(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 y and x y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } private static long pow(long a, long b, long c) { if (b == 0) return 1; long p = pow(a, b / 2, c); p = (p * p) % c; return (b % 2 == 0) ? p : (a * p) % c; } public static int ub(ArrayList<Integer> array, int low,int high, int value) { while (low < high) { final int mid = (low + high) / 2; if (value >= array.get(mid)) { low = mid + 1; } else { high = mid; } } return low; } 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()+1; int b = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); int[] arr =new int[n]; arr[0] = 0; for(int i=1;i<n;i++) { int a1 = arr[i-1] + x; int a2 = arr[i-1] - y; if(a1<=b) { arr[i] = a1; } else { arr[i] = a2; } } long sum =0; 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 11
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
cdfd490eb6a693f56e206a39c7ab9683
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; import java.util.*; public class A { public static void main(String[] args) { // TODO Auto-generated method stub 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]; 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); } } }
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 11
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
1125f46c39c4db94cc85740fb45787a6
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
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class B1657 { public static void main(String[] args) { FastReader fs = new FastReader(); int t = fs.nextInt(); StringBuilder sb = new StringBuilder(); while (t-- > 0) { long n = fs.nextLong(), B = fs.nextLong(), x = fs.nextLong(), y = fs.nextLong(); long sum = 0, max = (long) 1e9; long start = 0; while (n-- > 0) { start += x; if (start > B || start > max) { start -= (x + y); } sum += start; } sb.append(sum).append("\n"); } System.out.println(sb); } private static int lowerBound(long[] arr, long val, int start) { int low = 0, high = arr.length - 1; while (low <= high) { int mid = (low + high) / 2; if (arr[mid] >= val) { high = mid - 1; } else { low = mid + 1; } } return low; } private static int upperBound(long[] arr, long val, int start) { int low = start, high = arr.length - 1; while (low < high) { int mid = (low + high) / 2; if (arr[mid] <= val) { low = mid + 1; } else { high = mid; } } return low; } private static boolean isPrime(long num) { if (num == 2L) { return true; } for (long i = 2; i * i <= num; i++) { if (num % i == 0) { return false; } } return true; } private static long getGcd(long a, long b) { if (b == 0) return a; return getGcd(b, a % b); } private static long binaryExponentiation(long a, long b) { long res = 1; while (b > 0) { if ((b & 1) == 1) res = res * a; a = a * a; b >>= 1; } return res; } 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; } long[] readArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = Integer.parseInt(next()); return arr; } } }
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 11
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
7b11bbcf144839b3d94d6c4ca205c13a
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.HashSet; import java.util.Scanner; import java.util.Set; public class sol{ 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 a[]=new long[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; } for(int i=0;i<=n;i++) { sum+=a[i]; } System.out.println(sum); } sc.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 11
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
2ed341abcd8470b9972c077f6135e6ec
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.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Codeforces { public static void main(String[] args) { try { FastScanner sc = new FastScanner(); int T = Integer.parseInt(sc.next()); while (T-- > 0) { long n = (long) Integer.parseInt(sc.next()); long b = (long) Integer.parseInt(sc.next()); long x = (long) Integer.parseInt(sc.next()); long y = (long) Integer.parseInt(sc.next()); long sum = 0; long lastNum = 0; long MOD = 1000000007; while (n > 0) { if (lastNum + x <= b) { lastNum = lastNum + x % MOD; sum += lastNum; } else if (lastNum - y <= b) { lastNum = lastNum - y; sum += lastNum; } n--; } System.out.println(sum); } } catch (Exception e) { // } } static void printArr(int arr[]) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println(); } 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()); } } }
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 11
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
a874c45917e15269edad391cfcdde352
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 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(int i, int j) { int temp = i; i = j; j = temp; } static class Pair{ int i,t; Pair(int x, int y) { i=x; t= y; } } static class Interval{ long st,e; Interval(long x, long y) { st=x; e=y; } } static long mod = 1000000007; static boolean ans= false; static StringBuffer sb = new StringBuffer(""); public static void main(String[] args) throws Exception { //Read input from user //Scanner scn = new Scanner(System.in); FastReader scn = new FastReader(); int t = scn.nextInt(); while(t>0) { int n = scn.nextInt(); long b = scn.nextLong(); long x = scn.nextLong(); long y = scn.nextLong(); long p = 0; long sum =0; for(int i=1;i<=n;i++) { if(p+x>b) { p = p-y; }else { p= p+x; } sum+=p; } 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 11
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
88c207d1477d79a88f0b375a03b53677
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 E { private 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) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } double nextDouble() { return Double.parseDouble(next()); } } public static void main(String[] args) { FastReader in = new FastReader(); int t = in.nextInt(); StringBuilder sb = new StringBuilder(); while(t-- > 0) { int n = in.nextInt(); long B = in.nextLong(); long x = in.nextLong(); long y = in.nextLong(); long sum = 0; long [] arr = new long [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; } sum += arr[i]; } 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 11
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
cb51306e598f0c9a08b8833f72de6a98
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(); int i; 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]; a[0] =0; for(i=1;i<n+1;i++){ if(a[i-1] + x <= b){ a[i] = a[i-1] + x ; } else{ a[i] = a[i-1] - y ; } } long sum = 0; for(i=1;i<n+1;i++){ sum = 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 11
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
e77001fa7d19ae598f8c8d0a33b7e65b
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){ long n=sc.nextLong(); long b=sc.nextLong(); long x=sc.nextLong(); long y=sc.nextLong(); long count=0; long prev=0; for(long i=0;i<n;i++){ if(prev<=b-x){ prev+=x; count+=prev; } else{ prev-=y; count+=prev; } // System.out.println("count="+count+" prev="+prev); } System.out.println(count); } } }
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 11
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
5c6734e03c61cc27ba83c559b5f527b6
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 cp1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t=sc.nextInt(); for (int tt = 0; tt < t; tt++) { int n=sc.nextInt(); int b=sc.nextInt(); int x =sc.nextInt(); int y =sc.nextInt(); long ans=0; long sum=0; for(int i=1;i<=n;i++){ if(ans+x<=b){ ans= ans+x; }else{ ans=ans-y; } sum=sum+ans; } System.out.println(sum); } sc.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 17
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
78ee4d03d151e4ded9339f01ac61343a
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.math.BigInteger; import java.util.*; import static java.lang.Math.*; import static java.lang.System.*; import static java.util.Arrays.*; import static java.util.stream.IntStream.iterate; public class Template { private static final FastScanner scanner = new FastScanner(); private static void solve() { int n = i(), B = i(), x = i(), y = i(); var a = new int[n+1]; for (int i = 1; i<=n; i++) { if ((a[i-1]+x)>B) a[i] = a[i-1]-y; else a[i] = a[i-1]+x; } out.println(stream(a).asLongStream().sum()); } public static void main(String[] args) { int t = i(); while (t-->0) { solve(); } } private static long fac(int n) { long res = 1; for (int i = 2; i<=n; i++) { res*=i; } return res; } private static BigInteger factorial(int n) { BigInteger res = BigInteger.valueOf(1); for (int i = 2; i <= n; i++){ res = res.multiply(BigInteger.valueOf(i)); } return res; } private static long l() { return scanner.nextLong(); } private static int i() { return scanner.nextInt(); } private static String s() { return scanner.next(); } private static void yes() { out.println("YES"); } private static void no() { out.println("NO"); } private static int toInt(char c) { return Integer.parseInt(c+""); } private static void printArray(long[] a) { StringBuilder builder = new StringBuilder(); for (long i : a) builder.append(i).append(' '); out.println(builder); } private static void printArray(int[] a) { StringBuilder builder = new StringBuilder(); for (int i : a) builder.append(i).append(' '); out.println(builder); } private static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } private static int binPow(int a, int n) { if (n == 0) return 1; if (n % 2 == 1) return binPow(a, n-1) * a; else { int b = binPow(a, n/2); return b * b; } } private static boolean isPrime(long n) { return iterate(2, i -> (long) i * i <= n, i -> i + 1).noneMatch(i -> n % i == 0); } private static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } private static void stableSort(int[] a) { List<Integer> list = stream(a).boxed().sorted().toList(); setAll(a, list::get); } private static int getKthMax(int[] a, int low, int high, int k) { int pivot = low; for (int j = low; j < high; j++) { if (a[j] <= a[high]) { swap(a, pivot++, j); } } swap(a, pivot, high); int count = high - pivot + 1; if (count == k) return a[pivot]; if (count > k) return getKthMax(a, pivot + 1, high, k); return getKthMax(a, low, pivot - 1, k - count); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(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[] readLong(int n) { long[] a = new long[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 class SegmentTreeRMXQ { static int getMid(int s, int e) { return s + (e - s) / 2; } static int MaxUtil(int[] st, int ss, int se, int l, int r, int node) { if (l <= ss && r >= se) return st[node]; if (se < l || ss > r) return -1; int mid = getMid(ss, se); return max( MaxUtil(st, ss, mid, l, r, 2 * node + 1), MaxUtil(st, mid + 1, se, l, r, 2 * node + 2)); } static void updateValue(int[] arr, int[] st, int ss, int se, int index, int value, int node) { if (index < ss || index > se) { System.out.println("Invalid Input"); return; } if (ss == se) { arr[index] = value; st[node] = value; } else { int mid = getMid(ss, se); if (index <= mid) updateValue(arr, st, ss, mid, index, value, 2 * node + 1); else updateValue(arr, st, mid + 1, se, index, value, 2 * node + 2); st[node] = max(st[2 * node + 1], st[2 * node + 2]); } } static int getMax(int[] st, int n, int l, int r) { if (l < 0 || r > n - 1 || l > r) { System.out.print("Invalid Input\n"); return -1; } return MaxUtil(st, 0, n - 1, l, r, 0); } static int constructSTUtil(int[] arr, int ss, int se, int[] st, int si) { if (ss == se) { st[si] = arr[ss]; return arr[ss]; } int mid = getMid(ss, se); st[si] = max( constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2)); return st[si]; } static int[] constructST(int[] arr, int n) { int x = (int)Math.ceil(Math.log(n) / Math.log(2)); int max_size = 2 * (int)Math.pow(2, x) - 1; int[] st = new int[max_size]; constructSTUtil(arr, 0, n - 1, st, 0); return st; } } static class SegmentTreeRMNQ { int[] st; int minVal(int x, int y) { return min(x, y); } int getMid(int s, int e) { return s + (e - s) / 2; } int RMQUtil(int ss, int se, int qs, int qe, int index) { if (qs <= ss && qe >= se) return st[index]; if (se < qs || ss > qe) return Integer.MAX_VALUE; int mid = getMid(ss, se); return minVal(RMQUtil(ss, mid, qs, qe, 2 * index + 1), RMQUtil(mid + 1, se, qs, qe, 2 * index + 2)); } int RMQ(int n, int qs, int qe) { if (qs < 0 || qe > n - 1 || qs > qe) { System.out.println("Invalid Input"); return -1; } return RMQUtil(0, n - 1, qs, qe, 0); } int constructSTUtil(int[] arr, int ss, int se, int si) { if (ss == se) { st[si] = arr[ss]; return arr[ss]; } int mid = getMid(ss, se); st[si] = minVal(constructSTUtil(arr, ss, mid, si * 2 + 1), constructSTUtil(arr, mid + 1, se, si * 2 + 2)); return st[si]; } void constructST(int[] arr, int n) { int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); int max_size = 2 * (int) Math.pow(2, x) - 1; st = new int[max_size]; // allocate memory constructSTUtil(arr, 0, n - 1, 0); } } static class SegmentTreeRSQ { int[] st; // The array that stores segment tree nodes /* Constructor to construct segment tree from given array. This constructor allocates memory for segment tree and calls constructSTUtil() to fill the allocated memory */ SegmentTreeRSQ(int[] arr, int n) { // Allocate memory for segment tree //Height of segment tree int x = (int) (Math.ceil(Math.log(n) / Math.log(2))); //Maximum size of segment tree int max_size = 2 * (int) Math.pow(2, x) - 1; st = new int[max_size]; // Memory allocation constructSTUtil(arr, 0, n - 1, 0); } // A utility function to get the middle index from corner indexes. int getMid(int s, int e) { return s + (e - s) / 2; } /* A recursive function to get the sum of values in given range of the array. The following are parameters for this function. st --> Pointer to segment tree si --> Index of current node in the segment tree. Initially 0 is passed as root is always at index 0 ss & se --> Starting and ending indexes of the segment represented by current node, i.e., st[si] qs & qe --> Starting and ending indexes of query range */ int getSumUtil(int ss, int se, int qs, int qe, int si) { // If segment of this node is a part of given range, then return // the sum of the segment if (qs <= ss && qe >= se) return st[si]; // If segment of this node is outside the given range if (se < qs || ss > qe) return 0; // If a part of this segment overlaps with the given range int mid = getMid(ss, se); return getSumUtil(ss, mid, qs, qe, 2 * si + 1) + getSumUtil(mid + 1, se, qs, qe, 2 * si + 2); } /* A recursive function to update the nodes which have the given index in their range. The following are parameters st, si, ss and se are same as getSumUtil() i --> index of the element to be updated. This index is in input array. diff --> Value to be added to all nodes which have I in range */ void updateValueUtil(int ss, int se, int i, int diff, int si) { // Base Case: If the input index lies outside the range of // this segment if (i < ss || i > se) return; // If the input index is in range of this node, then update the // value of the node and its children st[si] = st[si] + diff; if (se != ss) { int mid = getMid(ss, se); updateValueUtil(ss, mid, i, diff, 2 * si + 1); updateValueUtil(mid + 1, se, i, diff, 2 * si + 2); } } // The function to update a value in input array and segment tree. // It uses updateValueUtil() to update the value in segment tree void updateValue(int arr[], int n, int i, int new_val) { // Check for erroneous input index if (i < 0 || i > n - 1) { System.out.println("Invalid Input"); return; } // Get the difference between new value and old value int diff = new_val - arr[i]; // Update the value in array arr[i] = new_val; // Update the values of nodes in segment tree updateValueUtil(0, n - 1, i, diff, 0); } // Return sum of elements in range from index qs (query start) to // qe (query end). It mainly uses getSumUtil() int getSum(int n, int qs, int qe) { // Check for erroneous input values if (qs < 0 || qe > n - 1 || qs > qe) { System.out.println("Invalid Input"); return -1; } return getSumUtil(0, n - 1, qs, qe, 0); } // A recursive function that constructs Segment Tree for array[ss..se]. // si is index of current node in segment tree st int constructSTUtil(int arr[], int ss, int se, int si) { // If there is one element in array, store it in current node of // segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then recur for left and // right subtrees and store the sum of values in this node int mid = getMid(ss, se); st[si] = constructSTUtil(arr, ss, mid, si * 2 + 1) + constructSTUtil(arr, mid + 1, se, si * 2 + 2); return st[si]; } } }
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 17
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
13e584244b4e9f4aa41ad923f6ecc89c
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; import java.util.Scanner; import java.util.HashSet; import java.util.Set; public class Main { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int t = sc.nextInt(); while (t > 0) { tinh(); t = t - 1; } } public static void tinh() { int n =sc.nextInt(); long B = sc.nextLong(); long x = sc.nextInt(); long y = sc.nextLong(); long [] A = new long [n+1]; A[0]=0; long S=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; } } for(int i=0; i<=n; i++){ S=S+A[i]; } System.out.println(S); } public static int max(int a, int b){ if(a <b){ return b; }else{ return a; } } public static int min(int a, int b){ if(a > b){ return b; }else{ 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 17
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
842080218124ff6a0118c1053d309358
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 MyClass { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int tc = sc.nextInt(); while(tc-- > 0){ int n = sc.nextInt(); int b = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); int pre = 0; // int c = 0; long ans = 0; for(int i = 1; i <= n; i++){ if((pre+x) <= b){ pre = pre+x; ans += (long)pre; }else{ pre = pre-y; ans += (long)pre; } } 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 17
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
bccbabae982b61fcd059694be0000ff3
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.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class B { public static void main(String[] args) throws java.lang.Exception { BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter write = new BufferedWriter(new OutputStreamWriter(System.out)); long TestCases = Long.parseLong(read.readLine().trim()); for (int tItr = 0; tItr < TestCases; tItr++) { String[] str = read.readLine().toString().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]); // String string = read.readLine(); long total =0 ; int value = 0; // write.write(value + " "); for (int i = 0; i < n; i++) { int count = value + x; if(count <= b){ // write.write(count + " "); total += count; value = count; }else{ // while((value + x) > b){ value -= y; // } // value += y; total += value; // write.write(value+" "); } } write.write(total+"\n"); } write.flush(); write.close(); read.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
e79c4db53089ad790fc93166b2889cb5
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.Scanner; import java.io.*; import java.math.BigInteger; public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t; t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); int limit=sc.nextInt(); int plus=sc.nextInt(); int minus=sc.nextInt(); int[]ans=new int[n+1]; ans[0]=0; BigInteger sum=BigInteger.valueOf(0); for(int i=1;i<=n;i++) { if(ans[i-1]+plus<=limit) { ans[i]=ans[i-1]+plus; } else { ans[i]=ans[i-1]-minus; } } for(int i=0;i<=n;i++) { sum=sum.add(BigInteger.valueOf(ans[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
6b235331f1313d9622fff787caa7e6d9
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
0de92de45f65b953c80041f6668cdb79
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.math.BigInteger; import java.util.*; import java.util.Map; public class Main { public static void main(String[] args) throws IOException { BufferedReader cin = new BufferedReader(new InputStreamReader(System.in)); // Scanner sc = new Scanner(System.in); int X = Integer.parseInt(cin.readLine()); for (int i = 0; i < X; i++) { String[] s = cin.readLine().split(" "); int n,B,x,y; n = Integer.parseInt(s[0]); B = Integer.parseInt(s[1]); x = Integer.parseInt(s[2]); y = Integer.parseInt(s[3]); int[] arr = new int[n+1]; for (int j = 1; j <= n; j++) { if (arr[j-1]+x<=B){ arr[j] = arr[j-1]+x; }else { arr[j] = arr[j-1]-y; } } long sum = 0; for (int j = 0; j <= n; 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
6e48fa1e09eba487e36118f499d73018
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 hashcode; import java.io.*; //checkTriangle import java.util.*; //isSorted //isPrime //print //sort //input public class G { static PrintWriter out = new PrintWriter(System.out); static StringBuilder ans=new StringBuilder(); static FastReader in=new FastReader(); public static void main(String args[])throws IOException { int t=i(); outer:while(t-->0) { long n = l(); long B= l(); long x=l(); long y=l(); //n=n+1; long[] A = new long[(int)n+1]; A[0]=0;int i=1; while(i<n+1) { if(A[i-1]+x>B) { A[i]=A[i-1]-y; } else { A[i]=A[i-1]+x; } i++; } //print(A); long sum = Sum(A); out.println(sum); } out.close(); } public static long Sum(long[] A) { long sum=0; for(int i=0;i<A.length;i++) { sum+=A[i]; } return sum; } public static int checkTriangle(long a, long b, long c) { // check condition if (a + b <= c || a + c <= b || b + c <= a) return 0; else return 1; } static boolean isSorted(long A[]) { for(int i=1; i<A.length; i++)if(A[i]<A[i-1])return false; return true; } static int kadane(int A[]) { int lsum=A[0],gsum=A[0]; for(int i=1; i<A.length; i++) { lsum=Math.max(lsum+A[i],A[i]); gsum=Math.max(gsum,lsum); } return gsum; } static void print(char A[]) { for(char c:A)System.out.print(c); System.out.println(); } static void print(boolean A[]) { for(boolean c:A)System.out.print(c+" "); System.out.println(); } static void print(int A[]) { for(int a:A)System.out.print(a+" "); System.out.println(); } static void print(long A[]) { for(long i:A)System.out.print(i+ " "); System.out.println(); } static void print(ArrayList<Long> A) { for(long a:A)System.out.print(a+" "); System.out.println(); } static void sort(long[] a) //check for long { 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 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 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; } static long GCD(long a,long b) { if(b==0) { return a; } else return GCD(b,a%b ); } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static String S() { return in.next(); } 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; } } 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
3f04e98e7a4641e7c9fca5af6b4c4438
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.InputStreamReader; import java.io.IOException; public class Main{ private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static String readln() throws IOException{ String line = br.readLine(); return line; } public static void main(String[] args) throws IOException{ int testCases = Integer.parseInt(readln().split(" ")[0]); for(int i=0; i<testCases; i++){ String[] input = readln().split(" "); solve(Long.parseLong(input[0]), Long.parseLong(input[1]),Long.parseLong(input[2]), Long.parseLong(input[3])); } } private static void solve(long n, long B,long x,long y){ long sum= 0; long a = 0; for(long i = 0 ; i<n ; i++){ if( x+a <= B){ a = a+x; } else{ a = a-y; } sum = sum +a; } 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
8a07927aafabe97070c642fdb05b5735
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 class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { 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) throws Exception { FastScanner sc=new FastScanner(); int t=sc.nextInt(); while(t-->0){ int n=sc.nextInt(); long b=sc.nextLong(); long x=sc.nextLong(); long y=sc.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 sum=0; for(long v:arr) sum+=v; 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
f7ca8b78817df56f47aebab8a670f40e
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 sksss { public static void main(String args[]) { Scanner sc=new Scanner(System.in); sksss ob=new sksss(); int t=sc.nextInt(); for(int i=0;i<t;i++) { long n=sc.nextLong(); long B=sc.nextLong(); long x=sc.nextLong(); long y=sc.nextLong(); System.out.println(ob.compute(n,B,x,y)); } } long compute(long n,long B,long x,long y) { long s=0,sum=0; for(int i=0;i<n;i++){ s=s+x; if(s>B) s=s-(x+y); sum+=s; } return 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
9d9c0dc9b4d89db435098e14ae0fe269
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 handling { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scn=new Scanner(System.in); int t=scn.nextInt(); while(t>0) { int n=scn.nextInt(); long b=scn.nextLong(); long x=scn.nextLong(); long y=scn.nextLong(); long ans=0; long val=0; for(int i=1; i<=n; i++) { if(val+x<=b) { val+=x; ans+=val; }else { val-=y; ans+=val; } }System.out.println(ans); 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
05cd1f1e838ee7e5af28df44efa090f5
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; public class EduB { static FastScanner sc=new FastScanner(); static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { int T=sc.nextInt(); for (int tt=0; tt<T; tt++) { solve(); } } private static void solve() { // TODO Auto-generated method stub int n = sc.nextInt(); int B = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); ArrayList<Integer>ls = new ArrayList<>(); ls.add(0); int idx =0; while(n>0) { if(ls.get(idx)+x<=B) { ls.add(ls.get(idx)+x); idx++; } else if(ls.get(idx)-y<=B) { ls.add(ls.get(idx)-y); idx++; } n--; } long sum =0; for(int i=0;i<ls.size();i++) { sum+=ls.get(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
dcc9d6b2402fe8293d98a60c953eb2c3
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 codeforces2 { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for (int j = 0; j < t; j++) { long n=sc.nextLong(); long B=sc.nextLong(); long x=sc.nextLong(); long y=sc.nextLong(); long[] arr=new long[(int) (n+1)]; arr[0]=0; for (int i = 1; i < n+1; i++) { if(B>=arr[i-1]+x) arr[i]=arr[i-1]+x; else arr[i]=arr[i-1]-y; } long sum=0; for (int i = 0; i < n+1; 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
3a905278bdc0d71b24aa2309451b1716
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 ar[]) { 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 intial = 0; for (int i = 0; i < n; i++) { if (intial + x > b) { intial -= y; } else { intial += x; } sum += intial; } 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
68e445153b77b817ecdc66c7cc7a1f67
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 codeforces; import java.util.*; import java.io.*; public class Solution { 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[] arr= new long[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; } } long sum = 0; for(long cur:arr ) { sum+=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
93acbddeb0ccf49191737dcd18113af6
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.lang.*; import java.io.*; import java.util.*; public class My { public static void main(String[] args) { Scanner scn=new Scanner(System.in); int t=scn.nextInt(); while(t-->0) { long sum=0; long n=scn.nextLong(); long b=scn.nextLong(); long x=scn.nextLong(); long y=scn.nextLong(); if(b>=x) { long arr[]=new long[(int)n+1]; arr[0]=0; for(int i=1;i<(n+1);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); } else{ long arr[]=new long [(int)n+1]; arr[0]=0; for(int i=1;i<(n+1);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
e8704e7cbdc9de9e52879447b413a15b
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; import java.util.StringTokenizer; public class ThirdAttemptPORCODIO { public static void main(String[] args) { Scanner scan = new Scanner(System.in); StringTokenizer st; int t = Integer.parseInt(scan.nextLine()); int n; long b, x, y, prev; long[] result = new long[t]; long[] summatory; for(int i = 0; i < t; i++) { prev = 0; st = new StringTokenizer(scan.nextLine()); n = Integer.parseInt(st.nextToken()); b = Long.parseLong(st.nextToken()); x = Long.parseLong(st.nextToken()); y = Long.parseLong(st.nextToken()); summatory = new long[n]; for(int j = 0; j < n; j++) { if(prev + x <= b) { prev += x; } else { prev -= y; } summatory[j] = prev; } for(int j = 0; j < n; j++) result[i] += summatory[j]; } for(int i = 0; i < t; i++) System.out.println(result[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
b45c36ef89cd3321a459d07eb8d2dfdf
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.math.BigInteger; import java.util.*; public class Main { /// // Two theorams are used to find ncrp // Lucas theoram and the fermit theoram // a^p-1=1(modp) example 2 and 3 // Lucas theoram // ncr=ncr(n/mod,n/mod)*ncr1(n%p,r%mod); static long max = Integer.MAX_VALUE; //C0C2+C1C1+C2C0 static int catalanNumber(int n, int dp[]) { if (n <= 2) { return 1; } if (dp[n] != -1) { return dp[n]; } //C0C1+C1C2+C20 int answer = 0; for (int i = 0; i < n; ++i) { //01 //10 //This is the dynamic programming answer += catalanNumber(i, dp) * catalanNumber(n - i - 1, dp); // System.out.println(answer); } return dp[n] = answer; } static int printNthcatalanNumber(int n) { int dp[] = new int[n + 1]; Arrays.fill(dp, -1); return catalanNumber(n, dp); } static int power(int a, int b, int mod) { int res = 1; while (b > 0) { if (b % 2 == 1) { res *= a; } a *= a; b >>= 1; } return res; } static int fermittheoram(int a, int mod) { return power(a, mod - 2, mod); } 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; } } static PrintWriter out=new PrintWriter(System.out); static FastReader scan=new FastReader(); static boolean check=false; static class Pair{ int first; int second; long distance; Pair(int first,int second,long distance){ this.first=first; this.second=second; this.distance=distance; } } static long INF = (long) 1e17; static long NINF = (long)INF*(-1); static boolean check(int n){ return Math.ceil( Math.sqrt(n))==Math.floor(Math.sqrt(n)); } static void solve() { int t=scan.nextInt(); while(t-->0){ int n=scan.nextInt(); int b=scan.nextInt(); int x=scan.nextInt(); int y=scan.nextInt(); int arr[]=new int[n+1]; arr[0]=0; long sum=0; for(int i=1;i<=n;++i){ //either 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); } } public static void main(String[] args) { solve(); 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
f194dd0afd700d67820c09d0488917ec
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.math.BigInteger; import java.util.*; public class Main { /// // Two theorams are used to find ncrp // Lucas theoram and the fermit theoram // a^p-1=1(modp) example 2 and 3 // Lucas theoram // ncr=ncr(n/mod,n/mod)*ncr1(n%p,r%mod); static long max = Integer.MAX_VALUE; //C0C2+C1C1+C2C0 static int catalanNumber(int n, int dp[]) { if (n <= 2) { return 1; } if (dp[n] != -1) { return dp[n]; } //C0C1+C1C2+C20 int answer = 0; for (int i = 0; i < n; ++i) { //01 //10 //This is the dynamic programming answer += catalanNumber(i, dp) * catalanNumber(n - i - 1, dp); // System.out.println(answer); } return dp[n] = answer; } static int printNthcatalanNumber(int n) { int dp[] = new int[n + 1]; Arrays.fill(dp, -1); return catalanNumber(n, dp); } static int power(int a, int b, int mod) { int res = 1; while (b > 0) { if (b % 2 == 1) { res *= a; } a *= a; b >>= 1; } return res; } static int fermittheoram(int a, int mod) { return power(a, mod - 2, mod); } 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; } } static PrintWriter out=new PrintWriter(System.out); static FastReader scan=new FastReader(); static boolean check=false; static class Pair{ int first; int second; long distance; Pair(int first,int second,long distance){ this.first=first; this.second=second; this.distance=distance; } } static long INF = (long) 1e17; static long NINF = (long)INF*(-1); static boolean check(int n){ return Math.ceil( Math.sqrt(n))==Math.floor(Math.sqrt(n)); } static void solve() { int t=scan.nextInt(); while(t-->0){ int n=scan.nextInt(); int b=scan.nextInt(); int x=scan.nextInt(); int y=scan.nextInt(); int arr[]=new int[n+1]; arr[0]=0; long sum=0; for(int i=1;i<=n;++i){ //either 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); } } public static void main(String[] args) { solve(); 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
efde998a0ecefd9cbddba5acb6e351dc
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 boolean[] prime = new boolean[10000000]; final static long mod = 998244353; public static void main(String[] args) { // sieve(); InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = in.nextInt(); while(t-- > 0){ int n = in.nextInt()+1, b = in.nextInt(), x = in.nextInt(), y = in.nextInt(); int k = 0; long ans = 0; while(n-- > 0){ ans += k; if(k + x > b){ k -= y; }else{ k += x; } } out.println(ans); } out.flush(); } static int gcd(int a, int b) { int temp; while (b > 0) { a %= b; temp = a; a = b; b = temp; } return a; } static Integer[] intInput(int n, InputReader in) { Integer[] a = new Integer[n]; for (int i = 0; i < a.length; i++) a[i] = in.nextInt(); return a; } static Long[] longInput(int n, InputReader in) { Long[] a = new Long[n]; for (int i = 0; i < a.length; i++) a[i] = in.nextLong(); return a; } static String[] strInput(int n, InputReader in) { String[] a = new String[n]; for (int i = 0; i < a.length; i++) a[i] = in.next(); return a; } // static void sieve() { // for (int i = 2; i * i < prime.length; i++) { // if (prime[i]) // continue; // for (int j = i * i; j < prime.length; j += i) { // prime[j] = true; // } // } // } } class Data { int ind; double w; Data(int val, double ind) { this.ind = val; this.w = ind; } } class Graph { ArrayList<ArrayList<Data>> g; Graph(int n) { g = new ArrayList<>(); for (int i = 0; i < n; i++) { g.add(new ArrayList<>()); } } void addEdge(int i, int j, double x, double y) { g.get(i).add(new Data(j, x / (x + y))); g.get(j).add(new Data(i, y / (x + y))); } } // class compareVal implements Comparator<Data> { // @Override // public int compare(Data o1, Data o2) { // return (o1.val - o2.val); // } // } 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()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = reader.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
6d0c5d8d66862ce36604f164348bec90
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 whatever //do not write package name here */ import java.util.*; public class GFG { public static void main (String[] args) { Scanner sc=new Scanner(System.in); 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[] arr=new int[n+1]; arr[0]=0; for(int i=1;i<n+1;i++){ int a=arr[i-1]+x; if(a>b){ arr[i]=arr[i-1]-y; } else{ arr[i]=a; } } long sum=0; for(int i=0;i<n+1;i++){ sum+=arr[i]; //System.out.println(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
bdf513a3f6bc9c7457369c3797fab9a9
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; 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