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 | 51192a5708fb48bea8fb8921d516d468 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class MyClass {
public static void main(String[] args)
{
FastReader sc=new FastReader();
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
int a=sc.nextInt();
int b=sc.nextInt();
long[] arr=new long[n];
long sum=0;
for(int i=0; i<n; i++){
arr[i]=sc.nextLong();
sum+=arr[i];
}
long ans=sum*b;
long tempsum=0;
for(int i=0; i<n; i++){
long kbja=arr[i]*b;
long cap=arr[i]*a;
tempsum+=arr[i];
long tempans=kbja+cap+(b*((sum-tempsum)-((n-i-1)*arr[i])));
ans=Math.min(ans,tempans);
}
System.out.println(ans);
}
}
}
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 | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 0b35c08089a88076f1bb451aa5bed976 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 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 TaskC {
public static void main(String[] args) {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
TaskC s = new TaskC();
s.solve(in, out);
out.flush();
}
void solveOne(FastScanner in, PrintWriter out) {
int n = in.nextInt();
long a = in.nextLong(), b = in.nextLong();
long[] x = new long[n+1];
for (int i = 1; i <= n; i++) x[i] = in.nextLong();
long[] conquer = new long[n+1];
long suff = 0;
for (int i = n; i >= 1; i--) {
suff += x[i];
conquer[i] = b * (suff - x[i-1] * (n-i+1));
}
long ans = conquer[1];
long t = 0;
for (int i = 1; i <= n-1; i++) {
t += (b + a) * (x[i] - x[i-1]);
ans = Math.min(ans, t + conquer[i+1]);
}
out.println(ans);
}
void solve(FastScanner in, PrintWriter out) {
int t = 1;
t = in.nextInt();
for (int tc = 1; tc <= t; tc++) {
// out.printf("Case #%d: ", tc);
solveOne(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) {
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 | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 66810fba15d165ea7f3628165b80e650 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
CLineEmpire solver = new CLineEmpire();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class CLineEmpire {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt(), a = in.nextInt(), b = in.nextInt();
int[] arr = in.nextIntArray(n);
int last = 0;
long[] suff = new long[n];
suff[n - 1] = arr[n - 1];
for (int i = n - 2; i >= 0; i--) suff[i] = suff[i + 1] + arr[i];
long ans = suff[0] * b;
for (int i = 0; i < n - 1; i++) {
long temp = (long) a * (arr[i]) + (long) b * (arr[i]) + (long) (suff[i + 1] - (long) (n - 1 - i) * arr[i]) * b;
ans = Math.min(ans, temp);
}
out.println(ans);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(long i) {
writer.println(i);
}
}
static class InputReader {
BufferedReader reader;
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[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 781368978600f0d8d887ed7afc35713c | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
// Contest 1659, Problem C
// Line Empire
import java.io.*;
import java.util.*;
public class C {
static BufferedInputStream bis;
public static int readInt() throws IOException {
int num = 0;
int b = bis.read();
while(b < '0' || b > '9') b = bis.read();
while(b >= '0') {
num = num*10 + b - '0';
b = bis.read();
}
return num;
}
public static void main(String[] args) throws IOException {
bis = new BufferedInputStream(System.in);
StringBuilder sb = new StringBuilder();
int T = readInt();
for(int cN=0; cN<T; cN++) {
int N = readInt()+1;
long a = readInt();
long b = readInt();
long [] x = new long [N];
for(int i=1; i<N; i++) {
x[i] = readInt();
}
long c = a+b;
long cost = c*x[N-1];
long sum = 0;
long past = 1;
long min = cost;
for(int i=N-2; i>=0; i--) { // where i is the last location capital gets advanced to
sum += b*past*(x[i+1] - x[i]);
cost = c*x[i] + sum;
if(cost < min) min = cost;
past++;
}
sb.append(min).append('\n');
}
System.out.print(sb);
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 862ef491ba0e788a745d6850138544da | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.util.*;
import java.io.*;
public class Main {
static StringBuilder sb;
static dsu dsu;
static long fact[];
static int mod = (int) (1e9 + 7);
static void solve() {
int n=i();
long a=l();
long b=l();
long[]arr=new long[n];
for(int i=0;i<n;i++)arr[i]=l();
long[]ps=new long [n];
ps[0]=arr[0];
for(int i=1;i<n;i++){
ps[i]=ps[i-1]+arr[i];
}
long ans=b*(ps[n-1]);
for(int i=0;i<n;i++){
long c1=a*(arr[i])+b*(arr[i]);
long rem=n-(i+1);
long s=ps[n-1]-ps[i];
// if(i==1)System.out.println(ps[n-1]+" "+ps[i]);
long c2=b*(s-(rem*arr[i]));
// if(i==1)System.out.println(c1+" "+c2);
ans=Math.min(ans,c1+c2);
}
sb.append(ans+"\n");
}
public static void main(String[] args) {
sb = new StringBuilder();
int test = i();
fact=new long[(int)1e6+10];
fact[0]=fact[1]=1;
for(int i=2;i<fact.length;i++)
{ fact[i]=((long)(i%mod)*(long)(fact[i-1]%mod))%mod; }
while (test-- > 0) {
solve();
}
System.out.println(sb);
}
//**************NCR%P******************
static long ncr(int n, int r) {
if (r > n)
return (long) 0;
long res = fact[n] % mod;
// System.out.println(res);
res = ((long) (res % mod) * (long) (p(fact[r], mod - 2) % mod)) % mod;
res = ((long) (res % mod) * (long) (p(fact[n - r], mod - 2) % mod)) % mod;
// System.out.println(res);
return res;
}
static long p(long x, long y)// POWER FXN //
{
if (y == 0)
return 1;
long res = 1;
while (y > 0) {
if (y % 2 == 1) {
res = (res * x) % mod;
y--;
}
x = (x * x) % mod;
y = y / 2;
}
return res;
}
//**************END******************
// *************Disjoint set
// union*********//
static class dsu {
int parent[];
dsu(int n) {
parent = new int[n];
for (int i = 0; i < n; i++)
parent[i] = i;
}
int find(int a) {
if (parent[a] ==a)
return a;
else {
int x = find(parent[a]);
parent[a] = x;
return x;
}
}
void merge(int a, int b) {
a = find(a);
b = find(b);
if (a == b)
return;
parent[b] = a;
}
}
//**************PRIME FACTORIZE **********************************//
static TreeMap<Integer, Integer> prime(long n) {
TreeMap<Integer, Integer> h = new TreeMap<>();
long num = n;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (n % i == 0) {
int nt = 0;
while (n % i == 0) {
n = n / i;
nt++;
}
h.put(i, nt);
}
}
if (n != 1)
h.put((int) n, 1);
return h;
}
//****CLASS PAIR ************************************************
static class Pair implements Comparable<Pair> {
int x;
long y;
Pair(int x, long y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return (int) (this.y - o.y);
}
}
//****CLASS PAIR **************************************************
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 class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
static InputReader in = new InputReader(System.in);
static OutputWriter out = new OutputWriter(System.out);
public static long[] sort(long[] a2) {
int n = a2.length;
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static char[] sort(char[] a2) {
int n = a2.length;
ArrayList<Character> l = new ArrayList<>();
for (char i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
public static long pow(long x, long y) {
long res = 1;
while (y > 0) {
if (y % 2 != 0) {
res = (res * x);// % modulus;
y--;
}
x = (x * x);// % modulus;
y = y / 2;
}
return res;
}
//GCD___+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
// ******LOWEST COMMON MULTIPLE
// *********************************************
public static long lcm(long x, long y) {
return (x * (y / gcd(x, y)));
}
//INPUT PATTERN********************************************************
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();
}
public static int[] readArrayi(int n) {
int A[] = new int[n];
for (int i = 0; i < n; i++) {
A[i] = i();
}
return A;
}
public static long[] readArray(long n) {
long A[] = new long[(int) n];
for (int i = 0; i < n; i++) {
A[i] = l();
}
return A;
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 552599f41820dec68a4f1ad1438d4673 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.util.*;
import java.io.*;
public class C {
public static void main(String[] args)throws IOException {
FastReader sc = new FastReader();
StringBuilder sb = new StringBuilder();
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
long a = sc.nextLong();
long b = sc.nextLong();
long[] arr = new long[n+1];
for(int i =1;i<n+1;i++)arr[i]=sc.nextLong();
long[] p = new long[n+1];
for(int i =1;i<=n;i++) {
p[i]=p[i-1]+arr[i];
}
long ans = Long.MAX_VALUE;
for(int i = 0;i<=n;i++) {
ans = Math.min(ans, (a*arr[i]+b*(p[n]-p[i]-(n-i-1)*arr[i])));
}
sb.append(ans).append("\n");
}
System.out.println(sb);
}
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());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | ed84b7b8540eca815d839bec89e84749 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces
{
public static void main(String args[])throws Exception
{
BufferedReader bu=new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb=new StringBuilder();
int t=Integer.parseInt(bu.readLine());
while(t-->0)
{
String s[]=bu.readLine().split(" ");
int n=Integer.parseInt(s[0]),a=Integer.parseInt(s[1]),b=Integer.parseInt(s[2]);
int c[]=new int[n+1],i;
s=bu.readLine().split(" ");
for(i=1;i<=n;i++) c[i]=Integer.parseInt(s[i-1]);
long sum[]=new long[n+2];
for(i=n;i>=0;i--) sum[i]=sum[i+1]+c[i];
long ans=sum[0]*b;
int cap=0;
for(i=1;i<=n;i++)
{
long cur=ans-(sum[i+1]-1l*(n-i)*cap)*b;
cur+=(sum[i+1]-1l*(n-i)*c[i])*b;
cur+=1l*a*(c[i]-cap);
if(cur<ans)
{
ans=cur;
cap=c[i];
}
}
sb.append(ans+"\n");
}
System.out.print(sb);
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 8f4ef68f7d194ab067f1d5dd3739186f | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 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();
int a = sc.nextInt();
int b = sc.nextInt();
long[] arr = new long[n];
for(int i=0;i<n;i++) arr[i] = sc.nextLong();
long[] pre = new long[n];
pre[0]=arr[0];
for(int i=1;i<n;i++) {
pre[i]=pre[i-1]+arr[i];
}
// System.out.println(Arrays.toString(pre));
long ans = pre[n-1]*b;
for(int i=0;i<n-1;i++) {
long makeCapital = arr[i]*(a+b);
long concurRemaining = (pre[n-1]-pre[i]-(n-i-1)*arr[i])*b;
long tempAns = makeCapital + concurRemaining;
// System.out.println(makeCapital +" "+ concurRemaining);
if(tempAns<ans) ans = tempAns;
}
System.out.println(ans);
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | b12b24dc8a97f81023e05bd488958b6e | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-->0){
int[] nab = Arrays.stream(br.readLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
long n = nab[0]; long a = nab[1]; long b = nab[2];
long[] vals = Arrays.stream(br.readLine().split("\\s+")).mapToLong(Integer::parseInt).toArray();
long ans = vals[0]*b; long lastCapital = 0;
for(int i=1;i<n;i++){
// System.out.println(ans);
long costtoMove = (vals[i-1]-lastCapital)*a;
if(costtoMove<(vals[i-1]-lastCapital)*(n-i)*b){
ans+=costtoMove;
lastCapital = vals[i-1];
}
ans+= (vals[i]-lastCapital)*b;
}
System.out.println(ans);
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 7479b62862c0f0a91f4d8816757fe45f | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class C {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(int arr[],int l1,int r1,int l2,int r2) {
int tmp[]=new int[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(long arr[],int l1,int r1,int l2,int r2) {
long tmp[]=new long[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
StringBuilder ans=new StringBuilder("");
int test=input.scanInt();
for(int tt=1;tt<=test;tt++) {
int n=input.scanInt();
long a=input.scanInt();
long b=input.scanInt();
long arr[]=new long[n];
for(int i=0;i<n;i++) {
arr[i]=input.scanInt();
}
long prefix[]=new long[n];
prefix[0]=arr[0];
for(int i=1;i<n;i++) {
prefix[i]=arr[i]+prefix[i-1];
}
long min=Long.MAX_VALUE;
long sum=0;
sum+=a*0;
sum+=b*0;
sum+=b*get(0,n-1,prefix);
min=Math.min(min,sum);
for(int i=0;i<n;i++) {
sum=0;
sum+=a*arr[i];
sum+=b*arr[i];
sum+=b*(get(i+1,n-1,prefix)-((n-i-1)*arr[i]));
min=Math.min(min,sum);
// System.out.println(i+" "+sum);
}
ans.append(min+"\n");
}
System.out.println(ans);
}
public static long get(int l,int r,long arr[]) {
// System.out.println(l+" "+r);
return arr[r]-(l==0?0:arr[l-1]);
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 9603a6feb21fb3a9f5376dac44387102 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | /*
Rating: 1378
Date: 22-04-2022
Time: 15-25-50
Author: Kartik Papney
Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/
Leetcode: https://leetcode.com/kartikpapney/
Codechef: https://www.codechef.com/users/kartikpapney
----------------------------Jai Shree Ram----------------------------
*/
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class C_Line_Empire {
public static void s() {
int n = sc.nextInt();
long a = sc.nextLong(), b = sc.nextLong();
long[] arr = sc.readLongArray(n);
long capitalat = 0;
long ans = b*arr[0];
// p.writes(ans);
for(int i=1; i<n; i++) {
boolean check = (a <= (n-i)*b);
if(check) {
ans += a*(arr[i-1] - capitalat);
capitalat = arr[i-1];
}
ans += b*(arr[i]-capitalat);
// p.writes(ans);
}
// p.writeln();
p.writeln(ans);
}
public static void main(String[] args) {
int t = 1;
t = sc.nextInt();
while (t-- != 0) {
s();
}
p.print();
}
public static boolean debug = false;
static void debug(String st) {
if(debug) p.writeln(st);
}
static final Integer MOD = (int) 1e9 + 7;
static final FastReader sc = new FastReader();
static final Print p = new Print();
static class Functions {
static void sort(int... a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static void sort(long... a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static int max(int... a) {
int max = Integer.MIN_VALUE;
for (int val : a) max = Math.max(val, max);
return max;
}
static int min(int... a) {
int min = Integer.MAX_VALUE;
for (int val : a) min = Math.min(val, min);
return min;
}
static long min(long... a) {
long min = Long.MAX_VALUE;
for (long val : a) min = Math.min(val, min);
return min;
}
static long max(long... a) {
long max = Long.MIN_VALUE;
for (long val : a) max = Math.max(val, max);
return max;
}
static long sum(long... a) {
long sum = 0;
for (long val : a) sum += val;
return sum;
}
static int sum(int... a) {
int sum = 0;
for (int val : a) sum += val;
return sum;
}
public static long mod_add(long a, long b) {
return (a % MOD + b % MOD + MOD) % MOD;
}
public static long pow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) != 0)
res = mod_mul(res, a);
a = mod_mul(a, a);
b >>= 1;
}
return res;
}
public static long mod_mul(long a, long b) {
long res = 0;
a %= MOD;
while (b > 0) {
if ((b & 1) > 0) {
res = mod_add(res, a);
}
a = (2 * a) % MOD;
b >>= 1;
}
return res;
}
public static long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
public static long factorial(long n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res = (i % MOD * res % MOD) % MOD;
}
return res;
}
public static int count(int[] arr, int x) {
int count = 0;
for (int val : arr) if (val == x) count++;
return count;
}
public static ArrayList<Integer> generatePrimes(int n) {
boolean[] primes = new boolean[n];
for (int i = 2; i < primes.length; i++) primes[i] = true;
for (int i = 2; i < primes.length; i++) {
if (primes[i]) {
for (int j = i * i; j < primes.length; j += i) {
primes[j] = false;
}
}
}
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < primes.length; i++) {
if (primes[i]) arr.add(i);
}
return arr;
}
}
static class Print {
StringBuffer strb = new StringBuffer();
public void write(Object str) {
strb.append(str);
}
public void writes(Object str) {
char c = ' ';
strb.append(str).append(c);
}
public void writeln(Object str) {
char c = '\n';
strb.append(str).append(c);
}
public void writeln() {
char c = '\n';
strb.append(c);
}
public void yes() {
char c = '\n';
writeln("YES");
}
public void no() {
writeln("NO");
}
public void writes(int... arr) {
for (int val : arr) {
write(val);
write(' ');
}
}
public void writes(long... arr) {
for (long val : arr) {
write(val);
write(' ');
}
}
public void writeln(int... arr) {
for (int val : arr) {
writeln(val);
}
}
public void print() {
System.out.print(strb);
}
public void println() {
System.out.println(strb);
}
}
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());
}
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;
}
double[] readArrayDouble(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
String nextLine() {
String str = new String();
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 691a09dbf66db96480e21260e47dc528 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.io.*;
import java.math.*;
import java.util.*;
// @author : Dinosparton
public class test {
static class Pair{
long x;
long y;
Pair(long x,long y){
this.x = x;
this.y = y;
}
}
static class Sort implements Comparator<Pair>
{
@Override
public int compare(Pair a, Pair b)
{
if(a.x!=b.x)
{
return (int)(a.x - b.x);
}
else
{
return (int)(a.y-b.y);
}
}
}
static class Compare {
void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.x!=p2.x) {
return (int)(p1.x - p2.x);
}
else {
return (int)(p1.y - p2.y);
}
}
});
// for (int i = 0; i < n; i++) {
// System.out.print(arr[i].x + " " + arr[i].y + " ");
// }
// System.out.println();
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String args[]) throws Exception {
Scanner sc = new Scanner();
StringBuilder res = new StringBuilder();
int tc = sc.nextInt();
while(tc-->0) {
int n = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
long arr[] = new long[n];
for(int i=0;i<n;i++) {
arr[i] = sc.nextLong();
}
long cnt = 0;
long suff[] = new long[n];
suff[n-1] = arr[n-1];
for(int i=n-2;i>=0;i--) {
suff[i] = suff[i+1] + arr[i];
}
// for(int i=0;i<n;i++) {
// System.out.print(suff[i]+" ");
// }
// System.out.println();
long last = 0;
for(int i=0;i<n;i++) {
if((suff[i] - (long)((last)*(n-i)))*b >= a*(arr[i]-last) + (suff[i] - (long)((n-i)*arr[i]))*b + b*(arr[i]-last)) {
cnt += a*(arr[i]-last) + b*(arr[i]-last);
last = arr[i];
}
else {
cnt += b*(arr[i]-last);
}
}
res.append(cnt+"\n");
}
System.out.println(res);
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 6cdd16e3658f9733cda09c07984bacee | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main{
public static FastReader cin;
public static PrintWriter out;
public static void main(String[] args) throws Exception{
out = new PrintWriter(new BufferedOutputStream(System.out));
cin = new FastReader();
int t=cin.nextInt();
for(int z=0;z<t;z++) {
int n=cin.nextInt();
int a=cin.nextInt();
int b=cin.nextInt();
long ans=Long.MAX_VALUE;
long []pos=new long [n+1];
long []presum=new long [n+1];
pos[0]=0;
for(int i=1;i<=n;i++) {
pos[i]=cin.nextLong();
}
for(int i=1;i<=n;i++) {
presum[i]=presum[i-1]+pos[i];
//System.out.printf("presum[%d]=%d ",i,presum[i]);
}
for(int k=0;k<=n;k++) {//i为最终capital定址
long cost=(pos[k]-pos[0])*(a+b)+(presum[n]-presum[k]-(n-k)*pos[k])*b;//转移代价
ans=Math.min(ans,cost);
}
out.println(ans);
}
out.close();
}
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 89e8dd278f5c24547c52f59a6003d2f6 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
public static void main(String[]args){
long s = System.currentTimeMillis();
new Solver().run();
System.err.println(System.currentTimeMillis()-s+"ms");
}
}
class Solver{
final long mod = (long)1e9+7l;
final boolean DEBUG = true, MULTIPLE_TC = true;
FastReader sc;
PrintWriter out;
final long oo = (long)1e17;
int N;
long A, B, totalSum, arr[];
void init(){
N = ni();
A = nl();
B = nl();
totalSum = 0l;
arr = new long[N + 1];
for(int i = 1; i <= N; i++){
arr[i] = nl();
totalSum += arr[i];
}
}
void process(int testNumber){
init();
long costOfMovingCapitals = 0l, res = totalSum * B, sumSoFar = 0l;
for(int i = 1; i <= N; i++){
sumSoFar += arr[i];
costOfMovingCapitals += ((arr[i] - arr[i - 1]) * B) + ((arr[i] - arr[i - 1]) * A);
long op = costOfMovingCapitals + ((totalSum - sumSoFar) - (N - i) * arr[i]) * B;
res = Math.min(res, op);
}
pn(res);
}
void run(){
sc = new FastReader();
out = new PrintWriter(System.out);
int t = MULTIPLE_TC ? ni() : 1;
for(int test = 1; test <= t; test++){
process(test);
}
out.flush();
}
void trace(Object... o){ if(!DEBUG) return; System.err.println(Arrays.deepToString(o)); };
void pn(Object o){ out.println(o); }
void p(Object o){ out.print(o); }
int ni(){ return Integer.parseInt(sc.next()); }
long nl(){ return Long.parseLong(sc.next()); }
double nd(){ return Double.parseDouble(sc.next()); }
String nln(){ return sc.nextLine(); }
long gcd(long a, long b){ return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){ return (b==0)?a:gcd(b,a%b); }
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();
}
String nextLine(){
String str = "";
try{ str = br.readLine(); } catch (IOException e) { e.printStackTrace(); }
return str;
}
}
}
class pair implements Comparable<pair> {
int first, second;
public pair(int first, int second){
this.first = first;
this.second = second;
}
@Override
public int compareTo(pair ob){
if(this.first != ob.first)
return this.first - ob.first;
return this.second - ob.second;
}
@Override
public String toString(){
return this.first + " " + this.second;
}
static public pair from(int f, int s){
return new pair(f, s);
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 1f97054fb10801c747ea56805ebe2063 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
//need to be careful about negative sum resulting negative mod.
public class Solution {
static long mod = 1000000007;
static long inv(long a, long b) {return 1 < a ? b - inv(b % a, a) * b / a : 1;}
static long mi(long a) {return inv(a, mod);}
static InputReader sc = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException {
//setUp("input.txt", "output.txt");
//setUp("maxcross.in", "maxcross.out");
int T = 1; T = sc.nextInt();
while(T-- > 0){
int n = sc.nextInt();
long a = sc.nextLong();
long b = sc.nextLong();
int[] A = sc.readArray(n);
int capitalPos = 0;
int capitalIdx = -1;
long cost = 0;
for(int i = 0; i < n; i++){
cost = cost + b*(A[i] - capitalPos);
while(capitalIdx < i){
long movingCapital = a *(A[capitalIdx + 1] - capitalPos);
int cities = i - capitalIdx - 1;
long adjustment = b * (cities * (capitalPos - A[capitalIdx + 1]));
long newCost = cost + movingCapital + adjustment;
if(cost >= newCost){
cost = newCost;
capitalIdx++;
capitalPos = A[capitalIdx];
}else{
break;
}
}
}
out.println(cost);
}
out.close();
}
public static void solve(){
}
static void setUp(String input, String output) throws IOException {
sc = new InputReader(new FileInputStream(input));
out = new PrintWriter(new BufferedWriter(new FileWriter(output)));
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.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 = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return str;
}
int[] readArray(int N){
int[] arr = new int[N];
for(int i = 0 ; i < N; i++){
arr[i] = sc.nextInt();
}
return arr;
}
long[] readLongArray(int N){
long[] arr = new long[N];
for(int i = 0 ; i < N; i++){
arr[i] = sc.nextLong();
}
return arr;
}
}
public static void shuffle(int[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
int temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
public static void shuffle(long[] a) {
Random get = new Random();
for (int i = 0; i < a.length; i++) {
int r = get.nextInt(a.length);
long temp = a[i];
a[i] = a[r];
a[r] = temp;
}
}
static class Pair<K,V> {
K key;
V val;
public Pair(K key, V val){
this.key = key;
this.val = val;
}
}
public static int gcd(int a, int b){
if(b == 0) return a;
return gcd(b, a % b);
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 2269e1d5b8d1c1141d2bfc0494571e12 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.util.*;
import static java.util.Arrays.*;
public class CodeforcesTemp {
public static void main(String[] args) throws IOException {
Reader scan = new Reader();
FastPrinter out = new FastPrinter();
int t =scan.nextInt();
for (int tc = 1; tc <= t; tc++) {
int n= scan.nextInt();
long a= scan.nextLong();
long b= scan.nextLong();
long[] arr= scan.nextLongArray(n);
long[] suffix=new long[n];
suffix[n-1]=arr[n-1];
for (int i = n-2; i >=0 ; i--) {
suffix[i]=suffix[i+1]+arr[i];
}
long ans=arr[0]*b;
long cap=0;
for (int i = 1; i < n; i++) {
long res1=(suffix[i]*b-((n-i)*cap*b));
long res2=(suffix[i]*b-((n-i)*arr[i-1]*b))+(arr[i-1]-cap)*a;
if(res2<res1){
ans+=(arr[i-1]-cap)*a;
cap=arr[i-1];
}
ans+=(arr[i]-cap)*b;
}
out.println(ans);
}
out.close();
}
static class Reader {
private final InputStream in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
private static final long LONG_MAX_TENTHS = 922337203685477580L;
private static final int LONG_MAX_LAST_DIGIT = 7;
private static final int LONG_MIN_LAST_DIGIT = 8;
public Reader(InputStream in) {
this.in = in;
}
public Reader() {
this(System.in);
}
private boolean hasNextByte() {
if (ptr < buflen) {
return true;
} else {ptr = 0;
try {buflen = in.read(buffer);} catch (IOException e) {e.printStackTrace();}
if (buflen <= 0) {return false;}
}return true;
}
private int readByte() {if (hasNextByte()) return buffer[ptr++];else return -1;}
private static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
return hasNextByte();}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (isPrintableChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
long n = 0;
boolean minus = false;
int b = readByte();
if (b == '-') {
minus = true;
b = readByte();
}
if (b < '0' || '9' < b) {
throw new NumberFormatException();
}
while (true) {
if ('0' <= b && b <= '9') {
int digit = b - '0';
if (n >= LONG_MAX_TENTHS) {
if (n == LONG_MAX_TENTHS) {
if (minus) {
if (digit <= LONG_MIN_LAST_DIGIT) {
n = -n * 10 - digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("not number"));
}
}
} else {
if (digit <= LONG_MAX_LAST_DIGIT) {
n = n * 10 + digit;
b = readByte();
if (!isPrintableChar(b)) {
return n;
} else if (b < '0' || '9' < b) {
throw new NumberFormatException(
String.format("not number"));
}
}
}
}
throw new ArithmeticException(
String.format(" overflows long."));
}
n = n * 10 + digit;
} else if (b == -1 || !isPrintableChar(b)) {
return minus ? -n : n;
} else {
throw new NumberFormatException();
}
b = readByte();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
throw new NumberFormatException();
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long[] nextLongArray(int length) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = this.nextLong();
return array;
}
public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) {
long[] array = new long[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsLong(this.nextLong());
return array;
}
public int[] nextIntArray(int length) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = this.nextInt();
return array;
}
public int[][] nextIntArrayMulti(int length, int width) {
int[][] arrays = new int[width][length];
for (int i = 0; i < length; i++) {
for (int j = 0; j < width; j++)
arrays[j][i] = this.nextInt();
}
return arrays;
}
public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) {
int[] array = new int[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsInt(this.nextInt());
return array;
}
public double[] nextDoubleArray(int length) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = this.nextDouble();
return array;
}
public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) {
double[] array = new double[length];
for (int i = 0; i < length; i++)
array[i] = map.applyAsDouble(this.nextDouble());
return array;
}
public long[][] nextLongMatrix(int height, int width) {
long[][] mat = new long[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextLong();
}
return mat;
}
public int[][] nextIntMatrix(int height, int width) {
int[][] mat = new int[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextInt();
}
return mat;
}
public double[][] nextDoubleMatrix(int height, int width) {
double[][] mat = new double[height][width];
for (int h = 0; h < height; h++)
for (int w = 0; w < width; w++) {
mat[h][w] = this.nextDouble();
}
return mat;
}
public char[][] nextCharMatrix(int height, int width) {
char[][] mat = new char[height][width];
for (int h = 0; h < height; h++) {
String s = this.next();
for (int w = 0; w < width; w++) {
mat[h][w] = s.charAt(w);
}
}
return mat;
}
}
static class FastPrinter extends PrintWriter {
public FastPrinter(PrintStream stream) {
super(stream);
}
public FastPrinter() {
super(System.out);
}
private static String dtos(double x, int n) {
StringBuilder sb = new StringBuilder();
if (x < 0) {
sb.append('-');
x = -x;
}
x += Math.pow(10, -n) / 2;
sb.append((long) x);
sb.append(".");
x -= (long) x;
for (int i = 0; i < n; i++) {
x *= 10;
sb.append((int) x);
x -= (int) x;
}
return sb.toString();
}
@Override
public void print(float f) {
super.print(dtos(f, 20));
}
@Override
public void println(float f) {
super.println(dtos(f, 20));
}
@Override
public void print(double d) {
super.print(dtos(d, 20));
}
@Override
public void println(double d) {
super.println(dtos(d, 20));
}
public void printArray(int[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(int[] array) {
this.printArray(array, " ");
}
public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsInt(array[i]));
super.print(separator);
}
super.println(map.applyAsInt(array[n - 1]));
}
public void printArray(int[] array, java.util.function.IntUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printArray(long[] array, String separator) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(array[i]);
super.print(separator);
}
super.println(array[n - 1]);
}
public void printArray(long[] array) {
this.printArray(array, " ");
}
public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
super.print(map.applyAsLong(array[i]));
super.print(separator);
}
super.println(map.applyAsLong(array[n - 1]));
}
public void printArray(long[] array, java.util.function.LongUnaryOperator map) {
this.printArray(array, " ", map);
}
public void printMatrix(int[][] arr) {
for (int i = 0; i < arr.length; i++) {
this.printArray(arr[i]);
}
}
}
static Random __r = new Random();
static int randInt(int min, int max) {return __r.nextInt(max - min + 1) + min;}
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;}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 952e1e017a5ab2b7f412a0f6badf0b6e | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import javax.sound.midi.Soundbank;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.util.zip.InflaterInputStream;
public class Main {
static int N = 1001;
// Array to store inverse of 1 to N
static long[] factorialNumInverse = new long[N + 1];
// Array to precompute inverse of 1! to N!
static long[] naturalNumInverse = new long[N + 1];
// Array to store factorial of first N numbers
static long[] fact = new long[N + 1];
// Function to precompute inverse of numbers
public static void InverseofNumber(int p)
{
naturalNumInverse[0] = naturalNumInverse[1] = 1;
for(int i = 2; i <= N; i++)
naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p;
}
public static void InverseofFactorial(int p)
{
factorialNumInverse[0] = factorialNumInverse[1] = 1;
// Precompute inverse of natural numbers
for(int i = 2; i <= N; i++)
factorialNumInverse[i] = (naturalNumInverse[i] *
factorialNumInverse[i - 1]) % p;
}
// Function to calculate factorial of 1 to N
public static void factorial(int p)
{
fact[0] = 1;
// Precompute factorials
for(int i = 1; i <= N; i++)
{
fact[i] = (fact[i - 1] * (long)i) % p;
}
}
public static long ncp(int N, int R, int p)
{
// n C r = n!*inverse(r!)*inverse((n-r)!)
long ans = ((fact[N] * factorialNumInverse[R]) %
p * factorialNumInverse[N - R]) % p;
return ans;
}
public static void main(String[] args) throws IOException {
Reader.init(System.in);
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Reader.nextInt();
while (t > 0){
int n = Reader.nextInt();
int a = Reader.nextInt();
int b = Reader.nextInt();
long [] arr = new long[n+1];
long [] endSum = new long[n+1];
long [] consecutiveSum = new long[n+1];
int i = 1;
while ( i <= n){
arr[i] = Reader.nextLong();
endSum[i] = arr[i]*b+endSum[i-1];
consecutiveSum[i] = (arr[i]-arr[i-1])+consecutiveSum[i-1];
i++;
}
i = 0;
long ans = Long.MAX_VALUE;
while ( i <= n){
long totalCost = consecutiveSum[i]*a;
//System.out.println(totalCost);
if ( i != n){
totalCost += consecutiveSum[i+1]*b;
if ( i <= n-1){
long diff = endSum[n]-endSum[i+1];
diff-=(b*arr[i]*(n-(i+2)+1));
totalCost+=diff;
}
}
else{
totalCost += consecutiveSum[i]*b;
}
if ( totalCost < ans){
ans = totalCost;
}
//System.out.println(i+" "+totalCost);
i++;
}
output.write(ans+"\n");
t--;
}
output.flush();
}
public static boolean help(int m,int r,int b){
boolean ans = true;
while ( r > 0 ){
if ( b == 0 && r > m){
ans =false;
break;
}
else{
r-=(int)min(m,r);
b--;
}
}
return ans;
}
public static void primeFactorisation(long n,HashMap<Long,Integer> map){
long i = 2;
while ( n%i == 0){
if ( map.containsKey(i)){
map.put(i,map.get(i)+1);
}
else{
map.put(i,1);
}
n/=i;
}
i = 3;
long last = (long)Math.pow(n,0.5);
while ( i <= last){
while ( n%i == 0){
if ( map.containsKey(i)){
map.put(i,map.get(i)+1);
}
else{
map.put(i,1);
}
n/=i;
}
i++;
}
if ( n > 2){
map.put(n,1);
}
}
public static boolean prime(int n){
int i = 2;
int r = (int)Math.pow(n,0.5);
boolean ans = true;
while ( i<=r){
if ( n%i == 0){
ans = false;
break;
}
i++;
}
return ans;
}
public static long log2(long N)
{
long result = (long)(Math.log(N) / Math.log(2));
return result;
}
private static int bs(int low,int high,int [] array,long find){
if ( low <= high ){
int mid = low + (high-low)/2;
if ( array[mid] > find){
high = mid -1;
return bs(low,high,array,find);
}
else if ( array[mid] < find){
low = mid+1;
return bs(low,high,array,find);
}
return mid;
}
return -1;
}
private static long max(long a, long b) {
return Math.max(a,b);
}
private static long min(long a,long b){
return Math.min(a,b);
}
public static long modularExponentiation(long a,long b,long mod){
if ( b == 1){
return a;
}
else{
long ans = modularExponentiation(a,b/2,mod)%mod;
if ( b%2 == 1){
return (a*((ans*ans)%mod))%mod;
}
return ((ans*ans)%mod);
}
}
public static long sum(long n){
return (n*(n+1))/2;
}
public static long abs(long a){
return a < 0 ? (-1*a) : a;
}
public static long gcd(long a,long b){
if ( a == 0){
return b;
}
else{
return gcd(b%a,a);
}
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
static long nextLong() throws IOException{
return Long.parseLong(next());
}
}
class NComparator implements Comparator<Node>{
@Override
public int compare(Node o1, Node o2) {
if ( o2.infected == 1 && o1.infected == 0){
return -1;
}
else if ( o2.infected == 0 && o1.infected == 1){
return 1;
}
else{
if ( o2.value > o1.value){
return 1;
}
else if ( o2.value < o1.value){
return -1;
}
else{
return 0;
}
}
}
}
class DComparator implements Comparator<Integer>{
@Override
public int compare(Integer o1, Integer o2) {
if ( o2 > o1){
return 1;
}
else if ( o2 < o1){
return -1;
}
else{
return 0;
}
}
}
class Node{
int value;
int infected;
Node(int A,int B){
value= A;
infected = B;
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | d92f336397eeedff6ac50522b2a13fb1 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
// when can't think of anything -->>
// 1. In sorting questions try to think about all possibilities like starting from start, end, middle.
// 2. Two pointers, brute force.
// 3. In graph query questions try to solve it reversely or try to process all the queries in a single parse.
// 4. If order does not matter then just sort the data if constraints allow. It'll never harm.
// 5. In greedy problems if you are just overwhelmed by the possibilities and stuck, try to code whatever you come up with.
// 6. Try to solve it from back or reversely.
// 7. When can't prove something take help of contradiction.
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter writer = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
long a = sc.nextLong();
long b = sc.nextLong();
long arr[] = new long[n];
Arrays.setAll(arr, i->sc.nextLong());
long ans = Long.MAX_VALUE;
long presum[] = new long[n];
presum[n-1] = 0;
for(int i = n-2; i >= 0; i--) {
presum[i] = presum[i+1] + arr[i+1];
}
// writer.println(Arrays.toString(presum));
for(int i = 0; i < n; i++) {
long one = b*(presum[i] - ((n-i-1)*arr[i]));
if(one < 0)one = 0;
long sec = b*(arr[i]);
long thir = a*(arr[i]);
long val = one+sec+thir;
ans = Math.min(val, ans);
// writer.println(val);
}
ans = Math.min(ans, b*(presum[0]+arr[0]));
writer.println(ans);
}
writer.flush();
writer.close();
}
private static int findVal(String a, String b, int n) {
int diff1 = 0;
for(int i = 0; i < n; i++) {
if(a.charAt(i) != b.charAt(i))diff1++;
}
return diff1;
}
private static long power (long a, long n, long p) {
long res = 1;
while(n!=0) {
if(n%2==1) {
res=(res*a)%p;
n--;
}else {
a= (a*a)%p;
n/=2;
}
}
return res;
}
private static boolean isPrime(int c) {
for (int i = 2; i*i <= c; i++) {
if(c%i==0)return false;
}
return true;
}
private static int find(int a , int arr[]) {
if(arr[a] != a) return arr[a] = find(arr[a], arr);
return arr[a];
}
private static void union(int a, int b, int arr[]) {
int aa = find(a,arr);
int bb = find(b, arr);
arr[aa] = bb;
}
private static int gcd(int a, int b) {
if(a==0)return b;
return gcd(b%a, a);
}
public static int[] readIntArray(int size, FastReader s) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = s.nextInt();
}
return array;
}
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;
}
}
}
class Pair implements Comparable<Pair>{
int a;
int b;
Pair(int a, int b){
this.a = a;
this.b = b;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null || obj.getClass()!= this.getClass()) return false;
Pair pair = (Pair) obj;
return (pair.a == this.a && pair.b == this.b);
}
@Override
public int hashCode()
{
return Objects.hash(a,b);
}
@Override
public int compareTo(Pair o) {
if(this.a == o.a) {
return Integer.compare(this.b, o.b);
}else {
return Integer.compare(this.a, o.a);
}
}
@Override
public String toString() {
return this.a + " " + this.b;
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 6822d4937d83ed5cd89802f03c210874 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class E {
static class Reader {
BufferedReader br;
StringTokenizer st;
public Reader() {
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 class Pair<T extends Comparable<T>, U extends Comparable<U>>
implements Comparable<Pair<T, U>>{
// note first and second must not change
T first;
U second;
private int hashCode;
public Pair(T first, U second) {
this.first = first;
this.second = second;
hashCode = Objects.hash(first, second);
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pair p = (Pair) o;
return first.equals(p.first) && second.equals(p.second);
}
public int hashCode() {
return hashCode;
}
public int compareTo(Pair<T, U> o) {
return first.compareTo(o.first);
}
}
public static void main(String args[]) {
Reader rd = new Reader();
int tcs = rd.nextInt();
for (int tc = 1; tc <= tcs; tc++) {
int n = rd.nextInt();
int a = rd.nextInt();
int b = rd.nextInt();
long[] x = new long[n];
long o = 0;
for (int i = 0; i < n; i++) {
x[i] = rd.nextLong();
o += x[i] * b;
}
long capital = 0;
for (int i = 0; i < n; i++) {
long save = (x[i] - capital) * (n - 1 - i) * b;
long spend = a * (x[i] - capital);
if (spend <= save) {
o -= save - spend;
capital = x[i];
}
}
System.out.println(o);
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | dc144d3067a3821a506037011ecc4558 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class new1{
static int mod = 998244353;
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static void main(String[] args) throws IOException{
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader s = new FastReader();
int t = s.nextInt();
for(int z = 1; z <= t; z++) {
int n = s.nextInt();
int b = s.nextInt();
int a = s.nextInt();
int[] arr = new int[n + 1];
for(int i = 1; i <= n; i++) arr[i] = s.nextInt();
long[] left = new long[n + 1];
for(int i = 1; i <= n; i++) {
left[i] = left[i - 1] + arr[i] - arr[i - 1];
}
long[] right = new long[n + 1];
right[n] = arr[n];
for(int i = n - 1; i >= 0; i--) {
right[i] = right[i + 1] + arr[i];
}
// System.out.println(Arrays.toString(arr));
//System.out.println(Arrays.toString(left));
//System.out.println(Arrays.toString(right));
long ans = Long.MAX_VALUE;
for(int i = 0; i <= n; i++) {
long aa = (a + b) * (left[i]);
long bb = a * (right[i] - (n + 1L - i) * arr[i]);
long a1 = aa + bb;
// System.out.println(aa + " " + bb + " " + a1);
ans = Math.min(ans, a1);
}
System.out.println(ans);
}
//output.flush();
}
}
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();
}
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 = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 421a2a0c2af8325cbdc8fdc307aea6ea | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | /***** ---> :) Vijender Srivastava (: <--- *****/
import java.util.*;
import java.lang.*;
// import java.lang.reflect.Array;
import java.io.*;
public class Main
{
static FastReader sc =new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static long mod=(long)32768;
static StringBuilder sb = new StringBuilder();
/* start */
public static void main(String [] args)
{
// int testcases = 1;
int testcases = i();
// calc();
while(testcases-->0)
{
solve();
}
out.flush();
out.close();
}
static void solve()
{
int n = i();
long a = l();
long b = l();
long ar[] = inputL(n);
long x[] = new long [n];
x[0] = ar[0];
for(int i=1;i<n;i++) x[i]+=(ar[i]+x[i-1]);
long cnt[] = new long[n];
cnt[0] = b*ar[0];
for(int i=1;i<n;i++)
{
cnt[i] = (ar[i]-ar[i-1])*b+cnt[i-1];
}
// for(int i=1;i<n;i++) cnt[i]+=cnt[i-1];
// for(int i=0;i<n;i++)p(x[i]+" ");
// pl("");
long ans = sum(cnt);
for(int i=0;i<n;i++)
{
long z = ((x[n-1]-x[i])-(ar[i]*(n-i-1)))*b;
long z_ = (cnt[i])+ar[i]*a;
if(z>0 && z_>0)
ans = min(ans,(z+z_));
// pl(z+" "+z_);
}
pl(ans);
}
/* end */
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 void p(Object o)
{
out.print(o);
}
static void pl(Object o)
{
out.println(o);
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static char[] inputC()
{
String s = sc.nextLine();
return s.toCharArray();
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static long[] putL(long a[]) {
long A[]=new long[a.length];
for(int i=0;i<a.length;i++) {
A[i]=a[i];
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void print(int A[]) {
for(int i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) ;
y = y >> 1;
x = (x * x);
}
return res;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long[] sort(long a[]) {
ArrayList<Long> arr = new ArrayList<>();
for(long i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
static int[] sort(int a[])
{
ArrayList<Integer> arr = new ArrayList<>();
for(Integer i : a) {
arr.add(i);
}
Collections.sort(arr);
for(int i = 0; i < arr.size(); i++) {
a[i] = arr.get(i);
}
return a;
}
//pair class
private static class Pair implements Comparable<Pair> {
long first, second;
public Pair(long f, long s) {
first = f;
second = s;
}
@Override
public int compareTo(Pair p) {
if (first < p.first)
return 1;
else if (first > p.first)
return -1;
else {
if (second > p.second)
return 1;
else if (second < p.second)
return -1;
else
return 0;
}
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 96dd378e8a66457d30c8c1cb8a3d80e6 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class Practice {
static boolean multipleTC = true;
final static int mod2 = 1000000007;
final static int mod = 998244353;
final double E = 2.7182818284590452354;
final double PI = 3.14159265358979323846;
int MAX = 200001;
void pre() throws Exception {
}
// All the best
void solve(int TC) throws Exception {
int n = ni();
long a = ni(), b = ni();
int arr[] = readArr(n);
long cost[] = new long[n];
int diff = 0, prev = 0;
for (int i = 0; i < n; i++) {
diff += (arr[i] - prev);
prev = arr[i];
cost[i] = (long) ((long) arr[i] * (long) a) + (diff * b);
}
long ans = Long.MAX_VALUE;
long mul = 1,payload=0;
for(int i=n-2;i>=0;i--) {
long dif = arr[i+1]-arr[i];
long cur = payload+ (mul*dif);
payload = cur;
mul++;
ans = min(ans,(cur*b)+cost[i]);
}
long cur = payload +(mul*arr[0]);
ans = min(ans,cur*b);
pn(ans);
}
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);
}
double dist(int x1, int y1, int x2, int y2) {
double a = x1 - x2, b = y1 - y2;
return Math.sqrt((a * a) + (b * b));
}
int[] readArr(int n) throws Exception {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
void sort(int arr[], int left, int right) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = left; i <= right; i++)
arr[i] = list.get(i - left);
}
void sort(int arr[]) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < arr.length; i++)
arr[i] = list.get(i);
}
public long max(long... arr) {
long max = arr[0];
for (long itr : arr)
max = Math.max(max, itr);
return max;
}
public int max(int... arr) {
int max = arr[0];
for (int itr : arr)
max = Math.max(max, itr);
return max;
}
public long min(long... arr) {
long min = arr[0];
for (long itr : arr)
min = Math.min(min, itr);
return min;
}
public int min(int... arr) {
int min = arr[0];
for (int itr : arr)
min = Math.min(min, itr);
return min;
}
public long sum(long... arr) {
long sum = 0;
for (long itr : arr)
sum += itr;
return sum;
}
public long sum(int... arr) {
long sum = 0;
for (int itr : arr)
sum += itr;
return sum;
}
String bin(long n) {
return Long.toBinaryString(n);
}
String bin(int n) {
return Integer.toBinaryString(n);
}
static int bitCount(int x) {
return x == 0 ? 0 : (1 + bitCount(x & (x - 1)));
}
static void dbg(Object... o) {
System.err.println(Arrays.deepToString(o));
}
int bit(long n) {
return (n == 0) ? 0 : (1 + bit(n & (n - 1)));
}
int abs(int a) {
return (a < 0) ? -a : a;
}
long abs(long a) {
return (a < 0) ? -a : a;
}
void p(Object o) {
out.print(o);
}
void pn(Object o) {
out.println(o);
}
void pni(Object o) {
out.println(o);
out.flush();
}
void pn(int[] arr) {
int n = arr.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(arr[i] + " ");
}
pn(sb);
}
void pn(long[] arr) {
int n = arr.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(arr[i] + " ");
}
pn(sb);
}
String n() throws Exception {
return in.next();
}
String nln() throws Exception {
return in.nextLine();
}
int ni() throws Exception {
return Integer.parseInt(in.next());
}
long nl() throws Exception {
return Long.parseLong(in.next());
}
double nd() throws Exception {
return Double.parseDouble(in.next());
}
public static void main(String[] args) throws Exception {
new Practice().run();
}
FastReader in;
PrintWriter out;
void run() throws Exception {
in = new FastReader();
out = new PrintWriter(System.out);
int T = (multipleTC) ? ni() : 1;
pre();
for (int t = 1; t <= T; t++)
solve(t);
out.flush();
out.close();
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception {
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | cbe3729fc6b433dfa5760d557032c09c | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class Main {
private static int MOD = 1000000007;
private static long Inf = (long) 1E15;
public static void main(String[] args) throws Exception {
InputStream inputStream = System.in;
//InputStream inputStream = new FileInputStream(new File("/tmp/input.txt"));
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int t = in.nextInt();
for (int i = 0; i < t; i++) {
Task solver = new Task();
solver.solve(in, out);
}
out.close();
}
static class Task {
public void solve(Scanner in, PrintWriter out) {
int n = in.nextInt();
long a = in.nextInt();
long b = in.nextInt();
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = in.nextLong();
}
long[] suf = new long[n];
suf[n-1] = x[n-1];
for (int i = n-2; i >= 0; i--) {
suf[i] = suf[i+1] + x[i];
}
long cost = 0;
int capital = -1;
long capLoc = 0;
for (int i = 0; i < n; i++) {
cost += b * Math.abs(x[i] - capLoc);
if (capital == i-1) {
int k = n-1-i;
if (a <= k * b) {
capital++;
cost += a * (x[capital] - capLoc);
capLoc = x[capital];
}
}
}
out.println(cost);
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | d11470fb24c2b13a892d92557c70e0f3 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class q3 {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// public static long mod = 1000000007;
public static void solve() throws Exception {
String[] parts = br.readLine().split(" ");
int n = Integer.parseInt(parts[0]);
long a = Integer.parseInt(parts[1]);
long b = Integer.parseInt(parts[2]);
long[] arr = new long[n + 1];
parts = br.readLine().split(" ");
for(int i = 0;i < n;i++){
arr[i + 1] = Integer.parseInt(parts[i]);
}
long[] dp = new long[n + 1];
long ans = 0;
for(int i = 1;i < n + 1;i++) ans += b * (arr[i] - arr[i - 1]);
long[] psum = new long[n + 1];
for(int i = 1;i < n + 1;i++) psum[i] = psum[i - 1] + arr[i];
long min = Long.MAX_VALUE;
for(int i = 0;i < n;i++){
min = Math.min(min,arr[i] * a + (psum[n - 1] - psum[i] - (arr[i] * (n - 1 - i))) * b);
}
System.out.println(ans + min);
}
public static void main(String[] args) throws Exception {
int tests = Integer.parseInt(br.readLine());
for (int test = 1; test <= tests; test++) {
solve();
}
}
// public static ArrayList<Integer> primes;
// public static void seive(int n){
// primes = new ArrayList<>();
// boolean[] arr = new boolean[n + 1];
// Arrays.fill(arr,true);
//
// for(int i = 2;i * i <= n;i++){
// if(arr[i]) {
// for (int j = i * i; j <= n; j += i) {
// arr[j] = false;
// }
// }
// }
// for(int i = 2;i <= n;i++) if(arr[i]) primes.add(i);
// }
// public static void sort(int[] arr){
// ArrayList<Integer> temp = new ArrayList<>();
// for(int val : arr) temp.add(val);
//
// Collections.sort(temp);
//
// for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i);
// }
// public static void sort(long[] arr){
// ArrayList<Long> temp = new ArrayList<>();
// for(long val : arr) temp.add(val);
//
// Collections.sort(temp);
//
// for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i);
// }
//
// public static long power(long a,long b,long mod){
// if(b == 0) return 1;
//
// long p = power(a,b / 2,mod);
// p = (p * p) % mod;
//
// if(b % 2 == 1) return (p * a) % mod;
// return p;
// }
// public static long modDivide(long a,long b,long mod){
// return ((a % mod) * (power(b,mod - 2,mod) % mod)) % mod;
// }
//
// public static int GCD(int a,int b){
// return b == 0 ? a : GCD(b,a % b);
// }
// public static long GCD(long a,long b){
// return b == 0 ? a : GCD(b,a % b);
// }
//
// public static int LCM(int a,int b){
// return a * b / GCD(a,b);
// }
// public static long LCM(long a,long b){
// return a * b / GCD(a,b);
// }
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 79f45ecc58420fa913e9045f1600e0e6 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class Question4 {
static boolean multipleTC = true;
final static int Mod = 1000000007;
final static int Mod2 = 998244353;
final double PI = 3.14159265358979323846;
int MAX = 1000000007;
void pre() throws Exception {
}
long knapSack(long W, long wt[], long val[], int n){
long []dp = new long[(int) (W + 1)];
for (int i = 1; i < n + 1; i++) {
for (long w = W; w >= 0; w--) {
if (wt[i - 1] <= w)
dp[(int) w] = Math.max(dp[(int) w], dp[(int) (w - wt[i - 1])] + val[i - 1]);
}
}
return dp[(int) W];
}
void solve(int t) throws Exception {
int n = ni();
long a = nl();
long b = nl();
long arr[] = new long[n+1];
for(int i=1;i<=n;i++)
arr[i] = nl();
long suffix[] = new long[n+1];
for(int i=n;i>=1;i--) {
if(i < n)
suffix[i] = (suffix[i+1] + arr[i]);
else
suffix[i] = arr[i];
}
long min = Long.MAX_VALUE;
for(int i=0;i<n;i++) {
long sum = arr[i]*a + (suffix[i+1] - ((n-i-1)*arr[i]))*b;
min = Math.min(min, sum);
}
pn(min);
}
double dist(int x1, int y1, int x2, int y2) {
double a = x1 - x2, b = y1 - y2;
return Math.sqrt((a * a) + (b * b));
}
int[] readArr(int n) throws Exception {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
void sort(int arr[], int left, int right) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = left; i <= right; i++)
arr[i] = list.get(i - left);
}
void sort(int arr[]) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < arr.length; i++)
arr[i] = list.get(i);
}
public long max(long... arr) {
long max = arr[0];
for (long itr : arr)
max = Math.max(max, itr);
return max;
}
public int max(int... arr) {
int max = arr[0];
for (int itr : arr)
max = Math.max(max, itr);
return max;
}
public long min(long... arr) {
long min = arr[0];
for (long itr : arr)
min = Math.min(min, itr);
return min;
}
public int min(int... arr) {
int min = arr[0];
for (int itr : arr)
min = Math.min(min, itr);
return min;
}
public long sum(long... arr) {
long sum = 0;
for (long itr : arr)
sum += itr;
return sum;
}
public long sum(int... arr) {
long sum = 0;
for (int itr : arr)
sum += itr;
return sum;
}
String bin(long n) {
return Long.toBinaryString(n);
}
String bin(int n) {
return Integer.toBinaryString(n);
}
static int bitCount(int x) {
return x == 0 ? 0 : (1 + bitCount(x & (x - 1)));
}
static void dbg(Object... o) {
System.err.println(Arrays.deepToString(o));
}
int bit(long n) {
return (n == 0) ? 0 : (1 + bit(n & (n - 1)));
}
int abs(int a) {
return (a < 0) ? -a : a;
}
long abs(long a) {
return (a < 0) ? -a : a;
}
void p(Object o) {
out.print(o);
}
void pn(Object o) {
out.println(o);
}
void pni(Object o) {
out.println(o);
out.flush();
}
void pn(int[] arr) {
int n = arr.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(arr[i] + " ");
}
pn(sb);
}
void pn(long[] arr) {
int n = arr.length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(arr[i] + " ");
}
pn(sb);
}
String n() throws Exception {
return in.next();
}
String nln() throws Exception {
return in.nextLine();
}
int ni() throws Exception {
return Integer.parseInt(in.next());
}
long nl() throws Exception {
return Long.parseLong(in.next());
}
double nd() throws Exception {
return Double.parseDouble(in.next());
}
public static void main(String[] args) throws Exception {
new Question4().run();
}
FastReader in;
PrintWriter out;
void run() throws Exception {
in = new FastReader();
out = new PrintWriter(System.out);
int T = (multipleTC) ? ni() : 1;
pre();
for (int t = 1; t <= T; t++)
solve(t);
out.flush();
out.close();
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception {
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
throw new Exception(e.toString());
}
return str;
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 2df6af145e109f40ae57d2a040cab91c | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.DataInputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import static java.lang.Math.min;
public class Round782_C {
@SuppressWarnings("FieldCanBeLocal")
private static Reader in;
private static PrintWriter out;
private static String YES = "YES";
private static String NO = "NO";
private static void solve() throws IOException{
//Your Code Goes Here;
int n = in.nextInt();
long c1 = in.nextLong(), c2 = in.nextLong();
int[] arr = readArray(n);
long ans = 0;
for(int i = 0; i < n; i++){
int nToRight = n - i;
int last = i == 0 ? 0 : arr[i - 1];
long dist = arr[i] - last;
ans += dist * min(nToRight * c2, c1 + c2);
}
System.out.println(ans);
}
public static void main(String[] args) throws IOException, InterruptedException {
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 Random random = new Random();
static final int mod = 1_000_000_007;
private static boolean check(long[] arr, long n){
long ch = arr[0];
for(int i = 0; i < n; i++){
if(ch != arr[i]){
return false;
}
}
return true;
}
//This function gives the max occuring of any element in an array;(HashMap version)
private static long maxFreqHashMap(long[] arr, long n){
Map<Long, Long> hp = new HashMap<Long, Long>();
for(int i = 0; i < n; i++) {
long key = arr[i];
if(hp.containsKey(key)) {
long freq = hp.get(key);
freq++;
hp.put(key, freq);
}
else {
hp.put(key, 1L);
}
}
long max_count = 0, res = 1, count = 0;
for(Map.Entry<Long, Long> val : hp.entrySet()) {
if (max_count < val.getValue()) {
res = Math.toIntExact(val.getKey());
max_count = Math.toIntExact(val.getValue());
count = max_count;
}
}
return count;
}
//This function gives the max occuring of any element in an array; this also known as the "MOORE's Algorithm"
private static long maxFreq(long []arr, long n) {
// using moore's voting algorithm
long res = 0;
long count = -1;
for(int i = 1; i < n; i++) {
if(arr[i] == arr[(int) res]) {
count++;
}
else {
count--;
}
if(count == 0) {
res = i;
count = 1;
}
}
return arr[(int) res];
/*
you've to add below code in the solve()
long freq = maxFreq(arr, n);
int count = 0;
for(int i = 0; i < n; i++){
if(arr[i] == freq){
count++;
}
}
*/
}
private static int LCSubStr(char[] X, char[] Y, int m, int n) {
int[][] LCStuff = new int[m + 1][n + 1];
int result = 0;
for (int i = 0; i <= m; i++){
for (int j = 0; j <= n; j++) {
if (i == 0 || j == 0) {
LCStuff[i][j] = 0;
}
else if (X[i - 1] == Y[j - 1]) {
LCStuff[i][j] = LCStuff[i - 1][j - 1] + 1;
result = Integer.max(result, LCStuff[i][j]);
}
else {
LCStuff[i][j] = 0;
}
}
}
return result;
}
private static long longCusBsearch(long[] arr, long n, long h){
long ans = h;
long l = 1;
long r = h;
while(l <= r){
long mid = (l + r) / 2;
long ok = 0;
for(long i = 0; i < n; i++){
if(i == n - 1) {
ok += mid;
}
else{
long x = arr[(int) (i + 1)] - arr[(int) i];
if(x >= mid) {
ok += mid;
}
else{
ok += x;
}
}
}
if(ok >= h){
ans = mid;
r = mid - 1;
}
else{
l = mid + 1;
}
}
return ans;
}
public static int intCusBsearch(int[] arr, int n, int h){
int ans = h, l = 1, r = h;
while(l <= r){
int mid = (l + r) / 2;
int ok = 0;
for(int i = 0; i < n; i++){
if(i == n - 1){
ok += mid;
}
else{
int x = arr[i + 1] - arr[i];
if(x >= mid){
ok += mid;
}
else{
ok += x;
}
}
}
if(ok >= h){
ans = mid;
r = mid - 1;
}
else{
l = mid + 1;
}
}
return ans;
}
/* Method to check if x is power of 2*/
private static boolean isPowerOfTwo (int x) {
/* First x in the below expression is
for the case when x is 0 */
return x!=0 && ((x&(x-1)) == 0);
}
//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);
}
private static void longRuffleSort(long[] a){
long n = a.length;
for(long i = 0;i<n;i++){
long oi = random.nextInt((int) n), temp = a[(int) oi];
a[(int) oi] = a[(int) i]; a[(int) i] = temp;
}
longSort(a);
}
private static int gcd(int a, int b){
if(a == 0 || b == 0){
return 0;
}
while(b != 0){
int temp;
temp = a % b;
a = b;
b = temp;
}
return a;
}
private static long gcd(long a, long b){
if(a == 0 || b == 0){
return 0;
}
while(b != 0){
long temp;
temp = a % b;
a = b;
b = temp;
}
return a;
}
private static int lowestCommonMultiple(int a, int b){
return (a / gcd(a, b) * b);
}
/*
The Below func: the for loop runs in sqrt times. The second if is used to if the
divisors are equal to print only one of them otherwise we're printing both;
*/
private static void pDivisors(int n){
for(int i=1;i<Math.sqrt(n);i++){
if(n % i == 0){
if(n / i == i){
System.out.println(i + " ");
}
else{
System.out.println(i + " " + (n / i) + " ");
}
}
}
}
private static void returnNothing(){return;}
private static int numOfdigitsinN(int a){return (int) (Math.floor(Math.log10(a)) + 1);}
//prime Num till N: it takes any number and prints all the prime till that
//num;
private static boolean isPrime(int n){
if(n <= 1) return false;
if(n <= 3) return true;
//This is checked so that we can skip middle five number in below loop:
if(n % 2 == 0 || n % 3 == 0) return false;
for(int i = 5; i * i <= n; i = i + 6){
if(n % i == 0 || n % (i + 2) == 0){
return false;
}
}
return true;
}
//below func is to print the isPrime func();
private static void printTheIsPrimeFunc(int n){
for(int i=2;i<=n;i++){
if(isPrime(i)) System.out.println(i + " ");
}
}
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 evenOdd(int num){
//System.out.println((num & 1) == 0 ? "EVEN" : "ODD");
return (num & 1) == 0 ? true : false;
}
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 int log2(int n){
int result = (int) (Math.log(n) / Math.log(2));
return result;
}
private static long add(long a, long b){
return (a + b) % mod;
}
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);
}
private static void longSort(long[] a){
ArrayList<Long> l = new ArrayList<Long>();
for(long 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;
}
//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;*/
private 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();
}
}
/* A class representing a Mutable Multiset (Collectied From TechieDelight)*/
private static class Multiset<E> {
/* List to store distinct values */
private List<E> values;
/* List to store counts of distinct values */
private List<Integer> frequency;
private final String ERROR_MSG = "Count cannot be negative: ";
/* Constructor */
public Multiset() {
values = new ArrayList<>();
frequency = new ArrayList<>();
}
/**
* Adds an element to this multiset specified number of times
*
* @param `element` The element to be added
* @param `count` The number of times
* @return The previous count of the element
*/
public int add(E element, int count) {
if (count < 0) {
throw new IllegalArgumentException(ERROR_MSG + count);
}
int index = values.indexOf(element);
int prevCount = 0;
if (index != -1) {
prevCount = frequency.get(index);
frequency.set(index, prevCount + count);
}
else if (count != 0) {
values.add(element);
frequency.add(count);
}
return prevCount;
}
/**
* Adds specified element to this multiset
*
* @param `element` The element to be added
* @return true always
*/
public boolean add(E element) {
return add(element, 1) >= 0;
}
/**
* Adds all elements in the specified collection to this multiset
*
* @param `c` Collection containing elements to be added
* @return true if all elements are added to this multiset
*/
boolean addAll(Collection<? extends E> c) {
for (E element: c) {
add(element, 1);
}
return true;
}
/**
* Adds all elements in the specified array to this multiset
*
* @param `arr` An array containing elements to be added
*/
public void addAll(E... arr) {
for (E element: arr) {
add(element, 1);
}
}
/**
* Performs the given action for each element of the Iterable,
* including duplicates
*
* @param `action` The action to be performed for each element
*/
public void forEach(Consumer<? super E> action) {
List<E> all = new ArrayList<>();
for (int i = 0; i < values.size(); i++)
{
for (int j = 0; j < frequency.get(i); j++) {
all.add(values.get(i));
}
all.forEach(action);
}
}
/**
* Removes a single occurrence of the specified element from this multiset
*
* @param `element` The element to removed
* @return true if an occurrence was found and removed
*/
public boolean remove(Object element) {
return remove(element, 1) > 0;
}
/**
* Removes a specified number of occurrences of the specified element
* from this multiset
*
* @param `element` The element to removed
* @param `count` The number of occurrences to be removed
* @return The previous count
*/
public int remove(Object element, int count) {
if (count < 0) {
throw new IllegalArgumentException(ERROR_MSG + count);
}
int index = values.indexOf(element);
if (index == -1) {
return 0;
}
int prevCount = frequency.get(index);
if (prevCount > count) {
frequency.set(index, prevCount - count);
}
else {
values.remove(index);
frequency.remove(index);
}
return prevCount;
}
/**
* Check if this multiset contains at least one occurrence of the
* specified element
*
* @param `element` The element to be checked
* @return true if this multiset contains at least one occurrence
* of the element
*/
public boolean contains(Object element) {
return values.contains(element);
}
/**
* Check if this multiset contains at least one occurrence of each element
* in the specified collection
*
* @param `c` The collection of elements to be checked
* @return true if this multiset contains at least one occurrence
* of each element
*/
public boolean containsAll(Collection<?> c) {
return values.containsAll(c);
}
/**
* Update the frequency of an element to the specified count or
* add element to this multiset if not present
*
* @param `element` The element to be updated
* @param `count` The new count
* @return The previous count
*/
public int setCount(E element, int count) {
if (count < 0) {
throw new IllegalArgumentException(ERROR_MSG + count);
}
if (count == 0) {
remove(element);
}
int index = values.indexOf(element);
if (index == -1) {
return add(element, count);
}
int prevCount = frequency.get(index);
frequency.set(index, count);
return prevCount;
}
/**
* Find the frequency of an element in this multiset
*
* @param `element` The element to be counted
* @return The frequency of the element
*/
public int count(Object element) {
int index = values.indexOf(element);
return (index == -1) ? 0 : frequency.get(index);
}
/**
* @return A view of the set of distinct elements in this multiset
*/
public Set<E> elementSet() {
return values.stream().collect(Collectors.toSet());
}
/**
* @return true if this multiset is empty
*/
public boolean isEmpty() {
return values.size() == 0;
}
/**
* @return Total number of elements in this multiset, including duplicates
*/
public int size() {
int size = 0;
for (Integer i: frequency) {
size += i;
}
return size;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("[");
for (int i = 0; i < values.size(); i++)
{
sb.append(values.get(i));
if (frequency.get(i) > 1) {
sb.append(" x ").append(frequency.get(i));
}
if (i != values.size() - 1) {
sb.append(", ");
}
}
return sb.append("]").toString();
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | dc7d930226cce1784cbf57f51a974efe | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class C {
public static void main(String args[]) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
StringBuilder sb = new StringBuilder();
while (t-- > 0) {
int inp[] = nextIntArray(3, br);
int arr[] = nextIntArray(inp[0], br);
sb.append(solve(arr, inp[1], inp[2])).append("\n");
}
br.close();
System.out.println(sb);
}
private static long solve(int arr[], int A, int B) {
long dp[] = new long[arr.length];
// int capital[] = new int[arr.length];
// Arrays.fill(dp, Integer.MAX_VALUE);
// B For attack
// A for relocate
for (int i = 0; i < dp.length; i++)
dp[i] = findCost(arr[i], 0, B);
// System.out.println(Arrays.toString(dp));
for (int i = 1; i < dp.length; i++)
dp[i] += dp[i - 1];
int prev = 0;
long diffSum = 0;
for (int i = 0; i < dp.length; i++) {
long costRelocate = findCost(arr[i], prev, A);
long diff = (dp.length - 1 - i) * 1l * (arr[i] - prev) * B;
if (diff > costRelocate) {
prev = arr[i];
diffSum += diff;
diffSum -= costRelocate;
// System.out.println("Change Location at " + arr[i]);
}
}
// System.out.println(Arrays.toString(dp) + " " + diffSum);
// System.out.println(Arrays.toString(capital));
return dp[arr.length - 1] - diffSum;
}
private static long findCost(int C1, int C2, int multiplier) {
return 1l * Math.abs(C1 - C2) * multiplier;
}
private static int[] nextIntArray(int N, BufferedReader br) throws Exception {
StringTokenizer st = new StringTokenizer(br.readLine().trim(), " ");
int arr[] = new int[N];
for (int i = 0; i < N; i++)
arr[i] = Integer.parseInt(st.nextToken());
return arr;
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 4bc23869291951332920758acd999718 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class LineEmpire {
public static void main(String[] args) throws Exception {
FastIO in = new FastIO();
int t = in.nextInt();
for (int tc=0; tc<t; tc++) {
int n = in.nextInt();
long a = in.nextInt();
long b = in.nextInt();
long[] pos = new long[n+1];
for (int i=1; i<=n; i++) {
pos[i] = in.nextLong();
pos[i]+=pos[i-1];
}
long minAns = pos[n]*b;
long currAns = 0;
for (int i = 1; i <= n; i++) {
long dist = pos[i] - pos[i - 1];
if (i > 1) dist -= (pos[i - 1] - pos[i - 2]);
currAns+=a*dist;
currAns+=b*dist;
minAns = Math.min(minAns, currAns + b * ((pos[n] - pos[i]) - (n - i) * (pos[i] - pos[i - 1])));
}
System.out.println(minAns);
}
}
static class FastIO {
BufferedReader br;
StringTokenizer st;
PrintWriter pr;
public FastIO() throws IOException
{
br = new BufferedReader(
new InputStreamReader(System.in));
pr = new PrintWriter(System.out);
}
public String next() throws IOException
{
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); }
public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); }
public double nextDouble() throws NumberFormatException, IOException
{
return Double.parseDouble(next());
}
public String nextLine() throws IOException
{
String str = br.readLine();
return str;
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 8cddeadc6cc0b62cd94897f2e93ee277 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static int[][] size;
static int[][][] parent;
static char[][] a;
public static void main(String[] args) {
MyScanner in = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = in.nextInt();
while(t-- > 0) {
int n = in.nextInt();
long a = in.nextLong(); // move
long b = in.nextLong(); // conquer
long [] x = new long[n];
for(int i = 0; i < n; i++) {
x[i] = in.nextLong();
}
long[] partSum = new long[n+1]; //partSum[i+1] = x_0 + x2... + xi
for(int i = 1; i <= n; i++) {
partSum[i] = partSum[i-1] + x[i-1];
}
long min = b * partSum[n];
for(int i = 0; i < n; i++) {
long val = ( a + b) * x[i];
long dist = n - 1 - i;
val += b * (partSum[n] - partSum[i+1] - dist * x[i]);
min = Math.min(min, val);
}
out.println(min);
}
out.close();
return;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------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;
}
}
//--------------------------------------------------------
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 5ed9d37957390507fb79af7d393846e0 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class CodeForces {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int tt = Integer.parseInt(in.readLine());
while (tt-- > 0) {
String[] tokens = in.readLine().split("\\s+");
long[] cities = Arrays.stream(in.readLine().split("\\s+")).mapToLong(Long::parseLong).toArray();
long n = Long.parseLong(tokens[0]);
long a = Long.parseLong(tokens[1]);
long b = Long.parseLong(tokens[2]);
long res = 0;
long currPos = 0;
for (int i = 0; i < cities.length; i++){
res += b*(cities[i]-currPos);
if (b*(n-i-1)>a){
res += a*(cities[i]-currPos);
currPos = cities[i];
}
}
System.out.println(res);
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 6d2b6886262a8b5aece7b117bcfd32f5 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class A4 {
static long memo[];
static long arr[];
static int a;
static int b;
static int n;
public static void main(String[] args) throws Exception {
int t = sc.nextInt();
while (t-- > 0) {
n = sc.nextInt();
a = sc.nextInt();
b = sc.nextInt();
arr = new long[n + 1];
for (int i = 1; i <= n; i++) {
arr[i] = sc.nextLong();
}
long ans = 0;
int j = 0;
for (int i = 1; i <= n; i++) {
long cost2 = b * Math.abs(arr[j] - arr[i]);
ans += cost2;
if (b * Math.abs(arr[j] - arr[i]) * (n - i) >= a * Math.abs(arr[j] - arr[i])) {
ans += a * Math.abs(arr[j] - arr[i]);
j = i;
}
// j++;
}
pw.println(ans);
}
pw.flush();
}
public static long dp(int idx1, int idx2) {
if (idx2 > n - 1)
return 0;
boolean f = false;
long move1 = Math.abs(arr[idx2] - arr[idx2 - 1]);
long move2 = Math.abs(arr[idx2] - arr[idx1]);
f = move1 < move2;
long conquer = b * Math.abs(arr[idx2 + 1] - arr[idx1]);
if (f)
return Math.min(conquer + a * move1 + dp(idx2 - 1, idx2 + 1), conquer + dp(idx1, idx2 + 1));
return conquer + dp(idx1, idx2 + 1);
}
public static long fibo(int n) {
if (n == 0)
return 0;
if (n == 1)
return 1;
if (memo[n] != -1)
return memo[n];
return memo[n] = fibo(n - 1) + fibo(n - 2);
}
public static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (long i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
public static BigInteger fib(int n) {
BigInteger a = BigInteger.valueOf(0);
BigInteger b = BigInteger.valueOf(1);
for (int i = 0; i < n; i++) {
BigInteger c = a.add(b);
a = b;
b = c;
}
return b;
}
// isPrime using BigInteger
public static boolean isPrime(BigInteger n) {
if (n.compareTo(BigInteger.valueOf(2)) < 0)
return false;
if (n.compareTo(BigInteger.valueOf(2)) == 0)
return true;
if (n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0)) == 0)
return false;
BigInteger d = n.subtract(BigInteger.valueOf(1));
BigInteger s = d.divide(BigInteger.valueOf(2));
while (s.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0)) == 0) {
s = s.divide(BigInteger.valueOf(2));
}
for (BigInteger i = BigInteger.valueOf(2); i.compareTo(s) <= 0; i = i.add(BigInteger.valueOf(1))) {
if (n.mod(i).compareTo(BigInteger.valueOf(0)) == 0)
return false;
}
return true;
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 753447b6f2a852cbd1e4267cc62ee850 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | //some updates in import stuff
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
//key points learned
//max space ever that could be alloted in a program to pass in cf
//int[][] prefixSum = new int[201][200_005]; -> not a single array more!!!
//never allocate memory again again to such bigg array, it will give memory exceeded for sure
//believe in your fucking solution and keep improving it!!! (sometimes)
public class Main{
static int mod = (int) (Math.pow(10, 9)+7);
static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 };
static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };
static final double eps = 1e-10;
static List<Integer> primeNumbers = new ArrayList<>();
public static void main(String[] args) {
MyScanner sc = new MyScanner(); //pretty important for sure -
out = new PrintWriter(new BufferedOutputStream(System.out)); //dope shit output for sure
//code here
int test = sc.nextInt();
while(test --> 0){
int n = sc.nextInt();
long a = sc.nextLong();
long b = sc.nextLong();
long[] capital = new long[n];
for(int i= 0; i < n; i++){
capital[i] = sc.nextLong(); //simple as that
}
long ans = Long.MAX_VALUE; //simply taking this as answer
//gotta make a prefix sum for sure
long[] prefix = new long[n];
prefix[n-1] = capital[n-1];
for(int i= n-2; i >= 0; i--){
prefix[i] = capital[i] + prefix[i + 1];
}
ans = min(prefix[0] * b, ans);
// System.out.println(Arrays.toString(prefix));
long last = 0;
long add = 0;
for(int i= 0; i < n - 1; i++){
add += capital[i] - last;
last = capital[i];
long currAns = add * (a + b);
long rem = prefix[i+1] - capital[i] * (n-i-1);
currAns += rem * b;
ans = min(ans, currAns);
}
System.out.println(ans);
}
out.close();
}
//new stuff to learn (whenever this is need for them, then only)
//Lazy Segment Trees
//Persistent Segment Trees
//Square Root Decomposition
//Geometry & Convex Hull
//High Level DP -- yk yk
//String Matching Algorithms
//Heavy light Decomposition
//Updation Required
//Fenwick Tree - both are done (sum)
//Segment Tree - both are done (min, max, sum)
//-----CURRENTLY PRESENT-------//
//Graph
//DSU
//powerMODe
//power
//Segment Tree (work on this one)
//Prime Sieve
//Count Divisors
//Next Permutation
//Get NCR
//isVowel
//Sort (int)
//Sort (long)
//Binomial Coefficient
//Pair
//Triplet
//lcm (int & long)
//gcd (int & long)
//gcd (for binomial coefficient)
//swap (int & char)
//reverse
//primeExponentCounts
//Fast input and output
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//GRAPH (basic structure)
public static class Graph{
public int V;
public ArrayList<ArrayList<Integer>> edges;
//2 -> [0,1,2] (current)
Graph(int V){
this.V = V;
edges = new ArrayList<>(V+1);
for(int i= 0; i <= V; i++){
edges.add(new ArrayList<>());
}
}
public void addEdge(int from , int to){
edges.get(from).add(to);
edges.get(to).add(from);
}
}
//DSU (path and rank optimised)
public static class DisjointUnionSets {
int[] rank, parent;
int n;
public DisjointUnionSets(int n)
{
rank = new int[n];
parent = new int[n];
Arrays.fill(rank, 1);
Arrays.fill(parent,-1);
this.n = n;
}
public int find(int curr){
if(parent[curr] == -1)
return curr;
//path compression optimisation
return parent[curr] = find(parent[curr]);
}
public void union(int a, int b){
int s1 = find(a);
int s2 = find(b);
if(s1 != s2){
//union by size
if(rank[s1] < rank[s2]){
parent[s1] = s2;
rank[s2] += rank[s1];
}else{
parent[s2] = s1;
rank[s1] += rank[s2];
}
}
}
}
//with mod
public static long powerMOD(long x, long y)
{
long res = 1L;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0){
x %= mod;
res %= mod;
res = (res * x)%mod;
}
// y must be even now
y = y >> 1; // y = y/2
x%= mod;
x = (x * x)%mod; // Change x to x^2
}
return res%mod;
}
//without mod
public static long power(long x, long y)
{
long res = 1L;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0){
res = (res * x);
}
// y must be even now
y = y >> 1; // y = y/
x = (x * x);
}
return res;
}
public static class segmentTree{
//so let's make a constructor function for this bad boi for sure!!!
public long[] arr;
public long[] tree;
//COMPLEXITY (normal segment tree, stuff)
//build -> O(n)
//query -> O(logn)
//update -> O(logn)
//update-range -> O(n) (worst case)
//simple iteration and stuff for sure
public segmentTree(long[] arr){
int n = arr.length;
this.arr = new long[n];
for(int i= 0; i < n; i++){
this.arr[i] = arr[i];
}
tree = new long[4*n + 1];
}
//pretty basic idea if you read the code once
//first make child node once
//then form the parent node using them
public void buildTree(int s, int e, int index){
if(s == e){
tree[index] = arr[s];
return;
}
//recursive case
int mid = (s + e)/2;
buildTree(s, mid, 2 * index);
buildTree(mid + 1, e, 2*index + 1);
//the condition we want from children be like this
tree[index] = min(tree[2 * index], tree[2 * index + 1]);
return;
}
//definitely index based 0 query!!!
//only int index = 1!!
//baaki everything is simple as fuck
public long query(int s, int e, int qs , int qe, int index){
//complete overlap
if(s >= qs && e <= qe){
return tree[index];
}
//no overlap
if(qe < s || qs > e){
return Long.MAX_VALUE;
}
//partial overlap
int mid = (s + e)/2;
long left = query( s, mid , qs, qe, 2*index);
long right = query( mid + 1, e, qs, qe, 2*index + 1);
return min(left, right);
}
//gonna do range updates for sure now!!
//let's do this bois!!! (solve this problem for sure)
public void updateRange(int s, int e, int l, int r, long increment, int index){
//out of bounds
if(l > e || r < s){
return;
}
//leaf node
if(s == e){
tree[index] += increment;
return; //behnchoda return tera baap krvayege?
}
//recursive case
int mid = (s + e)/2;
updateRange(s, mid, l, r, increment, 2 * index);
updateRange(mid + 1, e, l, r, increment, 2 * index + 1);
tree[index] = min(tree[2 * index], tree[2 * index + 1]);
}
}
public static class SegmentTreeLazy{
//so let's make a constructor function for this bad boi for sure!!!
public long[] arr;
public long[] tree;
public long[] lazy;
//COMPLEXITY (normal segment tree, stuff)
//build -> O(n)
//query-range -> O(logn)
//lazy update-range -> O(logn) (imp)
//simple iteration and stuff for sure
public SegmentTreeLazy(long[] arr){
int n = arr.length;
this.arr = new long[n];
for(int i= 0; i < n; i++){
this.arr[i] = arr[i];
}
tree = new long[4*n + 1];
lazy = new long[100000]; //pretty big for no inconvenience (no?)
}
//pretty basic idea if you read the code once
//first make child node once
//then form the parent node using them
public void buildTree(int s, int e, int index){
if(s == e){
tree[index] = arr[s];
return;
}
//recursive case
int mid = (s + e)/2;
buildTree(s, mid, 2 * index);
buildTree(mid + 1, e, 2*index + 1);
//the condition we want from children be like this
tree[index] = min(tree[2 * index], tree[2 * index + 1]);
return;
}
//definitely index based 0 query!!!
//only int index = 1!!
//baaki everything is simple as fuck
public long queryLazy(int s, int e, int qs, int qe, int index){
//before going down resolve if it exist
if(lazy[index] != 0){
tree[index] += lazy[index];
//non leaf node
if(s != e){
lazy[2*index] += lazy[index];
lazy[2*index + 1] += lazy[index];
}
lazy[index] = 0; //clear the lazy value at current node for sure
}
//no overlap
if(s > qe || e < qs){
return Long.MAX_VALUE;
}
//complete overlap
if(s >= qs && e <= qe){
return tree[index];
}
//partial overlap
int mid = (s + e)/2;
long left = queryLazy(s, mid, qs, qe, 2 * index);
long right = queryLazy(mid + 1, e, qs, qe, 2 * index + 1);
return Math.min(left, right);
}
//update range in O(logn) -- using lazy array
public void updateRangeLazy(int s, int e, int l, int r, int inc, int index){
//before going down resolve if it exist
if(lazy[index] != 0){
tree[index] += lazy[index];
//non leaf node
if(s != e){
lazy[2*index] += lazy[index];
lazy[2*index + 1] += lazy[index];
}
lazy[index] = 0; //clear the lazy value at current node for sure
}
//no overlap
if(s > r || l > e){
return;
}
//another case
if(l <= s && e <= r){
tree[index] += inc;
//create a new lazy value for children node
if(s != e){
lazy[2*index] += inc;
lazy[2*index + 1] += inc;
}
return;
}
//recursive case
int mid = (s + e)/2;
updateRangeLazy(s, mid, l, r, inc, 2*index);
updateRangeLazy(mid + 1, e, l, r, inc, 2*index + 1);
//update the tree index
tree[index] = Math.min(tree[2*index], tree[2*index + 1]);
return;
}
}
//prime sieve
public static void primeSieve(int n){
BitSet bitset = new BitSet(n+1);
for(long i = 0; i < n ; i++){
if (i == 0 || i == 1) {
bitset.set((int) i);
continue;
}
if(bitset.get((int) i)) continue;
primeNumbers.add((int)i);
for(long j = i; j <= n ; j+= i)
bitset.set((int)j);
}
}
//number of divisors
public static int countDivisors(long number){
if(number == 1) return 1;
List<Integer> primeFactors = new ArrayList<>();
int index = 0;
long curr = primeNumbers.get(index);
while(curr * curr <= number){
while(number % curr == 0){
number = number/curr;
primeFactors.add((int) curr);
}
index++;
curr = primeNumbers.get(index);
}
if(number != 1) primeFactors.add((int) number);
int current = primeFactors.get(0);
int totalDivisors = 1;
int currentCount = 2;
for (int i = 1; i < primeFactors.size(); i++) {
if (primeFactors.get(i) == current) {
currentCount++;
} else {
totalDivisors *= currentCount;
currentCount = 2;
current = primeFactors.get(i);
}
}
totalDivisors *= currentCount;
return totalDivisors;
}
//primeExponentCounts
public static int primeExponentsCount(int n) {
if (n <= 1)
return 0;
int sqrt = (int) Math.sqrt(n);
int remainingNumber = n;
int result = 0;
for (int i = 2; i <= sqrt; i++) {
while (remainingNumber % i == 0) {
result++;
remainingNumber /= i;
}
}
//in case of prime numbers this would happen
if (remainingNumber > 1) {
result++;
}
return result;
}
//now adding next permutation function to java hehe
public static boolean next_permutation(int[] p) {
for (int a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (int b = p.length - 1;; --b)
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
return false;
}
//finding the value of NCR in O(RlogN) time and O(1) space
public static long getNcR(int n, int r)
{
long p = 1, k = 1;
if (n - r < r) r = n - r;
if (r != 0) {
while (r > 0) {
p *= n;
k *= r;
long m = __gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
}
else {
p = 1;
}
return p;
}
//is vowel function
public static boolean isVowel(char c)
{
return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U');
}
//to sort the array with better method
public 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);
}
//sort long
public static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//for calculating binomialCoeff
public static int binomialCoeff(int n, int k)
{
int C[] = new int[k + 1];
// nC0 is 1
C[0] = 1;
for (int i = 1; i <= n; i++) {
// Compute next row of pascal
// triangle using the previous row
for (int j = Math.min(i, k); j > 0; j--)
C[j] = C[j] + C[j - 1];
}
return C[k];
}
//Pair with int int
public static class Pair{
public int a;
public int b;
public int hashCode;
Pair(int a , int b){
this.a = a;
this.b = b;
this.hashCode = Objects.hash(a, b);
}
@Override
public String toString(){
return a + " -> " + b;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair that = (Pair) o;
return a == that.a && b == that.b;
}
@Override
public int hashCode() {
return this.hashCode;
}
}
//Triplet with int int int
public static class Triplet{
public int a;
public int b;
public int c;
Triplet(int a , int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
@Override
public String toString(){
return a + " -> " + b;
}
}
//Shortcut function
public static long lcm(long a , long b){
return a * (b/gcd(a,b));
}
//let's make one for calculating lcm basically
public static int lcm(int a , int b){
return (a * b)/gcd(a,b);
}
//int version for gcd
public static int gcd(int a, int b){
if(b == 0)
return a;
return gcd(b , a%b);
}
//long version for gcd
public static long gcd(long a, long b){
if(b == 0)
return a;
return gcd(b , a%b);
}
//for ncr calculator(ignore this code)
public static long __gcd(long n1, long n2)
{
long gcd = 1;
for (int i = 1; i <= n1 && i <= n2; ++i) {
// Checks if i is factor of both integers
if (n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}
return gcd;
}
//swapping two elements in an array
public static void swap(int[] arr, int left , int right){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
//for char array
public static void swap(char[] arr, int left , int right){
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
//reversing an array
public static void reverse(int[] arr){
int left = 0;
int right = arr.length-1;
while(left <= right){
swap(arr, left,right);
left++;
right--;
}
}
public static long expo(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if ((b & 1) == 1L) res = (res * a) % mod; //think about this one for a second
a = (a * a) % mod;
b = b >> 1;
}
return res;
}
//SOME EXTRA DOPE FUNCTIONS
public static long mminvprime(long a, long b) {
return expo(a, b - 2, b);
}
public static long mod_add(long a, long b, long m) {
a = a % m;
b = b % m;
return (((a + b) % m) + m) % m;
}
public static long mod_sub(long a, long b, long m) {
a = a % m;
b = b % m;
return (((a - b) % m) + m) % m;
}
public static long mod_mul(long a, long b, long m) {
a = a % m;
b = b % m;
return (((a * b) % m) + m) % m;
}
public static long mod_div(long a, long b, long m) {
a = a % m;
b = b % m;
return (mod_mul(a, mminvprime(b, m), m) + m) % m;
}
//O(n) every single time remember that
public static long nCr(long N, long K , long mod){
long upper = 1L;
long lower = 1L;
long lowerr = 1L;
for(long i = 1; i <= N; i++){
upper = mod_mul(upper, i, mod);
}
for(long i = 1; i <= K; i++){
lower = mod_mul(lower, i, mod);
}
for(long i = 1; i <= (N - K); i++){
lowerr = mod_mul(lowerr, i, mod);
}
// out.println(upper + " " + lower + " " + lowerr);
long answer = mod_mul(lower, lowerr, mod);
answer = mod_div(upper, answer, mod);
return answer;
}
// long[] fact = new long[2 * n + 1];
// long[] ifact = new long[2 * n + 1];
// fact[0] = 1;
// ifact[0] = 1;
// for (long i = 1; i <= 2 * n; i++)
// {
// fact[(int)i] = mod_mul(fact[(int)i - 1], i, mod);
// ifact[(int)i] = mminvprime(fact[(int)i], mod);
// }
//ifact is basically inverse factorial in here!!!!!(imp)
public static long combination(long n, long r, long m, long[] fact, long[] ifact) {
long val1 = fact[(int)n];
long val2 = ifact[(int)(n - r)];
long val3 = ifact[(int)r];
return (((val1 * val2) % m) * val3) % m;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------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;
}
}
//--------------------------------------------------------
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | f19f5f4b98c9705311a6d58ab9285abc | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
/**
*
* @author eslam
*/
public class IceCave {
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 FastReader input = new FastReader();
static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
int t = input.nextInt();
while (t-- > 0) {
int n = input.nextInt();
long a = input.nextInt();
long b = input.nextInt();
long ar[] = new long[n + 1];
long br[] = new long[n + 1];
long sum = 0;
long ans = Long.MAX_VALUE;
long s = 0;
for (int i = 1; i < ar.length; i++) {
ar[i] = input.nextInt();
sum += ar[i];
}
for (int i = 1; i < ar.length; i++) {
br[i] = (ar[i] - ar[i - 1]) * b;
}
for (int i = 1; i < br.length; i++) {
br[i] = br[i] + br[i - 1];
}
for (int i = 0; i < n; i++) {
long c = ((sum - ar[i] * (n - i)));
long d = br[i] + c * b;
ans = Math.min(ans, d + s);
sum -= ar[i + 1];
s += (ar[i + 1] - ar[i]) * a;
}
log.write(ans + "\n");
}
log.flush();
}
public static long binaryToDecimal(String w) {
long r = 0;
long l = 0;
for (int i = w.length() - 1; i > -1; i--) {
long x = (w.charAt(i) - '0') * (long) Math.pow(2, l);
r = r + x;
l++;
}
return r;
}
public static String decimalToBinary(long n) {
String w = "";
while (n > 0) {
w = n % 2 + w;
n /= 2;
}
return w;
}
public static boolean isSorted(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
public static void print(int a[]) throws IOException {
for (int i = 0; i < a.length; i++) {
log.write(a[i] + " ");
}
log.write("\n");
}
public static void read(long[] a) {
for (int i = 1; i < a.length; i++) {
a[i] = input.nextInt();
}
}
static class pair {
int x;
int y;
public pair(int x, int y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
}
public static long LCM(long x, long y) {
return (x * y) / GCD(x, y);
}
public static long GCD(long x, long y) {
if (y == 0) {
return x;
}
return GCD(y, x % y);
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 0042ebae1b40b51e7ba3b0ac1823b0cd | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class q3 {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// public static long mod = 1000000007;
public static void solve() throws Exception {
String[] parts = br.readLine().split(" ");
int n = Integer.parseInt(parts[0]);
long a = Integer.parseInt(parts[1]);
long b = Integer.parseInt(parts[2]);
long[] arr = new long[n + 1];
parts = br.readLine().split(" ");
for(int i = 0;i < n;i++){
arr[i + 1] = Integer.parseInt(parts[i]);
}
long[] dp = new long[n + 1];
long ans = 0;
for(int i = 1;i < n + 1;i++) ans += b * (arr[i] - arr[i - 1]);
long[] psum = new long[n + 1];
for(int i = 1;i < n + 1;i++) psum[i] = psum[i - 1] + arr[i];
long min = Long.MAX_VALUE;
for(int i = 0;i < n;i++){
min = Math.min(min,arr[i] * a + (psum[n - 1] - psum[i] - (arr[i] * (n - 1 - i))) * b);
}
System.out.println(ans + min);
}
public static void main(String[] args) throws Exception {
int tests = Integer.parseInt(br.readLine());
for (int test = 1; test <= tests; test++) {
solve();
}
}
// public static ArrayList<Integer> primes;
// public static void seive(int n){
// primes = new ArrayList<>();
// boolean[] arr = new boolean[n + 1];
// Arrays.fill(arr,true);
//
// for(int i = 2;i * i <= n;i++){
// if(arr[i]) {
// for (int j = i * i; j <= n; j += i) {
// arr[j] = false;
// }
// }
// }
// for(int i = 2;i <= n;i++) if(arr[i]) primes.add(i);
// }
// public static void sort(int[] arr){
// ArrayList<Integer> temp = new ArrayList<>();
// for(int val : arr) temp.add(val);
//
// Collections.sort(temp);
//
// for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i);
// }
// public static void sort(long[] arr){
// ArrayList<Long> temp = new ArrayList<>();
// for(long val : arr) temp.add(val);
//
// Collections.sort(temp);
//
// for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i);
// }
//
// public static long power(long a,long b,long mod){
// if(b == 0) return 1;
//
// long p = power(a,b / 2,mod);
// p = (p * p) % mod;
//
// if(b % 2 == 1) return (p * a) % mod;
// return p;
// }
// public static long modDivide(long a,long b,long mod){
// return ((a % mod) * (power(b,mod - 2,mod) % mod)) % mod;
// }
//
// public static int GCD(int a,int b){
// return b == 0 ? a : GCD(b,a % b);
// }
// public static long GCD(long a,long b){
// return b == 0 ? a : GCD(b,a % b);
// }
//
// public static int LCM(int a,int b){
// return a * b / GCD(a,b);
// }
// public static long LCM(long a,long b){
// return a * b / GCD(a,b);
// }
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | e0e3fb09ed50f294dacb5651a172d2fc | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
while (t-- > 0) {
// int n = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
int nota = Integer.parseInt(st.nextToken()) + 1;
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int arr[] = new int[nota];
long sumlong = 0;
for(int i = 1; i < nota; i++)
{
arr[i] = Integer.parseInt(st.nextToken());
sumlong+=arr[i];
}
// sumlong-=arr[0];
// arr[0] = 0;
long res = Long.MAX_VALUE;
for(int i = 0; i < nota; i++)
{
long mkl = a + b;
long mkl2 = mkl * (long)arr[i];
sumlong-=arr[i];
long temp3 = ((nota-i-1) * (long)arr[i]);
long tempmkl = (sumlong - temp3);
long temp2 = tempmkl * b;
mkl2+=temp2;
res = Math.min(res, mkl2);
}
output.write(res + "\n");
// int k = Integer.parseInt(st.nextToken());
// char arr[] = br.readLine().toCharArray();
// output.write();
// int n = Integer.parseInt(st.nextToken());
// HashMap<Character, Integer> map = new HashMap<Character, Integer>();
// if
// output.write("YES\n");
// else
// output.write("NO\n");
// long a = Long.parseLong(st.nextToken());
// long b = Long.parseLong(st.nextToken());
// if(flag == 1)
// output.write("NO\n");
// else
// output.write("YES\n" + x + " " + y + " " + z + "\n");
// output.write(n+ "\n");
}
output.flush();
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 06b7707ba7969c158185eafe34b09f4f | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF1659C extends PrintWriter {
CF1659C() { super(System.out); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF1659C o = new CF1659C(); o.main(); o.flush();
}
void main() {
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
int[] xx = new int[n + 1];
for (int i = 1; i <= n; i++)
xx[i] = sc.nextInt();
long[] qq = new long[n + 2];
for (int i = n; i >= 1; i--)
qq[i] = xx[i] + qq[i + 1];
long ans = (long) b * qq[1], p = 0;
for (int i = 1; i <= n; i++) {
p += (long) (a + b) * (xx[i] - xx[i - 1]);
long q = b * (qq[i + 1] - (long) (n - i) * xx[i]);
ans = Math.min(ans, p + q);
}
println(ans);
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | ea12f0eb7d15b58071893655992a86a6 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | // JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.*;
import java.lang.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.io.*;
public class CodeForces {
static private final String INPUT = "input.txt";
static private final String OUTPUT = "output.txt";
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter out = new PrintWriter(System.out);
static DecimalFormat df = new DecimalFormat("0.00000");
final static int mod = (int) (1e9 + 7);
final static int MAX = Integer.MAX_VALUE;
final static int MIN = Integer.MIN_VALUE;
final static long INF = Long.MAX_VALUE;
final static long NEG_INF = Long.MIN_VALUE;
static Random rand = new Random();
// ======================= MAIN ==================================
public static void main(String[] args) throws IOException {
long time = System.currentTimeMillis();
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
// ==== start ====
input();
preprocess();
int t = 1;
t = readInt();
while (t-- > 0) {
solve();
}
out.flush();
// ==== end ====
if (!oj)
System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
}
private static void solve() throws IOException {
int n = readInt(), a = readInt(), b = readInt();
int[] arr = readIntArray(n);
sort(arr);
long[] suf = new long[n + 1];
for (int i = n - 1; i >= 0; i--)
suf[i] = suf[i + 1] + arr[i];
long ans = suf[0] * b;
for (int i = 0; i < n; i++) {
long cur = 1L * arr[i] * (a + b) + (suf[i + 1] - 1L * (n - i - 1) * arr[i]) * b;
ans = Math.min(ans, cur);
}
out.println(ans);
}
private static void preprocess() throws IOException {
}
// cd C:\Users\Eshan Bhatt\Visual Studio Code\Competitive Programming\CodeForces
// javac CodeForces.java
// java CodeForces
// javac CodeForces.java && java CodeForces
// ==================== CUSTOM CLASSES ================================
static class Pair {
int first, second;
Pair(int f, int s) {
first = f;
second = s;
}
public int compareTo(Pair o) {
if (this.first == o.first)
return this.second - o.second;
return this.first - o.first;
}
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
Pair other = (Pair) (obj);
if (this.first != other.first)
return false;
if (this.second != other.second)
return false;
return true;
}
@Override
public int hashCode() {
return this.first ^ this.second;
}
@Override
public String toString() {
return this.first + " " + this.second;
}
}
static class DequeNode {
DequeNode prev, next;
int val;
DequeNode(int val) {
this.val = val;
}
DequeNode(int val, DequeNode prev, DequeNode next) {
this.val = val;
this.prev = prev;
this.next = next;
}
}
// ======================= FOR INPUT ==================================
private static void input() {
FileInputStream instream = null;
PrintStream outstream = null;
try {
instream = new FileInputStream(INPUT);
outstream = new PrintStream(new FileOutputStream(OUTPUT));
System.setIn(instream);
System.setOut(outstream);
} catch (Exception e) {
System.err.println("Error Occurred.");
}
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(readLine());
return st.nextToken();
}
static long readLong() throws IOException {
return Long.parseLong(next());
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
static double readDouble() throws IOException {
return Double.parseDouble(next());
}
static char readCharacter() throws IOException {
return next().charAt(0);
}
static String readString() throws IOException {
return next();
}
static String readLine() throws IOException {
return br.readLine().trim();
}
static int[] readIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = readInt();
return arr;
}
static int[][] read2DIntArray(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
arr[i] = readIntArray(m);
return arr;
}
static List<Integer> readIntList(int n) throws IOException {
List<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(readInt());
return list;
}
static long[] readLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = readLong();
return arr;
}
static long[][] read2DLongArray(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++)
arr[i] = readLongArray(m);
return arr;
}
static List<Long> readLongList(int n) throws IOException {
List<Long> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(readLong());
return list;
}
static char[] readCharArray(int n) throws IOException {
return readString().toCharArray();
}
static char[][] readMatrix(int n, int m) throws IOException {
char[][] mat = new char[n][m];
for (int i = 0; i < n; i++)
mat[i] = readCharArray(m);
return mat;
}
// ========================= FOR OUTPUT ==================================
private static void printIList(List<Integer> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printLList(List<Long> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printIArray(int[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DIArray(int[][] arr) {
for (int i = 0; i < arr.length; i++)
printIArray(arr[i]);
}
private static void printLArray(long[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DLArray(long[][] arr) {
for (int i = 0; i < arr.length; i++)
printLArray(arr[i]);
}
// ====================== TO CHECK IF STRING IS NUMBER ========================
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static boolean isLong(String s) {
try {
Long.parseLong(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
// ==================== FASTER SORT ================================
private static void sort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void sort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>(n);
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
// ==================== MATHEMATICAL FUNCTIONS ===========================
private static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static long power(long a, long b) {
if (b == 0)
return 1L;
long ans = power(a, b >> 1);
ans *= ans;
if ((b & 1) == 1)
ans *= a;
return ans;
}
private static int mod_power(int a, int b, int mod) {
if (b == 0)
return 1;
int temp = mod_power(a, b >> 1, mod);
temp %= mod;
temp = (int) ((1L * temp * temp) % mod);
if ((b & 1) == 1)
temp = (int) ((1L * temp * a) % mod);
return temp;
}
private static int multiply(int a, int b) {
return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod);
}
private static boolean isPrime(long n) {
for (long i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
private static long nCr(long n, long r) {
if (n - r > r)
r = n - r;
long ans = 1L;
for (long i = r + 1; i <= n; i++)
ans *= i;
for (long i = 2; i <= n - r; i++)
ans /= i;
return ans;
}
// ==================== Primes using Seive =====================
private static List<Integer> getPrimes(int n) {
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i)
prime[j] = false;
}
}
// return prime;
List<Integer> list = new ArrayList<>();
for (int i = 2; i <= n; i++)
if (prime[i])
list.add(i);
return list;
}
private static int[] SeivePrime(int n) {
int[] primes = new int[n];
for (int i = 0; i < n; i++)
primes[i] = i;
for (int i = 2; i * i < n; i++) {
if (primes[i] != i)
continue;
for (int j = i * i; j < n; j += i) {
if (primes[j] == j)
primes[j] = i;
}
}
return primes;
}
// ==================== STRING FUNCTIONS ================================
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
private static String sortString(String str) {
int[] arr = new int[256];
for (char ch : str.toCharArray())
arr[ch]++;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 256; i++)
while (arr[i]-- > 0)
sb.append((char) i);
return sb.toString();
}
// ==================== LIS & LNDS ================================
private static int LIS(int arr[], int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find1(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find1(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) >= val) {
ret = mid;
j = mid - 1;
} else {
i = mid + 1;
}
}
return ret;
}
private static int LNDS(int[] arr, int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find2(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find2(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) <= val) {
i = mid + 1;
} else {
ret = mid;
j = mid - 1;
}
}
return ret;
}
// =============== Lower Bound & Upper Bound ===========
// less than or equal
private static int lower_bound(List<Integer> list, int val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(List<Long> list, long val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(int[] arr, int val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(long[] arr, long val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
// greater than or equal
private static int upper_bound(List<Integer> list, int val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(List<Long> list, long val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(int[] arr, int val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(long[] arr, long val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
// ==================== UNION FIND =====================
private static int find(int x, int[] parent) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x], parent);
}
private static boolean union(int x, int y, int[] parent, int[] rank) {
int lx = find(x, parent), ly = find(y, parent);
if (lx == ly)
return false;
if (rank[lx] > rank[ly])
parent[ly] = lx;
else if (rank[lx] < rank[ly])
parent[lx] = ly;
else {
parent[lx] = ly;
rank[ly]++;
}
return true;
}
// ================== SEGMENT TREE (RANGE SUM & RANGE UPDATE) ==================
public static class SegmentTree {
int n;
int[] arr, tree, lazy;
SegmentTree(int arr[]) {
this.arr = arr;
this.n = arr.length;
this.tree = new int[(n << 2)];
this.lazy = new int[(n << 2)];
build(1, 0, n - 1);
}
void build(int id, int start, int end) {
if (start == end)
tree[id] = arr[start];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
build(left, start, mid);
build(right, mid + 1, end);
tree[id] = tree[left] + tree[right];
}
}
void update(int l, int r, int val) {
update(1, 0, n - 1, l, r, val);
}
void update(int id, int start, int end, int l, int r, int val) {
distribute(id, start, end);
if (end < l || r < start)
return;
if (start == end)
tree[id] += val;
else if (l <= start && end <= r) {
lazy[id] += val;
distribute(id, start, end);
} else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
update(left, start, mid, l, r, val);
update(right, mid + 1, end, l, r, val);
tree[id] = tree[left] + tree[right];
}
}
int query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
int query(int id, int start, int end, int l, int r) {
if (end < l || r < start)
return 0;
distribute(id, start, end);
if (start == end)
return tree[id];
else if (l <= start && end <= r)
return tree[id];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r);
}
}
void distribute(int id, int start, int end) {
if (start == end)
tree[id] += lazy[id];
else {
tree[id] += lazy[id] * (end - start + 1);
lazy[(id << 1)] += lazy[id];
lazy[(id << 1) + 1] += lazy[id];
}
lazy[id] = 0;
}
}
// ==================== TRIE ================================
static class Trie {
class Node {
Node[] children;
boolean isEnd;
Node() {
children = new Node[26];
}
}
Node root;
Trie() {
root = new Node();
}
void insert(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
curr.children[ch - 'a'] = new Node();
curr = curr.children[ch - 'a'];
}
curr.isEnd = true;
}
boolean find(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
return false;
curr = curr.children[ch - 'a'];
}
return curr.isEnd;
}
}
// ==================== FENWICK TREE ================================
static class FT {
long[] tree;
int n;
FT(int[] arr, int n) {
this.n = n;
this.tree = new long[n + 1];
for (int i = 1; i <= n; i++) {
update(i, arr[i - 1]);
}
}
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += idx & -idx;
}
}
long query(int l, int r) {
return getSum(r) - getSum(l - 1);
}
long getSum(int idx) {
long ans = 0L;
while (idx > 0) {
ans += tree[idx];
idx -= idx & -idx;
}
return ans;
}
}
// ==================== BINARY INDEX TREE ================================
static class BIT {
long[][] tree;
int n, m;
BIT(int[][] mat, int n, int m) {
this.n = n;
this.m = m;
tree = new long[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
update(i, j, mat[i - 1][j - 1]);
}
}
}
void update(int x, int y, int val) {
while (x <= n) {
int t = y;
while (t <= m) {
tree[x][t] += val;
t += t & -t;
}
x += x & -x;
}
}
long query(int x1, int y1, int x2, int y2) {
return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1);
}
long getSum(int x, int y) {
long ans = 0L;
while (x > 0) {
int t = y;
while (t > 0) {
ans += tree[x][t];
t -= t & -t;
}
x -= x & -x;
}
return ans;
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 9b4d874c11583b27a9b45da721585963 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Problem782C
{
public static void main(String[] args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(reader.readLine());
List<Long> outputs = new ArrayList<>();
for (int i = 0; i < T; i++)
{
int[] nums = Stream.of(reader.readLine().split(" ")).mapToInt(Integer::parseInt).toArray();
long a = nums[1];
long b = nums[2];
long[] l = Stream.of(("0 " + reader.readLine()).split(" ")).mapToLong(Long::parseLong).toArray();
long long_conq = b * (l[l.length - 1] - l[l.length - 2]);
if (nums[0] == 1)
{
outputs.add(b * l[1]);
continue;
}
int j = 0;
for (j = l.length - 3; j >= 0; j--)
{
long diff = l[j + 1] - l[j];
long new_long_conq = b * diff * (l.length - j - 1);
long alternate = a * diff + b * diff;
if (alternate < new_long_conq)
{
break;
}
long_conq = long_conq + new_long_conq;
}
long score = long_conq;
long diff = 0;
for (int k = 0; k <= j; k++)
{
diff = l[k + 1] - l[k];
score += b * diff;
score += a * diff;
}
outputs.add(score);
}
for (long output : outputs)
{
System.out.println(output);
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 61525e8e3696cc11d179768ae76c6ac7 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
public class LineEmpire {
private static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st = new StringTokenizer("");
private String token;
// 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"));
}
private String peek() {
if (token == null)
try {
while (!st.hasMoreTokens()) {
String line = r.readLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
token = st.nextToken();
} catch (IOException e) { }
return token;
}
public boolean hasMoreTokens() { return peek() != null; }
public String next() {
String ans = peek();
token = null;
return ans;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
public static void main(String[] args) throws IOException {
Kattio scanner = new Kattio();
int tests = scanner.nextInt();
while (tests-- > 0) {
int numOfCities = scanner.nextInt();
int move = scanner.nextInt();
int conquer = scanner.nextInt();
long[] cities = new long[numOfCities+1];
cities[0] = 0;
for (int i = 1; i <= numOfCities; i++) {
cities[i] = scanner.nextLong();
}
int curr = 0;
long ans = 0;
for (int i = 1; i <= numOfCities; i++) {
ans += conquer*(cities[i]-cities[curr]);
if (move*(cities[i]-cities[curr]) <= conquer*(cities[i]-cities[curr])*(numOfCities-i)) {
ans += move*(cities[i]-cities[curr]);
curr = i;
}
// scanner.println(i + " |" + cities[i] +" "+ cities[curr] +"| "+curr + " " + ans);
}
scanner.println(ans);
}
scanner.close();
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | d166916a08713456d22c989a684300a1 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main {
private static FS sc = new FS();
private static class FS {
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());
}
}
private static class extra {
static int[] intArr(int size) {
int[] a = new int[size];
for(int i = 0; i < size; i++) a[i] = sc.nextInt();
return a;
}
static long[] longArr(int size) {
Scanner scc = new Scanner(System.in);
long[] a = new long[size];
for(int i = 0; i < size; i++) a[i] = sc.nextLong();
return a;
}
static long intSum(int[] a) {
long sum = 0;
for(int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
static long longSum(long[] a) {
long sum = 0;
for(int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
static LinkedList[] graphD(int vertices, int edges) {
LinkedList<Integer>[] temp = new LinkedList[vertices+1];
for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>();
for(int i = 0; i < edges; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
temp[x].add(y);
}
return temp;
}
static LinkedList[] graphUD(int vertices, int edges) {
LinkedList<Integer>[] temp = new LinkedList[vertices+1];
for(int i = 0; i <= vertices; i++) temp[i] = new LinkedList<>();
for(int i = 0; i < edges; i++) {
int x = sc.nextInt();
int y = sc.nextInt();
temp[x].add(y);
temp[y].add(x);
}
return temp;
}
static void printG(LinkedList[] temp) { for(LinkedList<Integer> aa:temp) System.out.println(aa); }
static long cal(long val, long pow, long mod) {
if(pow == 0) return 1;
long ans = cal(val, pow / 2, mod);
long ret = (ans * ans) % mod;
if(pow % 2 == 0) return ret;
return (ret * val) % mod;
}
static long gcd(long a, long b) { return b == 0 ? a:gcd(b, a%b); }
}
static int mod = 998244353;
// static int mod = (int) 1e9 + 7;
//
static long inf = (long) Long.MAX_VALUE - 1;
static LinkedList<Integer>[] temp;
public static void main(String[] args) {
int t = sc.nextInt();
// int t = 1;
StringBuilder ret = new StringBuilder();
while(t-- > 0) {
int n = sc.nextInt(), c = sc.nextInt(), p = sc.nextInt();
int[] a = extra.intArr(n);
long[] pre = new long[n];
pre[0] = a[0];
for(int i = 1; i < n; i++) pre[i] = pre[i - 1] + a[i];
long ans = p * 1L * pre[n - 1], sumP = 0;
// System.out.println(ans);
int prevK = 0;
for(int i = 0; i < n; i++) {
// conquer
long val = p * 1L * (a[i] - prevK) + c * 1L * (a[i] - prevK);
long temp = val;
prevK = a[i];
long tot = pre[n - 1] - pre[i];
val += p * (tot - (n - i - 1) * 1L * prevK);
// System.out.println(tot + " " + val + " " + sumP);
ans = Math.min(val + sumP, ans);
sumP += temp;
// placed now
}
ret.append(ans + "\n");
}
System.out.println(ret);
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | ad01b0ae33bc4cc519069025b65b71ac | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
static long mod = 1000000007;
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args) throws IOException {
FastReader sc = new FastReader();
int t = sc.nextInt();
while( t-- > 0) {
int n = sc.nextInt();
long a = sc.nextLong();
long b = sc.nextLong();
long c = 0;
long ans= 0;
long arr[] = new long[n];
long sum = 0;
long presum[] = new long[n+1];
for( int i = 0 ;i < n; i++) {
arr[i] = sc.nextLong();
sum+=arr[i];
presum[i+1] =sum;
}
for( int i = 0 ;i < n ;i++) {
ans+=b*(arr[i] - c);
// out.println(b*(arr[i] - c));
if( i != n-1) {
// presum[n] - pre[i+1] - n-i-1*b + a*(
sum = presum[n] - presum[i+1];
long fst = (sum - (n-i-1)*arr[i])*b + a*(arr[i] - c) ;
long scnd = (sum - (n-i-1)*c)*b;
if( fst < scnd) {
ans+=a*(arr[i] - c);
c = arr[i] ;
}
// out.println(fst + " " + scnd + " " + sum);
}
}
out.println(ans);
}
out.flush();
}
/*
* time for a change
*/
public static boolean ifpowof2(long n ) {
return ((n&(n-1)) == 0);
}
static boolean isprime(long x ) {
if( x== 2) {
return true;
}
if( x%2 == 0) {
return false;
}
for( long i = 3 ;i*i <= x ;i+=2) {
if( x%i == 0) {
return false;
}
}
return true;
}
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;
}
public static int[] nextLargerElement(int[] arr, int n) {
Stack<Integer> stack = new Stack<>();
int rtrn[] = new int[n];
rtrn[n-1] = -1;
stack.push( n-1);
for( int i = n-2 ;i >= 0 ; i--){
int temp = arr[i];
int lol = -1;
while( !stack.isEmpty() && arr[stack.peek()] <= temp){
if(arr[stack.peek()] == temp ) {
lol = stack.peek();
}
stack.pop();
}
if( stack.isEmpty()){
if( lol != -1) {
rtrn[i] = lol;
}
else {
rtrn[i] = -1;
}
}
else{
rtrn[i] = stack.peek();
}
stack.push( i);
}
return rtrn;
}
static void mysort(int[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
int loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static void mySort(long[] arr) {
for(int i=0;i<arr.length;i++) {
int rand = (int) (Math.random() * arr.length);
long loc = arr[rand];
arr[rand] = arr[i];
arr[i] = loc;
}
Arrays.sort(arr);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b)
{
return (a / gcd(a, b)) * b;
}
static long rightmostsetbit(long n) {
return n&-n;
}
static long leftmostsetbit(long n)
{
long k = (long)(Math.log(n) / Math.log(2));
return k;
}
static HashMap<Long,Long> primefactor( long n){
HashMap<Long ,Long> hm = new HashMap<>();
long temp = 0;
while( n%2 == 0) {
temp++;
n/=2;
}
if( temp!= 0) {
hm.put( 2L, temp);
}
long c = (long)Math.sqrt(n);
for( long i = 3 ; i <= c ; i+=2) {
temp = 0;
while( n% i == 0) {
temp++;
n/=i;
}
if( temp!= 0) {
hm.put( i, temp);
}
}
if( n!= 1) {
hm.put( n , 1L);
}
return hm;
}
static ArrayList<Long> allfactors(long abs) {
HashMap<Long,Integer> hm = new HashMap<>();
ArrayList<Long> rtrn = new ArrayList<>();
for( long i = 2 ;i*i <= abs; i++) {
if( abs% i == 0) {
hm.put( i , 0);
hm.put(abs/i, 0);
}
}
for( long x : hm.keySet()) {
rtrn.add(x);
}
if( abs != 0) {
rtrn.add(abs);
}
return rtrn;
}
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;
}
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 | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 8e6da757aab767e6b1ccb40d7ad9aa24 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
public class Main {
public static int mod = (int)1e9 + 7;
// **** -----> Disjoint Set Union(DSU) Start **********
public static int findPar(int node, int[] parent) {
if (parent[node] == node)
return node;
return parent[node] = findPar(parent[node], parent);
}
public static boolean union(int u, int v, int[] rank, int[] parent) {
u = findPar(u, parent);
v = findPar(v, parent);
if(u == v) return false;
if (rank[u] < rank[v])
parent[u] = v;
else if (rank[u] > rank[v])
parent[v] = u;
else {
parent[u] = v;
rank[v]++;
}
return true;
}
// **** DSU Ends ***********
//Pair with int int
public static class Pair{
public int a;
public int b;
Pair(int a , int b){
this.a = a;
this.b = b;
}
@Override
public String toString(){
return a + " -> " + b;
}
}
public static String toBinary(int decimal) {StringBuilder sb = new StringBuilder();while (decimal > 0) {sb.append(decimal % 2);decimal = decimal / 2;}return sb.reverse().toString();}
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 boolean isPalindrome(int[] arr) {int i = 0, j = arr.length - 1;while (i < j) {if (arr[i] != arr[j])return false;}return true;}
public static int pow(int x, int y) {int res = 1;x = x % mod;if (x == 0)return 0;while (y > 0) {if ((y & 1) != 0)res = (res * x) % mod;y = y >> 1;x = (x * x) % mod;}return res;}
public static int gcd(int a, int b) {if (b == 0)return a;return gcd(b, a % b);}
public static long gcd(long a, long b) {if (b == 0)return a;return gcd(b, a % b);}
public static void sort(long[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);long temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);}
public static void reverse(int a[]) {int i, k, t, n = a.length;for (i = 0; i < n / 2; i++) {t = a[i];a[i] = a[n - i - 1];a[n - i - 1] = t;}}
public static void sort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);}
public static void revSort(int[] a) {Random rnd = new Random();for (int i = 0; i < a.length; i++) {int pos = i + rnd.nextInt(a.length - i);int temp = a[i];a[i] = a[pos];a[pos] = temp;}Arrays.sort(a);reverse(a);}
public static long LCM(long a, long b) {if (a > b) {long t = a;a = b;b = t;}a /= gcd(a, b);return (a * b);}
public static int findMax(int[] a, int left, int right) {int res = left;int max = a[left];for (int i = left + 1; i <= right; i++) {if (a[i] > max) {max = a[i];res = i;}}return res;}
public static long findClosest(long arr[], long target) {int n = arr.length;if (target <= arr[0])return arr[0];if (target >= arr[n - 1])return arr[n - 1];int i = 0, j = n, mid = 0;while (i < j) {mid = (i + j) / 2;if (arr[mid] == target)return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];}
public static long getClosest(long val1, long val2, long target) {if (target - val1 >= val2 - target)return val2;else return val1;}
public static int findClosest(int arr[], int target) { int n = arr.length; if (target <= arr[0]) return arr[0]; if (target >= arr[n - 1]) return arr[n - 1]; int i = 0, j = n, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid];if (target < arr[mid]) {if (mid > 0 && target > arr[mid - 1])return getClosest(arr[mid - 1], arr[mid], target);j = mid;} else {if (mid < n - 1 && target < arr[mid + 1])return getClosest(arr[mid], arr[mid + 1], target);i = mid + 1;}}return arr[mid];}
public static int getClosest(int val1, int val2, int target) {if (target - val1 >= val2 - target)return val2;else return val1;}
public static String reverse(String str) {String nstr = "";char ch;for (int i = 0; i < str.length(); i++) {ch = str.charAt(i);nstr = ch + nstr;}return nstr;}
public static boolean isPrime(int n){if (n <= 1)return false;if (n <= 3)return true;if (n % 2 == 0 || n % 3 == 0)return false;for (int i = 5; i * i <= n; i = i + 6)if (n % i == 0 || n % (i + 2) == 0)return false;return true;}
public static int xorSum(int arr[], int n){int bits = 0;for (int i = 0; i < n; ++i)bits |= arr[i];int ans = bits * (int)Math.pow(2, n-1);return ans;}
public static ArrayList<Integer> primeFactors(int n) { ArrayList<Integer> res = new ArrayList<>();while (n%2 == 0) { res.add(2); n = n/2; } for (int i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;}
public static ArrayList<Long> primeFactors(long n) { ArrayList<Long> res = new ArrayList<>();while (n%2 == 0) { res.add(2L); n = n/2; } for (long i = 3; i <= Math.sqrt(n); i = i+2) { while (n%i == 0) { res.add(i); n = n/i; } } if (n > 2) res.add(n);return res;}
static int lower_bound(int array[], int low, int high, int key){
int mid;
while (low < high) {
mid = low + (high - low) / 2;
if (key <= array[mid]) high = mid;
else low = mid + 1;
}
if (low < array.length && array[low] < key) low++;
return low;
}
/********************************* Start Here ***********************************/
// int mod = 1000000007;
static HashMap<Character, TreeSet<Integer>> map;
public static void main(String[] args) throws java.lang.Exception {
if (System.getProperty("ONLINE_JUDGE") == null) {
PrintStream ps = new PrintStream(new File("output.txt"));
System.setOut(ps);
}
FastScanner sc = new FastScanner("input.txt");
StringBuilder result = new StringBuilder();
// sieveOfEratosthenes(1000000);
int T = sc.nextInt();
// int T = 1;
for(int test = 1; test <= T; test++){
int n = sc.nextInt();
int a = sc.nextInt();
int b = sc.nextInt();
long[] arr = new long[n+1];
n++;
long sum=0;
for(int i=1;i<n;++i){
arr[i] = sc.nextLong();
sum+=arr[i];
}
long res = Long.MAX_VALUE;
for(int i=0;i<n;++i){
long cur = (a+b)*arr[i];
sum-=arr[i];
cur+=(sum-(n-i-1)*arr[i])*b;
res=Math.min(res,cur);
}
result.append(res+"\n");
}
System.out.println(result);
System.out.close();
}
public static int solve(int n){
// int n = sc.nextInt();
// int r = sc.nextInt();
// int b = sc.nextInt();
// StringBuilder sb = new StringBuilder();
// if(b == 1){
// for(int i = 0; i < n; i++){
// if(i == n/2) sb.append('B');
// else sb.append('R');
// }
// result.append(sb+"\n");
// continue;
// }
// ArrayList<StringBuilder> res = new ArrayList<>();
// for(int i = 0; i <= b; i++){
// res.add(new StringBuilder(""));
// }
// int i = 0;
// while(r > 0){
// res.get(i).append('R');
// i++;
// if(i == b+1) i = 0;
// r--;
// }
// for(i = 0; i < res.size()-1; i++)
// result.append(res.get(i)+"B");
// result.append(res.get(res.size()-1));
return 1;
}
// static void 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] == true){
// for (int i = p * p; i <= n; i += p)
// prime[i] = false;
// }
// }
// for (int i = 2; i <= n; i++){
// if (prime[i] == true)
// set.add(i);
// }
// }
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
public String nextLine() {
if (st == null || !st.hasMoreTokens()) {
try {
return br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken("\n");
}
public String[] readStringArray(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) {
a[i] = next();
}
return a;
}
int nextInt() {
return Integer.parseInt(nextToken());
}
String next() {
return nextToken();
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
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;
}
int[][] read2dArray(int n, int m) {
int[][] a = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = nextInt();
}
}
return a;
}
long[][] read2dlongArray(int n, int m) {
long[][] a = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = nextLong();
}
}
return a;
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | bd50383b6d09a123af97d67803c59d02 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
C();
}
public static void C(){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while (t-->0) {
int n = in.nextInt(), a = in.nextInt(), b = in.nextInt();
int[] kingdoms = new int[n];
long[] changes = new long[n];
for (int i=0;i<n;i++) kingdoms[i] = in.nextInt();
long cost = (long) n * b * kingdoms[0];
changes[0] = (long) kingdoms[0] *a - (long) (n - 1) * b * kingdoms[0];
for (int i=1;i<n;i++) {
int dis = kingdoms[i] - kingdoms[i - 1];
changes[i] = (long) a * dis - (long) b *(n-1-i)* dis;
cost += (long) b * (n-i) * dis;
}
long min = 0, cur = 0;
for (long change: changes) {
cur += change;
min = Long.min(cur, min);
}
System.out.println(cost + min);
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | df88bd95e7480a266196163e37d2c2d0 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
long t=sc.nextLong();
while(t-->0){
int n=sc.nextInt();
long x=sc.nextLong();
long y=sc.nextLong();
long dp[]=new long[n+1];
long h=0;
dp[0]=0;
for(int i=1;i<=n;i++){
dp[i]=sc.nextLong();
h+=dp[i];
}
long ans=Long.MAX_VALUE;
for(int i=0;i<=n;i++){
long k=dp[i];
h-=dp[i];
long b=((h-(n-i)*dp[i])*y);
b+=(x+y)*k;
ans=Math.min(ans,b);
}
System.out.println(ans);
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | d1fa844a4e3c09805f96bb7be24edd75 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class C {
public static void printArr(int arr[], int n) {
StringBuilder s = new StringBuilder();
for (long no :
arr) {
s.append(no).append(" ");
}
System.out.println(s.toString());
}
public static void main(String[] args) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int T=fs.nextInt();
for (int tt=0; tt<T; tt++) {
int n=fs.nextInt();
int a=fs.nextInt();
int b = fs.nextInt();
int[] arr = fs.readArray(n);
Arrays.sort(arr);
long ans =0;
int first = arr[0];
ans += (long) arr[0] * b;
int cc = 0, lc=arr[0];
for (int i = 1; i < n; i++) {
long moveCost = (long) Math.abs(lc - cc) * a;
long conquerCost = (long) Math.abs(lc - cc) * b * (n - i);
if (moveCost<conquerCost) {
ans += moveCost;
cc = lc;
}
ans += (long) Math.abs(arr[i] - cc) * b;
lc = arr[i];
}
System.out.println(ans);
}
}
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 | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | dcca1c2c825a05deb163daef3ec21ac5 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
new Main().run();
}
void run() {
Scanner sc=new Scanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
int T=sc.nextInt();
while (T-->0) {
int N=sc.nextInt();
long A=sc.nextLong();//move
long B=sc.nextLong();//conquer
long[] x=new long[N+1];
for (int i=0;i<N;++i) x[i+1]=sc.nextLong();
int pre=0;
long ans=0;
for (int i=1;i<=N;++i) {
ans+=(x[i]-x[pre])*B;
if ((x[i]-x[pre])*B*(N-i) > (x[i]-x[pre])*A) {
ans+=(x[i]-x[pre])*A;
pre=i;
}
}
pw.println(ans);
}
pw.close();
}
void tr(Object...o) {System.out.println(Arrays.deepToString(o));}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 99b40bf56a2f848dc0b74547e883de61 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.Scanner;
public class C {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int testCases = sc.nextInt();
for (int i = 1; i <= testCases; ++i) {
solve(i);
}
}
private static void solve(int t) {
int n = sc.nextInt();
long a = sc.nextInt();
long b = sc.nextInt();
int [] arr = new int [n];
for (int i = 0; i < n; ++i)
arr[i] = sc.nextInt();
long [] distances = new long [arr.length];
long sum = 0;
int prev = 0;
for (int i = 0; i < arr.length; ++i) {
sum += arr[i];
}
long max = sum * b;
long current = max;
long savings;
long left = n;
for (int i = 0; i < arr.length - 1; ++i) {
--left;
savings = left * b* (arr[i] - prev);
current -= savings;
current += a * (arr[i] - prev);
prev = arr[i];
max = Math.min(max, current);
}
System.out.println(max);
}
public static void print(int test, long result) {
System.out.println("Case #" + test + ": " + result);
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 1c052c763f8b2c13bfda754871095936 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
/**
* A simple template for competitive programming problems.
*/
public class Solution {
//InputReader in = new InputReader("input.txt");
final InputReader in = new InputReader(System.in);
final PrintWriter out = new PrintWriter(System.out);
static final int mod = 1000000007;
void solve() {
int t = in.nextInt();
while(t-->0) {
int n = in.nextInt();
long a = in.nextInt(); // change
long b = in.nextInt(); // conquer
int[] arr = in.nextArray(n);
int cap = 0;
long[] ps = new long[n]; ps[0] = arr[0];
for(int i=1; i<n; i++) {
ps[i] = arr[i] + ps[i-1];
}
long ans = ps[n-1]*b;
for(int i=0; i<n; i++) {
long cand = (b+a)*arr[i];
long sumOfRem = ps[n-1]-ps[i];
cand += b*(sumOfRem-(long)(n-1-i)*arr[i]);
ans = Math.min(cand, ans);
}
out.println(ans);
}
}
public static void main(final String[] args) throws FileNotFoundException {
final Solution s = new Solution();
final Long t1 = System.currentTimeMillis();
s.solve();
System.err.println(System.currentTimeMillis() - t1 + " ms");
s.out.close();
}
public Solution() throws FileNotFoundException {
}
private static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
Random r = new Random();
InputReader(final InputStream stream) {
this.stream = stream;
}
InputReader(final String fileName) {
InputStream stream = null;
try {
stream = new FileInputStream(fileName);
} catch (final FileNotFoundException e) {
e.printStackTrace();
}
this.stream = stream;
}
int[] nextArray(final int n) {
final int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = nextInt();
return arr;
}
int[][] nextMatrix(final int n, final int m) {
final int[][] matrix = new int[n][m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
matrix[i][j] = nextInt();
return matrix;
}
String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
final StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
final StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
int[] randomArray(int n, int low, int up) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = low + r.nextInt(up - low + 1);
}
return arr;
}
double nextDouble() {
return Double.parseDouble(nextString());
}
private int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (final IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
private boolean isSpaceChar(final int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(final int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | a627b7672d41ab4e215b975f566b9336 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import com.sun.security.jgss.GSSUtil;
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Reader.nextInt();
// int t = 1;
while(t-->0) {
int n = Reader.nextInt();
long a = Reader.nextLong();
long b = Reader.nextLong();
long[] arr = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Reader.nextLong();
}
long ans = 0;
ans += b*(arr[0]);
long c = 0;
for(int i = 0;i<n-1;i++){
if(a*(arr[i]-c) < b*(n-i-1)*(arr[i]-c)){
ans += a*(arr[i]-c);
c = arr[i];
}
ans += b*(arr[i+1]-c);
}
output.write(ans+"\n");
}
output.close();
}
public static long[] factorial(int n){
long[] factorials = new long[n+1];
factorials[1] = 1;
for(int i = 2;i<=n;i++){
factorials[i] = (factorials[i-1]*i)%((int)1e9+7);
}
return factorials;
}
public static long numOfBits(long n){
long ans = 0;
while(n>0){
n = n & (n-1);
ans++;
}
return ans;
}
public static long ceilOfFraction(long x, long y){
// ceil using integer division: ceil(x/y) = (x+y-1)/y
// using double may go out of range.
return (x+y-1)/y;
}
public static int power(int x, int y, int p) {
int res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
public static int modInverse(int n, int p) {
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
public static int nCrModPFermat(int n, int r, int p) {
if (n<r) return 0;
if (r == 0) return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
int[] fac = new int[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p;
}
public static long ncr(long n, long r) {
long p = 1, k = 1;
if (n - r < r) {
r = n - r;
}
if (r != 0) {
while (r > 0) {
p *= n;
k *= r;
long m = gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
} else {
p = 1;
}
return p;
}
public 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; (long) i * i <= n; i = i + 6){
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
public static int powOf2JustSmallerThanN(int n) {
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return n ^ (n >> 1);
}
public static int mergeSortAndCount(int[] arr, int l, int r) {
int count = 0;
if (l < r) {
int m = (l + r) / 2;
count += mergeSortAndCount(arr, l, m);
count += mergeSortAndCount(arr, m + 1, r);
count += mergeAndCount(arr, l, m, r);
}
return count;
}
public static int mergeAndCount(int[] arr, int l, int m, int r) {
int[] left = Arrays.copyOfRange(arr, l, m + 1);
int[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l, swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
public static void reverseArray(int[] arr,int start, int end) {
int temp;
while (start < end) {
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
public static long gcd(long a, long b){
if(a==0){
return b;
}
return gcd(b%a,a);
}
public static long lcm(long a, long b){
if(a>b) return a/gcd(b,a) * b;
return b/gcd(a,b) * a;
}
public static long largeExponentMod(long x,long y,long mod){
// computing (x^y) % mod
x%=mod;
long ans = 1;
while(y>0){
if((y&1)==1){
ans = (ans*x)%mod;
}
x = (x*x)%mod;
y = y >> 1;
}
return ans;
}
public static boolean[] numOfPrimesInRange(long L, long R){
boolean[] isPrime = new boolean[(int) (R-L+1)];
Arrays.fill(isPrime,true);
long lim = (long) Math.sqrt(R);
for (long i = 2; i <= lim; i++){
for (long j = Math.max(i * i, (L + i - 1) / i * i); j <= R; j += i){
isPrime[(int) (j - L)] = false;
}
}
if (L == 1) isPrime[0] = false;
return isPrime;
}
public static ArrayList<Long> primeFactors(long n){
ArrayList<Long> factorization = new ArrayList<>();
if(n%2==0){
factorization.add(2L);
}
while(n%2==0){
n/=2;
}
if(n%3==0){
factorization.add(3L);
}
while(n%3==0){
n/=3;
}
if(n%5==0){
factorization.add(5L);
}
while(n%5==0){
// factorization.add(5L);
n/=5;
}
int[] increments = {4, 2, 4, 2, 4, 6, 2, 6};
int i = 0;
for (long d = 7; d * d <= n; d += increments[i++]) {
if(n%d==0){
factorization.add(d);
}
while (n % d == 0) {
// factorization.add(d);
n /= d;
}
if (i == 8)
i = 0;
}
if (n > 1)
factorization.add(n);
return factorization;
}
}
class DSU {
int[] size, parent;
int n;
public DSU(int n){
this.n = n;
size = new int[n];
parent = new int[n];
for(int i = 0;i<n;i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int u){
if(parent[u]==u){
return u;
}
return parent[u] = find(parent[u]);
}
public void merge(int u, int v){
u = find(u);
v = find(v);
if(u!=v){
if(size[u]>size[v]){
parent[v] = u;
size[u] += size[v];
}
else{
parent[u] = v;
size[v] += size[u];
}
}
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static String nextLine() throws IOException {
return reader.readLine();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException{
return Long.parseLong(next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 80562b571f933da458d3e9c367b7af03 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | // package c1659;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.lang.invoke.MethodHandles;
import java.util.Random;
import java.util.StringTokenizer;
//
// Codeforces Round #782 (Div. 2) 2022-04-17 07:35
// C. Line Empire
// https://codeforces.com/contest/1659/problem/C
// time limit per test 1 second; memory limit per test 256 megabytes
// public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*'
//
// Consider a number axis. The capital of your empire is initially at 0. There are n unconquered
// kingdoms at positions 0<x_1<x_2<...<x_n. You want to conquer all other kingdoms.
//
// There are two actions available to you:
// * You can change the location of your capital (let its current position be c_1) to any other
// kingdom (let its position be c_2) at a cost of a* |c_1-c_2|.
// * From the current capital (let its current position be c_1) you can conquer an unconquered
// kingdom (let its position be c_2) at a cost of b* |c_1-c_2|. You conquer a kingdom if there is
// an unconquered kingdom between the target and your capital.
//
// Note that you place the capital at a point without a kingdom. In other words, at any point, your
// capital can only be at 0 or one of x_1,x_2,...,x_n. Also note that conquering a kingdom does not
// change the position of your capital.
//
// Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end.
//
// Input
//
// The first line contains a single integer t (1 <= t <= 1000) -- the number of test cases. The
// description of each test case follows.
//
// The first line of each test case contains 3 integers n, a, and b (1 <= n <= 2 * 10^5; 1 <= a,b <=
// 10^5).
//
// The second line of each test case contains n integers x_1, x_2, ..., x_n (1 <= x_1 < x_2 < ... <
// x_n <= 10^8).
//
// The sum of n over all test cases does not exceed 2 * 10^5.
//
// Output
//
// For each test case, output a single integer -- the minimum cost to conquer all kingdoms.
//
// Example
/*
input:
4
5 2 7
3 5 12 13 21
5 6 3
1 5 6 21 30
2 9 3
10 15
11 27182 31415
16 18 33 98 874 989 4848 20458 34365 38117 72030
output:
173
171
75
3298918744
*/
// Note
//
// Here is an optimal sequence of moves for the second test case:
// 1. Conquer the kingdom at position 1 with cost 3*(1-0)=3.
// 2. Move the capital to the kingdom at position 1 with cost 6*(1-0)=6.
// 3. Conquer the kingdom at position 5 with cost 3*(5-1)=12.
// 4. Move the capital to the kingdom at position 5 with cost 6*(5-1)=24.
// 5. Conquer the kingdom at position 6 with cost 3*(6-5)=3.
// 6. Conquer the kingdom at position 21 with cost 3*(21-5)=48.
// 7. Conquer the kingdom at position 30 with cost 3*(30-5)=75.
//
// The total cost is 3+6+12+24+3+48+75=171. You cannot get a lower cost than this.
//
public class C1659C {
static final int MOD = 998244353;
static final Random RAND = new Random();
static long solve(int[] x, int a, int b) {
int n = x.length;
long ans = 0;
int xc = 0;
for (int i = 0; i < n; i++) {
long cc = (long)(x[i] - xc) * b;
ans += cc;
// relocate capital from xc to x[i]?
// There are (n-1-i) kingdom after, relocate will save
long rs = (long)(n-1-i) * b * (x[i] - xc);
long rc = (long)(x[i] - xc) * a;
boolean relocate = rs > rc;
if (relocate) {
ans += rc;
xc = x[i];
}
// System.out.format(" i:%d xi:%4d cc:%4d rs:%4d rc:%4d rel:%5b xc:%4d ans:%d\n",
// i, x[i], cc, rs, rc, relocate, xc, ans);
}
return ans;
}
static boolean test = false;
static void doTest() {
if (!test) {
return;
}
long t0 = System.currentTimeMillis();
System.out.format("%d msec\n", System.currentTimeMillis() - t0);
System.exit(0);
}
public static void main(String[] args) {
doTest();
MyScanner in = new MyScanner();
int T = in.nextInt();
for (int t = 1; t <= T; t++) {
int n = in.nextInt();
int a = in.nextInt();
int b = in.nextInt();
int[] x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = in.nextInt();
}
long ans = solve(x, a, b);
System.out.println(ans);
}
}
static void output(int[] a) {
if (a == null) {
System.out.println("-1");
return;
}
StringBuilder sb = new StringBuilder();
for (int v : a) {
sb.append(v);
sb.append(' ');
if (sb.length() > 500) {
System.out.print(sb.toString());
sb.setLength(0);
}
}
System.out.println(sb.toString());
}
static void myAssert(boolean cond) {
if (!cond) {
throw new RuntimeException("Unexpected");
}
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
try {
final String USERDIR = System.getProperty("user.dir");
String cname = MethodHandles.lookup().lookupClass().getCanonicalName().replace(".MyScanner", "");
cname = cname.lastIndexOf('.') > 0 ? cname.substring(cname.lastIndexOf('.') + 1) : cname;
final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in");
br = new BufferedReader(new InputStreamReader(fin.exists()
? new FileInputStream(fin) : System.in));
} catch (Exception e) {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
public String next() {
try {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | a4ff2e60666e57e4877f0c03157b29f9 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class C {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(int arr[],int l1,int r1,int l2,int r2) {
int tmp[]=new int[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(long arr[],int l1,int r1,int l2,int r2) {
long tmp[]=new long[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
StringBuilder ans=new StringBuilder("");
int test=input.scanInt();
for(int tt=1;tt<=test;tt++) {
int n=input.scanInt();
long a=input.scanInt();
long b=input.scanInt();
long arr[]=new long[n];
for(int i=0;i<n;i++) {
arr[i]=input.scanInt();
}
long prefix[]=new long[n];
prefix[0]=arr[0];
for(int i=1;i<n;i++) {
prefix[i]=arr[i]+prefix[i-1];
}
long min=Long.MAX_VALUE;
long sum=0;
sum+=a*0;
sum+=b*0;
sum+=b*get(0,n-1,prefix);
min=Math.min(min,sum);
for(int i=0;i<n;i++) {
sum=0;
sum+=a*arr[i];
sum+=b*arr[i];
sum+=b*(get(i+1,n-1,prefix)-((n-i-1)*arr[i]));
min=Math.min(min,sum);
// System.out.println(i+" "+sum);
}
ans.append(min+"\n");
}
System.out.println(ans);
}
public static long get(int l,int r,long arr[]) {
// System.out.println(l+" "+r);
return arr[r]-(l==0?0:arr[l-1]);
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | caae45bf9a5461f389b5cedef650e6f8 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.Flushable;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Cepera
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Input in = new Input(inputStream);
Output out = new Output(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
private void solve(Input in, Output out) throws Exception {
int testsTotal = in.readInt();
for (int currentTest = 0; currentTest < testsTotal; currentTest++) {
int n = in.readInt();
long a = in.readLong();
long b = in.readLong();
long[] x = new long[n + 1];
x[0] = 0;
for (int i = 1; i <= n; i++) {
x[i] = in.readInt();
}
long[] d = new long[n + 1];
d[n] = 0;
for (int i = n - 1; i >= 0; i--) {
d[i] = b * (x[i + 1] - x[i]);
if ((n - i - 1) * b > a) {
d[i] += a * (x[i + 1] - x[i]) + d[i + 1];
} else {
d[i] += (n - i - 1) * b * (x[i + 1] - x[i]) + d[i + 1];
}
}
out.println(d[0]);
}
}
public void solve(int testNumber, Input in, Output out) {
try {
solve(in, out);
out.flush();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
static class Input {
public final BufferedReader reader;
private String line = "";
private int pos = 0;
public Input(InputStream inputStream) {
reader = new BufferedReader(new InputStreamReader(inputStream));
}
private boolean isSpace(char ch) {
return ch <= 32;
}
public String readWord() throws IOException {
skip();
int start = pos;
while (pos < line.length() && !isSpace(line.charAt(pos))) {
pos++;
}
return line.substring(start, pos);
}
public int readInt() throws IOException {
return Integer.parseInt(readWord());
}
public long readLong() throws IOException {
return Long.parseLong(readWord());
}
private void skip() throws IOException {
while (true) {
if (pos >= line.length()) {
line = reader.readLine();
pos = 0;
}
while (pos < line.length() && isSpace(line.charAt(pos))) {
pos++;
}
if (pos < line.length()) {
return;
}
}
}
}
static class Output implements Closeable, Flushable {
private final BufferedWriter writer;
public Output(OutputStream stream) {
this.writer = new BufferedWriter(new OutputStreamWriter(stream), 1 << 18);
}
public Output(Writer writer) {
this.writer = (writer instanceof BufferedWriter) ? (BufferedWriter) writer : new BufferedWriter(writer, 1 << 18);
}
public void println(Object x) throws IOException {
writer.write(String.valueOf(x));
writer.write('\n');
}
public void flush() throws IOException {
writer.flush();
}
public void close() {
try {
flush();
writer.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 11 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | e5ce6072f6ec690b2c6b6cd5187828c9 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Scanner;
/**
*
* @author xpeng
*/
public class codeforces {
static class obj {
String s;
long l,r;
obj(String s,long l,long r){
this.s=s;this.l=l;this.r=r;
}
}
static class Q implements Comparable<Q>{
long at;
char ans;
public Q(long at) {
this.at=at;
}
public int compareTo(Q o) {
return Long.compare(at, o.at);
}
}
static Scanner in = new Scanner(System.in);
static BufferedWriter write = new BufferedWriter(new OutputStreamWriter(System.out));
static PrintWriter out = new PrintWriter(write);
public static boolean possible(int test,int[] cnt){
long sum=0;
for (int i = 0; i < cnt.length; i++) {
if (cnt[i]<test) {
int num=test-cnt[i];
sum+=(int)(num/2.0);
}
else if (cnt[i]>test) {
sum-=(cnt[i]-test);
if (sum<0) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
int t = in.nextInt();
for (int k = 0; k < t; k++) {
int n = in.nextInt();
long a=in.nextInt(),b=in.nextInt();
int[] arr = new int[n];
int last=0;
for (int i = 0; i < n; i++) {
arr[i]=in.nextInt();
}
long ans=0;
//int nright=0;
for (int i = 0; i < n; i++) {
last=i==0?0:arr[i-1];
ans+=((arr[i]-last)*Math.min(a+b, (n-i)*b));
// if (a+b<=(n-i)*b) {
// ans+=(a*Math.abs(arr[i]-last)+b*Math.abs(arr[i]-last));
// }else{
// ans+=((n-i)*(b*Math.abs(arr[i]-last)));
// }
}
System.out.println(ans);
}
}
public static int search(ArrayList<Integer> array, int num) {
int low = 0;
int high = array.size() - 1;
while (low < high) { // low<high when we search for the index that needs to be filled in
int mid = (low + high) / 2;
if (array.get(mid) < num && array.get(mid + 1) >= num) {
return mid + 1;
} else if (array.get(mid) < num) {
low = mid + 1;
} else if (array.get(mid) >= num) { // these inequality signs are specifically catering for the first appearance of the number
//2,3,3,3
// if we search for 3, we would get 1 instead of like 4
high = mid;
}
}
return -1;
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 2acb0e737b69217986b4d1981e73d674 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import static java.lang.System.out;
import java.util.*;
import java.io.*;
import java.math.*;
/*
getOrDefault
valueOf
System.out.println();
*/
public class HelloWorld{
public static void main(String []args) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int T=Integer.parseInt(st.nextToken());
for(int z=0;z<T;z++){
st = new StringTokenizer(infile.readLine());
long n=Long.parseLong(st.nextToken());
long a=Long.parseLong(st.nextToken());
long b=Long.parseLong(st.nextToken());
st = new StringTokenizer(infile.readLine());
long[] arr=new long[(int)n+1];
build(arr,n,st,1);
long ans=b*(arr[1]);
long cap=0;
for(int i=2;i<=n;i++){
if((n-i+1)*b>=a){
ans+= a*(arr[i-1]-cap);
cap=arr[i-1];
}
ans+= b*(arr[i]-cap);
}
System.out.println(ans);
}
}
public static void build(long[] arr, long n, StringTokenizer st,int lo){
for(int i=lo;i<arr.length;i++) arr[i]=Long.parseLong(st.nextToken());
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 72f057d86a0b451277210c7c92e66559 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class C_Line_Empire {
static Scanner in = new Scanner();
static PrintWriter out = new PrintWriter(System.out);
static StringBuilder ans = new StringBuilder();
static int n, testCases;
static long a, b;
static long arr[];
static long max = Long.MAX_VALUE, mod = (long) (1e9);
static long solve(int index, long max, long summation) {
if (index >= n) {
return max;
}
long present = /*Long.parseLong(mul(String.valueOf((a + b)), String.valueOf(arr[index])))*/ (a + b) * arr[index];
summation -= arr[index];
long X = /*Long.parseLong(sub(String.valueOf(summation), (mul(String.valueOf((n - index - 1)), String.valueOf(arr[index])))))*/ summation - (n - index - 1) * arr[index];
present += /*Long.parseLong(sum(mul(String.valueOf(X), String.valueOf(b)), String.valueOf(present)))*/ X * b;
//present %= mod;
max = Math.min(max, present);
return solve(index + 1, max, summation);
}
static void solve(int t) {
long sum = detect_sum(0, arr, 0);
max = Long.MAX_VALUE;
ans.append(solve(0, max, sum));
if (t != testCases) {
ans.append("\n");
}
}
public static void main(String[] priya) throws IOException {
testCases = in.nextInt();
for (int t = 0; t < testCases; ++t) {
n = in.nextInt() + 1;
a = in.nextLong();
b = in.nextLong();
arr = new long[n];
for (int i = 1; i < n; ++i) {
arr[i] = in.nextLong();
}
solve(t + 1);
}
out.print(ans.toString());
out.flush();
in.close();
}
static boolean isSmaller(String str1, String str2) {
// Calculate lengths of both string
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2) {
return true;
}
if (n2 < n1) {
return false;
}
for (int i = 0; i < n1; i++) {
if (str1.charAt(i) < str2.charAt(i)) {
return true;
} else if (str1.charAt(i) > str2.charAt(i)) {
return false;
}
}
return false;
}
static String sub(String str1, String str2) {
if (isSmaller(str1, str2)) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n1 - n2;
int carry = 0;
for (int i = n2 - 1; i >= 0; i--) {
int sub
= (((int) str1.charAt(i + diff) - (int) '0')
- ((int) str2.charAt(i) - (int) '0')
- carry);
if (sub < 0) {
sub = sub + 10;
carry = 1;
} else {
carry = 0;
}
str += String.valueOf(sub);
}
for (int i = n1 - n2 - 1; i >= 0; i--) {
if (str1.charAt(i) == '0' && carry > 0) {
str += "9";
continue;
}
int sub = (((int) str1.charAt(i) - (int) '0')
- carry);
if (i > 0 || sub > 0) {
str += String.valueOf(sub);
}
carry = 0;
}
return new StringBuilder(str).reverse().toString();
}
static String sum(String str1, String str2) {
if (str1.length() > str2.length()) {
String t = str1;
str1 = str2;
str2 = t;
}
String str = "";
int n1 = str1.length(), n2 = str2.length();
int diff = n2 - n1;
int carry = 0;
for (int i = n1 - 1; i >= 0; i--) {
int sum = ((int) (str1.charAt(i) - '0')
+ (int) (str2.charAt(i + diff) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
for (int i = n2 - n1 - 1; i >= 0; i--) {
int sum = ((int) (str2.charAt(i) - '0') + carry);
str += (char) (sum % 10 + '0');
carry = sum / 10;
}
if (carry > 0) {
str += (char) (carry + '0');
}
return new StringBuilder(str).reverse().toString();
}
static long detect_sum(int i, long a[], long sum) {
if (i >= a.length) {
return sum;
}
return detect_sum(i + 1, a, sum + a[i]);
}
static String mul(String num1, String num2) {
int len1 = num1.length();
int len2 = num2.length();
if (len1 == 0 || len2 == 0) {
return "0";
}
int result[] = new int[len1 + len2];
int i_n1 = 0;
int i_n2 = 0;
for (int i = len1 - 1; i >= 0; i--) {
int carry = 0;
int n1 = num1.charAt(i) - '0';
i_n2 = 0;
for (int j = len2 - 1; j >= 0; j--) {
int n2 = num2.charAt(j) - '0';
int sum = n1 * n2 + result[i_n1 + i_n2] + carry;
carry = sum / 10;
result[i_n1 + i_n2] = sum % 10;
i_n2++;
}
if (carry > 0) {
result[i_n1 + i_n2] += carry;
}
i_n1++;
}
int i = result.length - 1;
while (i >= 0 && result[i] == 0) {
i--;
}
if (i == -1) {
return "0";
}
String s = "";
while (i >= 0) {
s += (result[i--]);
}
return s;
}
static class Node<T> {
T data;
Node<T> next;
public Node() {
this.next = null;
}
public Node(T data) {
this.data = data;
this.next = null;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Node<T> getNext() {
return next;
}
public void setNext(Node<T> next) {
this.next = next;
}
@Override
public String toString() {
return this.getData().toString() + " ";
}
}
static class ArrayList<T> {
Node<T> head, tail;
int len;
public ArrayList() {
this.head = null;
this.tail = null;
this.len = 0;
}
int size() {
return len;
}
boolean isEmpty() {
return len == 0;
}
int indexOf(T data) {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
int index = -1, i = 0;
while (temp != null) {
if (temp.getData() == data) {
index = i;
}
i++;
temp = temp.getNext();
}
return index;
}
void add(T data) {
Node<T> newNode = new Node<>(data);
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
tail.setNext(newNode);
tail = newNode;
len++;
}
}
void see() {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
while (temp != null) {
out.print(temp.getData().toString() + " ");
out.flush();
temp = temp.getNext();
}
out.println();
out.flush();
}
void inserFirst(T data) {
Node<T> newNode = new Node<>(data);
Node<T> temp = head;
if (isEmpty()) {
head = newNode;
tail = newNode;
len++;
} else {
newNode.setNext(temp);
head = newNode;
len++;
}
}
T get(int index) {
if (isEmpty() || index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> temp = head;
int i = 0;
T data = null;
while (temp != null) {
if (i == index) {
data = temp.getData();
}
i++;
temp = temp.getNext();
}
return data;
}
void addAt(T data, int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
Node<T> newNode = new Node<>(data);
int i = 0;
Node<T> temp = head;
while (temp.next != null) {
if (i == index) {
newNode.setNext(temp.next);
temp.next = newNode;
}
i++;
temp = temp.getNext();
}
// temp.setNext(temp);
len++;
}
void popFront() {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
}
if (head == tail) {
head = null;
tail = null;
} else {
head = head.getNext();
}
len--;
}
void removeAt(int index) {
if (index >= len) {
throw new ArrayIndexOutOfBoundsException();
}
if (index == 0) {
this.popFront();
return;
}
Node<T> temp = head;
int i = 0;
Node<T> n = new Node<>();
while (temp != null) {
if (i == index) {
n.next = temp.next;
temp.next = n;
break;
}
i++;
n = temp;
temp = temp.getNext();
}
tail = n;
--len;
}
void clearAll() {
this.head = null;
this.tail = null;
}
}
static void merge(long a[], int left, int right, int mid) {
int n1 = mid - left + 1, n2 = right - mid;
long L[] = new long[n1];
long R[] = new long[n2];
for (int i = 0; i < n1; i++) {
L[i] = a[left + i];
}
for (int i = 0; i < n2; i++) {
R[i] = a[mid + 1 + i];
}
int i = 0, j = 0, k1 = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
a[k1] = L[i];
i++;
} else {
a[k1] = R[j];
j++;
}
k1++;
}
while (i < n1) {
a[k1] = L[i];
i++;
k1++;
}
while (j < n2) {
a[k1] = R[j];
j++;
k1++;
}
}
static void sort(long a[], int left, int right) {
if (left >= right) {
return;
}
int mid = (left + right) / 2;
sort(a, left, mid);
sort(a, mid + 1, right);
merge(a, left, right, mid);
}
static class Scanner {
BufferedReader in;
StringTokenizer st;
public Scanner() {
in = new BufferedReader(new InputStreamReader(System.in));
}
String next() throws IOException {
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
String nextLine() throws IOException {
return in.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
void close() throws IOException {
in.close();
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | bde11ce82c19ec65f974560e3b1511e8 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | // package faltu;
import java.util.*;
import java.util.Map.Entry;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
// ***********************MATHS--STARTS************************************************* //
private static ArrayList<Long> get_divisor(long x) {
ArrayList<Long>a=new ArrayList<Long>();
for(long i=1;i*i<=x;i++) {
if(x%i==0) {
a.add((long) i);
if(x/i!=i)a.add(x/i);
}
}
return a;
}
static long[] sieve;
static long[] smallestPrime;
public static void sieve()
{
int n=4000000+1;
sieve=new long[n];
smallestPrime=new long[n];
sieve[0]=1;
sieve[1]=1;
for(int i=2;i<n;i++){
sieve[i]=i;
smallestPrime[i]=i;
}
for(int i=2;i*i<n;i++){
if(sieve[i]==i){
for(int j=i*i;j<n;j+=i){
if(sieve[j]==j)sieve[j]=1;
if(smallestPrime[j]==j||smallestPrime[j]>i)smallestPrime[j]=i;
}
}
}
}
static long nCr(long n,long r,long MOD) {
if(n<r)return 0;
if(r==0)return 1;
return fact[(int) n]*mod_inv(fact[(int) r],MOD)%MOD*mod_inv(fact[(int) (n-r)],MOD)%MOD;
}
static long[]fact;
static void computeFact(long n,long MOD) {
fact=new long[(int)n+1];
fact[0]=1;
for(int i=1;i<=n;i++)fact[i]=(fact[i-1]*i%MOD)%MOD;
}
static long bin_expo(long a,long b,long MOD) {
if(b == 0)return 1;
long ans = bin_expo(a,b/2,MOD);
ans = (ans*ans)%MOD;
if(b % 2!=0){
ans = (ans*a)%MOD;
}
return ans%MOD;
}
static long mod_add(long a, long b, long m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
static long mod_mul(long a, long b, long m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
static long mod_sub(long a, long b, long m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
static long mod_inv(long n,long p) {return bin_expo(n,p-2,p);}
static long gcd(long a, long b){if (a == 0) {return b;}return gcd(b % a, a); }
static int gcd(int a, int b){if (a == 0) {return b; }return gcd(b % a, a); }
static long lcm(long a,long b){return (a / gcd(a, b)) * b;}
static long min(long x,long y) {return Math.min(x, y);}static long max(long x,long y) {return Math.max(x, y);}
static int min(int x,int y) {return Math.min(x, y);}static int max(int x,int y) {return Math.max(x, y);}
static ArrayList<String>powof2s;
static void powof2S() {
long i=1;
while(i<(long)2e18) {
powof2s.add(String.valueOf(i));
i*=2;
}
}
static long power(long a, long b){
a %=MOD;long out = 1;
while (b > 0) {
if((b&1)!=0)out = out * a % MOD;
a = a * a % MOD;
b >>= 1;
a*=a;
}
return out;
}
static boolean coprime(int a, long l){return (gcd(a, l) == 1);}
// ****************************MATHS-ENDS*****************************************************
// ***********************BINARY-SEARCH STARTS***********************************************
public static int upperBound(long[] arr, long m, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(arr[mid]<=m) l=mid+1;
else r=mid-1;
}
return l;
}
public static int lowerBound(long[] a, long m, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(a[mid]<m) l=mid+1;
else r=mid-1;
}
return l;
}
public static int lowerBound(ArrayList<Integer> ar,int k){
int s=0,e=ar.size();
while (s!=e){
int mid = s+e>>1;
if (ar.get(mid) <k)s=mid+1;
else e=mid;
}
if(s==ar.size())return -1;
return s;
}
public static int upperBound(ArrayList<Integer> ar,int k){
int s=0,e=ar.size();
while (s!=e){
int mid = s+e>>1;
if (ar.get(mid) <=k)s=mid+1;
else e=mid;
}
if(s==ar.size())return -1;
return s;
}
public static long getClosest(long val1, long val2,long target){if (target - val1 >= val2 - target)return val2; else return val1;}
static void ruffleSort(long[] a) {
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
long oi=r.nextInt(n), temp=a[i];
a[i]=a[(int)oi];
a[(int)oi]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(int[] a){
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n), temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
Arrays.sort(a);
}
int ceilIndex(int input[], int T[], int end, int s){
int start = 0;
int middle;
int len = end;
while(start <= end){
middle = (start + end)/2;
if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){
return middle+1;
}else if(input[T[middle]] < s){
start = middle+1;
}else{
end = middle-1;
}
}
return -1;
}
static int lowerLimitBinarySearch(ArrayList<Long> v,long k) {
int n =v.size();
int first = 0,second = n;
while(first <second) {
int mid = first + (second-first)/2;
if(v.get(mid) > k) {
second = mid;
}else {
first = mid+1;
}
}
if(first < n && v.get(first) < k) {
first++;
}
return first; //1 index
}
public static int searchindex(long arr[], long t){int index = Arrays.binarySearch(arr, t);return (index < 0) ? -1 : index;}
public static long[] sort(long[] a) {ArrayList<Long> al = new ArrayList<>();for(int i=0;i<a.length;i++) al.add(a[i]);Collections.sort(al);for(int i=0;i<a.length;i++) a[i]=al.get(i);return a;}
public static int[] sort(int[] a) {ArrayList<Integer> al = new ArrayList<>();for(int i=0;i<a.length;i++) al.add(a[i]);Collections.sort(al);for(int i=0;i<a.length;i++) a[i]=al.get(i);return a;}
// *******************************BINARY-SEARCH ENDS***********************************************
// *********************************GRAPHS-STARTS****************************************************
// *******----SEGMENT TREE IMPLEMENT---*****
// -------------START---------------
void buildTree (int[] arr,int[] tree,int start,int end,int treeNode){
if(start==end){
tree[treeNode]=arr[start];
return;
}
buildTree(arr,tree,start,end,2*treeNode);
buildTree(arr,tree,start,end,2*treeNode+1);
tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1];
}
void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value){
if(start==end){
arr[idx]=value;
tree[treeNode]=value;
return;
}
int mid=(start+end)/2;
if(idx>mid)updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value);
else updateTree(arr,tree,start,mid,2*treeNode,idx,value);
tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1];
}
long query(int[]arr,int[]tree,int start,int end,int treeNode,int qleft,int qright) {
if(start>=qleft&&end<=qright)return tree[treeNode];
if(start>qright||end<qleft)return 0;
int mid=(start+end)/2;
long valLeft=query(arr,tree,start,mid-1,treeNode*2,qleft,qright);
long valRight=query(arr,tree,mid+1,end,treeNode*2+1,qleft,qright);
return valLeft+valRight;
}
// -------------ENDS---------------
//***********************DSU IMPLEMENT START*************************
static int parent[];
static int rank[];
static int[]Size;
static void makeSet(int n){
parent=new int[n];
rank=new int[n];
Size=new int[n];
for(int i=0;i<n;i++){
parent[i]=i;
rank[i]=0;
Size[i]=1;
}
}
static void union(int u,int v){
u=findpar(u);
v=findpar(v);
if(rank[u]<rank[v]) {
parent[u]=v;
Size[v]+=Size[u];
}
else if(rank[v]<rank[u]) {
parent[v]=u;
Size[u]+=Size[v];
}
else{
parent[v]=u;
rank[u]++;
Size[u]+=Size[v];
}
}
private static int findpar(int node){
if(node==parent[node])return node;
return parent[node]=findpar(parent[node]);
}
// *********************DSU IMPLEMENT ENDS*************************
// ****__________PRIMS ALGO______________________****
private static int prim(ArrayList<node>[] adj,int N,int node) {
int key[] = new int[N+1];
int parent[] = new int[N+1];
boolean mstSet[] = new boolean[N+1];
for(int i = 0;i<N;i++) {
key[i] = 100000000;
mstSet[i] = false;
}
PriorityQueue<node> pq = new PriorityQueue<node>(N, new node());
key[node] = 0;
parent[node] = -1;
pq.add(new node( node,key[node]));
for(int i = 0;i<N-1;i++) {
int u = pq.poll().getV();
mstSet[u] = true;
for(node it: adj[u]) {
if(mstSet[it.getV()] == false && it.getW() < key[it.getV()]) {
parent[it.getV()] = u;
key[it.getV()] = (int) it.getW();
pq.add(new node(it.getV(), key[it.getV()]));
}
}
}
int sum=0;
for(int i=1;i<N;i++) {
System.out.println(key[i]);
sum+=key[i];
}
System.out.println(sum);
return sum;
}
// ****____________DIJKSTRAS ALGO___________****
static int[]dist;
static int dijkstra(int u,int n,ArrayList<node>adj[]) {
dist=new int[n];
Arrays.fill(dist,Integer.MAX_VALUE);
dist[u]=0;
PriorityQueue<node>pq=new PriorityQueue<node>(new node());
pq.add(new node(u,0));
while(!pq.isEmpty()) {
node v=pq.poll();
for(node it:adj[v.getV()]) {
if(dist[it.getV()]>it.getW()+dist[v.getV()]) {
dist[it.getV()]=(int) (it.getW()+dist[v.getV()]);
pq.add(new node(it.getV(),dist[it.getV()]));
}
}
}
int sum=0;
for(int i=1;i<n;i++){
System.out.println(dist[i]);
sum+=dist[i];
}
return sum;
}
private static void setGraph(int n,int m){
vis=new boolean[n+1];
indeg=new int[n+1];
// adj=new ArrayList<ArrayList<Integer>>();
// for(int i=0;i<=n;i++)adj.add(new ArrayList<>());
// for(int i=0;i<m;i++){
// int u=s.nextInt(),v=s.nextInt();
// adj.get(u).add(v);
// adj.get(v).add(u);
// }
adj=new ArrayList[n+1];
// backadj=new ArrayList[n+1];
for(int i=0;i<=n;i++){
adj[i]=new ArrayList<Integer>();
// backadj[i]=new ArrayList<Integer>();
}
for(int i=0;i<m;i++){
int u=s.nextInt(),v=s.nextInt();
adj[u].add(v);
adj[v].add(u);
// backadj[v].add(u);
indeg[v]++;
indeg[u]++;
}
// weighted adj
// adj=new ArrayList[n+1];
// for(int i=0;i<=n;i++){
// adj[i]=new ArrayList<node>();
// }
// for(int i=0;i<m;i++){
// int u=s.nextInt(),v=s.nextInt();
// long w=s.nextInt();
// adj[u].add(new node(v,w));
//// adj[v].add(new node(u,w));
// }
}
// *********************************GRAPHS-ENDS****************************************************
static int[][] dirs8 = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
static int[][] dirs4 = {{1,0},{-1,0},{0,1},{0,-1}}; //d-u-r-l
static long MOD=(long) (1e9+7);
static int prebitsum[][];
static boolean[] vis;
static int[]indeg;
// static ArrayList<ArrayList<Integer>>adj;
static ArrayList<Integer> adj[];
static ArrayList<Integer> backadj[];
static FastReader s = new FastReader(System.in);
public static void main(String[] args) throws IOException
{
// sieve();
// computeFact((int)1e7+1,MOD);
// prebitsum=new int[2147483648][31];
// presumbit(prebitsum);
// powof2S();
//
// try {
int tt = s.nextInt();
// int tt=1;
for(int i=1;i<=tt;i++) {
solver();
}
// catch(Exception e) {return;}
}
private static void solver() {
int n = s.nextInt();
long a=s.nextInt(),b=s.nextInt();
int[] arr = new int[n];
int last=0;
for (int i = 0; i < n; i++) {
arr[i]=s.nextInt();
}
long ans=0;
for (int i = 0; i < n; i++) {
last=i==0?0:arr[i-1];
ans+=((arr[i]-last)*Math.min(a+b, (n-i)*b));
}
System.out.println(ans);
}
/* *********************BITS && TOOLS &&DEBUG STARTS***********************************************/
static boolean issafe(int i, int j, int r,int c, int sx, int sy, int d,boolean[][]vis){
if (i < 0 || j < 0 || i >= r || j >= c||((Math.abs(i-sx)+Math.abs(j-sy))<=d)||vis[i][j]==true)return false;
else return true;
}
static void presumbit(int[][]prebitsum) {
for(int i=1;i<=200000;i++) {
int z=i;
int j=0;
while(z>0) {
if((z&1)==1) {
prebitsum[i][j]+=(prebitsum[i-1][j]+1);
}else {
prebitsum[i][j]=prebitsum[i-1][j];
}
z=z>>1;
j++;
}
}
}
static void countOfSetBit(long[]a) {
for(int j=30;j>=0;j--) {
int cnt=0;
for(long i:a) {
if((i&1<<j)==1)cnt++;
}
// printing the current no set bit in all array element
System.out.println(cnt);
}
}
public static String revStr(String str){String input = str;StringBuilder input1 = new StringBuilder();input1.append(input);input1.reverse();return input1.toString();}
static void printA(int[]a) {for(int i:a)System.out.print(i+" ");System.out.println();}
static void pc2d(boolean[][] vis) {
int n=vis.length;
int m=vis[0].length;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
System.out.print(vis[i][j]+" ");
}
System.out.println();
}
}
static void pi2d(char[][] a) {
int n=a.length;
int m=a[0].length;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
static void p1d(int[]a) {
for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");
System.out.println();
}
// *****************BITS && TOOLS &&DEBUG ENDS***********************************************
}
// **************************I/O*************************
class FastReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastReader(InputStream stream) {reader = new BufferedReader(new InputStreamReader(stream), 32768);tokenizer = null;}
public String next() {while (tokenizer == null || !tokenizer.hasMoreTokens()) {try {tokenizer = new StringTokenizer(reader.readLine());} catch (IOException e) {throw new RuntimeException(e);}}return tokenizer.nextToken();}
public int nextInt(){ return Integer.parseInt(next());}
public long nextLong() {return Long.parseLong(next());}
public double nextDouble() {return Double.parseDouble(next());}
public String nextLine() {String str = "";try {str = reader.readLine();}catch (IOException e) {e.printStackTrace();}return str;}
}
class dsu{
int n;
static int parent[];
static int rank[];
static int[]Size;
public dsu(int n) {this.n=n;this.parent=new int[n];this.rank=new int[n];this.Size=new int[n];
for(int i=0;i<n;i++){parent[i]=i;rank[i]=0;Size[i]=1;}
}
static int findpar(int node) {if(node==parent[node])return node;return parent[node]=findpar(parent[node]);}
static void union(int u,int v){
u=findpar(u);v=findpar(v);
if(u!=v) {
if(rank[u]<rank[v]) {parent[u]=v;Size[v]+=Size[u];}
else if(rank[v]<rank[u]) {parent[v]=u;Size[u]+=Size[v];}
else{parent[v]=u;rank[u]++;Size[u]+=Size[v];}
}
}
}
class pair{
int x;int y;
long u,v;
public pair(int x,int y){this.x=x;this.y=y;}
public pair(long u,long v) {this.u=u;this.v=v;}
}
class node implements Comparator<node>{
private int v;
private long w;
node(int _v, long _w) { v = _v; w = _w; }
node() {}
int getV() { return v; }
long getW() { return w; }
@Override
public int compare(node node1, node node2) {
if (node1.w < node2.w) return -1;
if (node1.w > node2.w) return 1;
return 0;
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 964c31144d836116de27340838542ca3 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class LineEmpire {
static int mod = 1000000007;
public static void main(String[] args) throws IOException {
FastReader reader = new FastReader();
FastWriter writer = new FastWriter();
int numTests = reader.readSingleInt();
for(int t = 0; t<numTests; t++){
int[] nab = reader.readIntArray(3);
long n = nab[0], a = nab[1], b = nab[2];
int[] kingdoms = reader.readIntArray((int)n);
long cost = b*(kingdoms[0]-0);
//Move to one of the conquered kingdoms ahead and conquer the next kingdom that hasnt.
//Conquer the next kingdom that hasn't.
int current = 0;
for(int i = 1; i<n; i++){
if(a < b){
cost += a*(kingdoms[i-1]-current);
current = kingdoms[i-1];
cost += b*(kingdoms[i]-current);
} else {
long dm = b*(kingdoms[i-1]-current)*(n-i);
long m = a*(kingdoms[i-1]-current);
if(dm > m){
cost += a*(kingdoms[i-1]-current);
current = kingdoms[i-1];
cost += b*(kingdoms[i]-current);
} else {
cost += b*(kingdoms[i]-current);
}
}
}
writer.writeSingleLong(cost);
}
}
public static void mergeSort(int[] a, int n) {
if (n < 2) {
return;
}
int mid = n / 2;
int[] l = new int[mid];
int[] r = new int[n - mid];
for (int i = 0; i < mid; i++) {
l[i] = a[i];
}
for (int i = mid; i < n; i++) {
r[i - mid] = a[i];
}
mergeSort(l, mid);
mergeSort(r, n - mid);
merge(a, l, r, mid, n - mid);
}
public static void merge(int[] a, int[] l, int[] r, int left, int right) {
int i = 0, j = 0, k = 0;
while (i < left && j < right) {
if (l[i] <= r[j]) {
a[k++] = l[i++];
}
else {
a[k++] = r[j++];
}
}
while (i < left) {
a[k++] = l[i++];
}
while (j < right) {
a[k++] = r[j++];
}
}
public static class FastReader {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer;
public int readSingleInt() throws IOException {
return Integer.parseInt(reader.readLine());
}
public int[] readIntArray(int numInts) throws IOException {
int[] nums = new int[numInts];
tokenizer = new StringTokenizer(reader.readLine());
for(int i = 0; i<numInts; i++){
nums[i] = Integer.parseInt(tokenizer.nextToken());
}
return nums;
}
public String readString() throws IOException {
return reader.readLine();
}
}
public static class FastWriter {
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
public void writeSingleInteger(int i) throws IOException {
writer.write(Integer.toString(i));
writer.newLine();
writer.flush();
}
public void writeSingleLong(long i) throws IOException {
writer.write(Long.toString(i));
writer.newLine();
writer.flush();
}
public void writeIntArrayWithSpaces(int[] nums) throws IOException {
for(int i = 0; i<nums.length; i++){
writer.write(nums[i] + " ");
}
writer.newLine();
writer.flush();
}
public void writeIntArrayListWithSpaces(ArrayList<Integer> nums) throws IOException {
for(int i = 0; i<nums.size(); i++){
writer.write(nums.get(i) + " ");
}
writer.newLine();
writer.flush();
}
public void writeIntArrayWithoutSpaces(int[] nums) throws IOException {
for(int i = 0; i<nums.length; i++){
writer.write(Integer.toString(nums[i]));
}
writer.newLine();
writer.flush();
}
public void writeString(String s) throws IOException {
writer.write(s);
writer.newLine();
writer.flush();
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 182b9dabd77174c6d461b0c6df86a256 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | // When I wrote this code, only God & I knew what it did. Now only God knows !!
import java.util.*;
import java.io.*;
public class Main {
static class FastReader
{
private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public FastReader() { this(System.in); }public FastReader(InputStream is) { mIs = is;}
public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];}
public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;}
public String next(){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 long l(){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 int i(){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 double d() throws IOException {return Double.parseDouble(next()) ;}
public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; }
public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; }
public void scanIntArr(int [] arr){ for(int li=0;li<arr.length;++li){ arr[li]=i();}}
public void scanLongArr(long [] arr){for (int i=0;i<arr.length;++i){arr[i]=l();}}
public void shuffle(int [] arr){ for(int i=arr.length;i>0;--i) { int r=(int)(Math.random()*i); int temp=arr[i-1]; arr[i-1]=arr[r]; arr[r]=temp; } }
}
static int mod = (int)(1e9 + 7);
public static void main(String[] args) throws IOException {
FastReader fr = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
/*
inputCopy
4
5 2 7
3 5 12 13 21
5 6 3
1 5 6 21 30
2 9 3
10 15
11 27182 31415
16 18 33 98 874 989 4848 20458 34365 38117 72030
outputCopy
173
171
75
3298918744
*/
int t = fr.i();
outer: for (int ti = 0; ti < t; ++ti) {
int n = fr.i();
long a = fr.l(); // capital change cost
long b = fr.l();
long[] arr = new long[n];
fr.scanLongArr(arr);
long [] suffixSum = new long[n];
suffixSum[n-1]=arr[n-1];
for(int i = n-2;i>=0;--i)
suffixSum[i] =suffixSum[i+1] + arr[i];
long cost = 0;
long currCapital = 0;
for(int i=0;i<n;++i) {
cost += (arr[i] - currCapital) * b;
if(i + 1 <n) {
long extraCostWhenNotMovingCapital = ((suffixSum[i + 1] - currCapital * (n-i-1)) * b);
long extraCostWhenMovingCapital = ((suffixSum[i + 1] - arr[i] * (n-i-1)) * b) + (a * (arr[i]-currCapital));
if(extraCostWhenMovingCapital<extraCostWhenNotMovingCapital) {
cost += (a * (arr[i]-currCapital));
currCapital = arr[i];
}
}
}
pw.println(cost);
}
pw.flush();
pw.close();
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 1ff7a664415b21acaa15b3ca0fb9c450 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class C1659 { //
static int[] x = new int[2 * (int)1e5 + 5];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int testCnt = Integer.parseInt(br.readLine());
String[] line;
for (int t = 0; t < testCnt; t++) {
line = br.readLine().split(" ");
int n = Integer.parseInt(line[0]);
long a = Integer.parseInt(line[1]);
long b = Integer.parseInt(line[2]);
line = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
x[i + 1] = Integer.parseInt(line[i]);
}
long min = (a + b) * x[n];
long cur = min;
for (int i = n - 1; i >= 0; i--) {
cur -= a * (x[i + 1] - x[i]);
cur += b * (x[i + 1] - x[i]) * (n - i - 1);
if (cur < min) min = cur;
}
bw.write(min + "\n");
}
br.close();
bw.close();
}
public static void dbug(String s, long ... a) { // /*
String[] line = s.split("<>");
for (int i = 0; i < a.length; i++) {
System.out.print(line[i] + a[i]);
}
System.out.println(line[a.length]); // */
}
public static void print(int[] a) { // /*
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println(); // */
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 97f76d8574fc15471a105ca7239903da | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
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){
int n = scn.nextInt(), a = scn.nextInt(), b = scn.nextInt();
long x[] = new long[n + 1], sum = 0, min = Long.MAX_VALUE, ssf = 0;
for(int i = 1; i <= n; i++){
sum += (x[i] = scn.nextInt());
}
for(int i = 0; i <= n; i++){
min = Math.min(min, (a + b) * x[i] + b * (sum - (ssf += x[i]) - (n - i) * x[i]));
}
System.out.println(min);
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | f4d051995ccbd76ee3f70e0ce352e828 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 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){
int n = scn.nextInt(), a = scn.nextInt(), b = scn.nextInt();
long x[] = new long[n + 1], sum = 0;
long min = Long.MAX_VALUE;
for(int i = 1; i <= n; i++){
x[i] = scn.nextInt();
sum += x[i];
}
long ssf = 0;
for(int i = 0; i <= n; i++){
ssf += x[i];
min = Math.min(min, (a + b) * x[i] + b * (sum - ssf - (n - i) * x[i]));
}
System.out.println(min);
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | fc51b65bc74ca12b3362197858ebf0cc | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 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){
int n = scn.nextInt(), a = scn.nextInt(), b = scn.nextInt();
long[] x = new long[n + 1], pref = new long[n + 1];
long min = Long.MAX_VALUE;
for(int i = 1; i <= n; i++){
x[i] = scn.nextInt();
}
for(int i = 1; i <= n; i++){
pref[i] = pref[i - 1] + x[i];
}
for(int i = 0; i <= n; i++){
min = Math.min(min, (a + b) * x[i] + b * (pref[n] - pref[i] - (n - i) * x[i]));
}
System.out.println(min);
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 10c2188ab5ea94fce8510892c4c70af0 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | /* Author _trevorphillips_ */
import java.io.*;
import java.util.*;
public class C_Line_Empire {
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 f=new FastReader();
StringBuilder sb=new StringBuilder();
int t = f.nextInt();
while(t-->0){
int n=f.nextInt();
long a=f.nextLong();
long b=f.nextLong();
long[] arr=new long[n];
for(int i=0;i<n;i++){
arr[i]=f.nextLong();
}
long ans=0;
for(int i=0;i<n;i++){
long eler=n-i;
long last=i==0?0:arr[i-1];
long d=Math.abs(arr[i]-last);
ans+=d*Math.min(eler*b,a+b);
}
sb.append(ans+"\n");
}
System.out.println(sb);
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 983c3fd03e49cc56a3a4f7154b85e30c | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
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.Comparator;
import java.util.StringTokenizer;
import java.util.Vector;
public class CodeForces {
static int mod = (int)1e9+7;
public static void main(String[] args) throws InterruptedException {
// PrintWriter out = new PrintWriter("output.txt");
// File input = new File("input.txt");
// FastScanner fs = new FastScanner(input);
FastScanner fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int testNumber =fs.nextInt();
// thanks UpDownUpDown
for (int T =0;T<testNumber;T++){
int n = fs.nextInt();
int a = fs.nextInt();
int b = fs.nextInt();
long finalWhore = (long)1e18;
Vector<Long> fuck_Newtech66 = new Vector<>();
long fuck_TimeWarp101 = 0;
fuck_Newtech66.add((long)0);
for (int i=0;i<n;i++){
long num = fs.nextLong();
fuck_Newtech66.add(num);
fuck_TimeWarp101+=num;
}
for (int i =0;i<n+1;i++){
fuck_TimeWarp101-=fuck_Newtech66.get(i);
long littleBitch = (a+b)*fuck_Newtech66.get(i)+(fuck_TimeWarp101-(n-i)*fuck_Newtech66.get(i))*b;
finalWhore=Math.min(finalWhore,littleBitch);
}
out.print(finalWhore+"\n");
}
out.flush();
}
static long modInverse( long n, long p)
{
return FastPower(n, p - 2, p);
}
static int[] factorials(int max,int mod){
int [] ans = new int[max+1];
ans[0]=1;
for (int i=1;i<=max;i++){
ans[i]=ans[i-1]*i;
ans[i]%=mod;
}
return ans;
}
static String toBinary(int num,int bits){
String res =Integer.toBinaryString(bits);
while(res.length()<bits)res="0"+res;
return res;
}
static String toBinary(long num,int bits){
String res =Long.toBinaryString(bits);
while(res.length()<bits)res="0"+res;
return res;
}
static long LCM(long a,long b){
return a*b/gcd(a,b);
}
static long FastPower(long x,long p,long mod){
if(p==0)return 1;
long ans =FastPower(x, p/2,mod);
ans%=mod;
ans*=ans;
ans%=mod;
if(p%2==1)ans*=x;
ans%=mod;
return ans;
}
static double FastPower(double x,int p){
if(p==0)return 1.0;
double ans =FastPower(x, p/2);
ans*=ans;
if(p%2==1)ans*=x;
return ans;
}
static int FastPower(int x,int p){
if(p==0)return 1;
int ans =FastPower(x, p/2);
ans*=ans;
if(p%2==1)ans*=x;
return ans;
}
static ArrayList<Vertex> vertices = new ArrayList<>();
static class Vertex {
public ArrayList<Integer>edges = new ArrayList<>();
public boolean isEnd=false;
public Vertex (){
for(int i=0;i<26;i++){
edges.set(i, -1);
}
}
}
static class Trie {
private int root=0;
public Trie(){
vertices.add(new Vertex());
}
public void InsertWord(String s){
int current = root;
for(char c:s.toCharArray()){
int pos = c-'a';
if(vertices.get(current).edges.get(pos)==-1){
vertices.add(new Vertex());
Vertex x = vertices.get(current);
x.edges.set(pos,vertices.size()-1);
vertices.set(current, x);
}
current=vertices.get(current).edges.get(pos);
}
}
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(){
br=new BufferedReader(new InputStreamReader(System.in));
st=new StringTokenizer("");
}
public FastScanner(File f){
try {
br=new BufferedReader(new FileReader(f));
st=new StringTokenizer("");
} catch(FileNotFoundException e){
br=new BufferedReader(new InputStreamReader(System.in));
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());
}
double nextDouble() {
return Double.parseDouble(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());
}
}
public static long factorial(int n){
if(n==0)return 1;
return (long)n*factorial(n-1);
}
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static void sort (int[]a){
ArrayList<Integer> b = new ArrayList<>();
for(int i:a)b.add(i);
Collections.sort(b);
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static void sortReversed (int[]a){
ArrayList<Integer> b = new ArrayList<>();
for(int i:a)b.add(i);
Collections.sort(b,new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static void sort (long[]a){
ArrayList<Long> b = new ArrayList<>();
for(long i:a)b.add(i);
Collections.sort(b);
for(int i=0;i<b.size();i++){
a[i]=b.get(i);
}
}
static ArrayList<Integer> 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] == true)
{
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
ArrayList<Integer> ans = new ArrayList<>();
for (int i = 2; i <= n; i++)
{
if (prime[i] == true)
ans.add(i);
}
return ans;
}
static int binarySearchSmallerOrEqual(int arr[], int key)
{
int n = arr.length;
int left = 0, right = n;
int mid = 0;
while (left < right) {
mid = (right + left) >> 1;
if (arr[mid] == key) {
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
else if (arr[mid] > key)
right = mid;
else
left = mid + 1;
}
while (mid > -1 && arr[mid] > key)
mid--;
return mid;
}
static int binarySearchSmallerOrEqual(long arr[], long key)
{
int n = arr.length;
int left = 0, right = n;
int mid = 0;
while (left < right) {
mid = (right + left) >> 1;
if (arr[mid] == key) {
while (mid + 1 < n && arr[mid + 1] == key)
mid++;
break;
}
else if (arr[mid] > key)
right = mid;
else
left = mid + 1;
}
while (mid > -1 && arr[mid] > key)
mid--;
return mid;
}
public static int binarySearchStrictlySmaller(int[] arr, int target)
{
int start = 0, end = arr.length-1;
if(end == 0) return -1;
if (target > arr[end]) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
public static int binarySearchStrictlySmaller(long[] arr, long target)
{
int start = 0, end = arr.length-1;
if(end == 0) return -1;
if (target > arr[end]) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static int binarySearch(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static int binarySearch(long arr[], long x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static void init(int[]arr,int val){
for(int i=0;i<arr.length;i++){
arr[i]=val;
}
}
static void init(int[][]arr,int val){
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
arr[i][j]=val;
}
}
}
static void init(long[]arr,long val){
for(int i=0;i<arr.length;i++){
arr[i]=val;
}
}
static<T> void init(ArrayList<ArrayList<T>>arr,int n){
for(int i=0;i<n;i++){
arr.add(new ArrayList());
}
}
static int binarySearchStrictlySmaller(ArrayList<Pair> arr, int target)
{
int start = 0, end = arr.size()-1;
if(end == 0) return -1;
if (target > arr.get(end).y) return end;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr.get(mid).y >= target) {
end = mid - 1;
}
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static int binarySearchStrictlyGreater(int[] arr, int target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (arr[mid] <= target) {
start = mid + 1;
}
else {
ans = mid;
end = mid - 1;
}
}
return ans;
}
public static long pow(long n, long pow) {
if (pow == 0) {
return 1;
}
long retval = n;
for (long i = 2; i <= pow; i++) {
retval *= n;
}
return retval;
}
static String reverse(String s){
StringBuffer b = new StringBuffer(s);
b.reverse();
return b.toString();
}
static String charToString (char[] arr){
String t="";
for(char c :arr){
t+=c;
}
return t;
}
int[] copy (int [] arr , int start){
int[] res = new int[arr.length-start];
for (int i=start;i<arr.length;i++)res[i-start]=arr[i];
return res;
}
static int[] swap(int [] A,int l,int r){
int[] B=new int[A.length];
for (int i=0;i<l;i++){
B[i]=A[i];
}
int k=0;
for (int i=r;i>=l;i--){
B[l+k]=A[i];
k++;
}
for (int i=r+1;i<A.length;i++){
B[i]=A[i];
}
return B;
}
static int mex (int[] d){
int [] a = Arrays.copyOf(d, d.length);
sort(a);
if(a[0]!=0)return 0;
int ans=1;
for(int i=1;i<a.length;i++){
if(a[i]==a[i-1])continue;
if(a[i]==a[i-1]+1)ans++;
else break;
}
return ans;
}
static int[] mexes(int[] arr){
int[] freq = new int [100000+7];
for (int i:arr)freq[i]++;
int maxMex =0;
for (int i=0;i<=100000+7;i++){
if(freq[i]!=0)maxMex++;
else break;
}
int []ans = new int[arr.length];
ans[arr.length-1] = maxMex;
for (int i=arr.length-2;i>=0;i--){
freq[arr[i+1]]--;
if(freq[arr[i+1]]<=0){
if(arr[i+1]<maxMex)
maxMex=arr[i+1];
ans[i]=maxMex;
} else {
ans[i]=ans[i+1];
}
}
return ans;
}
static int [] freq (int[]arr,int max){
int []b = new int[max];
for (int i:arr)b[i]++;
return b;
}
static int[] prefixSum(int[] arr){
int [] a = new int[arr.length];
a[0]=arr[0];
for (int i=1;i<arr.length;i++)a[i]=a[i-1]+arr[i];
return a;
}
static class Pair {
int x;
long y;
int extra;
public Pair(int x,long y){
this.x=x;
this.y=y;
}
public Pair(int x,long y,int extra){
this.x=x;
this.y=y;
this.extra=extra;
}
// @Override
// public boolean equals(Object o) {
// if(o instanceof Pair){
// if(o.hashCode()!=hashCode()){
// return false;
// } else {
// return x==((Pair)o).x&&y==((Pair)o).y;
// }
// }
//
// return false;
//
// }
//
//
//
//
//
// @Override
// public int hashCode() {
// return x+(int)y*2;
// }
//
//
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 383b2f804f45c7aaa2565ed00d1a2bc9 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int TT = sc.nextInt();
while (TT-- > 0) {
int n = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt();
long[] res = new long[n + 1];
for (int i = 1; i <= n; i++) {
res[i] = sc.nextInt();
}
long[] sum = new long[n + 1];
for (int i = 1; i <= n; i++) {
sum[i] = sum[i - 1] + res[i];
}
long ans = Long.MAX_VALUE;
for (int i = 0; i < n; i++) {
long cur = res[i] * a + res[i] * b + (sum[n] - sum[i] - (n - i) * res[i]) * b;
ans = Math.min(ans, cur);
}
System.out.println(ans);
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 0854d397878218997c78ba5523377e4a | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import javax.management.Query;
import java.io.*;
public class practice {
// static int n,k;
//// static String t,s;
// static long[]memo;
// static int n;// int k=sc.nextInt();
static String s;
static HashMap<Long,Integer>hm;
public static void main(String[] args) throws Exception {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int a=sc.nextInt();
int b=sc.nextInt();
int[] x=sc.nextIntArray(n);
long[] y=new long[n];
for (int i = 0; i < n; i++) {
y[0]+=1l*b*x[i];
}
// y[n-1]=Long.MAX_VALUE/2;
for (int i = 1; i < n; i++) {
if(i==1) {
y[i] = y[i - 1] + 1l*a * (x[i - 1] - 0) - 1l*b * (x[i-1] - 0) * (n - i );
// pw.println("yyy="+(a * (x[i - 1] - 0))+" "+(b * (x[i-1] -0) * (n - i )));
}
else {
y[i] = y[i - 1] + 1l * a * (x[i - 1] - x[i - 2]) - 1l * b * (x[i - 1] - x[i - 2]) * (n - i);
// pw.println("yyy=" + (a * (x[i - 1] - x[i - 2])) + " " + (b * (x[i - 1] - x[i - 2]) * (n - i)));
}
}
long min=y[0];
for (int i = 0; i <n ; i++) {
if(y[i]<min)
min=y[i];
}
// pw.println(Long.MAX_VALUE/2);
pw.println(min);
}
pw.close();
}
public static void sort(int[] in) {
shuffle(in);
Arrays.sort(in);
}
public static void shuffle(int[] in) {
for (int i = 0; i < in.length; i++) {
int idx = (int) (Math.random() * in.length);
int tmp = in[i];
in[i] = in[idx];
in[idx] = tmp;
}
}
static LinkedList getfact(int n){
LinkedList<Integer>ll=new LinkedList<>();
LinkedList<Integer>ll2=new LinkedList<>();
for (int i = 1; i <= Math.sqrt(n); i++) {
if(n%i==0) {
ll.add(i);
if(i!=n/i)
ll2.addLast(n/i);
}
}
while (!ll2.isEmpty()){
ll.add(ll2.removeLast());
}
return ll;
}
static void rev(int n){
String s1=s.substring(0,n);
s=s.substring(n);
for (int i = 0; i <n ; i++) {
s=s1.charAt(i)+s;
}
}
static class SegmentTree { // 1-based DS, OOP
int N; //the number of elements in the array as a power of 2 (i.e. after padding)
long[] array, sTree;
Long[]lazy;
SegmentTree(long[] in)
{
array = in; N = in.length - 1;
sTree = new long[N<<1]; //no. of nodes = 2*N - 1, we add one to cross out index zero
lazy = new Long[N<<1];
build(1,1,N);
}
void build(int node, int b, int e) // O(n)
{
if(b == e)
sTree[node] = array[b];
else
{
int mid = b + e >> 1;
build(node<<1,b,mid);
build(node<<1|1,mid+1,e);
sTree[node] = sTree[node<<1]+sTree[node<<1|1];
}
}
void update_point(int index, int val) // O(log n)
{
index += N - 1;
sTree[index] += val;
while(index>1)
{
index >>= 1;
sTree[index] = sTree[index<<1] + sTree[index<<1|1];
}
}
void update_range(int i, int j, int val) // O(log n)
{
update_range(1,1,N,i,j,val);
}
void update_range(int node, int b, int e, int i, int j, int val)
{
if(i > e || j < b)
return;
if(b >= i && e <= j)
{
sTree[node] = (e-b+1)*val;
lazy[node] = val*1l;
}
else
{
int mid = b + e >> 1;
propagate(node, b, mid, e);
update_range(node<<1,b,mid,i,j,val);
update_range(node<<1|1,mid+1,e,i,j,val);
sTree[node] = sTree[node<<1] + sTree[node<<1|1];
}
}
void propagate(int node, int b, int mid, int e)
{
if(lazy[node]!=null) {
lazy[node << 1] = lazy[node];
lazy[node << 1 | 1] = lazy[node];
sTree[node << 1] = (mid - b + 1) * lazy[node];
sTree[node << 1 | 1] = (e - mid) * lazy[node];
}
lazy[node] = null;
}
long query(int i, int j)
{
return query(1,1,N,i,j);
}
long query(int node, int b, int e, int i, int j) // O(log n)
{
if(i>e || j <b)
return 0;
if(b>= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
propagate(node, b, mid, e);
long q1 = query(node<<1,b,mid,i,j);
long q2 = query(node<<1|1,mid+1,e,i,j);
return q1 + q2;
}
}
// public static long dp(int idx) {
// if (idx >= n)
// return Long.MAX_VALUE/2;
// return Math.min(dp(idx+1),memo[idx]+dp(idx+k));
// }
// if(num==k)
// return dp(0,idx+1);
// if(memo[num][idx]!=-1)
// return memo[num][idx];
// long ret=0;
// if(num==0) {
// if(s.charAt(idx)=='a')
// ret= dp(1,idx+1);
// else if(s.charAt(idx)=='?') {
// ret=Math.max(1+dp(1,idx+1),dp(0,idx+1) );
// }
// }
// else {
// if(num%2==0) {
// if(s.charAt(idx)=='a')
// ret=dp(num+1,idx+1);
// else if(s.charAt(idx)=='?')
// ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1));
// }
// else {
// if(s.charAt(idx)=='b')
// ret=dp(num+1,idx+1);
// else if(s.charAt(idx)=='?')
// ret=Math.max(1+dp(num+1,idx+1),dp(0,idx+1));
// }
// }
// }
static void putorrem(long x){
if(hm.getOrDefault(x,0)==1){
hm.remove(x);
}
else
hm.put(x,hm.getOrDefault(x,0)-1);
}
public static int max4(int a,int b, int c,int d) {
int [] s= {a,b,c,d};
Arrays.sort(s);
return s[3];
}
public static double logbase2(int k) {
return( (Math.log(k)+0.0)/Math.log(2));
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public long[] nextlongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Long[] nextLongArray(int n) throws IOException {
Long[] a = new Long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public boolean ready() throws IOException {
return br.ready();
}
}
static class pair implements Comparable<pair> {
long x;
long y;
public pair(long x, long y) {
this.x = x;
this.y = y;
}
public String toString() {
return x + " " + y;
}
public boolean equals(Object o) {
if (o instanceof pair) {
pair p = (pair) o;
return p.x == x && p.y == y;
}
return false;
}
public int hashCode() {
return new Long(x).hashCode() * 31 + new Long(y).hashCode();
}
public int compareTo(pair other) {
if (this.x == other.x) {
return Long.compare(this.y, other.y);
}
return Long.compare(this.x, other.x);
}
}
static class tuble implements Comparable<tuble> {
int x;
int y;
int z;
public tuble(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public String toString() {
return x + " " + y + " " + z;
}
public int compareTo(tuble other) {
if (this.x == other.x) {
if (this.y == other.y) {
return this.z - other.z;
}
return this.y - other.y;
} else {
return this.x - other.x;
}
}
public tuble add(tuble t){
return new tuble(this.x+t.x,this.y+t.y,this.z+t.z);
}
}
static long mod = 1000000007;
static Random rn = new Random();
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | e532d5fa4ec53d9d338c9a50177e6f79 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
import javax.sql.rowset.spi.SyncResolver;
import java.io.*;
import java.nio.channels.NonReadableChannelException;
import java.text.DateFormatSymbols;
public class CpTemp {
static int a[];
static long count=0l;
static long row = 1l;
static long col = 1l;
static FastScanner fs = null;
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
outer:
while (t-- > 0) {
int n = fs.nextInt();
long a = fs.nextLong();
long b = fs.nextInt();
long x[] = fs.readlongArray(n);
long suff[] = new long[n];
suff[n-1] = 0;
for(int i=n-2;i>=0;i--){
suff[i] = x[i+1] + suff[i+1];
}
ArrayList<Long> al = new ArrayList<>();
long left = 0;
for(int i=0;i<n;i++){
int rem = n-i-1;
long val = (suff[i] - rem*(x[i]))*b ;
// val += left;
left += (i==0?b*x[i]:b*(x[i]-x[i-1]));
val += a*(x[i]);
val += left;
al.add(val);
}
long val = (suff[0]+x[0])*b;
al.add(val);
Collections.sort(al);
out.println(al.get(0));
}
out.close();
}
static class Pair implements Comparable<Pair> {
int x;
int y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
public int compareTo(Pair o) {
return this.y-o.y;
}
}
static long power(long x, long y, long p) {
if (y == 0)
return 1;
if (x == 0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y) {
if (y == 0)
return 1;
if (x == 0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readlongArray(int n){
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static boolean prime[];
static void sieveOfEratosthenes(int n) {
prime = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a
// prime
if (prime[p] == true) {
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
}
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static long nCk(int n, int k) {
long res = 1;
for (int i = n - k + 1; i <= n; ++i)
res *= i;
for (int i = 2; i <= k; ++i)
res /= i;
return res;
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 08bd611596d4e7d4f7b27eb185b3fc10 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.util.*;
import java.io.*;
public class Main {
public static long[] arr = new long[300000];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t>0){
sc.nextLine();
long n=sc.nextLong();
long a = sc.nextLong();
long b = sc.nextLong();
sc.nextLine();
long ans=0;
long c=0;
int cInd=-1;
for(int i=0;i<n;i++){
long ele = sc.nextLong();
arr[i]=ele;
if(i>0 && cInd==-1 && (n-i)*b>a){
ans = ans + a*(arr[0]);
cInd++;
c=arr[cInd];
} else if(i>0 && (n-i)*b>a){
ans = ans + a*(arr[cInd+1]-arr[cInd]);
cInd++;
c=arr[cInd];
}
//System.out.println(ans);
long diff = ele -c;
ans = ans + b*(diff);
//System.out.print(ans+" ");
}
System.out.println(ans);
t--;
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 344ea0d2253a0b232c3925c909e0144c | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static int i, j, k, n, m, t, y, x, sum = 0;
static long mod = 1000000007;
static FastScanner fs = new FastScanner();
static PrintWriter out = new PrintWriter(System.out);
static String str;
static long ans;
public static void main(String[] args){
t = fs.nextInt();
while(t-- > 0){
n = fs.nextInt();
long x = fs.nextInt();
long y = fs.nextInt();
int [] arr = fs.readArray(n);
long[] suffix = new long[n];
suffix[n-1] = arr[n-1];
for(i=n-2;i>=0;i--){
suffix[i] = suffix[i+1]+arr[i];
}
long ans = Long.MAX_VALUE;
long capVal = 0;
for(i=0;i<n;i++){
long temp = capVal;
if(i==0){
temp += (suffix[i]*y);
}
else
{
temp+=(suffix[i] - ((long) arr[i - 1] * (n - i)))*y;
}
if(i==0){
capVal=arr[i]*(x+y) ;
}
else{
capVal+=(arr[i]-arr[i-1])*(x+y);
}
ans = Math.min(ans, temp);
}
out.println(ans);
}
out.close();
}
/**
* Returns the gretaest index of closest element less than x in arr
* returns -1 if all elemets are greater
*
*/
public static int lowerBound(int x, List<Integer> arr, int l, int r){
if(arr.get(l)>=x)
return -1;
if(arr.get(r)<x)
return r;
int mid = (l+r)/2;
if(arr.get(mid)<x && arr.get(mid+1)>x)
return mid;
if(arr.get(mid)>=x)
return lowerBound(x,arr,l,mid-1);
return lowerBound(x,arr,mid+1,r);
}
/**
* Returns the lowest index of closest element greater than or equal to x in arr
* returns -1 if all elements are lesser
*
*/
public static int upperBound(int x, List<Integer> arr, int l, int r){
if(arr.get(r) <=x)
return -1;
if(arr.get(l)>x)
return l;
int mid = (l+r)/2;
if(arr.get(mid)>x && arr.get(mid-1)<=x)
return mid;
if(arr.get(mid)<=x)
return upperBound(x,arr,mid+1,r);
return upperBound(x,arr,l,mid-1);
}
/**
* Returns the index of element if present else -1.
*
*/
public static int binSearch(int x, List<Integer> arr){
int y = Collections.binarySearch(arr, x);
if(y<0)
return -1;
return y;
}
/**
* Gets prime factorisation of a number in list of pairs.
* x is the factor and y is the occurrence.
*
*/
public static List<Pair> primeFactorization(int num){
List<Pair> ans = new ArrayList<>();
for(int i=2;i*i<=num;i++){
if(num % i ==0){
int count = 0;
while(num%i==0){
count++;
num=num/i;
}
ans.add(new Pair(i,count));
}
}
if(num!=1)
ans.add(new Pair(num, 1));
return ans;
}
/*static long nck(int n , int k){
long a = fact[n];
long b = modInv(fact[k]);
b*= modInv(fact[n-k]);
b%=mod;
return (a*b)%mod;
}
static void populateFact(){
fact[0]=1;
fact[1] = 1;
for(i=2;i<300005;i++){
fact[i]=i*fact[i-1];
fact[i]%=mod;
}
}
*/
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long exp(long base, long pow) {
if (pow == 0) return 1;
long half = exp(base, pow / 2);
if (pow % 2 == 0) return mul(half, half);
return mul(half, mul(half, base));
}
static long mul(long a, long b) {
return ((a % mod) * (b % mod)) % mod;
}
static long add(long a, long b) {
return ((a % mod) + (b % mod)) % mod;
}
static long modInv(long x) {
return exp(x, mod - 2);
}
static void ruffleSort(int[] a) {
//ruffle
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n), temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
//then sort
Arrays.sort(a);
}
static void ruffleSort(long[] a) {
//ruffle
int n = a.length;
Random r = new Random();
for (int i = 0; i < a.length; i++) {
int oi = r.nextInt(n);
long temp = a[i];
a[i] = a[oi];
a[oi] = temp;
}
Arrays.sort(a);
}
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());
}
}
static class Pair implements Comparable<Pair> {
public int x, y;
Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pair o) {
if (x == o.x)
return Integer.compare(y, o.y);
return Integer.compare(x, o.x);
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 757555053291e978cffd47f57a23a43d | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.*;
import static java.lang.System.out;
public class app {
public static long m;
static int mod = 998244353;
static int inf = (int) 1e9;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
//int t=1;
long fg[]=new long[40001];
while (t-- > 0) {
int ss=sc.nextInt();
long a=sc.nextInt();
long b=sc.nextInt();
long n[]=new long[ss+1];
long as=0;
for(int i=1;i<=ss;i++){
n[i]=sc.nextInt();
}
int ass=0;
long fd=Long.MAX_VALUE;
for(int i=ss-1;i>=0;i--){
as += (b*(n[i+1]-n[i])*(ss-i-1));
as -= ((n[i+1]-n[i])*a);
if(fd>=as){
fd=as;
ass=i;
}
}
as=(n[ass]*(a+b));
for(int i=ass+1;i<=ss;i++){
as+=(n[i]-n[ass])*b;
}
out.println(as);
}
}
//out.println(Arrays.toString(d).replace(",","").replace("]","").replace("[",""));
static long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
static long lcm(long a, long b) {
return a * b / gcd(a, b);
}
static void sort(int[] a) {
int n = a.length;
Integer[] b = new Integer[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static void sort(long[] a) {
int n = a.length;
Long[] b = new Long[n];
for (int i = 0; i < n; i++)
b[i] = a[i];
Arrays.sort(b);
for (int i = 0; i < n; i++)
a[i] = b[i];
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public int[] nextArrInt(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextArrLong(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public int[] nextArrIntSorted(int n) throws IOException {
int[] a = new int[n];
Integer[] a1 = new Integer[n];
for (int i = 0; i < n; i++)
a1[i] = nextInt();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[i].intValue();
return a;
}
public long[] nextArrLongSorted(int n) throws IOException {
long[] a = new long[n];
Long[] a1 = new Long[n];
for (int i = 0; i < n; i++)
a1[i] = nextLong();
Arrays.sort(a1);
for (int i = 0; i < n; i++)
a[i] = a1[i].longValue();
return a;
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 2c767ab6ab481fd12d6f6797212f3eeb | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.io.*;
import java.util.*;
public final class Main {
//int 2e9 - long 9e18
static PrintWriter out = new PrintWriter(System.out);
static FastReader in = new FastReader();
static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)};
static int mod = (int) (1e9 + 7);
static int mod2 = 998244353;
public static void main(String[] args) {
int tt = i();
while (tt-- > 0) {
solve();
}
out.flush();
}
public static void solve() {
int n = i();
long a = i();
long b = i();
int[] x = input(n);
long ans = (long) x[n - 1] * b;
long prev = 0;
for (int i = 0; i < n; i++) {
long d = x[i] - prev;
ans += Math.min((n - 1 - i) * b, a) * d;
prev = x[i];
}
out.println(ans);
}
// (10,5) = 2 ,(11,5) = 3
static long upperDiv(long a, long b) {
return (a / b) + ((a % b == 0) ? 0 : 1);
}
static long sum(int[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static int[] preint(int[] a) {
int[] pre = new int[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static long[] pre(int[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static long[] post(int[] a) {
long[] post = new long[a.length + 1];
post[0] = 0;
for (int i = 0; i < a.length; i++) {
post[i + 1] = post[i] + a[a.length - 1 - i];
}
return post;
}
static long[] pre(long[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static void print(char A[]) {
for (char c : A) {
out.print(c);
}
out.println();
}
static void print(boolean A[]) {
for (boolean c : A) {
out.print(c + " ");
}
out.println();
}
static void print(int A[]) {
for (int c : A) {
out.print(c + " ");
}
out.println();
}
static void print(long A[]) {
for (long i : A) {
out.print(i + " ");
}
out.println();
}
static void print(List<Integer> A) {
for (int a : A) {
out.print(a + " ");
}
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static String c() {
return in.next();
}
static int[][] inputWithIdx(int N) {
int A[][] = new int[N][2];
for (int i = 0; i < N; i++) {
A[i] = new int[]{i, in.nextInt()};
}
return A;
}
static int[] input(int N) {
int A[] = new int[N];
for (int i = 0; i < N; i++) {
A[i] = in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[] = new long[N];
for (int i = 0; i < A.length; i++) {
A[i] = in.nextLong();
}
return A;
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long GCD(long a, long b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long LCM(int a, int b) {
return (long) a / GCD(a, b) * b;
}
static long LCM(long a, long b) {
return a / GCD(a, b) * b;
}
// find highest i which satisfy a[i]<=x
static int lowerbound(int[] a, int x) {
int l = 0;
int r = a.length - 1;
while (l < r) {
int m = (l + r + 1) / 2;
if (a[m] <= x) {
l = m;
} else {
r = m - 1;
}
}
return l;
}
static void shuffle(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
}
static void shuffleAndSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int[] temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr, comparator);
}
static void shuffleAndSort(long[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
long temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static boolean isPerfectSquare(double number) {
double sqrt = Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static void swap(int A[], int a, int b) {
int t = A[a];
A[a] = A[b];
A[b] = t;
}
static void swap(char A[], int a, int b) {
char t = A[a];
A[a] = A[b];
A[b] = t;
}
static long pow(long a, long b, int mod) {
long pow = 1;
long x = a;
while (b != 0) {
if ((b & 1) != 0) {
pow = (pow * x) % mod;
}
x = (x * x) % mod;
b /= 2;
}
return pow;
}
static long pow(long a, long b) {
long pow = 1;
long x = a;
while (b != 0) {
if ((b & 1) != 0) {
pow *= x;
}
x = x * x;
b /= 2;
}
return pow;
}
static long modInverse(long x, int mod) {
return pow(x, mod - 2, mod);
}
static boolean isPrime(long N) {
if (N <= 1) {
return false;
}
if (N <= 3) {
return true;
}
if (N % 2 == 0 || N % 3 == 0) {
return false;
}
for (int i = 5; i * i <= N; i = i + 6) {
if (N % i == 0 || N % (i + 2) == 0) {
return false;
}
}
return true;
}
public static String reverse(String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
public static void reverse(int[] arr) {
for (int i = 0; i < arr.length / 2; i++) {
int tmp = arr[i];
arr[arr.length - 1 - i] = tmp;
arr[i] = arr[arr.length - 1 - i];
}
}
public static String repeat(char ch, int repeat) {
if (repeat <= 0) {
return "";
}
final char[] buf = new char[repeat];
for (int i = repeat - 1; i >= 0; i--) {
buf[i] = ch;
}
return new String(buf);
}
public static int[] manacher(String s) {
char[] chars = s.toCharArray();
int n = s.length();
int[] d1 = new int[n];
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1);
while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) {
k++;
}
d1[i] = k--;
if (i + k > r) {
l = i - k;
r = i + k;
}
}
return d1;
}
public static int[] kmp(String s) {
int n = s.length();
int[] res = new int[n];
for (int i = 1; i < n; ++i) {
int j = res[i - 1];
while (j > 0 && s.charAt(i) != s.charAt(j)) {
j = res[j - 1];
}
if (s.charAt(i) == s.charAt(j)) {
++j;
}
res[i] = j;
}
return res;
}
}
class Pair {
int i;
int j;
Pair(int i, int j) {
this.i = i;
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pair pair = (Pair) o;
return i == pair.i && j == pair.j;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
class ThreePair {
int i;
int j;
int k;
ThreePair(int i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ThreePair pair = (ThreePair) o;
return i == pair.i && j == pair.j && k == pair.k;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
class Node {
int val;
public Node(int val) {
this.val = val;
}
}
class ST {
int n;
Node[] st;
ST(int n) {
this.n = n;
st = new Node[4 * Integer.highestOneBit(n)];
}
void build(Node[] nodes) {
build(0, 0, n - 1, nodes);
}
private void build(int id, int l, int r, Node[] nodes) {
if (l == r) {
st[id] = nodes[l];
return;
}
int mid = (l + r) >> 1;
build((id << 1) + 1, l, mid, nodes);
build((id << 1) + 2, mid + 1, r, nodes);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
void update(int i, Node node) {
update(0, 0, n - 1, i, node);
}
private void update(int id, int l, int r, int i, Node node) {
if (i < l || r < i) {
return;
}
if (l == r) {
st[id] = node;
return;
}
int mid = (l + r) >> 1;
update((id << 1) + 1, l, mid, i, node);
update((id << 1) + 2, mid + 1, r, i, node);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
Node get(int x, int y) {
return get(0, 0, n - 1, x, y);
}
private Node get(int id, int l, int r, int x, int y) {
if (x > r || y < l) {
return new Node(0);
}
if (x <= l && r <= y) {
return st[id];
}
int mid = (l + r) >> 1;
return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y));
}
Node comb(Node a, Node b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
return new Node(GCD(a.val, b.val));
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 792f97e844e2e10dd3c79eaa5c69e8b1 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class codeforces1659C {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCases = Integer.parseInt(br.readLine());
for (int rep = 0; rep<numCases;rep++)
{
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
long [] kingdoms= new long[n+1];
kingdoms[0] = 0;
long [] arr = new long[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i<n;i++)
{
arr[i] = Long.parseLong(st.nextToken());
kingdoms[i+1] = arr[i]+kingdoms[i];
}
long currCapital = 0;
long cost = 0;
for (int i = 0;i<n;i++)
{
cost+=(arr[i]-currCapital)*b;
//change the capital if it decreases the cost of adding the rest
//System.out.println(((kingdoms[n]-kingdoms[i+1]-((n-i-1)*currCapital))*b)+" "+ (a*(arr[i]-currCapital)+((kingdoms[n]-kingdoms[i+1]-((n-i-1)*arr[i]))*b)));
if (((kingdoms[n]-kingdoms[i+1]-((n-i-1)*currCapital))*b)>(a*(arr[i]-currCapital)+((kingdoms[n]-kingdoms[i+1]-((n-i-1)*arr[i]))*b)))
{
//System.out.println("hit");
cost+=a*(arr[i]-currCapital);
currCapital = arr[i];
}
//System.out.println(cost+" "+arr[i]+" "+currCapital+" "+b);
//just move on because changing the capital isn't optimal
}
//cost+=b*(arr[n-1]-currCapital);
System.out.println(cost);
//System.out.println();
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 451ee9783551552e2ad7864c5090f732 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static int INF = 0x3f3f3f3f;
public static int mod = 1000000007;
public static int mod9 = 998244353;
public static int[] x = new int[2*(int)1e5+100];
public static void main(String args[]){
try {
PrintWriter o = new PrintWriter(System.out);
boolean multiTest = true;
// init
if(multiTest) {
int t = fReader.nextInt(), loop = 0;
while (loop < t) {loop++;solve(o);}
} else solve(o);
o.close();
} catch (Exception e) {e.printStackTrace();}
}
static void solve(PrintWriter o){
try {
//5 6 3
//1 5 6 21 30
int n = fReader.nextInt(), a = fReader.nextInt(), b = fReader.nextInt();
for(int i=0;i<n;i++) x[i] = fReader.nextInt();
long[] preSum = new long[n+1];
for(int i=1;i<=n;i++) preSum[i] = preSum[i-1] + x[i-1];
long res = b*preSum[n];
for(int i=0;i<n;i++){
res = Math.min(res, 1l*a*x[i] + 1l*b*x[i] + b*(preSum[n] - preSum[i+1] - 1l*(n-i-1)*x[i]));
}
o.println(res);
} catch (Exception e){e.printStackTrace();}
}
public static int upper_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) <= val) l = mid + 1;
else r = mid;
}
return l;
}
public static int lower_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) < val) l = mid + 1;
else r = mid;
}
return l;
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b, a%b);
}
public static void reverse(int[] array){
reverse(array, 0 , array.length-1);
}
public static void reverse(int[] array, int left, int right) {
if (array != null) {
int i = left;
for(int j = right; j > i; ++i) {
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
--j;
}
}
}
public static long qpow(long a, long n){
long ret = 1l;
while(n > 0){
if((n & 1) == 1) ret = ret * a % mod;
n >>= 1;
a = a * a % mod;
}
return ret;
}
public static class unionFind {
int[] parent;
int[] size;
int n;
public unionFind(int n){
this.n = n;
parent = new int[n+1];
size = new int[n+1];
for(int i=1;i<=n;i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int p){
while(p != parent[p]){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public void union(int p, int q){
int root_p = find(p);
int root_q = find(q);
if(root_p == root_q) return;
if(size[root_p] >= size[root_q]){
parent[root_q] = root_p;
size[root_p] += size[root_q];
size[root_q] = 0;
}
else{
parent[root_p] = root_q;
size[root_q] += size[root_p];
size[root_p] = 0;
}
n--;
}
public int getCount(){
return n;
}
public int[] getSize(){
return size;
}
}
public static class fReader {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer tokenizer = new StringTokenizer("");
private static String next() throws IOException{
while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {return Integer.parseInt(next());}
public static Long nextLong() throws IOException {return Long.parseLong(next());}
public static double nextDouble() throws IOException {return Double.parseDouble(next());}
public static char nextChar() throws IOException {return next().toCharArray()[0];}
public static String nextString() throws IOException {return next();}
public static String nextLine() throws IOException {return reader.readLine();}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | ac3bd756c90b9e4348cdf876871d820e | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.lang.*;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
/* 10^(7) = 1s.
* ceilVal = (a+b-1) / b */
static final int mod = 1000000007;
static final long temp = 998244353;
static final long MOD = 1000000007;
static final long M = (long)1e9+7;
static class Pair implements Comparable<Pair>
{
int first, second;
public Pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int compareTo(Pair ob)
{
return (int)(first - ob.first);
}
}
static class Tuple implements Comparable<Tuple>
{
long first, second,third;
public Tuple(long first, long second, long third)
{
this.first = first;
this.second = second;
this.third = third;
}
public int compareTo(Tuple o)
{
return (int)(o.third - this.third);
}
}
public static class DSU
{
int count = 0;
int[] parent;
int[] rank;
public DSU(int n)
{
count = n;
parent = new int[n];
rank = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(rank, 1);
}
public int find(int i)
{
return parent[i] < 0 ? i : (parent[i] = find(parent[i]));
}
public void union(int a, int b) //Union Find by Rank
{
a = find(a);
b = find(b);
if(a == b) return;
if(rank[a] < rank[b])
{
parent[a] = b;
}
else if(rank[a] > rank[b])
{
parent[b] = a;
}
else
{
parent[b] = a;
rank[a] = 1 + rank[a];
}
count--;
}
public int countConnected()
{
return count;
}
}
static class Reader
{
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) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] longReadArray(int n) throws IOException {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static long lcm(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static long LongLCM(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
//Count the number of coprime's upto N
public static long phi(long n) //euler totient/phi function
{
long ans = n;
// for(long i = 2;i*i<=n;i++)
// {
// if(n%i == 0)
// {
// while(n%i == 0) n/=i;
// ans -= (ans/i);
// }
// }
//
// if(n > 1)
// {
// ans -= (ans/n);
// }
for(long i = 2;i<=n;i++)
{
if(isPrime(i))
{
ans -= (ans/i);
}
}
return ans;
}
public static long fastPow(long x, long n)
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long powMod(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
{
res = (res * x) % p;
}
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return powMod(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCrModP(long n, long r,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
% p * modInverse(fac[(int)(n - r)], p)
% p)
% p;
}
public static long fact(long n)
{
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (long i = 1; i <= n; i++)
fac[(int)(i)] = fac[(int)(i - 1)] * i;
return fac[(int)(n)];
}
public static long nCr(long n, long k)
{
long ans = 1;
for(long i = 0;i<k;i++)
{
ans *= (n-i);
ans /= (i+1);
}
return ans;
}
//Modular Operations for Addition and Multiplication.
public static long perfomMod(long x)
{
return ((x%M + M)%M);
}
public static long addMod(long a, long b)
{
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long subMod(long a, long b)
{
return perfomMod(perfomMod(a)-perfomMod(b));
}
public static long mulMod(long a, long b)
{
return perfomMod(perfomMod(a)*perfomMod(b));
}
public static boolean isPrime(long n)
{
if(n == 1)
{
return false;
}
//check only for sqrt of the number as the divisors
//keep repeating so only half of them are required. So,sqrt.
for(int i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Long> SieveList(int n)
{
boolean prime[] = new boolean[(int)(n+1)];
Arrays.fill(prime, true);
List<Long> l = new ArrayList<>();
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;
}
}
}
for (long p = 2; p<=n; p++)
{
if (prime[(int)(p)] == true)
{
l.add(p);
}
}
return l;
}
public static int countDivisors(int x)
{
int c = 0;
for(int i = 1;i*i<=x;i++)
{
if(x%i == 0)
{
if(x/i != i)
{
c+=2;
}
else
{
c++;
}
}
}
return c;
}
public static long log2(long n)
{
long ans = (long)(log(n)/log(2));
return ans;
}
public static boolean isPow2(long n)
{
return (n != 0 && ((n & (n-1))) == 0);
}
public static boolean isSq(int x)
{
long s = (long)Math.round(Math.sqrt(x));
return s*s==x;
}
/*
*
* >= <=
0 1 2 3 4 5 6 7
5 5 5 6 6 6 7 7
lower_bound for 6 at index 3 (>=)
upper_bound for 6 at index 6(To get six reduce by one) (<=)
*/
public static int LowerBound(int a[], int x)
{
int l=-1,r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
public static int UpperBound(int a[], int 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;
}
public static void Sort(long[] a)
{
List<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
// Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void ssort(char[] a)
{
List<Character> l = new ArrayList<>();
for (char i : a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void main(String[] args) throws Exception
{
Reader sc = new Reader();
PrintWriter fout = new PrintWriter(System.out);
int tt = sc.nextInt();
fr: while(tt-- > 0)
{
int n = sc.nextInt();
int a = sc.nextInt(), b = sc.nextInt();
long[] arr = sc.longReadArray(n);
long ans = 0;
for(int i = 0;i<n;i++)
{
long rightPresent = n - i;
long leftPresent = (i == 0) ? 0 : arr[i-1];
long distance = arr[i] - leftPresent;
ans += distance * min(rightPresent * b, (a + b));
}
fout.println(ans);
}
fout.close();
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | c893524928a23ca3cdd51558b494e014 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.lang.*;
import java.io.InputStreamReader;
import static java.lang.Math.*;
import static java.lang.System.out;
import java.util.*;
import java.io.File;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
/* 10^(7) = 1s.
* ceilVal = (a+b-1) / b */
static final int mod = 1000000007;
static final long temp = 998244353;
static final long MOD = 1000000007;
static final long M = (long)1e9+7;
static class Pair implements Comparable<Pair>
{
int first, second;
public Pair(int first, int second)
{
this.first = first;
this.second = second;
}
public int compareTo(Pair ob)
{
return (int)(first - ob.first);
}
}
static class Tuple implements Comparable<Tuple>
{
long first, second,third;
public Tuple(long first, long second, long third)
{
this.first = first;
this.second = second;
this.third = third;
}
public int compareTo(Tuple o)
{
return (int)(o.third - this.third);
}
}
public static class DSU
{
int count = 0;
int[] parent;
int[] rank;
public DSU(int n)
{
count = n;
parent = new int[n];
rank = new int[n];
Arrays.fill(parent, -1);
Arrays.fill(rank, 1);
}
public int find(int i)
{
return parent[i] < 0 ? i : (parent[i] = find(parent[i]));
}
public void union(int a, int b) //Union Find by Rank
{
a = find(a);
b = find(b);
if(a == b) return;
if(rank[a] < rank[b])
{
parent[a] = b;
}
else if(rank[a] > rank[b])
{
parent[b] = a;
}
else
{
parent[b] = a;
rank[a] = 1 + rank[a];
}
count--;
}
public int countConnected()
{
return count;
}
}
static class Reader
{
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) throws IOException {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] longReadArray(int n) throws IOException {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public static int gcd(int a, int b)
{
if(b == 0)
return a;
else
return gcd(b,a%b);
}
public static long lcm(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
public static long LongGCD(long a, long b)
{
if(b == 0)
return a;
else
return LongGCD(b,a%b);
}
public static long LongLCM(long a, long b)
{
return (a / LongGCD(a, b)) * b;
}
//Count the number of coprime's upto N
public static long phi(long n) //euler totient/phi function
{
long ans = n;
// for(long i = 2;i*i<=n;i++)
// {
// if(n%i == 0)
// {
// while(n%i == 0) n/=i;
// ans -= (ans/i);
// }
// }
//
// if(n > 1)
// {
// ans -= (ans/n);
// }
for(long i = 2;i<=n;i++)
{
if(isPrime(i))
{
ans -= (ans/i);
}
}
return ans;
}
public static long fastPow(long x, long n)
{
if(n == 0)
return 1;
else if(n%2 == 0)
return fastPow(x*x,n/2);
else
return x*fastPow(x*x,(n-1)/2);
}
public static long powMod(long x, long y, long p)
{
long res = 1;
x = x % p;
while (y > 0)
{
if (y % 2 == 1)
{
res = (res * x) % p;
}
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long modInverse(long n, long p)
{
return powMod(n, p - 2, p);
}
// Returns nCr % p using Fermat's little theorem.
public static long nCrModP(long n, long r,long p)
{
if (n<r)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
% p * modInverse(fac[(int)(n - r)], p)
% p)
% p;
}
public static long fact(long n)
{
long[] fac = new long[(int)(n) + 1];
fac[0] = 1;
for (long i = 1; i <= n; i++)
fac[(int)(i)] = fac[(int)(i - 1)] * i;
return fac[(int)(n)];
}
public static long nCr(long n, long k)
{
long ans = 1;
for(long i = 0;i<k;i++)
{
ans *= (n-i);
ans /= (i+1);
}
return ans;
}
//Modular Operations for Addition and Multiplication.
public static long perfomMod(long x)
{
return ((x%M + M)%M);
}
public static long addMod(long a, long b)
{
return perfomMod(perfomMod(a)+perfomMod(b));
}
public static long subMod(long a, long b)
{
return perfomMod(perfomMod(a)-perfomMod(b));
}
public static long mulMod(long a, long b)
{
return perfomMod(perfomMod(a)*perfomMod(b));
}
public static boolean isPrime(long n)
{
if(n == 1)
{
return false;
}
//check only for sqrt of the number as the divisors
//keep repeating so only half of them are required. So,sqrt.
for(int i = 2;i*i<=n;i++)
{
if(n%i == 0)
{
return false;
}
}
return true;
}
public static List<Long> SieveList(int n)
{
boolean prime[] = new boolean[(int)(n+1)];
Arrays.fill(prime, true);
List<Long> l = new ArrayList<>();
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;
}
}
}
for (long p = 2; p<=n; p++)
{
if (prime[(int)(p)] == true)
{
l.add(p);
}
}
return l;
}
public static int countDivisors(int x)
{
int c = 0;
for(int i = 1;i*i<=x;i++)
{
if(x%i == 0)
{
if(x/i != i)
{
c+=2;
}
else
{
c++;
}
}
}
return c;
}
public static long log2(long n)
{
long ans = (long)(log(n)/log(2));
return ans;
}
public static boolean isPow2(long n)
{
return (n != 0 && ((n & (n-1))) == 0);
}
public static boolean isSq(int x)
{
long s = (long)Math.round(Math.sqrt(x));
return s*s==x;
}
/*
*
* >= <=
0 1 2 3 4 5 6 7
5 5 5 6 6 6 7 7
lower_bound for 6 at index 3 (>=)
upper_bound for 6 at index 6(To get six reduce by one) (<=)
*/
public static int LowerBound(int a[], int x)
{
int l=-1,r=a.length;
while(l+1<r)
{
int m=(l+r)>>>1;
if(a[m]>=x) r=m;
else l=m;
}
return r;
}
public static int UpperBound(int a[], int 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;
}
public static void Sort(long[] a)
{
List<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
// Collections.reverse(l); //Use to Sort decreasingly
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void ssort(char[] a)
{
List<Character> l = new ArrayList<>();
for (char i : a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
public static void main(String[] args) throws Exception
{
Reader sc = new Reader();
PrintWriter fout = new PrintWriter(System.out);
int tt = sc.nextInt();
fr: while(tt-- > 0)
{
int n = sc.nextInt();
long a = sc.nextLong(), b = sc.nextLong();
long[] kingdom = new long[n+1];
long ans = 0, nextDest = 0;
kingdom[0] = 0;
for(int i = 1;i<=n;i++) kingdom[i] = sc.nextLong();
for(int i = 1;i<=n;i++)
{
ans += (b * (kingdom[i] - nextDest));
if(a <= (n - i) * b)
{
ans += (a * (kingdom[i] - nextDest));
nextDest = kingdom[i];
}
}
fout.println(ans);
}
fout.close();
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | c1635c1824b1dd09bf72777f888aa0fd | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.io.*;
import java.util.*;
public class C {
static class Pair
{
int f;int s; //
Pair(){}
Pair(int f,int s){ this.f=f;this.s=s;}
}
static class Fast {
BufferedReader br;
StringTokenizer st;
public Fast() {
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());
}
int[] readArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readArray1(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine().trim();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
/* static long noOfDivisor(long a)
{
long count=0;
long t=a;
for(long i=1;i<=(int)Math.sqrt(a);i++)
{
if(a%i==0)
count+=2;
}
if(a==((long)Math.sqrt(a)*(long)Math.sqrt(a)))
{
count--;
}
return count;
}*/
static boolean isPrime(long a) {
for (long i = 2; i <= (long) Math.sqrt(a); i++) {
if (a % i == 0)
return false;
}
return true;
}
static void primeFact(int n) {
int temp = n;
HashMap<Integer, Integer> h = new HashMap<>();
for (int i = 2; i * i <= n; i++) {
if (temp % i == 0) {
int c = 0;
while (temp % i == 0) {
c++;
temp /= i;
}
h.put(i, c);
}
}
if (temp != 1)
h.put(temp, 1);
}
static void reverseArray(int a[]) {
int n = a.length;
for (int i = 0; i < n / 2; i++) {
a[i] = a[i] ^ a[n - i - 1];
a[n - i - 1] = a[i] ^ a[n - i - 1];
a[i] = a[i] ^ a[n - i - 1];
}
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sort(long [] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static long abs(long a)
{
return a>0?a:-1*a;
}
static long min(long a,long b)
{
return a>b?b:a;
}
static long rec(int capi,long cap,int idx,int a,int b,long x[],int n,int vis[],long dp[][])
{
if(idx==n)
return 0;
if(dp[capi+1][idx]!=-1)
return dp[capi+1][idx];
long l=Long.MAX_VALUE;
long r = b * abs(cap - x[idx]) + rec(capi,cap, idx + 1, a, b, x, n, vis,dp);//conquer
for(int i=capi+1;i<=idx;i++) {
// if (vis[idx] != -1) {
if(i>=n)
break;
if(i!=idx)
l = min(r,a * abs(cap - x[i]) + rec(i,x[i], idx+1, a, b, x, n, vis,dp));//capital
else
l = min(r,b * abs(cap - x[idx])+a * abs(cap - x[i]) + rec(i,x[i], idx+1, a, b, x, n, vis,dp));//capital
// }
}
return dp[capi+1][idx]=min(l,r);
}
public static void main(String args[]) throws IOException {
Fast sc = new Fast();
PrintWriter out = new PrintWriter(System.out);
int t1 = sc.nextInt();
while (t1-- > 0) {
int n=sc.nextInt();int a=sc.nextInt();int b=sc.nextInt();
long x[]=new long[n+1];x[0]=0;
for(int i=1;i<n+1;i++)
x[i]=sc.nextLong();
long pre[]=new long[n+1];
pre[0]=x[0];
for(int i=1;i<n+1;i++)
pre[i]=pre[i-1]+x[i];
long ans=Long.MAX_VALUE;
for(int i=0;i<n+1;i++)
{
long exp=a*x[i]+b*(pre[n]-(((n-i-1)*x[i])+pre[i]));
// out.println(exp+" "+i);
ans=min(ans,exp);
}
out.println(ans);
}
out.close();
}
}
/* public static void main(String args[]) throws IOException {
Fast sc = new Fast();
PrintWriter out = new PrintWriter(System.out);
int t1 = sc.nextInt();
while (t1-- > 0) {
int n=sc.nextInt();int a=sc.nextInt();int b=sc.nextInt();
long x[]=sc.readArray1(n);
int vis[]=new int[n];
Arrays.fill(vis,-1);
long dp[][]=new long[n+2][n+2];
for(long row[]:dp)
Arrays.fill(row,-1);
long ans=rec(-1,0,0,a,b,x,n,vis,dp);
out.println(ans);
}
out.close();
}
}
*/ | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 9bb25739c53722574db36acb263f0714 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces782{
static long mod = 1000000007L;
static MyScanner sc = new MyScanner();
static void solve() {
int n = sc.nextInt();
int r = sc.nextInt();
int b = sc.nextInt();
int arr[] = new int[2 * b + 1];
Arrays.fill(arr, 1);
r = r - b;
r--;
while (r > 0)
for (int i = 0; i < arr.length&& r > 0; i += 2) {
arr[i]++;
r--;
}
for(int i = 0;i<arr.length;i++){
if(i==0 || i%2==0){
for(int j = 0;j<arr[i];j++){
out.print('R');
}
}else{
out.print('B');
}
}
out.println();
}
static void solve2(){
int n = sc.nextInt();
int k = sc.nextInt();
char arr[] = sc.nextLine().toCharArray();
if(k%2!=0){
for(int i = 0;i<n;i++){
if(arr[i]=='1') arr[i] = '0';
else arr[i]='1';
}
}
int ans[] = new int[n];
for(int i = 0;i<n && k>0;i++){
if(arr[i]=='0'){
ans[i]++;
k--;
arr[i] = '1';
}
}
if(k>0){
if(k%2!=0){
if(arr[n-1]=='1') arr[n-1] = '0';
else arr[n-1]='1';
}
ans[n-1]+=k;
}
for(int i = 0;i<n;i++){
out.print(arr[i]);
}
out.println();
for(int i = 0;i<n;i++){
out.print(ans[i]+" ");
}
out.println();
}
static void solve3(){
int n= sc.nextInt();
long a = sc.nextLong();
long b = sc.nextLong();
long arr[] = new long[n+1];
for(int i=1;i<=n;i++) arr[i]=sc.nextLong();
for(int i=1;i<=n;i++) arr[i]+=arr[i-1];
long res=arr[n]*b;
for(int i=1;i<n;i++){
long d=arr[i]-arr[i-1];
long rem=n-i;
long val=b*((arr[n]-arr[i])-(rem*d))+d*(a+b);
//out.println(i+" "+val+" "+res);
res=Math.min(res,val);
}
out.println(res);
}
static long fun(long dp[][],long arr[],int i,int j,long a,long b){
if(j==arr.length) return 0;
if(dp[i][j]!=-1) return dp[i][j];
if(i+1==j){
dp[i][j] = b*(arr[j]-arr[i])+fun(dp,arr,i,j+1,a,b);
}else
dp[i][j] = Math.min(b*(arr[j]-arr[i])+fun(dp,arr,i,j+1,a,b),(a*(arr[i+1]-arr[i]))+fun(dp,arr,i+1,j,a,b));
return dp[i][j];
}
static void solve4(){
}
static void reverse(int arr[]){
int i = 0;int j = arr.length-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
static class Pair{
int odd;
int even;
Pair(int o,int e){
odd = o;
even = e;
}
}
static long pow(long a, long b) {
if (b == 0) return 1;
long res = pow(a, b / 2);
res = (res * res) % 1_000_000_007;
if (b % 2 == 1) {
res = (res * a) % 1_000_000_007;
}
return res;
}
static int lis(int arr[],int n){
int lis[] = new int[n];
lis[0] = 1;
for(int i = 1;i<n;i++){
lis[i] = 1;
for(int j = 0;j<i;j++){
if(arr[i]>arr[j]){
lis[i] = Math.max(lis[i],lis[j]+1);
}
}
}
int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
max = Math.max(lis[i],max);
}
return max;
}
static boolean isPali(String str){
int i = 0;
int j = str.length()-1;
while(i<j){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static boolean isprime(int n){
if(n==1) return false;
if(n==3 || n==2) return true;
if(n%2==0 || n%3==0) return false;
for(int i = 5;i*i<=n;i+= 6){
if(n%i== 0 || n%(i+2)==0){
return false;
}
}
return true;
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
// solve();
// solve2();
solve3();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------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[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
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;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | da30d5f3c2af03713aab63e6f891b4f9 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class CodeForces {
/*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/
public static void solve(int tCase) throws IOException {
int n = sc.nextInt();
long a = sc.nextInt();
long b = sc.nextInt();
long[] arr = new long[n+3];
for(int i=1;i<=n;i++)arr[i] = sc.nextInt();
long[] future = new long[n+10];
for(int i=n-1;i>=0;i--){
future[i] = future[i+1];
long del = arr[i+1] - arr[i];
future[i] += b * (n-i)*del;
}
long[] past = new long[n+10];
for(int i=1;i<=n;i++){
past[i] = past[i-1];
past[i] += (a+b) *(arr[i] - arr[i-1]);
}
long ans = inf_long;
for(int i=0;i<n;i++)
ans = Math.min(ans,future[i] + past[i]);
out.println(ans);
}
public static void main(String[] args) throws IOException {
openIO();
int testCase = 1;
testCase = sc.nextInt();
for (int i = 1; i <= testCase; i++) solve(i);
closeIO();
}
/*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/
/*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/
// public static int mod = (int) 1e9 + 7;
public static int mod = 998244353;
public static int inf_int = (int) 2e9+10;
public static long inf_long = (long) 2e18;
public static void _sort(int[] arr, boolean isAscending) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int ele : arr) list.add(ele);
Collections.sort(list);
if (!isAscending) Collections.reverse(list);
for (int i = 0; i < n; i++) arr[i] = list.get(i);
}
public static void _sort(long[] arr, boolean isAscending) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (long ele : arr) list.add(ele);
Collections.sort(list);
if (!isAscending) Collections.reverse(list);
for (int i = 0; i < n; i++) arr[i] = list.get(i);
}
// time : O(1), space : O(1)
public static int _digitCount(long num,int base){
// this will give the # of digits needed for a number num in format : base
return (int)(1 + Math.log(num)/Math.log(base));
}
// time : O(n), space: O(n)
public static long _fact(int n){
// simple factorial calculator
long ans = 1;
for(int i=2;i<=n;i++)
ans = ans * i % mod;
return ans;
}
// time for pre-computation of factorial and inverse-factorial table : O(nlog(mod))
public static long[] factorial , inverseFact;
public static void _ncr_precompute(int n){
factorial = new long[n+1];
inverseFact = new long[n+1];
factorial[0] = inverseFact[0] = 1;
for (int i = 1; i <=n; i++) {
factorial[i] = (factorial[i - 1] * i) % mod;
inverseFact[i] = _modExpo(factorial[i], mod - 2);
}
}
// time of factorial calculation after pre-computation is O(1)
public static int _ncr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod);
}
public static int _npr(int n,int r){
if(r > n)return 0;
return (int)(factorial[n] * inverseFact[n - r] % mod);
}
// euclidean algorithm time O(max (loga ,logb))
public static long _gcd(long a, long b) {
while (a>0){
long x = a;
a = b % a;
b = x;
}
return b;
// if (a == 0)
// return b;
// return _gcd(b % a, a);
}
// lcm(a,b) * gcd(a,b) = a * b
public static long _lcm(long a, long b) {
return (a / _gcd(a, b)) * b;
}
// binary exponentiation time O(logn)
public static long _modExpo(long x, long n) {
long ans = 1;
while (n > 0) {
if ((n & 1) == 1) {
ans *= x;
ans %= mod;
n--;
} else {
x *= x;
x %= mod;
n >>= 1;
}
}
return ans;
}
// function to find a/b under modulo mod. time : O(logn)
public static long _modInv(long a,long b){
return (a * _modExpo(b,mod-2)) % mod;
}
//sieve or first divisor time : O(mx * log ( log (mx) ) )
public static int[] _seive(int mx){
int[] firstDivisor = new int[mx+1];
for(int i=0;i<=mx;i++)firstDivisor[i] = i;
for(int i=2;i*i<=mx;i++)
if(firstDivisor[i] == i)
for(int j = i*i;j<=mx;j+=i)
if(firstDivisor[j]==j)firstDivisor[j] = i;
return firstDivisor;
}
// check if x is a prime # of not. time : O( n ^ 1/2 )
private static boolean _isPrime(long x){
for(long i=2;i*i<=x;i++)
if(x%i==0)return false;
return true;
}
static class Pair<K, V>{
K ff;
V ss;
public Pair(K ff, V ss) {
this.ff = ff;
this.ss = ss;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) return false;
Pair<?, ?> pair = (Pair<?, ?>) o;
return ff.equals(pair.ff) && ss.equals(pair.ss);
}
@Override
public int hashCode() {
return Objects.hash(ff, ss);
}
@Override
public String toString(){
return ff.toString()+" "+ss.toString();
}
}
/*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/
/*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/
static FastestReader sc;
static PrintWriter out;
private static void openIO() throws IOException {
sc = new FastestReader();
out = new PrintWriter(System.out);
}
public static void closeIO() throws IOException {
out.flush();
out.close();
sc.close();
}
private static final class FastestReader {
private static final int BUFFER_SIZE = 1 << 16;
private final DataInputStream din;
private final byte[] buffer;
private int bufferPointer, bytesRead;
public FastestReader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastestReader(String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
private static boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() throws IOException {
int b;
//noinspection StatementWithEmptyBody
while ((b = read()) != -1 && isSpaceChar(b)) {}
return b;
}
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');
return neg?-ret: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');
return neg?-ret: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);
return neg?-ret:ret;
}
public String nextLine() throws IOException {
final byte[] buf = new byte[(1<<10)]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
break;
}
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
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();
}
}
/*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/
}
/** Some points to keep in mind :
* 1. don't use Arrays.sort(primitive data type array)
* 2. try to make the parameters of a recursive function as less as possible,
* more use static variables.
*
**/ | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | c2a3f3e4359f3e0204751ab8e0f21bee | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
try{
FastReader read=new FastReader();
StringBuilder sb = new StringBuilder();
int t=read.nextInt();
while(t>0)
{
int n = read.nextInt();
long move = read.nextLong();
long acq = read.nextLong();
long x[] = new long[n+1];
long ans = 0,ans2=Long.MAX_VALUE;
for(int i=1;i<=n;i++)
{
x[i]=read.nextLong();
ans+=x[i];
}
for(int i=0;i<=n;i++)
{
long dono = (long)(move+acq)*(long)(x[i]);
ans-=x[i];
dono+=(ans-(n-i)*x[i])*acq;
ans2 = Math.min(ans2,dono);
}
sb.append(ans2);
sb.append('\n');
t--;
}
System.out.println(sb);
}
catch(Exception e)
{return;
}
}
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 void sort(int[] A) {
int n = A.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = A[i];
int randomPos = i + rnd.nextInt(n - i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A) {
int n = A.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = A[i];
int randomPos = i + rnd.nextInt(n - i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(ArrayList<Long> A,char ch) {
int n = A.size();
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = A.get(i);
int randomPos = i + rnd.nextInt(n - i);
A.set(i,A.get(randomPos));
A.set(randomPos,tmp);
}
Collections.sort(A);
}
static void sort(ArrayList<Integer> A) {
int n = A.size();
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = A.get(i);
int randomPos = i + rnd.nextInt(n - i);
A.set(i,A.get(randomPos));
A.set(randomPos,tmp);
}
Collections.sort(A);
}
static String sort(String s) {
Character ch[] = new Character[s.length()];
for (int i = 0; i < s.length(); i++) {
ch[i] = s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st = new StringBuffer("");
for (int i = 0; i < s.length(); i++) {
st.append(ch[i]);
}
return st.toString();
}
}
class pair implements Comparable<pair>
{
int X,Y,C;
pair(int s,int e,int c)
{
X=s;
Y=e;
C=c;
}
public int compareTo(pair p )
{
return this.C-p.C;
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | d3121bbf29ad7c227bba0a4c3f2dc718 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class problemC {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(br.readLine());
for(int i=0; i<t; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int[] arr = new int[n+1];
st = new StringTokenizer(br.readLine());
for(int j=1; j<=n; j++)
arr[j] = Integer.parseInt(st.nextToken());
long joyce_qu = (long)arr[n-1]*a;
joyce_qu += (long)b*(arr[n]);
for(int j=n-2; j>-1; j--) {
long add = 0;
add+=(long)(arr[j+1]-arr[j])*b*(n-2-j+1);
add-=(long)(arr[j+1]-arr[j])*a;
if(add>0)break;
joyce_qu+=add;
}
out.write(joyce_qu+"\n");
}
out.flush();
out.close();
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | ccf8d9c479e476693b472e4f6748de7a | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class C {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int t = Integer.parseInt(br.readLine());
while(t-->0)
{
String[] in = br.readLine().split(" ");
int n = Integer.parseInt(in[0]);
int a = Integer.parseInt(in[1]);
int b = Integer.parseInt(in[2]);
in = br.readLine().split(" ");
long [] arr = new long[in.length];
for(int i=0;i<arr.length;i++)
arr[i] = Long.parseLong(in[i]);
long ans=0;
long cur = 0;
for(int i=0;i<arr.length;i++)
{
long move = arr[i]-cur;
long rem = arr.length-i-1;
ans += b*(move);
if(move*a < move*b*rem)
{
ans = ans+move*a;
cur = arr[i];
}
}
pw.println(ans);
}
pw.flush();
pw.close();
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 7dc2ed92a9d409997bfc8b6e768f23c5 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.io.BufferedOutputStream;
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.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class Practice3 {
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
public static void find(int[][] arr,int i,int j,int sum) {
System.out.println("sum : "+sum);
if(i==0&&j==0) {
arr[i][j] -=sum;
return;
}
if(arr[i][j]<=0) return;
if(i==0) {
arr[i][j] +=sum;
int a=Math.min(arr[i][j-1],arr[i][j]);
arr[i][j-1]-=a;
arr[i][j]-=a+sum;
find(arr,i,j-1,sum+a);
}else if(j==0) {
arr[i][j] +=sum;
int a=Math.min(arr[i-1][j],arr[i][j]);
arr[i-1][j]-=a;
arr[i][j]-=a+sum;
find(arr,i-1,j,sum+a);
}else {
if(arr[i-1][j]!=0) {
arr[i][j] +=sum;
int a=Math.min( arr[i-1][j],arr[i][j]);
arr[i-1][j]-=a;
arr[i][j]-=a+sum;
find(arr,i-1,j,sum+a);
}
if(arr[i][j-1]!=0) {
arr[i][j] +=sum;
int a=Math.min( arr[i][j-1],arr[i][j]);
arr[i][j-1]-=a;
arr[i][j]-=a+sum;
find(arr,i,j-1,sum+a);
}
}
}
static long[] sort(long[] arr) {
int n=arr.length;
ArrayList<Long> al=new ArrayList<>();
for(int i=0;i<n;i++) {
al.add(arr[i]);
}
Collections.sort(al);
for(int i=0;i<n;i++) {
arr[i]=al.get(i);
}
return arr;
}
public static void main (String[] args) {
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
// out.print();
//out.println();
FastReader sc=new FastReader();
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt();
long a=sc.nextLong();
long b=sc.nextLong();
long[] arr=new long[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextLong();
}
long sum=0,val=0;
sum +=b*arr[0];
for(int i=0;i<n-1;i++) {
if(b*((long)n-(long)i-1l)*(arr[i]-val)>=a*(arr[i]-val)){
sum +=a*Math.abs(val-arr[i])+b*Math.abs(arr[i]-arr[i+1]);
val=arr[i];
}else {
sum +=b*Math.abs(val-arr[i+1]);
}
}
out.println(sum);
}
out.close();
}
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 | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 25ef828a98cefd591b9bc1a3ac1413b1 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.function.Supplier;
public class Solution {
private void solve() throws IOException {
int n = nextInt();
long a = nextLong();
long b = nextLong();
int[] x = new int[n + 1];
for (int i = 0; i < n; i++) {
x[i + 1] = nextInt();
}
long[] d = new long[n + 1];
for (int i = 1; i < n; i++) {
d[i] = d[i - 1] + (a + b) * (x[i] - x[i - 1]);
}
long res = Long.MAX_VALUE;
long dd = 0;
for (int i = n - 1; i >= 0; i--) {
dd += (n - i) * (long) (x[i+1] - x[i]) * b;
res = Math.min(res, d[i] + dd);
}
out.println(res);
}
private static final boolean runNTestsInProd = true;
private static final boolean printCaseNumber = false;
private static final boolean assertInProd = false;
private static final boolean logToFile = false;
private static final boolean readFromConsoleInDebug = false;
private static final boolean writeToConsoleInDebug = true;
private static final boolean testTimer = false;
private static Boolean isDebug = null;
private BufferedReader in;
private StringTokenizer line;
private PrintWriter out;
public static void main(String[] args) throws Exception {
isDebug = Arrays.asList(args).contains("DEBUG_MODE");
if (isDebug) {
log = logToFile ? new PrintWriter("logs/j_solution_" + System.currentTimeMillis() + ".log") : new PrintWriter(System.out);
clock = Clock.systemDefaultZone();
}
new Solution().run();
}
private void run() throws Exception {
in = new BufferedReader(new InputStreamReader(!isDebug || readFromConsoleInDebug ? System.in : new FileInputStream("input.txt")));
out = !isDebug || writeToConsoleInDebug ? new PrintWriter(System.out) : new PrintWriter("output.txt");
try (Timer totalTimer = new Timer("total")) {
int t = runNTestsInProd || isDebug ? nextInt() : 1;
for (int i = 0; i < t; i++) {
if (printCaseNumber) {
out.print("Case #" + (i + 1) + ": ");
}
if (testTimer) {
try (Timer testTimer = new Timer("test #" + (i + 1))) {
solve();
}
} else {
solve();
}
if (isDebug) {
out.flush();
}
}
}
in.close();
out.flush();
out.close();
}
private void println(Object... objects) {
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst) {
out.print(" ");
} else {
isFirst = false;
}
out.print(o.toString());
}
out.println();
}
private int[] nextIntArray(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
private long[] nextLongArray(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private char[] nextTokenChars() throws IOException {
return nextToken().toCharArray();
}
private String nextToken() throws IOException {
while (line == null || !line.hasMoreTokens()) {
line = new StringTokenizer(in.readLine());
}
return line.nextToken();
}
private static void assertPredicate(boolean p) {
if ((isDebug || assertInProd) && !p) {
throw new RuntimeException();
}
}
private static void assertPredicate(boolean p, String message) {
if ((isDebug || assertInProd) && !p) {
throw new RuntimeException(message);
}
}
private static <T> void assertNotEqual(T unexpected, T actual) {
if ((isDebug || assertInProd) && Objects.equals(actual, unexpected)) {
throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual);
}
}
private static <T> void assertEqual(T expected, T actual) {
if ((isDebug || assertInProd) && !Objects.equals(actual, expected)) {
throw new RuntimeException("assertEqual: " + expected + " != " + actual);
}
}
private static PrintWriter log = null;
private static Clock clock = null;
private static void log(Object... objects) {
log(true, objects);
}
private static void logNoDelimiter(Object... objects) {
log(false, objects);
}
private static void log(boolean printDelimiter, Object[] objects) {
if (isDebug) {
StringBuilder sb = new StringBuilder();
sb.append(LocalDateTime.now(clock)).append(" - ");
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst && printDelimiter) {
sb.append(" ");
} else {
isFirst = false;
}
sb.append(o.toString());
}
log.println(sb);
log.flush();
}
}
private static class Timer implements Closeable {
private final String label;
private final long startTime = isDebug ? System.nanoTime() : 0;
public Timer(String label) {
this.label = label;
}
@Override
public void close() throws IOException {
if (isDebug) {
long executionTime = System.nanoTime() - startTime;
String fraction = Long.toString(executionTime / 1000 % 1_000_000);
logNoDelimiter("Timer[", label, "]: ", executionTime / 1_000_000_000, '.', "00000".substring(0, 6 - fraction.length()), fraction, 's');
}
}
}
private static <T> T timer(String label, Supplier<T> f) throws Exception {
if (isDebug) {
try (Timer timer = new Timer(label)) {
return f.get();
}
} else {
return f.get();
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 046bdb789e10725528c1be04c7df69b3 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | //package kg.my_algorithms.Codeforces;
import java.util.*;
import java.io.*;
// If you know the reason for your existence, then act on it.
public class Solution {
private static final FastReader fr = new FastReader();
private static final long mod = 1_000_000_007L;
public static void main(String[] args) throws IOException {
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
int testCases = fr.nextInt();
for(int testCase=1;testCase<=testCases;testCase++){
int n = fr.nextInt();
long a = fr.nextLong();
long b = fr.nextLong();
long sum = 0L;
long[] arr = new long[n];
for(int i=0;i<n;i++){
long num = fr.nextLong();
arr[i] = num;
sum += num;
}
long min = b*sum;
long[] suffix = new long[n];
for(int i=n-2;i>=0;i--){
suffix[i] = suffix[i+1]+arr[i+1];
}
for(int i=0;i<n-1;i++){
int rem = n-i-1;
min = Math.min(min,(a+b)*arr[i]+b*(suffix[i]-rem*arr[i]));
}
sb.append(min).append("\n");
}
output.write(sb.toString());
output.flush();
}
}
//Fast Input
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
if(st.hasMoreTokens()){
str = st.nextToken("\n");
}
else{
str = br.readLine();
}
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 10b7f9a98f7b1e7160b73db6adc23254 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class LineEmpire {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t!=0){
solve(br,pr);
t--;
}
pr.flush();
pr.close();
}
public static void solve(BufferedReader br,PrintWriter pr) throws IOException{
String[] temp=br.readLine().split(" ");
int n=Integer.parseInt(temp[0]);
long a=Integer.parseInt(temp[1]);
long b=Integer.parseInt(temp[2]);
temp=br.readLine().split(" ");
int[] nums=new int[n];
for(int i=0;i<n;i++){
nums[i]=Integer.parseInt(temp[i]);
}
long[] preSum=new long[n+1];
for(int i=0;i<n;i++){
preSum[i+1]=preSum[i]+nums[i];
}
long res=b*preSum[n];
for(int i=0;i<n;i++){
long x=nums[i];
long rightCount=n-i-1;
long tempCost=x*b+x*a+((preSum[n]-preSum[i+1])-rightCount*x)*b;
res=Math.min(res,tempCost);
}
pr.println(res);
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 94a5032cc39d669356cccbf5320446b4 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | //package com.example.lib;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
public class Algorithm {
static int ans =0;
static boolean found= false;
public static void main(String[] rgs) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
// BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\USER\\AndroidStudioProjects\\MyApplication\\lib\\src\\main\\java\\com\\example\\lib\\inpu"));
StringBuilder stringBuilder = new StringBuilder();
int t = Integer.parseInt(bufferedReader.readLine());
for (int i = 0; i < t; i++) {
String [] in= bufferedReader.readLine().split(" ");
long conquer= Long.parseLong(in[2]);
long move= Long.parseLong(in[1]);
String [] s= bufferedReader.readLine().split(" ");
int [] input= new int[s.length];
for (int j = 0; j < s.length; j++) {
input[j]= Integer.parseInt(s[j]);
}
long cost=0;
int presentcapital= 0;
cost= conquer*input[0];
for (int j = 1; j < input.length; j++) {
int cur= input[j];
long costofcapitalmovement= move*(input[j-1]-presentcapital);
long othercost= conquer*(input[j-1]-presentcapital)*(input.length-j);
if(costofcapitalmovement<othercost){
presentcapital=input[j-1];
cost+=costofcapitalmovement;
}
cost+=conquer*(cur-presentcapital);
}
stringBuilder.append(cost);
stringBuilder.append("\n");
}
System.out.println(stringBuilder);
}
static int []addcol= {-1,0,1,0};
static int []addrow= {0,1,0,-1};
static int size =0;
private static int dfs(int row, int col, int[][] grid, boolean[][] visited) {
// if()
if(visited[row][col])return 0;
visited[row][col]=true;
int siz=1;
int cur= grid[row][col];
for (int i = 0; i < 4; i++) {
int anding = 1 <<i;
int andrem= anding & cur;
if(andrem == 0){
siz+=dfs(row+addrow[i],col+addcol[i],grid,visited);
}
}
return siz;
}
private static boolean palpossible(int bigsum, int mid, int k, int lengthofletter) {
if(mid%2==0){
return bigsum>=(mid/2)*k;
}else {
int temp= (mid/2)*k;
return bigsum>=temp && lengthofletter - (temp*2) >=k;
}
}
private static void gen(List<Integer> powetrs, List<Integer> comb, int sumSoFar, int pos) {
if(pos==powetrs.size()){
if(sumSoFar==0)return;
comb.add(sumSoFar);
return;
}
gen(powetrs,comb,sumSoFar+powetrs.get(pos),pos+1);
gen(powetrs,comb,sumSoFar,pos+1);
}
private static int gcd(int fir, int sec) {
if(fir==0)return sec;
if(fir>sec)return gcd(fir%sec,sec);
return gcd(sec%fir,fir);
}
// private static void rec(List<Integer> primes, int position, int productsofar, HashSet<Integer> divisors) {
// if(position==primes.size()){
// divisors.add(productsofar);
// return;
// }
// rec(primes,position+1,productsofar*primes.get(position),divisors);
// rec(primes,position+1,productsofar,divisors);
//
// }
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 50241970af3be09cb51ff9b027d22bb3 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | /*
* Author: rickytsung(En Chi Tsung)
* Date: 2022/7/30
* Problem: CF Round 782
*/
import java.util.*;
import java.time.*;
import java.io.*;
import java.math.*;
public class Main {
public static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public static BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
public static long ret,ans=0,x,y;
public static int reti,rd;
public static boolean neg;
public static final int mod=998244353,mod2=1_000_000_007,ma=600005;
public static Useful us=new Useful(mod);
public static int[] A=new int[ma];
public static int[] Aans=new int[ma];
public static int[] B=new int[ma];
public static int[] Bans=new int[ma];
public static void main(String[] args) throws Exception{
//String [] in=br.readLine().split(" ");
final int t=readint();
for(int z=0;z<t;z++) {
final int n=readint();
final long a=readint();
final long b=readint();
A[0]=0;
for(int i=1;i<=n;i++) {
A[i]=readint();
}
int r=n,l=0;
long ans=0;
for(int i=1;i<=n;i++) {
ans+=A[i]*b;
}
long cnt=ans;
for(int i=1;i<=n;i++) {
cnt-=r*b*(A[i]-A[i-1]);
cnt+=b*(A[i]-A[i-1]);
r--;
l++;
cnt+=a*(A[i]-A[i-1]);
ans=Math.min(cnt,ans);
}
bw.write(ans+"\n");
}
bw.flush();
}
public static int readint() throws Exception{
reti=0;
neg=false;
while(rd<48||rd>57) {
rd=br.read();
if(rd=='-') {
neg=true;
}
}
while(rd>47&&rd<58) {
reti*=10;
reti+=(rd&15);
rd=br.read();
}
if(neg)reti*=-1;
return reti;
}
public static long readlong() throws Exception{
ret=0;
neg=false;
while(rd<48||rd>57) {
rd=br.read();
if(rd=='-') {
neg=true;
}
}
while(rd>47&&rd<58) {
ret*=10;
ret+=(rd&15);
rd=br.read();
}
if(neg)ret*=-1;
return ret;
}
}
/*
*/
/*
*/
class Pii{
int x,y,z;
Pii(int a,int b,int c){
x=a;
y=b;
z=c;
}
@Override
public boolean equals(Object o) {
if (this==o) return true;
if (!(o instanceof Pii)) return false;
Pii key = (Pii) o;
return x==key.x&&y==key.y;
}
@Override
public int hashCode() {
long result=x;
result=31*result+y;
return (int)(result%998244353);
}
}
class Pll{
long x,y;
Pll(long a,long b){
x=a;
y=b;
}
@Override
public boolean equals(Object o) {
if (this==o) return true;
if (!(o instanceof Pll)) return false;
Pll key = (Pll) o;
return x==key.x&&y==key.y;
}
@Override
public int hashCode() {
long result=x;
result=31*result+y;
return (int)(result%998244353);
}
}
class Useful{
long mod;
Useful(long m){mod=m;}
void al(ArrayList<ArrayList<Integer>> a,int n){for(int i=0;i<n;i++) {a.add(new ArrayList<Integer>());}}
void arr(int[] a,int init) {for(int i=0;i<a.length;i++) {a[i]=init;}}
void arr(long[] a,long init) {for(int i=0;i<a.length;i++) {a[i]=init;}}
void arr(int[][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {a[i][j]=init;}}}
void arr(long[][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {a[i][j]=init;}}}
void arr(int[][][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {a[i][j][k]=init;}}}}
void arr(long[][][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {a[i][j][k]=init;}}}}
void arr(int[][][][] a,int init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {Arrays.fill(a[i][j][k],init);}}}}
void arr(long[][][][] a,long init) {for(int i=0;i<a.length;i++) {for(int j=0;j<a[i].length;j++) {for(int k=0;k<a[i][j].length;k++) {Arrays.fill(a[i][j][k],init);}}}}
long fast(long x,long pw) {
if(pw<=0)return 1;
if(pw==1)return x;
long h=fast(x,pw>>1);
if((pw&1)==0) {
return h*h%mod;
}
return h*h%mod*x%mod;
}
long[][] mul(long[][] a,long[][] b){
long[][] c=new long[a.length][b[0].length];
for(int i=0;i<a.length;i++) {
for(int j=0;j<b[0].length;j++) {
for(int k=0;k<a[0].length;k++) {
c[i][j]+=a[i][k]*b[k][j];
c[i][j]%=mod;
}
}
}
return c;
}
long[][] fast(long[][] x,int pw){
if(pw==1)return x;
long[][] h=fast(x,pw>>1);
if((pw&1)==0) {
return mul(h,h);
}
else {
return mul(mul(h,h),x);
}
}
int gcd(int a,int b) {
if(a==0)return b;
if(b==0)return a;
return gcd(b,a%b);
}
long gcd(long a,long b) {
if(a==0)return b;
if(b==0)return a;
return gcd(b,a%b);
}
long lcm(long a, long b){
return a*(b/gcd(a,b));
}
double log2(int x) {
return (Math.log(x)/Math.log(2));
}
double log2(long x) {
return (Math.log(x)/Math.log(2));
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | f7d5407ae4d21c15a978ba6e4cce245d | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.Scanner;
public class C1659 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t=0; t<T; t++) {
int N = in.nextInt();
long A = in.nextLong();
long B = in.nextLong();
long sum = 0;
int[] X = new int[N];
for (int n=0; n<N; n++) {
int x = in.nextInt();
sum += x;
X[n] = x;
}
long min = sum*B;
for (int n=0; n<N; n++) {
// X[n] is the capital at the end
long prev = (n == 0) ? 0 : X[n-1];
sum -= (N-n)*(X[n]-prev);
long cand = X[n]*(A+B) + sum*B;
min = Math.min(min, cand);
}
System.out.println(min);
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 5d6feb204655f2dd2e5f028c9ab6c098 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.*;
import java.util.*;
public class cp{
public static void main(String[] args)throws Exception{
BufferedReader br=new BufferedReader(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter pw=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t-->0){
String[] str=br.readLine().split(" ");
int n=Integer.parseInt(str[0]);
long a=Long.parseLong(str[1]);
long b=Long.parseLong(str[2]);
String[] str1=br.readLine().split(" ");
long[] arr=new long[n+1];
long[] prefix=new long[n+1];
long min=Long.MAX_VALUE;
for(int i=1;i<=n;i++){
arr[i]=Long.parseLong(str1[i-1]);
prefix[i]=prefix[i-1]+arr[i];
}
for(int i=0;i<=n;i++){
long val=arr[i]*(a+b)+b*(prefix[n]-prefix[i]-arr[i]*(n-i));
if(val<min)min=val;
}
pw.println(min);
}
pw.flush();
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 10533cdd6a0bc195f55ddd843a96f6c5 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class amd {
static int mod=1000000007;
static int n;
static int m;
static List<List<Integer>> g;
public static void main(String[] args){
FastScanner s = new FastScanner();
int t=s.nextInt();
while(t-->0) {
n=s.nextInt();
int a=s.nextInt();
int b=s.nextInt();
long arr[]= new long[n];
long cap[]= new long[n];
long wic[]= new long[n];
long pop[]= new long[n];
for(int i=0;i<n;i++) {
arr[i]=s.nextLong();
cap[i]=arr[i]*a;
wic[i]+=(arr[i])+(i==0?0:wic[i-1]);
}
long ans=(wic[n-1])*b;
// System.out.println(ans);
for(int i=0; i<n;i++) {
long p=(arr[i]*b)+cap[i];
long k=((wic[n-1]-wic[i])-((n-i-1)*arr[i]))*b;
//System.out.println(i+" "+wic[i]+" "+cap[i]+" "+((wic[n-1]-wic[i])-((n-i-1)*arr[i])));
//System.out.println((p+k));
ans=Math.min(ans, (p+k));
}
System.out.println(ans);
}
}
// Amd
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 | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | fe38ab4daa1c139e4756e26b5bccd0ee | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.util.*;
import java.io.*;
import java.math.*;
public class CF1
{
static FastReader sc=new FastReader();
// static long dp[][];
// static boolean v[][][];
// static int mod=998244353;;
// static int mod=1000000007;
static long oset[];
static int oset_p;
static long mod=1000000007;
// static int max;
// static int bit[];
//static long fact[];
//static HashMap<Long,Long> mp;
//static StringBuffer sb=new StringBuffer("");
//static HashMap<Integer,Integer> map;
//static List<Integer>list;
//static int m;
//static StringBuffer sb=new StringBuffer();
// static PriorityQueue<Integer>heap;
//static int dp[];
static PrintWriter out=new PrintWriter(System.out);
static int msg[];
public static void main(String[] args)
{
// StringBuffer sb=new StringBuffer("");
int ttt=1;
ttt =i();
int cccc=1;
outer :while (ttt-- > 0)
{
int n=i();
long a=l();
long b=l();
long arr[]=inputL1(n);
long suffix[]=new long[n+1];
for(int i=n-1;i>=0;i--)
{
suffix[i]=suffix[i+1]+arr[i+1];
}
long min=suffix[0]*b;
long dp[]=new long[n+1];
for(int i=1;i<=n;i++)
{
dp[i]=dp[i-1]+b*(arr[i]-arr[i-1]);
long term=dp[i]+(a*arr[i])+((suffix[i]*b)-(((long)n-(long)i)*arr[i]*b));
// out.print(term+" ");
min=min(term,min);
}
//out.println();
out.println(min);
}
out.close();
}
static boolean helper(int arr[][],int row,int col,int n,int m,boolean dp[][],int sum)
{
if(row<0||row>=n||col<0||col>=m)
return false;
if(row==n-1&&col==m-1)
{
sum+=arr[row][col];
if(sum==0)
return true;
return false;
}
if(dp[row][col])
return false;
if(helper(arr,row+1,col,n,m,dp,sum+arr[row][col]))
return true;
if(helper(arr,row,col+1,n,m,dp,sum+arr[row][col]))
return true;
dp[row][col]=true;
return false;
}
static int setBit(int n)
{
// int c=0;
for(int i=31;i>=0;i--)
{
if((n&(1<<i))!=0)
{
return (i+1);
}
}
return 0;
}
static int dfs(ArrayList<ArrayList<Integer>>adj,boolean visited[],int i,int dp[])
{
int ans=0;
for(int it:adj.get(i))
{
if(!visited[it])
{
visited[it]=true;
ans+=dfs(adj,visited,it,dp);
}
}
ans++;
dp[i]=ans;
return ans;
}
static int findPar(int par[],int x)
{
if(par[x]==x)
return x;
return par[x]=findPar(par,par[x]);
}
static void union(int rank[],int par[],int cost[],int u,int v)
{
int x=findPar(par,u);
int y=findPar(par,v);
// out.println(x+" "+y);
if(x==y)
return;
// out.println("S");
if(rank[x]>rank[y])
{
par[y]=x;
msg[x]=min(msg[x],cost[y]);
}
else
if(rank[y]>rank[x])
{
par[x]=y;
msg[y]=min(msg[y],cost[y]);
}
else
{
par[y]=x;
rank[x]++;
msg[x]=min(msg[x],cost[y]);
// out.println(msg[x]);
}
}
static class Pair implements Comparable<Pair>
{
int x;
int y;
// int z;
Pair(int x,int y){
this.x=x;
this.y=y;
// this.z=z;
}
@Override
public int compareTo(Pair o) {
if(this.x>o.x)
return 1;
else if(this.x<o.x)
return -1;
else {
if(this.y>o.y)
return 1;
else if(this.y<o.y)
return -1;
else
return 0;
}
}
public int hashCode()
{
final int temp = 14;
int ans = 1;
ans =x*31+y*13;
return ans;
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
Pair other = (Pair)o;
if (this.x != other.x || this.y!=other.y) {
return false;
}
return true;
}
//
/* FOR TREE MAP PAIR USE */
// public int compareTo(Pair o) {
// if (x > o.x) {
// return 1;
// }
// if (x < o.x) {
// return -1;
// }
// if (y > o.y) {
// return 1;
// }
// if (y < o.y) {
// return -1;
// }
// return 0;
// }
}
static ArrayList<Long> gLL()
{
return new ArrayList<>();
}
static ArrayList<Integer> gL()
{
return new ArrayList<>();
}
static StringBuffer gsb()
{
return new StringBuffer();
}
static int find(int A[],int a) {
if(A[a]==a)
return a;
return A[a]=find(A, A[a]);
}
//static int find(int A[],int a) {
// if(A[a]==a)
// return a;
// return find(A, A[a]);
//}
//FENWICK TREE
static class BIT{
int bit[];
BIT(int n){
bit=new int[n+1];
}
int lowbit(int i){
return i&(-i);
}
int query(int i){
int res=0;
while(i>0){
res+=bit[i];
i-=lowbit(i);
}
return res;
}
void update(int i,int val){
while(i<bit.length){
bit[i]+=val;
i+=lowbit(i);
}
}
}
//END
static long summation(long A[],int si,int ei)
{
long ans=0;
for(int i=si;i<=ei;i++)
ans+=A[i];
return ans;
}
static void add(long v,Map<Long,Long>mp) {
if(!mp.containsKey(v)) {
mp.put(v, (long)1);
}
else {
mp.put(v, mp.get(v)+(long)1);
}
}
static void remove(long v,Map<Long,Long>mp) {
if(mp.containsKey(v)) {
mp.put(v, mp.get(v)-(long)1);
if(mp.get(v)==0)
mp.remove(v);
}
}
public static int upper(long A[],long k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
ans=mid;
l=mid+1;
}
else {
u=mid-1;
}
}
return ans;
}
public static int lower(long A[],long k,int si,int ei)
{
int l=si;
int u=ei;
int ans=-1;
while(l<=u) {
int mid=(l+u)/2;
if(A[mid]<=k) {
l=mid+1;
}
else {
ans=mid;
u=mid-1;
}
}
return ans;
}
static int[] copy(int A[]) {
int B[]=new int[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static long[] copy(long A[]) {
long B[]=new long[A.length];
for(int i=0;i<A.length;i++) {
B[i]=A[i];
}
return B;
}
static int[] input(int n) {
int A[]=new int[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextInt();
}
return A;
}
static long[] inputL(int n) {
long A[]=new long[n];
for(int i=0;i<n;i++) {
A[i]=sc.nextLong();
}
return A;
}
static String[] inputS(int n) {
String A[]=new String[n];
for(int i=0;i<n;i++) {
A[i]=sc.next();
}
return A;
}
static long sum(int A[]) {
long sum=0;
for(int i : A) {
sum+=i;
}
return sum;
}
static long sum(long A[]) {
long sum=0;
for(long i : A) {
sum+=i;
}
return sum;
}
static void reverse(long A[]) {
int n=A.length;
long B[]=new long[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static void reverse(int A[]) {
int n=A.length;
int B[]=new int[n];
for(int i=0;i<n;i++) {
B[i]=A[n-i-1];
}
for(int i=0;i<n;i++)
A[i]=B[i];
}
static long[] inputL1(int n)
{
long arr[]=new long[n+1];
for(int i=1;i<=n;i++)
arr[i]=l();
return arr;
}
static int[] input1(int n)
{
int arr[]=new int[n+1];
for(int i=1;i<=n;i++)
arr[i]=i();
return arr;
}
static void input(int A[],int B[]) {
for(int i=0;i<A.length;i++) {
A[i]=sc.nextInt();
B[i]=sc.nextInt();
}
}
static long[][] inputL(int n,int m){
long A[][]=new long[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=l();
}
}
return A;
}
static int[][] input(int n,int m){
int A[][]=new int[n][m];
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
A[i][j]=i();
}
}
return A;
}
static char[][] charinput(int n,int m){
char A[][]=new char[n][m];
for(int i=0;i<n;i++) {
String s=s();
for(int j=0;j<m;j++) {
A[i][j]=s.charAt(j);
}
}
return A;
}
static int nextPowerOf2(int n)
{
if(n==0)
return 1;
n--;
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n++;
return n;
}
static int highestPowerof2(int x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static long highestPowerof2(long x)
{
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
static int max(int A[]) {
int max=Integer.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static int min(int A[]) {
int min=Integer.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long max(long A[]) {
long max=Long.MIN_VALUE;
for(int i=0;i<A.length;i++) {
max=Math.max(max, A[i]);
}
return max;
}
static long min(long A[]) {
long min=Long.MAX_VALUE;
for(int i=0;i<A.length;i++) {
min=Math.min(min, A[i]);
}
return min;
}
static long [] prefix(long A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] prefix(int A[]) {
long p[]=new long[A.length];
p[0]=A[0];
for(int i=1;i<A.length;i++)
p[i]=p[i-1]+A[i];
return p;
}
static long [] suffix(long A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static long [] suffix(int A[]) {
long p[]=new long[A.length];
p[A.length-1]=A[A.length-1];
for(int i=A.length-2;i>=0;i--)
p[i]=p[i+1]+A[i];
return p;
}
static void fill(int dp[]) {
Arrays.fill(dp, -1);
}
static void fill(int dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(int dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(int dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static void fill(long dp[]) {
Arrays.fill(dp, -1);
}
static void fill(long dp[][]) {
for(int i=0;i<dp.length;i++)
Arrays.fill(dp[i], -1);
}
static void fill(long dp[][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
Arrays.fill(dp[i][j],-1);
}
}
}
static void fill(long dp[][][][]) {
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
for(int k=0;k<dp[0][0].length;k++) {
Arrays.fill(dp[i][j][k],-1);
}
}
}
}
static int min(int a,int b) {
return Math.min(a, b);
}
static int min(int a,int b,int c) {
return Math.min(a, Math.min(b, c));
}
static int min(int a,int b,int c,int d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static int max(int a,int b) {
return Math.max(a, b);
}
static int max(int a,int b,int c) {
return Math.max(a, Math.max(b, c));
}
static int max(int a,int b,int c,int d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long min(long a,long b) {
return Math.min(a, b);
}
static long min(long a,long b,long c) {
return Math.min(a, Math.min(b, c));
}
static long min(long a,long b,long c,long d) {
return Math.min(a, Math.min(b, Math.min(c, d)));
}
static long max(long a,long b) {
return Math.max(a, b);
}
static long max(long a,long b,long c) {
return Math.max(a, Math.max(b, c));
}
static long max(long a,long b,long c,long d) {
return Math.max(a, Math.max(b, Math.max(c, d)));
}
static long power(long x, long y, long p)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y)
{
if(y==0)
return 1;
if(x==0)
return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1)
res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void dln(String s)
{
out.println(s);
}
static void d(String s)
{
out.print(s);
}
static void print(int A[]) {
for(int i : A) {
out.print(i+" ");
}
out.println();
}
static void print(long A[]) {
for(long i : A) {
System.out.print(i+" ");
}
System.out.println();
}
static long mod(long x) {
return ((x%mod + mod)%mod);
}
static String reverse(String s) {
StringBuffer p=new StringBuffer(s);
p.reverse();
return p.toString();
}
static int i() {
return sc.nextInt();
}
static String s() {
return sc.next();
}
static long l() {
return sc.nextLong();
}
static void sort(int[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static void sort(long[] A){
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i){
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
static String sort(String s) {
Character ch[]=new Character[s.length()];
for(int i=0;i<s.length();i++) {
ch[i]=s.charAt(i);
}
Arrays.sort(ch);
StringBuffer st=new StringBuffer("");
for(int i=0;i<s.length();i++) {
st.append(ch[i]);
}
return st.toString();
}
static HashMap<Integer,Long> hash(int A[]){
HashMap<Integer,Long> map=new HashMap<Integer, Long>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+(long)1);
}
else {
map.put(i, (long)1);
}
}
return map;
}
static HashMap<Long,Long> hash(long A[]){
HashMap<Long,Long> map=new HashMap<Long, Long>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+(long)1);
}
else {
map.put(i, (long)1);
}
}
return map;
}
static TreeMap<Integer,Integer> tree(int A[]){
TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>();
for(int i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static TreeMap<Long,Integer> tree(long A[]){
TreeMap<Long,Integer> map=new TreeMap<Long, Integer>();
for(long i : A) {
if(map.containsKey(i)) {
map.put(i, map.get(i)+1);
}
else {
map.put(i, 1);
}
}
return map;
}
static boolean prime(int n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static boolean prime(long n)
{
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | b6e4154ab501c56fb37cdd6657cb4b77 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class codeforces1659C {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numCases = Integer.parseInt(br.readLine());
for (int rep = 0; rep<numCases;rep++)
{
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
long [] kingdoms= new long[n+1];
kingdoms[0] = 0;
long [] arr = new long[n];
st = new StringTokenizer(br.readLine());
for (int i = 0; i<n;i++)
{
arr[i] = Long.parseLong(st.nextToken());
kingdoms[i+1] = arr[i]+kingdoms[i];
}
long currCapital = 0;
long cost = 0;
for (int i = 0;i<n;i++)
{
cost+=(arr[i]-currCapital)*b;
//change the capital if it decreases the cost of adding the rest
//System.out.println(((kingdoms[n]-kingdoms[i+1]-((n-i-1)*currCapital))*b)+" "+ (a*(arr[i]-currCapital)+((kingdoms[n]-kingdoms[i+1]-((n-i-1)*arr[i]))*b)));
if (((kingdoms[n]-kingdoms[i+1]-((n-i-1)*currCapital))*b)>(a*(arr[i]-currCapital)+((kingdoms[n]-kingdoms[i+1]-((n-i-1)*arr[i]))*b)))
{
//System.out.println("hit");
cost+=a*(arr[i]-currCapital);
currCapital = arr[i];
}
//System.out.println(cost+" "+arr[i]+" "+currCapital+" "+b);
//just move on because changing the capital isn't optimal
}
//cost+=b*(arr[n-1]-currCapital);
System.out.println(cost);
//System.out.println();
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | ad7ba0966a43a2d6435bfb1216212e1a | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main
{
public static void main(String args[])
{
FastScanner input = new FastScanner();
int tc = input.nextInt();
work:
while (tc-- > 0) {
int n = input.nextInt();
int aa =input.nextInt();
int b =input.nextInt();
long a[]= new long[n+1];
long dif[] = new long[n+1];
long prefix[] = new long[n+1];
long suffix[] = new long[n+2];
for (int i = 1; i <=n; i++) {
a[i] = input.nextInt();
}
// for (int i = 1; i <=n; i++) {
// if(i==1)
// {
// dif[i] = a[i];
// }
// else
// {
// dif[i]= a[i]-a[i-1];
// }
// }
// for (int i = 1; i <=n; i++) {
// if(i==1)
// {
// prefix[i] = dif[i];
// }
// else
// {
// prefix[i] = dif[i];
// prefix[i]+=prefix[i-1];
//
// }
// }
// System.out.println(Arrays.toString(dif));
for (int i = n; i>=1; i--) {
if(i==n)
{
suffix[i] = a[i];
}
else
{
suffix[i] = a[i];
suffix[i]+=suffix[i+1];
}
}
// System.out.println(Arrays.toString(suffix));
long ans = suffix[1]*b;
for (int i = 1; i <=n; i++) {
ans = Math.min(ans,a[i]*(aa+b)+(suffix[i+1]-((n-i)*a[i]))*b);
}
System.out.println(ans);
}
}
static class FastScanner
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next()
{
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine() throws IOException
{
return br.readLine();
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 15c9c8c0fe37c7925c527d89bd754147 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = in.nextInt();
for (int tt = 0; tt < t; tt++) {
int n = in.nextInt();
long a = in.nextLong(), b = in.nextLong();
long[] arr = new long[n + 1];
for (int i = 1; i <= n; i++) arr[i] = in.nextLong();
pw.println(solve(arr, n, a, b));
}
pw.close();
}
static long solve(long[] arr, int n, long a, long b) {
long res = Long.MAX_VALUE;
long[] d = new long[n + 1];
for (int i = 1; i <= n; i++) {
d[i] = d[i - 1] + (a + b) * (arr[i] - arr[i - 1]);
}
long temp = 0;
for (int i = n - 1; i >= 0; i--) {
temp += (n - i) * (arr[i + 1] - arr[i]) * b;
res = Math.min(res, d[i] + temp);
}
return res;
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 041147fa33e8b3e73017433606ee78ff | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class C {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int t = in.nextInt();
for (int tt = 0; tt < t; tt++) {
int n = in.nextInt();
long a = in.nextLong(), b = in.nextLong();
long[] arr = new long[n + 1];
for (int i = 1; i <= n; i++) arr[i] = in.nextLong();
pw.println(solve(arr, n, a, b));
}
pw.close();
}
static long solve(long[] arr, int n, long a, long b) {
long res = Long.MAX_VALUE;
long[] d = new long[n + 1];
for (int i = 1; i <= n; i++) {
d[i] = d[i - 1] + (a + b) * (arr[i] - arr[i - 1]);
}
long temp = 0;
for (int i = n - 1; i >= 0; i--) {
temp += (n - i) * (arr[i + 1] - arr[i]) * b;
res = Math.min(res, d[i] + temp);
}
return res;
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | a5f9a14829cd963b10b37c1ec3885384 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
// int a = 1;
int t;
t = in.nextInt();
//t = 1;
while (t > 0) {
// out.print("Case #"+(a++)+": ");
solver.call(in,out);
t--;
}
out.close();
}
static class TaskA {
public void call(InputReader in, PrintWriter out) {
int n;
long a, b;
n = in.nextInt();
a = in.nextLong();
b = in.nextLong();
int[] arr = new int[n];
long[] prefix = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
}
for (int i = 0; i < n; i++) {
if(i == 0){
prefix[i] = arr[i] * (a + b);
}
else
prefix[i] = (arr[i] - arr[i - 1]) * (b + a) + prefix[i - 1];
}
long ans = prefix[n - 1];
long ans1 = 0, ans2 = 0;
for (int i = n - 1; i >= 0; i--) {
ans1 += b*arr[i];
ans2++;
if(i == 0){
ans= Math.min(ans, ans1);
}
else {
ans = Math.min(ans, ans1 - ans2 * (arr[i - 1]) * b + prefix[i - 1]);
}
}
out.println(ans);
}
}
static int gcd(int a, int b )
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
static class answer implements Comparable<answer>{
int a, b;
long c;
public answer(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public answer(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer o) {
return Integer.compare(this.a, o.a);
}
}
static class answer1 implements Comparable<answer1>{
int a, b, c;
public answer1(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public answer1(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(answer1 o) {
if(this.b == o.b){
return Integer.compare(this.a, o.a);
}
return Integer.compare(this.b, o.b);
}
}
static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (Long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static final Random random=new Random();
static void shuffleSort(int[] a) {
int n = a.length;
for (int i=0; i<n; i++) {
int oi= random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 1f149d12ac32b44ae8f57be758954f60 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class c_1659 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FastScanner in = new FastScanner(System.in);
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
int T = in.nextInt();
for(int aa = 0; aa < T; aa++) {
int n = in.nextInt();
long a = in.nextLong();
long b = in.nextLong();
long c [] = new long [n];
long sum = 0;
for(int i = 0; i < n; i++) {
c[i] = in.nextLong();
sum = sum + b*c[i];
}
long last = 0;
for(int i = 0; i < n; i++) {
long dif = (n - i - 1)*(c[i] - last)*b - (c[i] - last)*a;
if(dif > 0) {
sum -= dif;
last = c[i];
}
}
out.println(sum);
}
out.close();
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
st = new StringTokenizer("");
}
public FastScanner(String fileName) throws Exception {
br = new BufferedReader(new FileReader(new File(fileName)));
st = new StringTokenizer("");
}
public String next() throws Exception {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws Exception {
return Integer.parseInt(next());
}
public long nextLong() throws Exception {
return Long.parseLong(next());
}
public Double nextDouble() throws Exception {
return Double.parseDouble(next());
}
public String nextLine() throws Exception {
if (st.hasMoreTokens()) {
StringBuilder str = new StringBuilder();
boolean first = true;
while (st.hasMoreTokens()) {
if (first) {
first = false;
} else {
str.append(" ");
}
str.append(st.nextToken());
}
return str.toString();
} else {
return br.readLine();
}
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 384e3430be0ca88b938b6266c5708bc6 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static class Pair implements Comparable < Pair > {
int d;
int i;
Pair(int d, int i) {
this.d = d;
this.i = i;
}
public int compareTo(Pair o) {
if (this.d == o.d)
return this.i - o.i;
if (this.d < o.d)
return -1;
else
return 1;
}
}
public static class SegmentTree {
long[] st;
long[] lazy;
int n;
SegmentTree(long[] arr, int n) {
this.n = n;
st = new long[4 * n];
lazy = new long[4 * n];
construct(arr, 0, n - 1, 0);
}
public long construct(long[] arr, int si, int ei, int node) {
if (si == ei) {
st[node] = arr[si];
return arr[si];
}
int mid = (si + ei) / 2;
long left = construct(arr, si, mid, 2 * node + 1);
long right = construct(arr, mid + 1, ei, 2 * node + 2);
st[node] = left + right;
return st[node];
}
public long get(int l, int r) {
return get(0, n - 1, l, r, 0);
}
public long get(int si, int ei, int l, int r, int node) {
if (r < si || l > ei)
return 0;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei)
return st[node];
int mid = (si + ei) / 2;
return get(si, mid, l, r, 2 * node + 1) + get(mid + 1, ei, l, r, 2 * node + 2);
}
public void update(int index, int value) {
update(0, n - 1, index, 0, value);
}
public void update(int si, int ei, int index, int node, int val) {
if (si == ei) {
st[node] = val;
return;
}
int mid = (si + ei) / 2;
if (index <= mid) {
update(si, mid, index, 2 * node + 1, val);
} else {
update(mid + 1, ei, index, 2 * node + 2, val);
}
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
public void rangeUpdate(int l, int r, int val) {
rangeUpdate(0, n - 1, l, r, 0, val);
}
public void rangeUpdate(int si, int ei, int l, int r, int node, int val) {
if (r < si || l > ei)
return;
if (lazy[node] != 0) {
st[node] += lazy[node] * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += lazy[node];
lazy[2 * node + 2] += lazy[node];
}
lazy[node] = 0;
}
if (l <= si && r >= ei) {
st[node] += val * (ei - si + 1);
if (si != ei) {
lazy[2 * node + 1] += val;
lazy[2 * node + 2] += val;
}
return;
}
int mid = (si + ei) / 2;
rangeUpdate(si, mid, l, r, 2 * node + 1, val);
rangeUpdate(mid + 1, ei, l, r, 2 * node + 2, val);
st[node] = st[2 * node + 1] + st[2 * node + 2];
}
}
static class Reader {
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException {
din = new DataInputStream(
new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n') {
if (cnt != 0) {
break;
} else {
continue;
}
}
buf[cnt++] = (byte)c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ') {
c = read();
}
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();
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();
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 {
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws IOException {
if (System.getProperty("ONLINE_JUDGE") == null) {
try {
System.setIn(new FileInputStream(new File("input.txt")));
System.setOut(new PrintStream(new File("output.txt")));
} catch (Exception e) {
}
}
Reader sc = new Reader();
int tc = sc.nextInt();
StringBuilder sb = new StringBuilder();
while (tc-- > 0) {
int n = sc.nextInt();
long a = sc.nextLong();
long b = sc.nextLong();
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = sc.nextLong();
long[] sum = new long[n];
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1)
sum[i] = arr[i];
else
sum[i] = sum[i + 1] + arr[i];
}
long ans = arr[n - 1] * (a + b);
for (int i = 0; i < n - 1; i++) {
ans = Math.min(ans, arr[i] * (a + b) + (sum[i + 1] - (arr[i] * (n - i - 1))) * b);
}
sb.append(Math.min(ans, sum[0] * b));
sb.append("\n");
}
System.out.println(sb);
}
public static int log2(long N) {
// calculate log2 N indirectly
// using log() method
int result = (int)Math.ceil((Math.log(N) / Math.log(2)));
return result;
}
static long power(long x, long y, long p) {
long res = 1; // Initialize result
// Update x if it is more
// than or equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply
// x with the result
if ((y & 1) > 0)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
public static int countBit(long n) {
int count = 0;
while (n > 0) {
count += (n & 1);
n /= 2;
}
return count;
}
public static int get(int[] dsu, int x) {
if (dsu[x] == x)
return x;
int k = get(dsu, dsu[x]);
dsu[x] = k;
return k;
}
static int gcd(int a, int b) {
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return gcd(a - b, b);
return gcd(a, b - a);
}
public static int Xor(int n) {
if (n % 4 == 0)
return n;
// If n%4 gives remainder 1
if (n % 4 == 1)
return 1;
// If n%4 gives remainder 2
if (n % 4 == 2)
return n + 1;
// If n%4 gives remainder 3
return 0;
}
public static long pow(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if ((b & 1) > 0)
res = (res * a) % mod;
b = b >> 1;
a = ((a % mod) * (a % mod)) % mod;
}
return (res % mod + mod) % mod;
}
public static double digit(long num) {
return Math.floor(Math.log10(num) + 1);
}
static int upperBound(ArrayList<Long> arr, long key) {
int mid, N = arr.size();
// Initialise starting index and
// ending index
int low = 0;
int high = N;
// Till low is less than high
while (low < high && low != N) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is greater than or equal
// to arr[mid], then find in
// right subarray
if (key >= arr.get(mid)) {
low = mid + 1;
}
// If key is less than arr[mid]
// then find in left subarray
else {
high = mid;
}
}
// If key is greater than last element which is
// array[n-1] then upper bound
// does not exists in the array
return low;
}
static int lowerBound(ArrayList<Long> array, long key) {
// Initialize starting index and
// ending index
int low = 0, high = array.size();
int mid;
// Till high does not crosses low
while (low < high) {
// Find the index of the middle element
mid = low + (high - low) / 2;
// If key is less than or equal
// to array[mid], then find in
// left subarray
if (key <= array.get(mid)) {
high = mid;
}
// If key is greater than array[mid],
// then find in right subarray
else {
low = mid + 1;
}
}
// If key is greater than last element which is
// array[n-1] then lower bound
// does not exists in the array
if (low < array.size() && array.get(low) < key) {
low++;
}
// Returning the lower_bound index
return low;
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 6b0cf9034ca72d262b725cbf5d9640b8 | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
/**
* #
*
* @author pttrung
*/
public class C_Round_782_Div2 {
public static int MOD = 998244353;
public static void main(String[] args) throws FileNotFoundException {
// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
// "output.txt")));
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
int Q = in.nextInt();
for (int z = 0; z < Q; z++) {
int n = in.nextInt();
long a = in.nextInt();
long b = in.nextInt();
long[] data = new long[n];
long total = 0;
for (int i = 0; i < n; i++) {
data[i] = in.nextInt();
total += data[i];
}
long re = total * b;
long pos = 0;
long cur = 0;
for (int i = 0; i < n; i++) {
total -= data[i];
cur += b * (data[i] - pos);
long tmp = cur + b * (total - ((n - i - 1) * data[i])) + a * (data[i] - pos);
//System.out.println(i + " " + cur + " " + tmp);
if (tmp <= re) {
re = tmp;
cur += a * (data[i] - pos);
pos = data[i];
}
}
out.println(re);
}
out.close();
}
static int abs(int a) {
return a < 0 ? -a : a;
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
public static class Point {
int x;
int y;
public Point(int start, int end) {
this.x = start;
this.y = end;
}
public String toString() {
return x + " " + y;
}
}
public static class FT {
long[] data;
FT(int n) {
data = new long[n];
}
public void update(int index, long value) {
while (index < data.length) {
data[index] += value;
index += (index & (-index));
}
}
public long get(int index) {
long result = 0;
while (index > 0) {
result += data[index];
index -= (index & (-index));
}
return result;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return (val * val);
} else {
return (val * ((val * a)));
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
//br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output | |
PASSED | 2ff4878c67d1b799e7d333dbc891570b | train_110.jsonl | 1650206100 | You are an ambitious king who wants to be the Emperor of The Reals. But to do that, you must first become Emperor of The Integers.Consider a number axis. The capital of your empire is initially at $$$0$$$. There are $$$n$$$ unconquered kingdoms at positions $$$0<x_1<x_2<\ldots<x_n$$$. You want to conquer all other kingdoms.There are two actions available to you: You can change the location of your capital (let its current position be $$$c_1$$$) to any other conquered kingdom (let its position be $$$c_2$$$) at a cost of $$$a\cdot |c_1-c_2|$$$. From the current capital (let its current position be $$$c_1$$$) you can conquer an unconquered kingdom (let its position be $$$c_2$$$) at a cost of $$$b\cdot |c_1-c_2|$$$. You cannot conquer a kingdom if there is an unconquered kingdom between the target and your capital. Note that you cannot place the capital at a point without a kingdom. In other words, at any point, your capital can only be at $$$0$$$ or one of $$$x_1,x_2,\ldots,x_n$$$. Also note that conquering a kingdom does not change the position of your capital.Find the minimum total cost to conquer all kingdoms. Your capital can be anywhere at the end. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class C {
public static void main(String[]args) throws IOException {
Scanner sc=new Scanner(System.in);
PrintWriter out=new PrintWriter(System.out);
int t=sc.nextInt();
while(t-->0) {
int n=sc.nextInt(),a=sc.nextInt(),b=sc.nextInt();
int[]arr=new int[n+1];
for(int i=1;i<=n;i++)arr[i]=sc.nextInt();
long ans=0l;
int j=0;
ans+=1l*b*arr[1];
for(int i=2;i<=n;i++) {
if(a<1l*b*(n-i+1)) {
ans+=1l*a*(arr[i-1]-arr[j]);
j=i-1;
}
ans+=1l*b*(arr[i]-arr[j]);
}
out.println(ans);
}
out.close();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public boolean hasNext() {return st.hasMoreTokens();}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public double nextDouble() throws IOException {return Double.parseDouble(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready(); }
}
}
| Java | ["4\n5 2 7\n3 5 12 13 21\n5 6 3\n1 5 6 21 30\n2 9 3\n10 15\n11 27182 31415\n16 18 33 98 874 989 4848 20458 34365 38117 72030"] | 1 second | ["173\n171\n75\n3298918744"] | NoteHere is an optimal sequence of moves for the second test case: Conquer the kingdom at position $$$1$$$ with cost $$$3\cdot(1-0)=3$$$. Move the capital to the kingdom at position $$$1$$$ with cost $$$6\cdot(1-0)=6$$$. Conquer the kingdom at position $$$5$$$ with cost $$$3\cdot(5-1)=12$$$. Move the capital to the kingdom at position $$$5$$$ with cost $$$6\cdot(5-1)=24$$$. Conquer the kingdom at position $$$6$$$ with cost $$$3\cdot(6-5)=3$$$. Conquer the kingdom at position $$$21$$$ with cost $$$3\cdot(21-5)=48$$$. Conquer the kingdom at position $$$30$$$ with cost $$$3\cdot(30-5)=75$$$. The total cost is $$$3+6+12+24+3+48+75=171$$$. You cannot get a lower cost than this. | Java 8 | standard input | [
"binary search",
"brute force",
"dp",
"greedy",
"implementation",
"math"
] | ce2b12f1d7c0388c39fee52f5410b94b | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. The description of each test case follows. The first line of each test case contains $$$3$$$ integers $$$n$$$, $$$a$$$, and $$$b$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$; $$$1 \leq a,b \leq 10^5$$$). The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ ($$$1 \leq x_1 < x_2 < \ldots < x_n \leq 10^8$$$). The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 1,500 | For each test case, output a single integer — the minimum cost to conquer all kingdoms. | standard output |
Subsets and Splits