description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$)Β β€” the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$)Β β€” time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = [int(i) for i in input().split()] line = [int(x) for x in input().split()] count = {(i + 1): (0) for i in range(100)} sum = 0 for i in line: p = sum + i t = 0 if p > m: for j in range(100, 0, -1): if count[j] == 0: continue elif p - count[j] * j > m: p -= count[j] * j t += count[j] else: l = (p - m) // j if p - l * j > m: l += 1 t += l break count[i] += 1 sum += i print(t, end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR STRING
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$)Β β€” the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$)Β β€” time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, M = map(int, input().split()) t = list(map(int, input().split())) f = [(0) for _ in range(101)] a = [] for i in range(n): Mi = M - t[i] c = 0 for j in range(1, 101): k = min(Mi // j, f[j]) Mi -= k * j c += k a.append(i - c) f[t[i]] += 1 print(" ".join(map(str, a)))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$)Β β€” the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$)Β β€” time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = list(map(int, input().split())) arr = list(map(int, input().split())) pre = [arr[0]] for i in range(1, n): pre.append(pre[-1] + arr[i]) ans = [] freq = [(0) for i in range(105)] for i in range(n): if pre[i] <= m: ans.append(0) else: x = pre[i] cnt = 0 d = x - m for j in range(104, 0, -1): if freq[j] > 0: if freq[j] * j <= d: d -= j * freq[j] cnt += freq[j] elif d % j == 0: y = d // j cnt += y d = 0 else: y = d // j + 1 cnt += y d = 0 if d == 0: break ans.append(cnt) freq[arr[i]] += 1 print(*ans)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
The only difference between easy and hard versions is constraints. If you write a solution in Python, then prefer to send it in PyPy to speed up execution time. A session has begun at Beland State University. Many students are taking exams. Polygraph Poligrafovich is going to examine a group of $n$ students. Students will take the exam one-by-one in order from $1$-th to $n$-th. Rules of the exam are following: The $i$-th student randomly chooses a ticket. if this ticket is too hard to the student, he doesn't answer and goes home immediately (this process is so fast that it's considered no time elapses). This student fails the exam. if the student finds the ticket easy, he spends exactly $t_i$ minutes to pass the exam. After it, he immediately gets a mark and goes home. Students take the exam in the fixed order, one-by-one, without any interruption. At any moment of time, Polygraph Poligrafovich takes the answer from one student. The duration of the whole exam for all students is $M$ minutes ($\max t_i \le M$), so students at the end of the list have a greater possibility to run out of time to pass the exam. For each student $i$, you should count the minimum possible number of students who need to fail the exam so the $i$-th student has enough time to pass the exam. For each student $i$, find the answer independently. That is, if when finding the answer for the student $i_1$ some student $j$ should leave, then while finding the answer for $i_2$ ($i_2>i_1$) the student $j$ student does not have to go home. -----Input----- The first line of the input contains two integers $n$ and $M$ ($1 \le n \le 2 \cdot 10^5$, $1 \le M \le 2 \cdot 10^7$)Β β€” the number of students and the total duration of the exam in minutes, respectively. The second line of the input contains $n$ integers $t_i$ ($1 \le t_i \le 100$)Β β€” time in minutes that $i$-th student spends to answer to a ticket. It's guaranteed that all values of $t_i$ are not greater than $M$. -----Output----- Print $n$ numbers: the $i$-th number must be equal to the minimum number of students who have to leave the exam in order to $i$-th student has enough time to pass the exam. -----Examples----- Input 7 15 1 2 3 4 5 6 7 Output 0 0 0 0 0 2 3 Input 5 100 80 40 40 40 60 Output 0 1 1 2 3 -----Note----- The explanation for the example 1. Please note that the sum of the first five exam times does not exceed $M=15$ (the sum is $1+2+3+4+5=15$). Thus, the first five students can pass the exam even if all the students before them also pass the exam. In other words, the first five numbers in the answer are $0$. In order for the $6$-th student to pass the exam, it is necessary that at least $2$ students must fail it before (for example, the $3$-rd and $4$-th, then the $6$-th will finish its exam in $1+2+5+6=14$ minutes, which does not exceed $M$). In order for the $7$-th student to pass the exam, it is necessary that at least $3$ students must fail it before (for example, the $2$-nd, $5$-th and $6$-th, then the $7$-th will finish its exam in $1+3+4+7=15$ minutes, which does not exceed $M$).
n, m = map(int, input().split()) t = list(map(int, input().split())) tIndex = {} tIndexSum = {} s = 0 for i in range(n): r = 0 if s + t[i] > m: keys = list(tIndex.keys()) keys.sort(reverse=True) s1 = s for k in keys: if s1 - tIndex[k] * k + t[i] > m: s1 -= tIndex[k] * k r += tIndex[k] else: r += (s1 + t[i] - m) // k s1 -= (s1 + t[i] - m) // k * k if s1 + t[i] > m: s1 -= k r += 1 if s1 + t[i] <= m: break print(r, end=" ") if not t[i] in tIndex: tIndex[t[i]] = 0 tIndex[t[i]] += 1 if not t[i] in tIndexSum: tIndexSum[t[i]] = 0 tIndexSum[t[i]] += t[i] s += t[i]
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
T = int(input()) for t in range(T): n = int(input()) cs = [int(x) for x in input().split()] bests = [cs[0], cs[1]] totals = [cs[0], cs[1]] ans = 12345677890123409875678 for i in range(2, n + 1): firsts = i - i // 2 - 1 seconds = i // 2 - 1 cost = ( bests[0] * (n - firsts) + (totals[0] - bests[0]) + bests[1] * (n - seconds) + (totals[1] - bests[1]) ) ans = min(ans, cost) if i < n: index = i % 2 bests[index] = min(bests[index], cs[i]) totals[index] += cs[i] print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys input = sys.stdin.readline I = lambda: list(map(int, input().split())) def ans(ar, n): mi = ar[0] ix = 0 pre = ar[0] bmi = ar[1] bix = 1 bpre = ar[1] su = n * (ar[0] + ar[1]) for i in range(2, n): if (i + 1) % 2: if mi > ar[i]: mi = ar[i] ix = i pre += ar[i] ax, bx = i // 2 + 1, i // 2 su = min(su, mi * (n - ax) + pre + bmi * (n - bx) + bpre) else: if bmi > ar[i]: bmi = ar[i] bix = i bpre += ar[i] ax, bx = (i + 1) // 2, (i + 1) // 2 su = min(su, mi * (n - ax) + pre + bmi * (n - bx) + bpre) return su (t,) = I() for _ in range(t): (n,) = I() ct = I() print(ans(ct, n))
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for s in [*open(0)][2::2]: a = s.split() i = len(a) j = 0 r = p = 1000000000000000.0 b = [0, 0] c = [p, p] for x in map(int, a): j ^= 1 i -= j b[j] += x c[j] = min(c[j], x) x = b[j] + c[j] * i r = min(r, p + x) p = x print(r)
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for tt in range(t): n = int(input()) c = list(map(int, input().split())) s = [c[0], c[1]] x = [c[0], c[1]] ans = sum(x) * n for i in range(2, n): I = i % 2 s[I] += c[i] x[I] = min(x[I], c[i]) ss = sum(s) + x[0] * (n - i // 2 - 1) + x[1] * (n - (i + 1) // 2) ans = min(ans, ss) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for x in range(int(input())): n = int(input()) arr = list(map(int, input().strip().split())) a = [arr[0]] b = [arr[1]] amin = arr[0] bmin = arr[1] asum = arr[0] bsum = arr[1] alen = 1 blen = 1 help = bmin * (n - blen) + bsum + amin * (n - alen) + asum for i in range(2, n): val = arr[i] if i % 2 == 1: b.append(val) bmin = min(bmin, val) bsum += val blen += 1 else: a.append(val) amin = min(val, amin) asum += val alen += 1 help = min(help, bmin * (n - blen) + bsum + amin * (n - alen) + asum) print(help)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) mc = a[0] * n + a[1] * n mec = a[0] moc = a[1] es = a[0] os = 0 ec = 1 oc = 0 for i in range(1, n): if i % 2 == 0: mec = min(mec, a[i]) es += a[i] ec += 1 else: moc = min(moc, a[i]) os += a[i] oc += 1 mc = min(mc, es + (n - ec) * mec + os + (n - oc) * moc) print(mc)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys def load_sys(): return sys.stdin.readlines() def load_local(): with open("input.txt", "r") as f: input = f.readlines() return input def f(n, costs): odd_sum = 0 even_sum = 0 odd_cnt = 0 even_cnt = 0 odd_mn = float("inf") even_mn = float("inf") ans = float("inf") for i in range(len(costs)): if i % 2 == 0: even_sum += costs[i] even_mn = min(even_mn, costs[i]) even_cnt += 1 else: odd_sum += costs[i] odd_mn = min(odd_mn, costs[i]) odd_cnt += 1 cost = ( odd_sum - odd_mn + (n - odd_cnt + 1) * odd_mn + even_sum - even_mn + (n - even_cnt + 1) * even_mn ) ans = min(ans, cost) return ans input = load_sys() for i in range(1, len(input), 2): n = int(input[i]) costs = [int(x) for x in input[i + 1].split()] print(f(n, costs))
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF FUNC_CALL VAR STRING STRING VAR ASSIGN VAR FUNC_CALL VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for testis in range(int(input())): n = int(input()) c = list(map(int, input().split())) x, y = c[0], c[1] res = [x * n + y * n] r = 0 for i in range(2, n): if i % 2 == 0: if c[i] < x: r += x x = c[i] else: r += c[i] res.append(x * (n - i // 2) + y * (n - i // 2 + 1) + r) else: if c[i] < y: r += y y = c[i] else: r += c[i] res.append(x * (n - i // 2) + y * (n - i // 2) + r) print(min(res))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def find(): n = int(input()) mas = list(map(int, input().split())) min1 = mas[0] min2 = mas[1] ans1 = mas[0] * n ans2 = mas[1] * n res = ans1 + ans2 s1 = mas[0] s2 = mas[1] j1 = j2 = 1 for j in range(2, len(mas)): if j % 2 == 0: s1 += mas[j] min1 = min(min1, mas[j]) ans1 = s1 - min1 + min1 * (n - j1) j1 += 1 else: s2 += mas[j] min2 = min(min2, mas[j]) ans2 = s2 - min2 + min2 * (n - j2) j2 += 1 res = min(res, ans1 + ans2) print(res) for i in range(int(input())): find()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) lh = [] lv = [] for i in range(0, n, 2): lh.append(l[i]) for i in range(1, n, 2): lv.append(l[i]) minans = 10000000000 minh = 100000000000 minv = 100000000000 sumh = 0 sumv = 0 count = 10**16 counth = 100000000000 countv = 100000000000 minv1 = 100000000000 for i in range(len(lv)): minh = min(minh, lh[i]) sumh += lh[i] counth = minh * (n - i - 1) + sumh minv1 = minv minv = min(minv, lv[i]) sumv += lv[i] if i == 0: countv = minv * (n - i - 1) + sumv else: countv = min(minv * (n - i - 1) + sumv, minv1 * (n - i) + sumv - lv[i]) count = min(count, counth + countv) if len(lh) != len(lv): count = min( count, sum(lh) + (n - len(lh)) * min(lh) + sum(lv) + (n - len(lv)) * min(lv) ) print(count)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): x = int(input()) zoo = list(map(int, input().split())) ans = zoo[0] * x + zoo[1] * x sum1 = zoo[0] sum2 = zoo[1] min1 = zoo[0] min2 = zoo[1] count1 = 1 count2 = 1 p2 = zoo[1] * x for i in range(2, x): if i % 2 == 0: sum1 += zoo[i] count1 += 1 min1 = min(min1, zoo[i]) p1 = sum1 + min1 * (x - count1) else: sum2 += zoo[i] count2 += 1 min2 = min(min2, zoo[i]) p2 = sum2 + min2 * (x - count2) ans = min(ans, p1 + p2) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) flag = True a = [] b = [] for i in l: if flag: a.append(i) else: b.append(i) flag = not flag x = len(a) y = len(b) z = max(x, y) la = lb = n ca = cb = 0 ta = tb = 1000000000 min_a = a[0] min_b = b[0] cost = [] s_a = 0 s_b = 0 for i in range(n): if i % 2 == 0: j = i // 2 s_a += a[j] min_a = min(min_a, a[j]) temp = s_a + s_b + (n - j - 1) * min_a + (n - j) * min_b cost.append(temp) else: j = i // 2 min_b = min(min_b, b[j]) s_b += b[j] temp = s_a + s_b + (n - j - 1) * min_b + (n - j - 1) * min_a cost.append(temp) print(min(cost))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) l = list(map(int, input().split())) odds = [] evens = [] for i in range(n): if i % 2 == 0: evens.append(l[i]) else: odds.append(l[i]) for i in range(1, len(evens)): evens[i] += evens[i - 1] for i in range(1, len(odds)): odds[i] += odds[i - 1] ans = l[0] * n + l[1] * n min0 = l[0] min1 = l[1] for i in range(2, n): temp = 0 if i % 2 == 0: if l[i] < min0: min0 = l[i] temp += evens[i // 2] temp += odds[i // 2 - 1] temp += min0 * (n - i // 2 - 1) temp += min1 * (n - i // 2 + 1 - 1) else: if l[i] < min1: min1 = l[i] temp += odds[i // 2] temp += evens[i // 2] temp += min1 * (n - i // 2 - 1) temp += min0 * (n - i // 2 - 1) ans = min(ans, temp) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) c = list(map(int, input().split())) c1 = [c[i] for i in range(0, n, 2)] c2 = [c[i] for i in range(1, n, 2)] pref1 = [0] pref2 = [0] for i in range(len(c1)): pref1.append(c1[i] + pref1[i]) for i in range(len(c2)): pref2.append(c2[i] + pref2[i]) l1 = [] l2 = [] mn_1 = float("inf") mn_2 = float("inf") for i in range(len(c1)): mn_1 = min(mn_1, c1[i]) l1.append(pref1[i + 1] + (n - i - 1) * mn_1) for i in range(len(c2)): mn_2 = min(mn_2, c2[i]) l2.append(pref2[i + 1] + (n - i - 1) * mn_2) ans = l1[0] + l2[0] for i in range(1, len(c2)): ans = min(ans, l1[i] + l2[i], l1[i] + l2[i - 1]) ans = min(ans, l1[-1] + l2[-1]) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for ad in range(int(input())): n = int(input()) l = list(map(int, input().split())) m1 = l[0] m2 = l[1] ans = m1 * n + m2 * n x = ans for i in range(2, n): if i % 2 == 0: if l[i] < m1: ans -= m1 * (n - i // 2) m1 = l[i] ans += m1 * (n - i // 2) else: ans += l[i] - m1 elif l[i] < m2: ans -= m2 * (n - i // 2) m2 = l[i] ans += m2 * (n - i // 2) else: ans += l[i] - m2 x = min(x, ans) print(x)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys input = iter(sys.stdin.read().splitlines()).__next__ def solve(): n = int(input()) c = list(map(int, input().split())) min_cost_right = c[0] min_cost_up = c[1] min_cost = min_cost_right * n + min_cost_up * n cost = min_cost for i in range(2, n): if i % 2: if c[i] < min_cost_up: cost -= min_cost_up * (n - i // 2) cost += c[i] * (n - i // 2) min_cost_up = c[i] else: cost += c[i] - min_cost_up elif c[i] < min_cost_right: cost -= min_cost_right * (n - i // 2) cost += c[i] * (n - i // 2) min_cost_right = c[i] else: cost += c[i] - min_cost_right min_cost = min(cost, min_cost) return min_cost t = int(input()) output = [] for _ in range(t): output.append(solve()) print(*output, sep="\n")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR STRING
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for u in range(int(input())): n = int(input()) x = [int(w) for w in input().split()] ans = n * x[0] + n * x[1] s1 = x[0] s2 = x[1] m1 = x[0] m2 = x[1] l1 = 1 l2 = 1 for k in range(2, n): if k % 2 == 0: s1 += x[k] l1 += 1 m1 = min(m1, x[k]) else: s2 += x[k] l2 += 1 m2 = min(m2, x[k]) temp = s1 + (n - l1) * m1 + (s2 + (n - l2) * m2) ans = min(ans, temp) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
num_test_case = int(input()) for t in range(num_test_case): num_segments = int(input()) segments = [int(x) for x in input().split(" ")] min_cost_x_segment = segments[0] min_cost_x = min_cost_x_segment * num_segments min_cost_y_segment = segments[1] min_cost_y = min_cost_y_segment * num_segments ans = min_cost_x + min_cost_y for k in range(2, num_segments): if k % 2 == 0: if segments[k] < min_cost_x_segment: min_cost_x -= (num_segments - k // 2) * ( min_cost_x_segment - segments[k] ) min_cost_x_segment = segments[k] else: min_cost_x += segments[k] - min_cost_x_segment elif segments[k] < min_cost_y_segment: min_cost_y -= (num_segments - k // 2) * (min_cost_y_segment - segments[k]) min_cost_y_segment = segments[k] else: min_cost_y += segments[k] - min_cost_y_segment ans = min(ans, min_cost_x + min_cost_y) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) c = list(map(int, input().split())) b0 = 10**10 b1 = 10**10 sm = 0 res = 10**18 for i in range(n): if i % 2 == 0: if c[i] < b0: b0 = c[i] elif c[i] < b1: b1 = c[i] sm += c[i] c0 = (i + 2) // 2 c1 = (i + 1) // 2 res = min(res, sm + (n - c0) * b0 + (n - c1) * b1) print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def calc(): n = int(input()) c = list(map(int, input().split(" "))) hor_sum = c[0] hor_min = c[0] ver_sum = c[1] ver_min = c[1] hors = 1 vers = 1 min_cost = n * c[0] + n * c[1] for i in range(2, n): if i % 2 == 0: hors += 1 hor_sum += c[i] hor_min = min([hor_min, c[i]]) else: vers += 1 ver_sum += c[i] ver_min = min([ver_min, c[i]]) hor_cost = (n - hors) * hor_min + hor_sum ver_cost = (n - vers) * ver_min + ver_sum min_cost = min([min_cost, hor_cost + ver_cost]) print(min_cost) t = int(input()) for j in range(t): calc()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) min1 = a[0] min2 = a[1] temp1 = min1 * n temp2 = min2 * n ans = temp1 + temp2 sm1 = 0 sm2 = 0 counter1 = 0 counter2 = 0 for i in range(n): if i % 2 == 0: sm1 += a[i] if a[i] < min1: min1 = a[i] temp1 = sm1 + (n - counter1 - 1) * min1 counter1 += 1 else: sm2 += a[i] if a[i] < min2: min2 = a[i] temp2 = sm2 + (n - counter2 - 1) * min2 counter2 += 1 if temp1 + temp2 < ans: ans = temp1 + temp2 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) c = [int(x) for x in input().split()] prices, mn_e, mn_o, s = 10**20, c[0], c[1], c[0] for i in range(2, n + 1): s += c[i - 1] if i % 2 == 0 and i != n: mn_e = min(mn_e, c[i]) elif i % 2 == 1 and i != n: mn_o = min(mn_o, c[i]) price = s + (n - (i + 1) // 2) * mn_e + (n - i // 2) * mn_o prices = min(price, prices) print(prices)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR BIN_OP NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
tc = int(input()) for t in range(tc): n = int(input()) path = [int(x) for x in input().split()] even = True curBest = n * path[0] + n * path[1] minEven = path[0] minOdd = path[1] curEvenSum = path[0] curOddSum = path[1] cntEven = 1 cntOdd = 1 for i in path[2:]: if even: cntEven += 1 curEvenSum += i if i < minEven: minEven = i else: cntOdd += 1 curOddSum += i if i < minOdd: minOdd = i s = curEvenSum + (n - cntEven) * minEven + curOddSum + (n - cntOdd) * minOdd curBest = min(curBest, s) even = not even print(curBest)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys t = int(sys.stdin.readline()) for _ in range(t): n = int(sys.stdin.readline()) l = list(map(int, sys.stdin.readline().split())) right = l[0] up = l[1] su = l[1] + l[0] udone = 1 rdone = 1 low = l[1] * n + l[0] * n for i in range(2, n): su += l[i] if i % 2 == 0: right = min(l[i], right) rdone += 1 low = min(low, (n - rdone) * right + (n - udone) * up + su) else: up = min(l[i], up) udone += 1 low = min(low, (n - rdone) * right + (n - udone) * up + su) print(low)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) min1 = a[0] min2 = 1000000000.0 ans = 1e18 sum = a[0] for i in range(1, n): sum = sum + a[i] if i % 2 == 1: min2 = min(min2, a[i]) else: min1 = min(min1, a[i]) res = sum - min1 - min2 j = i - 1 p = n - j // 2 q = n - (j - j // 2) res = res + p * min2 + q * min1 ans = min(ans, res) print(int(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) f1 = [0] * n f2 = [0] * n f1[0] = l[0] f2[1] = l[1] m1 = [0] * n m2 = [0] * n m1[0] = l[0] m2[1] = l[1] for i in range(2, n): if i % 2 == 0: f1[i] = f1[i - 2] + l[i] m1[i] = min(l[i], m1[i - 2]) else: f2[i] = f2[i - 2] + l[i] m2[i] = min(l[i], m2[i - 2]) m = n * (l[0] + l[1]) for i in range(2, n): if i % 2 == 0: a = i // 2 + 1 b = i // 2 temp = 0 temp += f1[i] - m1[i] temp += (n - a + 1) * m1[i] if b != 1: temp += f2[i - 1] - m2[i - 1] temp += (n - b + 1) * m2[i - 1] else: temp += n * m2[i - 1] else: a = i // 2 + 1 b = i // 2 + 1 temp = 0 temp += f2[i] - m2[i] temp += (n - b + 1) * m2[i] temp += f1[i - 1] - m1[i - 1] temp += (n - a + 1) * m1[i - 1] m = min(m, temp) print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def main(): t = int(input()) for _ in range(t): n = int(input()) c = list(map(int, input().split())) if n == 2: print(2 * (c[0] + c[1])) else: o_min = c[0] e_min = c[1] o_sum = c[0] e_sum = c[1] o_min_i = 0 e_min_i = 1 ans = n * (c[0] + c[1]) for k in range(2, n): if k % 2 == 0: if c[k] < o_min: o_min = c[k] o_min_i = k o_sum += c[k] else: if c[k] < e_min: e_min = c[k] e_min_i = k e_sum += c[k] ans = min( ans, e_sum + o_sum + c[e_min_i] * (n - (k + 1) // 2) + c[o_min_i] * (n - (k + 2) // 2), ) print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
from sys import stdin input = stdin.readline T = int(input()) for _ in range(T): n = int(input()) segs = list(map(int, input().split())) row = float("inf") col = segs[0] base_row = 0 base_col = segs[0] res_row = 0 res_col = segs[0] * n res = float("inf") for i in range(1, len(segs)): if i % 2: base_row += segs[i] if segs[i] < row: row = segs[i] res_row = base_row + row * (n - i // 2 - 1) else: base_col += segs[i] if segs[i] < col: col = segs[i] res_col = base_col + col * (n - i // 2 - 1) semi_res = res_row + res_col if res > semi_res: res = semi_res print(res)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) m = n costs = [int(i) for i in input().split()] horizontal, vertical, ch_old, cv_old = 0, 0, 0, 0 gesamt = sum(costs) * n var, var2 = True, False min_ch = max(costs) min_cv = min_ch for c in costs: if var: if c < min_ch: min_ch = c horizontal = ch_old + c * n else: horizontal = ch_old + c + min_ch * (n - 1) var = False n -= 1 ch_old += c else: if c < min_cv: min_cv = c vertical = cv_old + c * m else: vertical = cv_old + c + min_cv * (m - 1) var, var2 = True, True m -= 1 cv_old += c if c < min_cv: min_cv = c g = vertical + horizontal if g < gesamt and var2: gesamt = g print(gesamt)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
INF = 10**20 def solve(): n = int(input()) (*c,) = map(int, input().split()) min_odd = INF min_ev = INF sum_odd = 0 sum_ev = 0 ans = INF cnt_o = n cnt_e = n for i, x in enumerate(c): if i % 2 == 0: min_odd = min(min_odd, x) sum_odd += x cnt_o -= 1 else: min_ev = min(min_ev, x) sum_ev += x cnt_e -= 1 ans = min(ans, sum_ev + sum_odd + min_ev * cnt_e + min_odd * cnt_o) return ans t = int(input()) for _ in range(t): print(solve())
ASSIGN VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) c = list(map(int, input().split())) rmin = rtot = c[0] umin = utot = c[1] hprice = rmin * n uprice = umin * n ans = hprice + uprice for i in range(2, n): if i & 1: utot += c[i] umin = min(umin, c[i]) uprice = utot + umin * (n - i // 2 - 1) ans = min(ans, hprice + uprice) else: rtot += c[i] rmin = min(rmin, c[i]) hprice = rtot + rmin * (n - i // 2 - 1) ans = min(ans, hprice + uprice) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) best = float("inf") mn = [float("inf")] * 2 sm = [0] * 2 for i in range(n): mn[i % 2] = min(mn[i % 2], arr[i]) sm[i % 2] += arr[i] best = min(best, sum(sm[j] + mn[j] * (n - (i - j) // 2 - 1) for j in [0, 1])) print(best)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys input = sys.stdin.readline T = int(input()) for _ in range(T): n = int(input()) A = list(map(int, input().split())) mv1 = A[0] mv2 = A[1] v1, v2 = n, n ans = v1 * mv1 + v2 * mv2 ans1 = 0 for i in range(2, n): if i % 2 == 0: if A[i] < mv1: ans1 = ans1 + mv1 mv1 = A[i] else: ans1 = ans1 + A[i] v1 -= 1 ans = min(ans, ans1 + v1 * mv1 + v2 * mv2) else: if A[i] < mv2: ans1 = ans1 + mv2 mv2 = A[i] else: ans1 = ans1 + A[i] v2 -= 1 ans = min(ans, ans1 + v1 * mv1 + v2 * mv2) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for s in [*open(0)][2::2]: l = [*map(int, s.split())] q = l[::2] qn = len(q) w = l[1::2] wn = len(w) n = len(l) c = [] s = [0, 0] m = [1000000000000000.0, 1000000000000000.0] p = [n, n] for i in range(n): x = i & 1 m[x] = min(m[x], l[i]) s[x] += l[i] p[x] -= 1 c += [s[x] + p[x] * m[x]] print(min(x + y for x, y in zip(c, c[1:])))
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR LIST BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
from itertools import accumulate from sys import gettrace, stdin if gettrace(): def inputi(): return input() else: def input(): return next(stdin)[:-1] def inputi(): return stdin.buffer.readline() def solve(): n = int(input()) cc = [int(a) for a in input().split()] ccs = list(accumulate(cc)) ccmr = list(accumulate(cc[::2], func=lambda a, b: min(a, b))) ccmu = list(accumulate(cc[1::2], func=lambda a, b: min(a, b))) costs = [ ( ccs[i] + ccmr[i // 2] * (n - i // 2 - 1) + ccmu[(i - 1) // 2] * (n - (i - 1) // 2 - 1) ) for i in range(1, n) ] res = min(costs) print(res) def main(): t = int(input()) for _ in range(t): solve() main()
IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def solve(C, n): MUC = [0] * n MDC = [0] * n minUCost = minDCost = 10**10 for i in range(n): if i % 2 == 0: minUCost = min(C[i], minUCost) else: minDCost = min(C[i], minDCost) MUC[i] = minUCost MDC[i] = minDCost minTotalCost = 10**15 uStepCostSum = dStepCostSum = 0 for i in range(n): uSteps = (i + 2) // 2 dSteps = i + 1 - uSteps if i % 2 == 0: uStepCostSum += C[i] else: dStepCostSum += C[i] totalCost = ( MUC[i] * (n - uSteps) + uStepCostSum + MDC[i] * (n - dSteps) + dStepCostSum ) minTotalCost = min(totalCost, minTotalCost) return minTotalCost t = int(input()) for tc in range(t): n = int(input()) C = list(map(int, input().split())) result = solve(C, n) print(result)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
from sys import stdin input = stdin.readline t = int(input()) for _ in range(t): n = int(input()) c = [int(x) for x in input().split()] min_r = c[0] min_u = c[1] cc = [c[0] + c[1]] r = [min_r] u = [min_u] for i in range(2, n): if i % 2 == 0: min_r = min(min_r, c[i]) else: min_u = min(min_u, c[i]) cc.append(cc[-1] + c[i]) r.append(min_r) u.append(min_u) ans = float("inf") for i in range(len(r)): ans = min( ans, cc[i] - r[i] - u[i] + r[i] * (n - (i // 2 + (i % 2 > 0))) + u[i] * (n - i // 2), ) print(ans)
ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) def slv(n, a): m0, m1 = a[0], a[1] s0, s1 = a[0], a[1] c0, c1 = 1, 1 ans = m0 * n + m1 * n for i in range(2, n): if i % 2 == 0: m0 = min(m0, a[i]) c0 += 1 s0 += a[i] else: m1 = min(m1, a[i]) c1 += 1 s1 += a[i] ans = min(s0 + s1 + m0 * (n - c0) + m1 * (n - c1), ans) return ans for _ in range(t): n = int(input()) a = list(map(int, input().split())) print(slv(n, a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def solve(n, c): king_hoz, king_vert = c[0], c[1] stair_sum = 0 reach_sum = n * c[0] + n * c[1] m = stair_sum + reach_sum singles_sum = c[0] + c[1] hoz_singles = vert_singles = 1 for i in range(2, n): singles_sum += c[i] cost = singles_sum if i % 2 == 0: if c[i] < king_hoz: king_hoz = c[i] hoz_singles += 1 else: if c[i] < king_vert: king_vert = c[i] vert_singles += 1 cost += king_hoz * (n - hoz_singles) cost += king_vert * (n - vert_singles) m = min(m, cost) return m def main(): t = int(input()) for _ in range(t): n = int(input()) c = list(map(int, input().split())) print(solve(n, c)) main()
FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) c = list(map(int, input().split())) c1 = 10**9 c2 = 10**9 s1 = 0 s2 = 0 ans = 10**15 for i in range(n): if i % 2 == 0: s1 += c[i] if c1 > c[i]: c1 = c[i] if i % 2 == 1: s2 += c[i] if c2 > c[i]: c2 = c[i] if i > 0: ret = s1 - c1 + s2 - c2 ret += c1 * (n - i // 2) ret += c2 * (n - (i - 1) // 2) if ret < ans: ans = ret print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for g in range(t): n = int(input()) s = list(map(int, input().split())) a = [s[0], s[1]] mn = [s[0], s[1]] cnt = [1, 1] ans = (s[0] + s[1]) * n for i in range(2, n): mn[i % 2] = min(mn[i % 2], s[i]) a[i % 2] += s[i] cnt[i % 2] += 1 ans = min( ans, a[0] - mn[0] + (n - cnt[0] + 1) * mn[0] + a[1] - mn[1] + (n - cnt[1] + 1) * mn[1], ) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def main(): def win(n, weights): evenmin, oddmin = weights[0], weights[1] evenp, oddp = weights[0], weights[1] evenc, oddc = 1, 1 val = n * (weights[0] + weights[1]) for i in range(2, n): oddp = oddp + i % 2 * weights[i] evenp = evenp + (i + 1) % 2 * weights[i] if i % 2: oddmin = min(weights[i], oddmin) oddc += 1 else: evenmin = min(weights[i], evenmin) evenc += 1 newval = evenp + (n - evenc) * evenmin + oddp + (n - oddc) * oddmin val = min(val, newval) return val cases = int(input()) for case in range(cases): n = int(input()) weights = list(map(int, input().split())) print(win(n, weights)) main()
FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) c = list(map(int, input().split())) n1 = 0 n2 = 0 mx = float("inf") my = float("inf") cst = float("inf") sum = 0 for i in range(1, n + 1): sum += c[i - 1] if i % 2 == 0: n1 += 1 mx = min(mx, c[i - 1]) else: n2 += 1 my = min(my, c[i - 1]) cst = min(cst, sum + mx * (n - n1) + my * (n - n2)) print(cst)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
T = int(input()) for t in range(1, T + 1): N = int(input()) C = list(map(int, input().split())) min_cost = 2**100 sum_even, sum_odd = 0, C[0] min_even, min_odd = 2**100, C[0] even, odd = 0, 1 for i, c in enumerate(C[1:]): if i % 2 == 0: sum_even += c min_even = min(min_even, c) even += 1 else: sum_odd += c min_odd = min(min_odd, c) odd += 1 cost = sum_even + min_even * (N - even) + sum_odd + min_odd * (N - odd) min_cost = min(min_cost, cost) print(min_cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def MI(): return map(int, sys.stdin.readline().split()) def SI(): return sys.stdin.readline().strip() t = II() for s1 in range(t): n = II() l = LI() ans = l[0] * n + l[1] * n cur = l[0] + l[1] curmin = l[0] curminh = l[1] for i in range(2, n): if i % 2 == 0: curmin = min(curmin, l[i]) else: curminh = min(curminh, l[i]) cur += l[i] if (i + 1) % 2 == 0: if curmin * (n - (i + 1) // 2) + curminh * (n - (i + 1) // 2) + cur < ans: ans = curmin * (n - (i + 1) // 2) + curminh * (n - (i + 1) // 2) + cur elif curmin * (n - (i + 1) // 2 - 1) + curminh * (n - (i + 1) // 2) + cur < ans: ans = curmin * (n - (i + 1) // 2 - 1) + curminh * (n - (i + 1) // 2) + cur print(ans)
IMPORT FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR IF BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) costs = input().split() costs = [int(item) for item in costs] minPathCost = (costs[0] + costs[1]) * n horizSum = 0 vertSum = 0 minHoriz = 10**10 minVert = 10**10 numHoriz = 0 numVert = 0 for i in range(n): if i % 2 == 0: horizSum += costs[i] minHoriz = min(minHoriz, costs[i]) numHoriz += 1 if i % 2 != 0: vertSum += costs[i] minVert = min(minVert, costs[i]) numVert += 1 goodSum = ( minHoriz * (n - numHoriz) + minVert * (n - numVert) + horizSum + vertSum ) minPathCost = min(minPathCost, goodSum) print(minPathCost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n, clist1, clist2, p1, p = int(input()), [], [], 0, 0 clist = list(map(int, input().split(" "))) m1 = m2 = ans1 = float("inf") for i in range(n): if i & 1: clist1.append(clist[i]) m1 = min(m1, clist[i]) else: clist2.append(clist[i]) m2 = min(m2, clist[i]) p += clist[i] ans1 = min(ans1, p + m1 * (n - len(clist1)) + m2 * (n - len(clist2))) print(ans1)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR LIST LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) c = list(map(int, input().split())) d = [0] * n m = 10**10 s1 = [0] for i in range(0, n, 2): s1.append(s1[-1] + c[i]) m = min(m, c[i]) d[i] = s1[-1] + m * (n - len(s1) + 1) m = 10**10 s1 = [0] for i in range(1, n, 2): s1.append(s1[-1] + c[i]) m = min(m, c[i]) d[i] = s1[-1] + m * (n - len(s1) + 1) m = 10**32 for i in range(n - 1): m = min(m, d[i] + d[i + 1]) print(m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for i in range(t): n = int(input()) c = list(map(int, input().split())) sum_right = c[0] sum_up = c[1] min_right = c[0] min_up = c[1] count_up = 1 count_right = 1 cost = sum_right + sum_up + min_up * (n - count_up) + min_right * (n - count_right) for k in range(2, n): if k % 2 == 0: min_right = min(min_right, c[k]) sum_right += c[k] count_right += 1 else: min_up = min(min_up, c[k]) if k % 2 == 1 else min_up sum_up += c[k] count_up += 1 cost = min( cost, sum_right + sum_up + min_up * (n - count_up) + min_right * (n - count_right), ) print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) m1 = a[0] m2 = a[1] s1 = a[0] s2 = 0 z1 = 1 z2 = 0 min_cost = m1 * n + m2 * n for i in range(1, n): if i % 2 == 1: if a[i] < m2: m2 = a[i] cost = s1 + (n - z1) * m1 + s2 + (n - z2) * m2 if cost < min_cost: min_cost = cost s2 += a[i] z2 += 1 if i % 2 == 0: if a[i] < m1: m1 = a[i] cost = s1 + (n - z1) * m1 + s2 + (n - z2) * m2 if cost < min_cost: min_cost = cost s1 += a[i] z1 += 1 print(min_cost)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) count = arr[0] * n + arr[1] * n mi0 = arr[0] mi1 = arr[1] ma0 = arr[0] ma1 = arr[1] dp = [count] mi0_count = n mi1_count = n for i in range(2, n): if i % 2 == 0: if arr[i] < mi0: count -= (mi0_count - 1) * mi0 mi0_count -= 1 mi0 = arr[i] count += mi0_count * mi0 elif arr[i] >= mi0 and arr[i] < ma0: mi0_count -= 1 count -= mi0 count += arr[i] else: ma0 = arr[i] count -= mi0 count += ma0 mi0_count -= 1 elif arr[i] <= mi1: count -= (mi1_count - 1) * mi1 mi1_count -= 1 mi1 = arr[i] count += mi1_count * mi1 elif arr[i] > mi1 and arr[i] < ma1: mi1_count -= 1 count -= mi1 count += arr[i] else: ma1 = arr[i] count -= mi1 count += ma1 mi1_count -= 1 dp.append(count) print(min(dp))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def local_ans(n, count, _min, _sum): return (n - count + 1) * _min + (_sum - _min) def solve(n, cc): min_ans = None _next = 2 count1 = 1 min1 = cc[0] sum1 = cc[0] count2 = 0 min2 = cc[1] sum2 = 0 for i in range(1, len(cc)): if _next == 2: count2 += 1 min2 = min(min2, cc[i]) sum2 += cc[i] _next = 1 else: count1 += 1 min1 = min(min1, cc[i]) sum1 += cc[i] _next = 2 new_ans = local_ans(n, count1, min1, sum1) + local_ans(n, count2, min2, sum2) if min_ans is None or min_ans > new_ans: min_ans = new_ans return min_ans def main(): t = int(input()) for i in range(t): n = int(input()) cc = [int(x) for x in input().split()] print(solve(n, cc)) main()
FUNC_DEF RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def main(): for ii in range(int(input())): n = int(input()) a = list(map(int, input().split())) odd_sum = a[1] even_sum = a[0] xo, xe = a[1], a[0] c = xo * n + xe * n for i in range(2, n): if i % 2: odd_sum += a[i] xo = min(a[i], xo) else: even_sum += a[i] xe = min(xe, a[i]) cost = odd_sum + xo * (n - (i + 1) // 2) + even_sum + xe * (n - i // 2 - 1) c = min(c, cost) print(c) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for tt in range(t): n = int(input()) c = list(map(int, input().split())) s = [c[0], c[1]] x = [c[0], c[1]] ans = sum(x) * n for i in range(2, n): I = i % 2 s[I] += c[i] x[I] = min(x[I], c[i]) ss = sum(s) + x[0] * (n - i // 2 - 1) + x[1] * (n - (i + 1) // 2) ans = min(ans, ss) print(ans) num_inp = lambda: int(input()) arr_inp = lambda: list(map(int, input().split())) sp_inp = lambda: map(int, input().split()) str_inp = lambda: input()
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def answer(): ans = 1e50 m1, m2 = 1e50, 1e50 v1, v2, c1, c2 = 0, 0, 0, 0 for i in range(n): if i & 1: m1 = min(m1, a[i]) v1 += a[i] c1 += 1 else: m2 = min(m2, a[i]) v2 += a[i] c2 += 1 ans = min(ans, v1 + v2 + m1 * (n - c1) + m2 * (n - c2)) return ans for T in range(int(input())): n = int(input()) a = list(map(int, input().split())) print(answer())
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys input = sys.stdin.readline def main(): t = int(input()) for _ in range(t): N = int(input()) C = [int(x) for x in input().split()] ans = float("inf") mi_even = float("inf") mi_odd = float("inf") total_even = 0 total_odd = 0 cnt_even = 0 cnt_odd = 0 for i in range(N): if i % 2 == 0: total_even += C[i] cnt_even += 1 mi_even = min(mi_even, C[i]) else: total_odd += C[i] cnt_odd += 1 mi_odd = min(mi_odd, C[i]) if i >= 1: tmp = total_even + total_odd tmp += (N - cnt_even) * mi_even tmp += (N - cnt_odd) * mi_odd ans = min(ans, tmp) print(ans) main()
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def a(): t = int(input()) o = [] for i in range(t): n = int(input()) c = list(map(int, input().split())) x = [c[0]] y = [c[1]] mx = c[0] my = c[1] sx = c[0] sy = c[1] r = c[0] * n + c[1] * n for j in range(2, n): if j % 2 == 0: x.append(c[j]) sx += c[j] mx = min(mx, c[j]) else: y.append(c[j]) sy += c[j] my = min(my, c[j]) temp = sx + (n - len(x)) * mx temp += sy + (n - len(y)) * my r = min(r, temp) o.append(r) for i in o: print(i) a()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def solve(): n = int(input()) a = list(map(int, input().split())) m0 = a[0] m1 = a[1] s0, s1 = a[0], a[1] m, s = [0] * n, [0] * n m[0], m[1] = m0, m1 s[0], s[1] = s0, s1 for i in range(2, n): if i % 2: if a[i] < m1: m1 = a[i] m[i] = m1 else: if a[i] < m0: m0 = a[i] m[i] = m0 s[i] = s[i - 2] + a[i] cos = [0] * n for i in range(n): if i % 2: cos[i] = s[i] * 1 + m[i] * (n - (i + 1) // 2) else: cos[i] = s[i] * 1 + m[i] * (n - i // 2 - 1) p = cos[0] + cos[1] for i in range(n - 1): c = cos[i] + cos[i + 1] if c < p: p = c print(p) def main(): for ii in range(int(input())): solve() main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys input = sys.stdin.readline for _ in range(int(input())): n = int(input()) l = list(map(int, input().split())) ans = 10**18 som = 0 cnt1 = 0 cnt2 = 0 x = 10**18 y = 10**18 for i in range(n): if i % 2 == 0: x = min(x, l[i]) cnt1 += 1 else: y = min(y, l[i]) cnt2 += 1 som += l[i] if i > 0: ans = min(ans, som + x * (n - cnt1) + y * (n - cnt2)) print(ans)
IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for i in range(t): n = int(input()) zoz = [int(j) for j in input().split()] ma = 10**9 + 1 mb = 10**9 + 1 cs = 0 fin = [] for j in range(n): if j % 2 == 0: if zoz[j] < ma: if ma != 10**9 + 1: cs += ma ma = zoz[j] else: cs += zoz[j] if j >= 1: fin.append(cs + ma * (n - j // 2) + mb * (n - (j - 1) // 2)) else: if zoz[j] < mb: if mb != 10**9 + 1: cs += mb mb = zoz[j] else: cs += zoz[j] if j >= 1: fin.append(cs + ma * (n - j // 2) + mb * (n - (j - 1) // 2)) print(min(fin))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR IF VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
T = int(input()) r = 1 while r <= T: n = int(input()) arr = list(map(int, input().split())) minh, minv = arr[0], arr[1] tot = (minh + minv) * n ans = tot for i in range(2, n): if i % 2 == 0: if arr[i] < minh: tot -= (n - i // 2) * (minh - arr[i]) minh = arr[i] else: tot += arr[i] - minh elif arr[i] < minv: tot -= (n - i // 2) * (minv - arr[i]) minv = arr[i] else: tot += arr[i] - minv ans = min(ans, tot) print(ans) r += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def ints(): return list(map(int, input().split())) def case(): (n,) = ints() c = ints() cx = c[::2] cy = c[1::2] cy.append(cy[-1]) res = cx[0] * n + cy[0] * n mincx = cx[0] sumcx = cx[0] mincy = cy[0] sumcy = cy[0] for i in range(1, (n + 1) // 2): if cx[i] < mincx: mincx = cx[i] sumcx += cx[i] cost = sumcx + mincx * (n - i - 1) + sumcy + mincy * (n - i) if cost < res: res = cost if cy[i] < mincy: mincy = cy[i] sumcy += cy[i] cost = sumcx + mincx * (n - i - 1) + sumcy + mincy * (n - i - 1) if cost < res: res = cost return res (t,) = ints() for i in range(t): print(case())
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for t in range(int(input())): n = int(input()) l = list(map(int, input().split())) oc = n ec = n min_o = float("inf") min_e = float("inf") res = 0 ans = float("inf") for i in range(n): if i % 2 == 0: res += l[i] ec -= 1 min_e = min(min_e, l[i]) ans = min(ans, res + ec * min_e + oc * min_o) else: res += l[i] oc -= 1 min_o = min(min_o, l[i]) ans = min(ans, res + ec * min_e + oc * min_o) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def min_cost(n: int, costs: list) -> int: ans = float("inf") even, odd = 0, 0 even_cost, odd_cost = 0, 0 even_min, odd_min = float("inf"), float("inf") for cost in costs: if even > odd: odd += 1 odd_cost += cost odd_min = min(odd_min, cost) else: even += 1 even_cost += cost even_min = min(even_min, cost) even_costs = even_cost + (n - even) * even_min odd_costs = odd_cost + (n - odd) * odd_min ans = min(ans, even_costs + odd_costs) return ans for _ in range(int(input())): n = int(input()) costs = [int(cost) for cost in input().split()] print(min_cost(n, costs))
FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys input = sys.stdin.readline def print(x, end="\n"): sys.stdout.write(str(x) + end) for _ in range(int(input())): n = int(input()) c = list(map(int, input().strip().split())) total = c[0] + c[1] no_odd = 1 no_even = 1 min_odd = c[0] min_even = c[1] ans = total + (n - no_odd) * min_odd + (n - no_even) * min_even for i in range(2, n): total += c[i] if i % 2 == 0: no_odd += 1 min_odd = min(min_odd, c[i]) else: no_even += 1 min_even = min(min_even, c[i]) ans = min(ans, total + (n - no_odd) * min_odd + (n - no_even) * min_even) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF STRING EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) INF = 10**18 for _ in range(t): n = int(input()) c = list(map(int, input().split())) m = [INF, INF] s = [0, 0] cnt = [0, 0] ans = INF for i in range(n): m[i % 2] = min(c[i], m[i % 2]) s[i % 2] += c[i] cnt[i % 2] += 1 v = 0 for i in range(2): v += m[i] * (n - cnt[i]) + s[i] ans = min(ans, v) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) a = arr[0] b = arr[1] ans = a * n + b * n s = 0 for i in range(2, n): if i % 2 == 0: if a < arr[i]: s += arr[i] else: s += a a = arr[i] ans = min(ans, s + b * (n - (i - 1) // 2) + a * (n - i // 2)) else: if b < arr[i]: s += arr[i] else: s += b b = arr[i] ans = min(ans, s + (b + a) * (n - i // 2)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def main(n, l): minim = n * l[0] + n * l[1] min_odd = sum_odd = l[1] min_even = sum_even = l[0] even_count = odd_count = 1 for i in range(2, n): if i % 2 == 0: min_even = min(min_even, l[i]) sum_even += l[i] even_count += 1 else: min_odd = min(min_odd, l[i]) sum_odd += l[i] odd_count += 1 minim = min( minim, sum_even + (n - even_count) * min_even + sum_odd + (n - odd_count) * min_odd, ) print(minim) nn = int(input()) for _ in range(nn): n = int(input()) l = [int(s) for s in input().split()] main(n, l)
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys t = int(input()) for _t in range(t): n = int(input()) c_inp = [int(e) for e in input().strip().split()] minv = float("inf") sum0, min0, len0 = c_inp[0], c_inp[0], 1 sum1, min1, len1 = c_inp[1], c_inp[1], 1 for k in range(1, n): if k != 1: if k % 2 == 0: sum0 += c_inp[k] len0 += 1 min0 = min(min0, c_inp[k]) else: sum1 += c_inp[k] len1 += 1 min1 = min(min1, c_inp[k]) minv = min(minv, sum0 + min0 * (n - len0) + sum1 + min1 * (n - len1)) print(minv)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) c = list(map(int, input().split())) a = [[], []] for i, x in enumerate(c): a[i % 2].append(x) ma = [a[0].copy(), a[1].copy()] ms = [a[0].copy(), a[1].copy()] ma[0][0] = a[0][0] ms[0][0] = a[0][0] for i in range(1, len(a[0])): ms[0][i] = ms[0][i - 1] + a[0][i] ma[0][i] = min(ma[0][i - 1], a[0][i]) ms[1][0] = a[1][0] ma[1][0] = a[1][0] for i in range(1, len(a[1])): ms[1][i] = ms[1][i - 1] + a[1][i] ma[1][i] = min(ma[1][i - 1], a[1][i]) res = None for i in range(1, n): d1 = ms[0][i // 2] - ma[0][i // 2] + ma[0][i // 2] * (n - i // 2) d2 = ( ms[1][(i - 1) // 2] - ma[1][(i - 1) // 2] + ma[1][(i - 1) // 2] * (n - (i - 1) // 2) ) cur = d1 + d2 if res is None or res > cur: res = cur print(res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys input = sys.stdin.readline t = int(input()) for tests in range(t): n = int(input()) C = list(map(int, input().split())) A = [] B = [] for i in range(n): if i % 2 == 0: A.append(C[i]) else: B.append(C[i]) MINA = 1 << 60 SUMA = 0 ANSA = [] for i in range(len(A)): x = A[i] SUMA += x MINA = min(MINA, x) ANSA.append(SUMA - MINA + MINA * (n - i)) MINB = 1 << 60 SUMB = 0 ANSB = [] for i in range(len(B)): x = B[i] SUMB += x MINB = min(MINB, x) ANSB.append(SUMB - MINB + MINB * (n - i)) ANS = 1 << 60 for i in range(len(ANSA)): for j in [i - 1, i]: if 0 <= j < len(ANSB): ANS = min(ANS, ANSA[i] + ANSB[j]) print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR LIST BIN_OP VAR NUMBER VAR IF NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
list1 = [] for i in range(int(input())): n = int(input()) s = list(map(lambda x: int(x), input().split())) price = n * s[0] + n * s[1] min_1, min_2 = s[0], s[1] k = n - 1 price1 = price for j in range(2, n): if j % 2 == 0: price1 = price1 + s[j] - min_1 if s[j] < min_1: price1 -= (k - 1) * (min_1 - s[j]) min_1 = s[j] else: price1 += s[j] - min_2 k -= 1 if s[j] < min_2: price1 -= k * (min_2 - s[j]) min_2 = s[j] if price1 < price: price = price1 list1.append(price) print(*list1, sep="\n")
ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) c = [int(x) for x in input().split()] pre = [0] * n pre[0] = c[0] for i in range(1, n): pre[i] = pre[i - 1] + c[i] lmin2 = [[c[0], c[1]]] l1 = c[0] l2 = c[1] res = n * (l1 + l2) for i in range(2, n): if i + 1 & 1: l1 = min(l1, c[i]) else: l2 = min(l2, c[i]) seg1 = (i + 2) // 2 seg2 = i + 1 - seg1 seg1 -= 1 seg2 -= 1 res = min(res, l1 * (n - seg1) + l2 * (n - seg2) + pre[i] - (l1 + l2)) print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST LIST VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
T = int(input()) for _ in range(T): N = int(input()) c = list(map(int, input().split())) up, right = c[0], 0 mup, mright = c[0], 2000000000.0 cup, cright = 1, 0 M = 9e18 for i in range(1, N): if i & 1: right += c[i] cright += 1 mright = min(mright, c[i]) else: up += c[i] cup += 1 mup = min(mup, c[i]) S = up + right S += mup * (N - cup) S += mright * (N - cright) M = min(M, S) print(M)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) c = list(map(int, input().split())) cx = c[1] * n cy = c[0] * n odd_sum = min_odd = c[1] even_sum = min_even = c[0] odds = evens = 1 min_cost = cx + cy for i in range(2, n): if i & 1: odds += 1 odd_sum += c[i] min_odd = min(min_odd, c[i]) rest_sum = odd_sum - min_odd rem_dist = n - odds + 1 cx = rest_sum + rem_dist * min_odd else: evens += 1 even_sum += c[i] min_even = min(min_even, c[i]) rest_sum = even_sum - min_even rem_dist = n - evens + 1 cy = rest_sum + rem_dist * min_even min_cost = min(min_cost, cx + cy) print(min_cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys def solve(n, costs): pref = [costs[0]] for i in costs[1:]: pref.append(i + pref[-1]) i0 = 0 i1 = 0 min0 = costs[0] min1 = costs[1] m = n * (min0 + min1) for i in range(2, n): if i & 1: i1 += 1 min1 = min(min1, costs[i]) else: i0 += 1 min0 = min(min0, costs[i]) newm = pref[i] - min0 - min1 + (n - i0) * min0 + (n - i1) * min1 m = min(m, newm) return m for _ in range(int(input())): n = int(input()) cost = list(map(int, input().split())) print(solve(n, cost))
IMPORT FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def solve(n, arr): mnm = [0] * n mnm[0] = arr[0] mnm[1] = arr[1] sm = [0] * n sm[0] = arr[0] sm[1] = arr[1] for i in range(2, n): mnm[i] = min(arr[i], mnm[i - 2]) sm[i] = arr[i] + sm[i - 2] m = float("inf") for i in range(n - 1): x = sm[i] + sm[i + 1] if i % 2 == 0: i1 = i // 2 + 1 x += (n - i1) * (mnm[i] + mnm[i + 1]) else: i1 = i // 2 + 1 i2 = i1 + 1 x += (n - i1) * mnm[i] + (n - i2) * mnm[i + 1] if x < m: m = x return m t = int(input()) for i in range(t): n = int(input()) arr = list(map(int, input().split(" "))) print(solve(n, arr))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys from sys import stdin def solve(N, C): best_cost = N * C[0] + N * C[1] cur_cost = N * C[0] + N * C[1] min_1 = C[0] min_2 = C[1] use_min_1 = N + 0 use_min_2 = N + 0 for i in range(2, N): if i % 2 == 0: cost = C[i] if cost < min_1: cur_cost -= (min_1 - cost) * (use_min_1 - 1) min_1 = cost if cur_cost < best_cost: best_cost = cur_cost else: cur_cost += cost - min_1 use_min_1 -= 1 else: cost = C[i] if cost < min_2: cur_cost -= (min_2 - cost) * (use_min_2 - 1) min_2 = cost if cur_cost < best_cost: best_cost = cur_cost else: cur_cost += cost - min_2 use_min_2 -= 1 return best_cost def run(): out = "" T = int(input()) for i in range(T): N = int(stdin.readline()) C = [int(x) for x in stdin.readline().split()] print(solve(N, C)) run()
IMPORT FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def prob3(C, n): minCost = (C[0] + C[1]) * n oddMin = C[0] evenMin = C[1] oddSum, evenSum = C[0], C[1] oddCount, evenCount = 1, 1 for i in range(2, len(C)): if i % 2 == 0: oddCount += 1 oddSum += C[i] oddMin = min(oddMin, C[i]) minCost = min( minCost, evenSum + (n - evenCount) * evenMin + (oddSum + (n - oddCount) * oddMin), ) else: evenCount += 1 evenSum += C[i] evenMin = min(evenMin, C[i]) minCost = min( minCost, evenSum + (n - evenCount) * evenMin + (oddSum + (n - oddCount) * oddMin), ) return minCost t = int(input()) for t in range(t): n = int(input()) C = [int(i) for i in input().split()][:n] print(prob3(C, n))
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) arr = [int(i) for i in input().split()] emin = float("inf") omin = float("inf") ans = float("inf") temp = 0 od = n ev = n for i in range(n): if i % 2 == 0: val = temp + od * arr[i] + ev * emin ans = min(ans, val) od -= 1 omin = min(omin, arr[i]) temp += arr[i] else: val = temp + ev * arr[i] + od * omin ans = min(ans, val) ev -= 1 emin = min(emin, arr[i]) temp += arr[i] print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def solve(c): m = min(c) k = len(c) return m * (n - k) + sum(c) t = int(input()) ans = [] for i in range(t): n = int(input()) arr = [int(i) for i in input().split()] res = arr[0] * n + arr[1] * n curr = res rmin, umin = arr[0], arr[1] for i, v in enumerate(arr[2:], start=2): if i % 2 == 0: k = i // 2 + 1 if v < rmin: curr += (n - (k - 1)) * v - (n - (k - 1)) * rmin rmin = v else: curr += v - rmin else: k = (i + 1) // 2 if v < umin: curr += (n - (k - 1)) * v - (n - (k - 1)) * umin umin = v else: curr += v - umin if curr < res: res = curr ans.append(res) for i in ans: print(i)
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
from sys import stderr, stdin input = stdin.readline def dbp(*args, **kwargs): print(*args, file=stderr, **kwargs) def get_int_list(): return [int(x) for x in input().strip().split()] def do_thing(): n = int(input()) clist = get_int_list() mincost = float("inf") mineven = clist[0] minodd = float("inf") evensum = clist[0] oddsum = 0 evenc = 1 oddc = 0 for i in range(1, n): if i % 2 == 1: minodd = min(minodd, clist[i]) oddc += 1 oddsum += clist[i] else: mineven = min(mineven, clist[i]) evenc += 1 evensum += clist[i] cost = oddsum + evensum + ((n - evenc) * mineven + (n - oddc) * minodd) mincost = min(cost, mincost) return mincost def multicase(): maxcc = int(input().strip()) for cc in range(maxcc): print(do_thing()) multicase()
ASSIGN VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) l = list(map(int, input().split())) h = [] v = [] for i in range(n): if i % 2 == 0: h.append(l[i]) else: v.append(l[i]) hl = len(h) hv = len(v) p = n * (h[0] + v[0]) hmin = h[0] vmin = v[0] mm = p i = 1 j = 1 while i < hl or j < hv: if h[i] < hmin: pp = p - hmin * (n - i) + (n - i) * h[i] hmin = h[i] else: pp = p - hmin + h[i] p = pp if p < mm: mm = p if j < hv: if v[i] < vmin: pp = p - vmin * (n - j) + (n - j) * v[j] vmin = v[j] else: pp = p - vmin + v[i] p = pp if p < mm: mm = p i = min(i + 1, hl) j = min(j + 1, hv) print(mm)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for s in [*open(0)][2::2]: (*a,) = map(int, s.split()) n = len(a) r = p = 1e18 b = [p, p] + a a = [0, 0] + a for i in range(2, n + 2): a[i] += a[i - 2] b[i] = min(b[i], b[i - 2]) x = a[i] + b[i] * (n - i // 2) r = min(r, p + x) p = x print(r)
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys from sys import stdin tt = int(stdin.readline()) ANS = [] for loop in range(tt): n = int(stdin.readline()) c = list(map(int, stdin.readline().split())) ans = float("inf") anum = 0 asum = 0 amin = float("inf") bnum = 0 bsum = 0 bmin = float("inf") for i in range(n): if i % 2 == 0: anum += 1 asum += c[i] amin = min(amin, c[i]) else: bnum += 1 bsum += c[i] bmin = min(bmin, c[i]) if i >= 1: ans = min(ans, asum + amin * (n - anum) + bsum + bmin * (n - bnum)) ANS.append(str(ans)) print("\n".join(ANS))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) c = list(map(int, input().split())) mas1, mas2 = [], [] minn1, minn2, ans = 10000000000, 10000000000, 1000000000000000 summ = 0 for i in range(n): summ += c[i] if i % 2 == 1: minn1 = min(minn1, c[i]) mas1.append(c[i]) else: minn2 = min(minn2, c[i]) mas2.append(c[i]) ans = min(ans, summ + minn1 * (n - len(mas1)) + minn2 * (n - len(mas2))) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def cum_sum(in_list): for i in range(1, len(in_list)): in_list[i] += in_list[i - 1] return in_list def move(n, costs): cum_sum = costs[0] even_min, even_cnt = costs[0], 1 odd_min, odd_cnt = 10**9 + 1, 0 tl_cnt_odd = n // 2 tl_cnt_even = n - tl_cnt_odd best = None for i in range(1, len(costs)): cum_sum += costs[i] is_even = i % 2 == 0 if is_even: even_cnt += 1 if costs[i] < even_min: even_min = costs[i] else: odd_cnt += 1 if costs[i] < odd_min: odd_min = costs[i] total = cum_sum + (n - even_cnt) * even_min + (n - odd_cnt) * odd_min if best is None or total < best: best = total print(best) num = int(input()) for _ in range(num): n = int(input()) costs = [int(el) for el in input().split()] move(n, costs)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR IF VAR NONE VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
quist = int(input()) for count in range(quist): m = int(input()) c = list(map(int, input().split())) c1 = [] c2 = [] w = 0 for i in c: if w == 0: c1.append(i) w = 1 else: c2.append(i) w = 0 n1 = 0 s1 = 0 m1 = -1 d1 = -1 p1 = [] for i in c1: n1 += 1 s1 += i if m1 != -1: m1 = min(m1, i) else: m1 = i d1 = s1 + m1 * (m - n1) p1.append(d1) n2 = 0 s2 = 0 m2 = -1 d2 = -1 p2 = [] for i in c2: n2 += 1 s2 += i if m2 != -1: m2 = min(m2, i) else: m2 = i d2 = s2 + m2 * (m - n2) p2.append(d2) x = -1 i = 0 while i < len(p1) and i < len(p2): if x == -1: x = p1[i] + p2[i] else: x = min(x, p1[i] + p2[i]) i += 1 i = 0 while i + 1 < len(p1) and i < len(p2): if x == -1: x = p1[i + 1] + p2[i] else: x = min(x, p1[i + 1] + p2[i]) i += 1 print(x)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) costs = list(map(int, input().split())) cum_costs = [0] * n min_costs = [10000000000.0] * n min_total_cost = 1000000000000000.0 for i in range(n): if i < 2: cum_costs[i] = costs[i] min_costs[i] = costs[i] continue cum_costs[i] = cum_costs[i - 2] + costs[i] min_costs[i] = min(min_costs[i - 2], costs[i]) for k in range(2, n + 1): k1 = k // 2 k2 = k1 + k % 2 i = k - 1 tmp_cost1 = cum_costs[i - 1] + (n - k1) * min_costs[i - 1] tmp_cost2 = cum_costs[i] + (n - k2) * min_costs[i] total_cost = tmp_cost1 + tmp_cost2 min_total_cost = min(min_total_cost, total_cost) print(min_total_cost)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
def path(arr): ne, no = len(arr), len(arr) me, mo = float("inf"), float("inf") s, ans = 0, float("inf") for i in range(len(arr)): if i % 2 == 0: ne -= 1 me = min(me, arr[i]) s += arr[i] else: no -= 1 mo = min(mo, arr[i]) s += arr[i] ans = min(ans, s + me * ne + mo * no) return ans for i in range(int(input())): a = input() lst = list(map(int, input().strip().split())) print(path(lst))
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) cs = [int(c) for c in input().split()] sum_odds = 0 min_odds = 1000000000 num_odds = 0 sum_evens = 0 min_evens = 1000000000 num_evens = 0 ans = 1000000000000000.0 for i in range(len(cs)): if i % 2 == 0: num_evens += 1 sum_evens += cs[i] min_evens = min(min_evens, cs[i]) else: num_odds += 1 sum_odds += cs[i] min_odds = min(min_odds, cs[i]) ans = min( ans, sum_evens + min_evens * (n - num_evens) + sum_odds + min_odds * (n - num_odds), ) print(int(ans))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for _ in range(t): n = int(input()) l = list(list(map(int, input().split()))) dp = [0] * n dp[0] = float("inf") mn1 = l[0] mn2 = l[1] sm = l[0] + l[1] dp[1] = l[0] * n + l[1] * n for i in range(2, n): if i % 2 == 0: mn1 = min(mn1, l[i]) sm += l[i] dp[i] = sm + (n - 1 - i // 2) * mn1 + (n - i // 2) * mn2 else: mn2 = min(mn2, l[i]) sm += l[i] dp[i] = sm + (n - 1 - i // 2) * mn2 + (n - 1 - i // 2) * mn1 print(min(dp))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys input = sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip("\n") def lis(): return list(map(int, input().split())) def ma(): return map(int, input().split()) t = inp() while t: t -= 1 n = inp() a = lis() res = n * a[0] + n * a[1] ans = res li = [[n * a[0], a[0], n], [n * a[1], a[1], n]] for i in range(2, n): x = li[0][:] li = [li[1]][:] if a[i] <= x[1]: if x[2] != 1: nres = res - x[0] nres += x[1] nres += (x[2] - 1) * a[i] ans = min(ans, nres) res = nres li.append([(x[2] - 1) * a[i], a[i], x[2] - 1]) elif x[2] != 1: nres = res - x[0] nres += a[i] nres += (x[2] - 1) * x[1] ans = min(ans, nres) res = nres li.append([(x[2] - 1) * x[1], x[1], x[2] - 1]) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST LIST BIN_OP VAR VAR NUMBER VAR NUMBER VAR LIST BIN_OP VAR VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) right = [i for i in arr[::2]] up = [i for i in arr[1::2]] r, u = 999999999999999999999999, 99999999999999999999999 s1, s2 = 0, 0 total = 999999999999999999999999 for i in range(len(right)): s1 += right[i] r = min(r, right[i]) t1 = s1 + r * (n - i - 1) if i > len(up) - 1: break s2 += up[i] u = min(u, up[i]) t2 = s2 + u * (n - i - 1) total = min(total, t1 + t2) r, u = right[0], 9999999999999999999999 s1, s2 = right[0], 0 for i in range(1, len(right)): s1 += right[i] r = min(r, right[i]) t1 = s1 + r * (n - i - 1) s2 += up[i - 1] u = min(u, up[i - 1]) t2 = s2 + u * (n - i) total = min(total, t1 + t2) print(total)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) c = list(map(int, input().split())) ans = 10**200 hi, vi, hm, vm = 0, 0, 10**10, 10**10 x, y = 0, 0 for i in range(n): if i % 2 == 0: if c[i] < hm: hm = c[i] hi += 1 x = x + c[i] else: if c[i] < vm: vm = c[i] vi += 1 y = y + c[i] ans = min(ans, x + y + (n - hi) * hm + (n - vi) * vm) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER BIN_OP NUMBER NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Let's say you are standing on the $XY$-plane at point $(0, 0)$ and you want to reach point $(n, n)$. You can move only in two directions: to the right, i. e. horizontally and in the direction that increase your $x$ coordinate, or up, i. e. vertically and in the direction that increase your $y$ coordinate. In other words, your path will have the following structure: initially, you choose to go to the right or up; then you go some positive integer distance in the chosen direction (distances can be chosen independently); after that you change your direction (from right to up, or from up to right) and repeat the process. You don't like to change your direction too much, so you will make no more than $n - 1$ direction changes. As a result, your path will be a polygonal chain from $(0, 0)$ to $(n, n)$, consisting of at most $n$ line segments where each segment has positive integer length and vertical and horizontal segments alternate. Not all paths are equal. You have $n$ integers $c_1, c_2, \dots, c_n$ where $c_i$ is the cost of the $i$-th segment. Using these costs we can define the cost of the path as the sum of lengths of the segments of this path multiplied by their cost, i. e. if the path consists of $k$ segments ($k \le n$), then the cost of the path is equal to $\sum\limits_{i=1}^{k}{c_i \cdot length_i}$ (segments are numbered from $1$ to $k$ in the order they are in the path). Find the path of the minimum cost and print its cost. -----Input----- The first line contains the single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains the single integer $n$ ($2 \le n \le 10^5$). The second line of each test case contains $n$ integers $c_1, c_2, \dots, c_n$ ($1 \le c_i \le 10^9$) β€” the costs of each segment. It's guaranteed that the total sum of $n$ doesn't exceed $10^5$. -----Output----- For each test case, print the minimum possible cost of the path from $(0, 0)$ to $(n, n)$ consisting of at most $n$ alternating segments. -----Examples----- Input 3 2 13 88 3 2 3 1 5 4 3 2 1 4 Output 202 13 19 -----Note----- In the first test case, to reach $(2, 2)$ you need to make at least one turn, so your path will consist of exactly $2$ segments: one horizontal of length $2$ and one vertical of length $2$. The cost of the path will be equal to $2 \cdot c_1 + 2 \cdot c_2 = 26 + 176 = 202$. In the second test case, one of the optimal paths consists of $3$ segments: the first segment of length $1$, the second segment of length $3$ and the third segment of length $2$. The cost of the path is $1 \cdot 2 + 3 \cdot 3 + 2 \cdot 1 = 13$. In the third test case, one of the optimal paths consists of $4$ segments: the first segment of length $1$, the second one β€” $1$, the third one β€” $4$, the fourth one β€” $4$. The cost of the path is $1 \cdot 4 + 1 \cdot 3 + 4 \cdot 2 + 4 \cdot 1 = 19$.
t = int(input()) for i in range(t): n = int(input()) l = list(map(int, input().split())) cost = float("inf") u1, r1 = n, n a = 0 b = 0 i = 0 s1, s2 = float("inf"), float("inf") while i < n: if i % 2 == 0: s1 = min(s1, l[i]) cost = min(cost, a + b + s1 * r1 + s2 * u1) r1 -= 1 a += l[i] else: s2 = min(s2, l[i]) cost = min(cost, a + b + s2 * u1 + s1 * r1) u1 -= 1 b += l[i] i += 1 print(cost)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR