Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; typedef struct { int l, r; } TSave; const int N = 1000001; TSave a[N]; int main() { int n; long long L = 0, R = 0; scanf("%d", &n); for (int i = (0); i < (n); i++) { scanf("%d%d", &a[i].l, &a[i].r); L += a[i].l; R += a[i].r; } int ans = 0; long long mL = L, mR = R; for (int i = (0); i < (n); i++) { long long tL = L - a[i].l + a[i].r, tR = R - a[i].r + a[i].l; if (llabs(tL - tR) > llabs(mL - mR)) { mL = tL; mR = tR; ans = i + 1; } } printf("%d", ans); return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
def f(l): ab = lambda x: x if x>0 else -x k = len(l) dl = [c[0]-c[1] for c in l] ss = sum(dl) mb = ab(ss) mi = 0 for i in range(k): b = ab(ss-(dl[i]<<1)) if b>mb: mi = i+1 mb = b return mi k = int(input()) l = [list(map(int,input().split())) for _ in range(k)] print(f(l))
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; pair<int, int> a[n]; int diff[n]; int sumf = 0, sums = 0; int minidx, maxidx; int mindiff = INT_MAX, maxdiff = INT_MIN; for (int i = 0; i < n; ++i) { scanf("%d%d", &a[i].first, &a[i].second); diff[i] = a[i].first - a[i].second; if (maxdiff < a[i].first - a[i].second) { maxdiff = a[i].first - a[i].second; maxidx = i; } if (mindiff > a[i].first - a[i].second) { mindiff = a[i].first - a[i].second; minidx = i; } sumf += a[i].first; sums += a[i].second; } int sum = sumf - sums; int mint = abs(sum - 2 * mindiff); int maxt = abs(sum - 2 * maxdiff); if (abs(sum) >= mint and abs(sum) >= maxt) { cout << "0\n"; } else if (mint > maxt) { cout << minidx + 1 << endl; } else cout << maxidx + 1 << endl; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
# coding:utf-8 n = raw_input() n = int(n) v = [] zf = 0 ff = 0 for i in range(n): num = raw_input() num = num.split() a, b = int(num[0]), int(num[1]) dif = a-b v.append(dif) if dif >= 0: zf = 1 else: ff = 1 if zf + ff == 1: print(0) else: sum1 = 0 mmax = -1 mmin = 1010 res1 = 0 res2 = 0 id1 = 0 for x in v: if x >= 0: if x > mmax: mmax = x res1 = id1 else: if x < mmin: mmin = x res2 = id1 sum1 += x id1 += 1 tmp1 = sum1 - mmin - mmin tmp2 = sum1 - mmax - mmax tmp1 = tmp1 if tmp1 > 0 else -tmp1 tmp2 = tmp2 if tmp2 > 0 else -tmp2 if tmp1 >= tmp2: print(res2+1) else: print(res1+1)
PYTHON
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import sys from operator import itemgetter n = int(sys.stdin.readline()) colons = [map(int, sys.stdin.readline().split()) for _ in range(n)] L = sum(map(itemgetter(0), colons)) R = sum(map(itemgetter(1), colons)) max_beauty = abs(L - R) max_beauty_i = 0 for i, (l, r) in enumerate(colons): _l = L - l + r _r = R - r + l beauty = abs(_l - _r) if beauty > max_beauty: max_beauty = beauty max_beauty_i = i + 1 print(max_beauty_i)
PYTHON
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import math n = int(input()) l = [0]*n r = [0]*n for i in range(n): l[i],r[i] = [int(i) for i in input().rsplit(' ')] s = 0 for i in range(n): s += l[i]-r[i] bestDiff = math.fabs(s) bestIndex = -1 for i in range(n): if math.fabs(s + 2*(r[i]-l[i])) > bestDiff: #print('YES',bestDiff,end = '-' ) bestDiff = math.fabs(s + 2*(r[i]-l[i])) bestIndex = i #print(bestDiff ) else: pass #print('NO',math.fabs(s + 2*(r[i]-l[i])) ,bestDiff) print(bestIndex + 1)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) maxl = 0 maxr = 0 total = 0 li = 0 ri = 0 x = -1 for i in range(n): l, r = map(int, input().split()) val = l - r if val<maxr: maxr = val ri = i+1 elif val>maxl: maxl = val li = i+1 total += val if abs(total) < abs(total - 2 * maxl): x = li total = total - maxl if abs(total) < abs(total - 2 * maxr): x = ri total = total - maxr if x < 1: print ('0') else: print (str(x))
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int a[105], n, m, k; int color[200005]; bool visited[200005]; map<int, int> cnt; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int ret; vector<vector<int> > adjlist; long long power(long long a, long long b) { if (b == 1) { return a; } if (b == 0) { return 1; } if (b % 2 == 0) { return power(a * a, b / 2); } else { return a * power(a, (b - 1)); } } long long C(int n, int r) { if (r == 0) return 0; if (r > n / 2) r = n - r; long long ans = 1; for (int i = 1; i <= r; i++) { ans *= n - r + i; ans /= i; } return ans; } int main() { std::cin.tie(); std::ios::sync_with_stdio(false); cin >> n; vector<int> l, r; int ymen = 0, shmal = 0, res = 0; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; r.push_back(b), l.push_back(a); ymen += b; shmal += a; } int k = abs(ymen - shmal); for (int i = 0; i < n; i++) { int kam = ymen - r[i]; kam += l[i]; int kamm = shmal - l[i]; kamm += r[i]; if (abs(kam - kamm) > k) { res = i + 1; k = abs(kam - kamm); } } cout << res << endl; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.*; import java.io.*; import java.math.*; public class Test1{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] arr = new int[n][2]; for(int i=0;i<n;i++){ arr[i][0] = sc.nextInt(); arr[i][1] = sc.nextInt(); } int left_beauty = 0; for(int i=0;i<n;i++){ left_beauty+=arr[i][0]; } int right_beauty = 0; for(int i=0;i<n;i++) right_beauty+=arr[i][1]; int index = 0; int beauty = Math.abs(left_beauty-right_beauty); for(int i=0;i<n;i++){ int temp_left = left_beauty-arr[i][0]; int temp_right = right_beauty-arr[i][1]; temp_left+=arr[i][1]; temp_right+=arr[i][0]; int temp_beauty = Math.abs(temp_right-temp_left); if(temp_beauty>beauty){ beauty=temp_beauty; index = i+1; } } System.out.println(index); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) l, r = [0]*n, [0]*n for i in range(n): l[i], r[i] = map(int, input().split(" ")) L, R = sum(l), sum(r) index, beauty = -1, abs(L-R) for i in range(n): t = abs((L-l[i]+r[i]) - (R-r[i]+l[i])) if t > beauty: index, beauty = i, t print(index+1)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
t = int(input()) l,r = [],[] c1,c2=0,0 for i in range(t): a,b = map(int,input().split()) l.append(a) r.append(b) c1+=a c2+=b c = abs(c1-c2) d = 0 n = c for i in range(t): e = abs((c1-l[i]+r[i])-(c2-r[i]+l[i])) if e>n: d=i+1 n = e print(d)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import math,sys from collections import Counter, defaultdict, deque from sys import stdin, stdout input = stdin.readline li = lambda:list(map(int,input().split())) def solve(): n=int(input()) a=[] s=0 t=0 for i in range(n): p,q=li() s+=p t+=q a.append([p,q]) k=0 m=abs(s-t) for i in range(n): p,q=a[i] h,j=s,t h+=q j+=p j-=q h-=p if(abs(h-j)>m): k=i+1 m=abs(j-h) print(k) for _ in range(1): solve()
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
T = int(raw_input()) M = -float('inf') m = float('inf') S = 0 temp = [] newS = [] for i in xrange(T): a, b = map(int, raw_input().split() ) S += (a - b) temp.append(b - a) newS = [abs(S + 2 * temp[i]) for i in xrange(T)] newS.append(abs(S)) idx = newS.index(max(newS)) if idx == T: print 0 else: print idx + 1
PYTHON
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
//Codeforces Round #378 (Div. 2) import java.io.*; import java.util.*; public class TaskB { public static void main (String[] args) throws IOException { FastScanner fs = new FastScanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = fs.nextInt(); int[] l = new int[n]; int[] r = new int[n]; int ls = 0; int rs = 0; for (int i = 0; i < n; i++) { l[i] = fs.nextInt(); ls += l[i]; r[i] = fs.nextInt(); rs += r[i]; } int k = -1; int b = Math.abs(ls - rs); for (int i = 0; i < n; i++) { int t = Math.abs(ls - rs - l[i] - l[i] + r[i] + r[i]); if (t > b) { b = t; k = i; } } pw.println(k + 1); pw.close(); } static class FastScanner { BufferedReader reader; StringTokenizer tokenizer; FastScanner(InputStream i) { reader = new BufferedReader(new InputStreamReader(i)); tokenizer = new StringTokenizer(""); } String next() throws IOException { while(!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n=int(input()) L,R=[],[] l,r=0,0 for i in range(n): ch=(input().split()) L.append(int(ch[0])) l+=int(ch[0]) r+=int(ch[1]) R.append(int(ch[1])) be=abs(l-r) a=0 for i in range(n): if abs((l+R[i]-L[i])-(r-R[i]+L[i]))>be: be=abs((l+R[i]-L[i])-(r-R[i]+L[i])) a=i+1 print(a)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(raw_input()) left = [] right = [] for i in range(n): a, b = map(int, raw_input().split()) left.append(a) right.append(b) L = sum(left) R = sum(right) beauty = abs(L - R) best = beauty best_col = -1 for i in range(n): L -= left[i] R -= right[i] L += right[i] R += left[i] cur = abs(L - R) if cur > best: best = cur best_col = i L += left[i] R += right[i] L -= right[i] R -= left[i] if best_col == -1: print 0 else: print best_col + 1
PYTHON
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) left, right = [], [] L = R = 0 for _ in range(n): l,r = map(int, input().split()) left.append(l) right.append(r) L += l R += r max_k, max_i = abs(L-R), -1 for i in range(n): Li = L - left[i] + right[i] Ri = R - right[i] + left[i] k = abs(Li-Ri) if k > max_k: max_k, max_i = k, i print(max_i+1)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.*; public class Parade{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] left = new int[n]; int[] right = new int[n]; int lSum = 0; int rSum = 0; for(int i = 0; i < n; i++){ left[i] = in.nextInt(); right[i] = in.nextInt(); lSum+=left[i]; rSum+=right[i]; } int max = Math.abs(lSum - rSum); int pos = 0; for(int i = 0; i < n; i++){ int tmp = Math.abs(lSum-rSum-(left[i]-right[i])*2); if(tmp > max){ pos = i+1; max = tmp; } } System.out.println(pos); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) d_vec = [0] for i in range(n): l, u = [int(i) for i in input().split()] d_vec.append(l - u) d_sum = sum(d_vec) d_vec = [abs(d_sum - 2*d) for d in d_vec] d_max = max(d_vec) print(d_vec.index(d_max))
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
a = int(input()) v,r,t,l =[],0,-1,0 for i in range(a): q,w= map(int,input().split()) v.append([q,w]) l+=q;r+=w b = abs(l-r) for i in range(len(v)): w = abs((l+v[i][1]-v[i][0])-(r+v[i][0]-v[i][1])) if w>b:t,b = i,w print(t+1)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.Scanner; public class Solution { public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); // Scanner sc = new Scanner(new File("in.txt")); int n = sc.nextInt(); int[] cols = new int[n]; int totalDiff = 0; for(int i=1; i<=n; i++){ cols[i-1] = sc.nextInt()-sc.nextInt(); totalDiff += cols[i-1]; } int swCol=0; int bestBeauty = Math.abs(totalDiff); for (int i = 0; i < cols.length; i++) { if(Math.abs(totalDiff - 2*cols[i])>bestBeauty){ bestBeauty = Math.abs(totalDiff - 2*cols[i]); swCol = i+1; } } System.out.println(swCol); sc.close(); } } //class Column{ // int i; // int diff; // // public Column(int i, int diff) { // this.i = i; // this.diff = diff; // } // // //}
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) left = [] right = [] dif1 = [] dif2 = [] total = [] for i in range(n): l, r = map(int, input().split()) left.append(l) right.append(r) if l>r: dif1.append((abs(l-r),i)) else: dif2.append((abs(l-r),i)) total.append((abs(sum(left)-sum(right)),0)) try: left[max(dif1)[1]], right[max(dif1)[1]] = right[max(dif1)[1]], left[max(dif1)[1]] total.append((abs(sum(left)-sum(right)),max(dif1)[1]+1)) left[max(dif1)[1]], right[max(dif1)[1]] = right[max(dif1)[1]], left[max(dif1)[1]] except: pass try: left[max(dif2)[1]], right[max(dif2)[1]] = right[max(dif2)[1]], left[max(dif2)[1]] total.append((abs(sum(left)-sum(right)),max(dif2)[1]+1)) left[max(dif2)[1]], right[max(dif2)[1]] = right[max(dif2)[1]], left[max(dif2)[1]] except: pass print(max(total)[1])
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
# In this template you are not required to write code in main import sys inf = float("inf") #from cmath import sqrt #from collections import deque, Counter, OrderedDict,defaultdict #from heapq import nsmallest, nlargest, heapify,heappop ,heappush, heapreplace from math import ceil,floor,log,sqrt,factorial,pow,pi,gcd #from bisect import bisect_left,bisect_right abc='abcdefghijklmnopqrstuvwxyz' abd={'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7, 'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14, 'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21, 'w': 22, 'x': 23, 'y': 24, 'z': 25} mod,MOD=1000000007,998244353 vow=['a','e','i','o','u'] dx,dy=[-1,1,0,0],[0,0,1,-1] def get_array(): return list(map(int, sys.stdin.readline().strip().split())) def get_ints(): return map(int, sys.stdin.readline().strip().split()) def input(): return sys.stdin.readline().strip() n=int(input()) Arr=[];sum_1=0;sum_2=0 for i in range(n): a,b=get_ints() sum_1+=a;sum_2+=b Arr.append([a,b]) beauty=abs(sum_1-sum_2);ans=0 for i in range(n): x1=Arr[i][1]-Arr[i][0] x2=Arr[i][0]-Arr[i][1] if abs((sum_1+x1)-(sum_2+x2))>beauty: beauty=abs((sum_1+x1)-(sum_2+x2)) ans=i+1 print(ans)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
t=int(input()) sum1=0 sum2=0 ans=[] l=[] for _ in range(t): n,m=map(int,input().split()) l.append([n,m]) sum1+=n sum2+=m final=abs(sum1-sum2) ind=-1 for i in range(t): temp=sum1 temp2=sum2 temp=temp-l[i][0]+l[i][1] temp2=temp2-l[i][1]+l[i][0] if abs(temp-temp2)>final: final=abs(temp-temp2) ind=i+1 if ind==-1: print("0") else: print(ind)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const double PI = acos(-1.0); const int inf = (int)1e9 + 7; const long long INF = (long long)1e18 + 7; const int mod = (int)1e9 + 7; const int N = (int)2e5 + 7; int n, l[N], r[N], R, L, ans, maxi; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> l[i] >> r[i]; L += l[i]; R += r[i]; } maxi = abs(L - R); for (int i = 1; i <= n; i++) { int q = L - l[i] + r[i]; int w = R - r[i] + l[i]; if (abs(w - q) > maxi) maxi = abs(q - w), ans = i; } cout << ans << '\n'; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] l = new int[n]; int[] r = new int[n]; int ans = 0; int rSum = 0; int lSum = 0; for(int i=0;i<n;i++) { l[i] =sc.nextInt(); lSum +=l[i]; r[i] =sc.nextInt(); rSum+=r[i]; } int max = Math.abs(rSum-lSum); for(int i=0;i<n;i++) { int x = rSum-r[i]+l[i]; int y = lSum-l[i]+r[i]; if(Math.abs(y-x)>max) { ans = (i+1); max = Math.abs(y-x); } } System.out.println(ans); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if(x.charAt(0) == '-') { neg = true; start++; } for(int i = start; i < x.length(); i++) if(x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if(dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg?-1:1); } public boolean ready() throws IOException {return br.ready();} } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int x[maxn]; int y[maxn]; int n, sum1, sum2, ans; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> x[i] >> y[i]; } for (int i = 1; i <= n; i++) { sum1 += x[i]; sum2 += y[i]; } int mx = abs(sum1 - sum2); for (int i = 1; i <= n; i++) { if (mx < abs((sum1 - x[i] + y[i]) - (sum2 - y[i] + x[i]))) { mx = abs((sum1 - x[i] + y[i]) - (sum2 - y[i] + x[i])); ans = i; } } cout << ans << endl; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#!/usr/bin/env python3 # -*- coding: utf-8 -*- n=int(input()) d=[] for i in range(n): a,b=map(int,input().split()) d.append([a,b]) L,R=0,0 for i in range(n): L+=d[i][0] R+=d[i][1] max=abs(L-R) imax=-5 for i in range(n): if abs(L-2*d[i][0]+2*d[i][1]-R)>max: max=abs(L-2*d[i][0]+2*d[i][1]-R) imax=i if imax!=-5: print(imax+1) else: print(0)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.lang.*; import java.io.*; import java.util.*; public class C733B { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int [] l = new int[n]; int [] r = new int[n]; int sumL = 0, sumR = 0; boolean LR = false, RL = false; for (int i = 0; i < n; i++) { l[i] = in.nextInt(); r[i] = in.nextInt(); if (l[i] < r[i]) RL = true; if (r[i] < l[i]) LR = true; sumL += l[i]; sumR += r[i]; } int index = 0; if (LR && RL) { int maxSum = Math.abs(sumL - sumR); int maxSum1 = maxSum; for (int i = 0; i < n; i++) { if (maxSum1 < Math.abs(sumL - 2*l[i] - sumR + 2*r[i])) { index = i+1; maxSum1 = Math.abs(sumL - 2*l[i] - sumR + 2*r[i]); } } } System.out.print(index); in.close(); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int L[100001], R[100001]; int main() { int n, i, l = 0, r = 0, x, k, ret = 0, val; scanf("%d", &n); for (i = 1; i <= n; i++) { scanf("%d %d", L + i, R + i); l += L[i]; r += R[i]; } x = l - r; val = (int)abs(x); for (i = 1; i <= n; i++) { k = x - (L[i] - R[i]) + (R[i] - L[i]); k = (int)abs(k); if (k > val) { ret = i; val = k; } } printf("%d", ret); return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.Scanner; public class Parade { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int sL=0, sR=0; int[][] a = new int[n][2]; for(int i = 0; i < n; i++) { a[i][0]=scan.nextInt(); a[i][1]=scan.nextInt(); sL+=a[i][0]; sR+=a[i][1]; } int b=Math.abs(sL-sR); int max=b; int ind=0; for(int i = 0; i < n; i++) { int temp=Math.abs(sL-a[i][0]+a[i][1]-(sR-a[i][1]+a[i][0])); if(max<temp) { ind=i+1; max=Math.max(max, temp); } } System.out.println(ind); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
if __name__ == '__main__': n = input() l, r = [], [] for _ in xrange(n): s = raw_input().split() l.append(int(s[0])) r.append(int(s[1])) L, R = 0, 0 for i in xrange(n): L += l[i] R += r[i] res = 0 Nmax = 0 for i in xrange(n): N = abs(L-R-2*l[i]+2*r[i]) if N > Nmax: res = i Nmax = N if Nmax <= abs(L-R): print(0) else: print(res+1)
PYTHON
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n=int(input()) l=[] l2=0 r1=0 for i in range(n): l1,r=list(map(int,input().split())) l2+=l1 r1+=r l.append([l1,r]) maxim=abs(l2-r1) ind=-1 for i in range(n): s=abs((l2-l[i][0]+l[i][1])-(r1+l[i][0]-l[i][1])) if s>maxim: maxim=s ind=i print(ind+1)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) col = [] r = 0 l = 0 for i in range(n): col.append([int (x) for x in input().split()]) l += col[i][0] r += col[i][1] beauty = abs(r-l) best = -1 best_be = beauty for i in range(n): r1,l1 = col[i] diff = r1-l1 b2 = abs(r-l+2*diff) if b2 > best_be: best_be = b2 best = i print(best+1)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int l[1000000], r[1000000]; int main() { int n, L = 0, R = 0, d, cl, cr, a, b, D, f = 0, I; cin >> n; for (int i = 0; i < n; i++) { cin >> l[i] >> r[i]; L += l[i]; R += r[i]; d = abs(L - R); } for (int i = 0; i < n; i++) { cl = r[i]; cr = l[i]; a = L - l[i]; b = R - r[i]; a += cl; b += cr; D = abs(a - b); if (D > d) { I = i + 1; d = D; f = 1; } } if (f == 0) printf("0\n"); else printf("%d\n", I); return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) l = [] r = [] d = [] L = 0 R = 0 for i in range(n): tmp1, tmp2 = map(int,input().split()) l.append(tmp1) r.append(tmp2) L += tmp1 R += tmp2 dif = L - R selectedDif = dif selectedID = 0 for i in range(n): newDif = dif - 2*(l[i] - r[i]) if abs(selectedDif) < abs(newDif): selectedID = i + 1 selectedDif = newDif print(selectedID)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ //package cf; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.PriorityQueue; import java.util.Random; import java.util.Scanner; import java.util.TreeSet; import java.math.BigInteger; /** * * @author Ada */ public class Cf { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner lukija = new Scanner(System.in); System.out.println(b(lukija)); } public static long b(Scanner lukija) { int maara = Integer.parseInt(lukija.nextLine()); long oik = 0; long vas = 0; long[] kaikkiL = new long[maara + 1]; long[] kaikkiR = new long[maara + 1]; for (int i = 1; i < maara + 1; i++) { String rivi = lukija.nextLine(); long l = Long.parseLong(rivi.split(" ")[0]); long r = Long.parseLong(rivi.split(" ")[1]); vas += l; oik += r; kaikkiL[i] = l; kaikkiR[i] = r; } long pieninErotus = Math.abs(oik - vas); int parasI = 0; for (int i = 1; i < maara + 1; i++) { long oik2 = oik - kaikkiR[i] + kaikkiL[i]; long vas2 = vas + kaikkiR[i] - kaikkiL[i]; long erotus = Math.abs(oik2 - vas2); if (pieninErotus < erotus) { pieninErotus = erotus; parasI = i; } } return parasI; } public static long a(Scanner lukija) { String rivi = lukija.nextLine(); HashSet<Character> vokaalit2 = new HashSet(); char[] vokaalit = {'A', 'E', 'I', 'O', 'U', 'Y'}; for (int i = 0; i < vokaalit.length; i++) { vokaalit2.add(vokaalit[i]); } long laskuri = 0; long laskuriMax = 0; for (int i = 0; i < rivi.length(); i++) { char merkki = rivi.charAt(i); laskuri++; // for (int j = 0; j < vokaalit.length; j++) { // if (merkki == vokaalit[j]) { // laskuri = 0; // } // } if (vokaalit2.contains(merkki)) { laskuri = 0; } if (laskuriMax < laskuri) { laskuriMax = laskuri; } } return laskuriMax + 1; } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import static java.lang.Math.*; import java.math.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); Reader.init(System.in); int n = in.nextInt(); ArrayList<Integer> l = new ArrayList(); ArrayList<Integer> r = new ArrayList(); ArrayList<Integer> ans = new ArrayList(); long left=0; long right=0; for (int i = 0; i < n; i++) { l.add(in.nextInt()); r.add(in.nextInt()); left+=l.get(i); right+=r.get(i); ans.add(l.get(i) - r.get(i)); } int neg = 0, pos = 0; for (int i = 0; i < ans.size(); i++) { if (ans.get(i) < 0) { neg++; } else { pos++; } } int max = 0; int ind = -1; if (pos > 1 && neg > 1&&(left>right)) { for (int i = 0; i < ans.size(); i++) { if(ans.get(i)<0) { if (ans.get(i) < max) { max = ans.get(i); ind = i + 1; }} } System.out.println(ind==-1?0:ind); } else if (pos > 1 && neg > 1&&(left<right)) { for (int i = 0; i < ans.size(); i++) { if(ans.get(i)>0) { if (ans.get(i) > max) { max = ans.get(i); ind = i + 1; }} } System.out.println(ind==-1?0:ind); } else if (pos == 1 && neg > 1) { for (int i = 0; i < ans.size(); i++) { if (ans.get(i) > 0) { System.out.println(i + 1);return; } } } else if (neg == 1 && pos > 1) { for (int i = 0; i < ans.size(); i++) { if (ans.get(i) < 0) { System.out.println(i + 1);return; } } } else if (neg == pos) { for (int i = 0; i < ans.size(); i++) { if (ans.get(i) > max) { max = ans.get(i); ind = i + 1; } } System.out.println(ind); } else { System.out.println("0"); } } static int prime(int p) { int t = (int) sqrt(Integer.parseInt(p + "")); if (t <= 1) { return 0; } for (long i = 2; i <= t; i++) { if (p % i == 0) { return 0; } } return 1; } static int Obe(long a, long b) { BigInteger b1 = new BigInteger("" + a); BigInteger b2 = new BigInteger("" + b); BigInteger gcd = b1.gcd(b2); return gcd.intValue(); } static int che(int[] w) { Set<Integer> list = new LinkedHashSet<>(); for (int i = 0; i < w.length; i++) { list.add(w[i]); } return list.size(); } private static ArrayList<BigInteger> fibCache = new ArrayList<BigInteger>(); static { fibCache.add(BigInteger.ZERO); fibCache.add(BigInteger.ONE); } public static BigInteger fib(int n) { if (n >= fibCache.size()) { fibCache.add(n, fib(n - 1).add(fib(n - 2))); } return fibCache.get(n); } } class Reader { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int l[100001], r[100001], i, j, k, n, mx, S, L, R, a, b; int main() { cin >> n; for (i = 1; i <= n; ++i) { cin >> l[i] >> r[i]; L += l[i]; R += r[i]; } mx = abs(L - R); k = 0; a = L; b = R; for (i = 1; i <= n; ++i) { a = a - l[i] + r[i]; b = b - r[i] + l[i]; S = abs(a - b); if (S > mx) { mx = S; k = i; } a = L; b = R; } cout << k; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; long sumOf(int *array, int length) { int temp{0}; for (int i = 0; i < length; ++i) temp += array[i]; return temp; } int main() { int col, maxBeauty{0}, colChange{-1}; cin >> col; int marchl[col], marchr[col]; for (int i = 0; i < col; ++i) cin >> marchl[i] >> marchr[i]; int suml = sumOf(marchl, col); int sumr = sumOf(marchr, col); maxBeauty = abs(suml - sumr); for (int i = 0; i < col; ++i) { int temp = abs((suml - marchl[i] + marchr[i]) - (sumr + marchl[i] - marchr[i])); if (temp > maxBeauty) { maxBeauty = temp; colChange = i; } } cout << colChange + 1 << endl; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long int a, b, c, f, g = 0, right = 0, left = 0, low = 0, high = 0, i, x, post2, h = 0, sum1, sum2, post, pre, y, m = 0, n = 0; cin >> a; for (i = 0; i < a; i++) { cin >> b >> c; f = b - c; if (f > low) { low = f; x = i + 1; g = b; h = c; } else if (f < high) { high = f; y = i + 1; m = b; n = c; } right = right + b; left = left + c; } sum1 = right - g + h; sum2 = left - h + g; post = abs(sum1 - sum2); pre = abs(right - left); sum1 = right - m + n; sum2 = left - n + m; post2 = abs(sum1 - sum2); if (post > post2) { if (post > pre) { cout << x; } else { cout << "0"; } } else { if (post2 > pre) { cout << y; } else { cout << "0"; } } }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.Scanner; public class parade { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int col = scan.nextInt(); int[] left = new int[col]; int[] right = new int[col]; int leftTot = 0; int rightTot = 0; //get total int beau; int result = 0; int tempLeftTot; int tempRightTot; for (int i = 0; i < col; i++) { left[i] = scan.nextInt(); leftTot += left[i]; right[i] = scan.nextInt(); rightTot += right[i]; } beau = Math.abs(leftTot-rightTot); for (int i = 0; i < col; i++) { tempLeftTot = (leftTot -left[i]) + right[i]; tempRightTot = (rightTot - right[i]) +left[i]; int tempBeau = Math.abs(tempLeftTot-tempRightTot); if (tempBeau > beau) { beau = tempBeau; result = i+1; } } System.out.println(result); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int l[1000010]; int r[1000010]; int sum = 0; int sum2 = 0; int mx; int idx = 0; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int a, b; scanf("%d%d", &a, &b); l[i] = a; r[i] = b; sum += a; sum2 += b; } mx = sum2 - sum; if (mx < 0) mx *= -1; for (int i = 0; i < n; i++) { sum -= l[i]; sum2 -= r[i]; sum += r[i]; sum2 += l[i]; int ddd = sum2 - sum; if (ddd < 0) ddd *= -1; if (ddd > mx) { mx = ddd; idx = i + 1; } sum -= r[i]; sum += l[i]; sum2 -= l[i]; sum2 += r[i]; } cout << idx; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
z=int(input()) d=[] s=[] for i in range(z): x=list(map(int,input().split())) d.append(x[0]) s.append(x[1]) l=0 r=0 for i in s: l+=i for o in d: r+=o df=abs(l-r) qa=[] for i in range(len(s)): a=l b=r a-=s[i] a+=d[i] b-=d[i] b+=s[i] qa.append(abs(a-b)) sa=qa.index(max(qa))+1 if max(qa)<=df: print(0) else: print(sa)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()) { int n=sc.nextInt(); int max1=0,maxi1=0,max2=0,maxi2=0,f1=0,f2=0,sum1=0,sum2=0; for(int i=0;i<n;i++) { int l=sc.nextInt(),r=sc.nextInt(); sum1+=l;sum2+=r; if(l>r) { f1=1; if((l-r)>max1) { max1=l-r; maxi1=i; } } else if(l<r) { f2=1; if((r-l)>max2) { max2=r-l; maxi2=i; } } } if(f1==1&&f2==1) { if(sum2-sum1+2*max1>=sum1-sum2+2*max2) System.out.println(maxi1+1); else System.out.println(maxi2+1); } else System.out.println("0"); } } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = input() l = 0 r = 0 fir = [] sec = [] for _ in xrange(n): a,b = map(int,raw_input().split()) fir.append(a) sec.append(b) l += a r += b d = abs(l-r) cur = d ind = 0 if l > r: for i in xrange(n): a = fir[i]-sec[i] if a > 0: if 2*a -d > cur: cur = 2*a - d ind = i+1 else: if 2*abs(a) + d > cur: cur = 2*abs(a)+d ind = i+1 elif r > l: for i in xrange(n): a = sec[i]-fir[i] if a > 0: if 2*a-d > cur: cur = 2*a - d ind = i+1 else: if 2*abs(a) + d > cur: cur = 2*abs(a) +d ind = i+1 else: for i in xrange(n): a = fir[i]-sec[i] if abs(a) > cur: cur = abs(a) ind = i+1 if i == 0: print 0 else: print ind
PYTHON
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int l[n], r[n]; for (int i = 0; i < n; i++) cin >> l[i] >> r[i]; int lsum = 0, rsum = 0; for (int i = 0; i < n; i++) { lsum += l[i]; rsum += r[i]; } int maxdiff = abs(lsum - rsum), maxdiffindex = 0; bool ispossible = false; for (int i = 0; i < n; i++) { int t1 = lsum + r[i] - l[i]; int t2 = rsum + l[i] - r[i]; if (maxdiff < abs(t1 - t2)) { ispossible = true; maxdiff = abs(t1 - t2); maxdiffindex = i; } } if (ispossible) cout << (maxdiffindex + 1) << "\n"; else cout << 0 << "\n"; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#!/usr/bin/env python3 def main(): n = int(input()) l = [0] * n r = [0] * n for i in range(n): l[i], r[i] = [int(x) for x in input().split()] total_r = sum(r) total_l = sum(l) beauty = abs(total_l - total_r) new_beauty, row = max(( abs(total_l-total_r + 2*r[i] - 2*l[i]) , i) for i in range(n)) if new_beauty <= beauty: print(0) else: print(row+1) if __name__ == "__main__": main()
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.Scanner; public class parade { public static void main(String[] args) { Scanner scan=new Scanner(System.in); int n=scan.nextInt(), i, l_total=0, r_total=0, beauty, max_diff=0, min_diff=0; int[] l=new int[n]; int[] r=new int[n]; int[] diff=new int[n]; for (i=0; i<n; i++) { l[i]=scan.nextInt(); r[i]=scan.nextInt(); diff[i]=l[i]-r[i]; if (diff[i]>diff[max_diff]) { max_diff=i; } if (diff[i]<diff[min_diff]) { min_diff=i; } l_total+=l[i]; r_total+=r[i]; } beauty=l_total-r_total; if (Math.abs(diff[min_diff]) > Math.abs(beauty) || diff[max_diff] > Math.abs(beauty)) { if (Math.abs(diff[min_diff])>diff[max_diff]) { System.out.println(min_diff+1); } else { System.out.println(max_diff+1); } } else { if (beauty<0) { if (diff[max_diff]>0) { System.out.println(max_diff+1); } else { System.out.println(0); } } else if (beauty>0) { if (diff[min_diff]<0) { System.out.println(min_diff+1); } else { System.out.println(0); } } } } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int n, a[100005], b[100005], now, mx, swt; int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; now += a[i] - b[i]; } mx = max({mx, abs(now)}); for (int i = 0; i < n; ++i) { if (abs(now - a[i] + b[i] - a[i] + b[i]) > mx) { mx = abs(now - a[i] + b[i] - a[i] + b[i]); swt = i + 1; } } cout << swt << endl; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.*; import java.io.*; public class B { FastScanner in; PrintWriter out; boolean systemIO = true; public void solve() { int n = in.nextInt(); int[] a = new int[n]; int sum = 0; for (int i = 0; i < a.length; i++) { a[i] = in.nextInt() - in.nextInt(); sum += a[i]; } int max = Math.abs(sum); int x = 0; for (int i = 0; i < a.length; i++) { if (Math.abs(sum - 2 * a[i]) > max) { max = Math.abs(sum - 2 * a[i]); x = i + 1; } } out.println(x); } public void run() { if (systemIO) { in = new FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new FastScanner(new File("test.out")); out = new PrintWriter(System.out); } solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA public static void main(String[] arg) { new B().run(); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
def parad(l, r): s1, s2 = sum(l), sum(r) ans, ch = abs(s1 - s2), 0 for i in range(len(l)): z = abs(((s1 - l[i]) + r[i]) - ((s2 - r[i]) + l[i])) if z > ans: ans, ch = z, i + 1 return ch n = int(input()) L, R = list(), list() for j in range(n): s, t = [int(x) for x in input().split()] L.append(s) R.append(t) print(parad(L, R))
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> int main() { int n; std::map<int, int> m; std::cin >> n; std::vector<int> deviations(n); int total = 0; for (int i = 0; i < n; ++i) { int l, r; std::cin >> l >> r; total = total + l - r; deviations[i] = 2 * r - 2 * l; } for (int i = 0; i < n; ++i) { m.insert({std::abs(total + deviations[i]), i + 1}); } if (std::abs(total) > m.rbegin()->first) { std::cout << "0\n"; } else { std::cout << m.rbegin()->second << "\n"; } return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
number = int(raw_input()) left,right = {},{} for i in xrange(number): a,b = (raw_input().strip()).split() a,b = int(a),int(b) left[i] = a right[i] = b suml,sumr = sum(left.values()), sum(right.values()) diff = {} checkl,checkr = 1,1 for i in xrange(number): diff[i] = left[i] - right[i] if left[i] > right[i]: checkr = 0 elif right[i] > left[i]: checkl = 0 if checkl or checkr: print 0 else: if suml+(-1)*min(diff.values()) > sumr+max(diff.values()): k = min(diff.values()) for key,val in diff.iteritems(): if val == k: print key+1 break elif suml+(-1)*min(diff.values()) <= sumr+max(diff.values()): k = max(diff.values()) for key,val in diff.iteritems(): if val == k: print key+1 break
PYTHON
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) l = [] r = [] L = 0 R = 0 d = 0 chg = 0 for i in range(n): a, b = map(int, input().split()) l.append(a) r.append(b) L = L + l[i] R = R + r[i] d = abs(L-R) for i in range(n): if d < abs((L-l[i]+r[i]) - (R-r[i]+l[i])): d = abs((L-l[i]+r[i]) - (R-r[i]+l[i])) chg = i+1 print(chg)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Marazzi Francesco */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int[] left = new int[n]; int[] right = new int[n]; int L = 0, R = 0, odiff = 0; for (int i = 0; i < n; i++) { left[i] = in.nextInt(); right[i] = in.nextInt(); L += left[i]; R += right[i]; } odiff = Math.abs(L - R); int ndiff, index = 0; for (int i = 0; i < n; i++) { ndiff = Math.abs((L + (right[i] - left[i])) - (R + (left[i] - right[i]))); if (ndiff > odiff) { index = i + 1; odiff = ndiff; } } out.print(index); } } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
number_of_column = int(raw_input()) L, R = [], [] for i in range(number_of_column): l, r = map(int, raw_input().split()) L.append(l) R.append(r) max_seed = abs(sum(L) - sum(R)) max_temp, max_index = -1, -1 sum_left_total = sum(L) sum_right_total = sum(R) for i in range(number_of_column): new_left_sum = sum_left_total + R[i] - L[i] new_right_sum = sum_right_total + L[i] - R[i] temp_beauty = abs(new_left_sum - new_right_sum) if temp_beauty > max_seed: max_seed = temp_beauty max_index = i if max_index == -1: print 0 else: print max_index + 1
PYTHON
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; long int abc(long int x) { if (x < 0) return -x; else return x; } int main() { long int n, a, b, suma = 0, sumb = 0, maxa = 0, maxb = 0, na = 0, nb = 0, ans1, ans2, ans3; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; suma += a; sumb += b; if (b - a > maxa) { maxa = b - a; na = i + 1; } if (a - b > maxb) { maxb = a - b; nb = i + 1; } } ans1 = abc(suma - sumb); ans2 = abc(suma + maxa * 2 - sumb); ans3 = abc(sumb + maxb * 2 - suma); if ((ans1 > ans2) && (ans1 > ans3)) cout << 0; else if (ans2 > ans3) cout << na; else cout << nb; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 100; pair<pair<int, int>, int> val[maxn]; long long res = 0; long long resind = 0; int main() { ios::sync_with_stdio(false); int n; cin >> n; int sum = 0; for (int i = 0; i < n; i++) { cin >> val[i].first.first >> val[i].first.second; sum += val[i].first.first; sum -= val[i].first.second; } if (res < abs(sum)) { res = abs(sum); resind = 0; } for (int i = 0; i < n; i++) { sum -= val[i].first.first; sum += val[i].first.second; sum += val[i].first.second; sum -= val[i].first.first; if (res < abs(sum)) { res = abs(sum); resind = i + 1; } sum -= val[i].first.second; sum += val[i].first.first; sum += val[i].first.first; sum -= val[i].first.second; } cout << resind; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n=int(input()) T=[] J=[] l=0 r=0 for i in range(n): L,R=map(int,input().split()) T.append(L) J.append(R) l+=L r+=R B=abs(l-r) ind=0 for i in range(n): x=T[i] y=J[i] h=abs((l-x+y)-(r+x-y)) if B<h: B=h ind=i+1 print(ind)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) ans = 0 l = 0 r = 0 ml = 0 mr = 0 a = [] for i in range(n): x,y = map(int,input().split()) a.append((x,y)) l+=x r+=y best = abs(l - r) c = 0 new = 0 for v in a: c+=1 nl = l nr = r nl = nl - v[0] + v[1] nr = nr - v[1] + v[0] if abs(nl-nr) > best: new = c best = abs(nl-nr) print(new)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) columns = [] l_tot = 0 r_tot = 0 for i in range(n): l, r = [int(j) for j in input().split()] columns.append((l, r)) l_tot += l r_tot += r beauty = abs(l_tot - r_tot) best_idx = -1 for i, c in enumerate(columns): l, r = c tmp_beauty = abs((l_tot - l + r) - (r_tot - r + l)) if tmp_beauty > beauty: beauty = tmp_beauty best_idx = i if best_idx < 0: print(0) else: print(best_idx+1)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Thu Nov 3 19:54:47 2016 @author: KFakharany """ numberofcolumns = input() sumL = 0 sumR = 0 change = 0 inputs = [None] * (numberofcolumns * 2) for i in range(numberofcolumns) : a=map(int,raw_input().split()) inputs[i*2]=a[0] inputs[(i*2)+1]=a[1] for i in range(numberofcolumns) : sumL += inputs[i*2] for i in range(numberofcolumns) : sumR += inputs[(i*2)+1] beuty = abs(sumL-sumR) for i in range(numberofcolumns) : tempL = sumL tempR = sumR tempL = tempL - inputs[i*2] + inputs[(i*2)+1] tempR = tempR + inputs[i*2] - inputs[(i*2)+1] tempB = abs(tempL - tempR) if tempB > beuty : beuty = tempB change = i+1 print change
PYTHON
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import sun.rmi.server.InactiveGroupException; import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; PrintStream out = System.out; // try { // inputStream = new FileInputStream("input.txt"); // } catch (FileNotFoundException e) { // System.exit(13); // } Scanner in = new Scanner(inputStream); int n = in.nextInt(); List<Integer> l = new ArrayList<>(), r = new ArrayList<>(); int L = 0, R = 0; while (n --> 0) { l.add(in.nextInt()); r.add(in.nextInt()); L += l.get(l.size() - 1); R += r.get(r.size() - 1); } n = l.size(); int beauty = Math.abs(L - R); int answer = 0; for (int i = 0; i < n; ++i) { int candidate = Math.abs(L - l.get(i) + r.get(i) - (R - r.get(i) + l.get(i))); if (candidate > beauty) { beauty = candidate; answer = i + 1; } } out.println(answer); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n=int(input()) pairs,lsum,rsum=[],0,0 for i in range(n): a,b=map(int,input().split()) pairs.append((a,b)) lsum+=a rsum+=b x=abs(lsum-rsum) index=-1 for i in range(n): l,r=lsum,rsum l=l-pairs[i][0] r=r-pairs[i][1] l+=pairs[i][1] r+=pairs[i][0] a=abs(l-r) if a>x: x=a index=i+1 if index==-1: print(0) else: print(index)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import math n = int(input()) buff = [] for i in range(n): a, b = map(int, input().strip().split()) buff.append(a - b) answ = 0 for i in range(n): answ += buff[i] answ = math.fabs(answ) l = 0 k = 0 mi = buff.index(min(buff)) ma = buff.index(max(buff)) buff[mi] = -1 * buff[mi] for i in range(n): k += buff[i] k = math.fabs(k) if k > answ: answ = k l = mi + 1 buff[mi] = buff[mi] * -1 buff[ma] = -1 * buff[ma] k = 0 for i in range(n): k += buff[i] k = math.fabs(k) if k > answ: answ = k l = ma + 1 print(l)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; inline int abx(int x) { if (x > 0) return x; else return -x; } inline int Mx(int x, int y) { if (x > y) return x; else return y; } int n, l[maxn], r[maxn]; int ans, maxm, maxl, maxr, tmpl, tmpr; int main() { memset(l, 0, sizeof(l)); memset(r, 0, sizeof(r)); maxl = maxr = ans = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d %d", &l[i], &r[i]); maxl += l[i]; maxr += r[i]; } maxm = abx(maxl - maxr); for (int j = 1; j <= n; j++) { tmpl = maxl - l[j] + r[j]; tmpr = maxr - r[j] + l[j]; if (abx(tmpl - tmpr) > maxm) { maxm = abx(tmpl - tmpr); ans = j; } } printf("%d\n", ans); return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) ALLL = 0 ALLR = 0 MLP = 0 MRP = 0 MLPI = 0 MRPI = 0 for i in range(n): inp = input().split() L = int(inp[0]) R = int(inp[1]) ALLL += L ALLR += R LP = R - L RP = L - R if LP>MLP: MLP = LP MLPI = i if RP>MRP: MRP = RP MRPI = i ML = ALLL - ALLR + 2*MLP MR = ALLR - ALLL + 2*MRP if ML>MR: if MLP == 0: print(0) else: print(MLPI+1) else: if MRP == 0: print(0) else: print(MRPI+1)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.*; import java.io.*; import java.util.regex.*; public class Main { public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); Scanner scan = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int n = scan.nextInt(); int selisihGlobal = 0; boolean alwaysAsc = true; boolean alwaysDesc = true; int[] listLeft = new int[n]; int[] listRight = new int[n]; for(int ii=0; ii<n; ii++){ int left = scan.nextInt(); int right = scan.nextInt(); listLeft[ii] = left; listRight[ii] = right; if(left > right){ alwaysAsc = false; } if(right > left){ alwaysDesc = false; } selisihGlobal = selisihGlobal + (left - right); } if(alwaysAsc || alwaysDesc){ System.out.println(0); } else{ int tempMax = Math.abs(selisihGlobal); int maxIndex = 0; for(int ii=0; ii<n; ii++) { int tempLeft = listLeft[ii]; int tempRight = listRight[ii]; int tempSelisih = Math.abs(selisihGlobal - (tempLeft - tempRight) + (tempRight - tempLeft)); if(tempSelisih > tempMax){ tempMax = tempSelisih; maxIndex = ii; } } System.out.println(maxIndex+1); } } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; const int big = 1e5 + 7; int l[big], r[big], record[big]; int main() { int n, num = 0, p = 0, q = 0, large, temp; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d %d", &l[i], &r[i]); p += l[i]; q += r[i]; } large = p - q; if (large < 0) large = -large; record[num++] = 0; for (int i = 1; i <= n; i++) { p += (r[i] - l[i]); q += (l[i] - r[i]); temp = p - q; if (temp < 0) temp = -temp; if (temp == large) record[num++] = i; else if (temp > large) { large = abs(p - q); num = 0; record[num++] = i; } p += (l[i] - r[i]); q += (r[i] - l[i]); } printf("%d", record[0]); return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; import java.util.TreeMap; public class problem1{ public static void main(String[]args) throws IOException { // BufferedReader ll = new BufferedReader(new FileReader("input.txt")); BufferedReader ll = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(ll.readLine()); String[] t = null; int [][] arr = new int[n][2]; int sum0 = 0; int sum1 = 0; for(int i=0;i<n;i++){ t = (ll.readLine()).split(" "); int n0 = Integer.parseInt(t[0]); int n1 = Integer.parseInt(t[1]); sum0 +=n0; sum1 +=n1; arr[i][0] = n0; arr[i][1] = n1; } int max = Math.abs(sum0-sum1); int max_i = -1; for(int i=0;i<n;i++){ int tem0 = sum0-arr[i][0] + arr[i][1]; int tem1 = sum1-arr[i][1] + arr[i][0]; int abs = Math.abs(tem0-tem1); if(abs>max){ max = abs; max_i = i; } } System.out.println(max_i+1); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int a[100005], b[100005]; int main() { int n, s1 = 0, s2 = 0, max, col = 0; cin >> n; for (int i = 1; i <= n; ++i) { int x, y; cin >> x >> y; s2 += y; s1 += x; a[i] = x; b[i] = y; } max = abs(s1 - s2); for (int i = 1; i <= n; ++i) { int x = s1 - a[i] + b[i]; int y = s2 + a[i] - b[i]; if (abs(x - y) > max) { max = abs(x - y); col = i; } } cout << col; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int i =0; List<Integer> left = new ArrayList<Integer>(); List<Integer> right = new ArrayList<Integer>(); int lftsum = 0; int rightsum = 0; int buty = 0; int ansi = 0; while ( i < n) { int a = in.nextInt(); int b = in.nextInt(); left.add(a); right.add(b); lftsum = lftsum + a; rightsum = rightsum + b; i++; } // System.out.println("initial buty: " + buty); buty = Math.abs(lftsum - rightsum); int ans = buty; for (i = 0; i < left.size(); i++) { int newbuty = Math.abs((lftsum - left.get(i) + right.get(i)) - (rightsum + left.get(i) - right.get(i))); // System.out.println(newbuty); if (newbuty > ans) { ansi = i+1; ans = newbuty; } } System.out.println(ansi); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
if __name__=='__main__': n = int(input()) a,b = list(),list() for _ in range(n): x,y = map(int,input().split()) a.append(x) b.append(y) ll1 = sum(a) rr1 = sum(b) res = abs(ll1-rr1) ans = 0 for i in range(n): ll = ll1 rr = rr1 ll -= a[i] ll += b[i] rr -= b[i] rr += a[i] if res < abs(ll-rr): ans = i+1 res = abs(ll-rr) print(ans)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1000050; int n, suml, sumr, l[maxn], r[maxn]; void init() { suml = sumr = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &l[i], &r[i]); suml += l[i]; sumr += r[i]; } } void work() { int maxb = abs(suml - sumr), ans = 0; for (int i = 0; i < n; i++) { if (abs((suml - l[i] + r[i]) - (sumr - r[i] + l[i])) > maxb) { maxb = abs((suml - l[i] + r[i]) - (sumr - r[i] + l[i])); ans = i + 1; } } cout << ans << endl; } int main() { init(); work(); fclose(stdin); fclose(stdout); return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.io.*; import java.net.HttpURLConnection; import java.net.URLConnection; import java.util.Scanner; import java.util.StringTokenizer; /** * Created by hapsidra on 22.10.2016. */ public class Main { public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String fileName) { try { br = new BufferedReader(new FileReader(fileName)); } catch (FileNotFoundException e) { } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } public static boolean findNext(int a[]) { for(int i=a.length-2;i>=0;i--){ if(a[i]<a[i+1]){ int r=i+1; for(int j=a.length-1;j>i+1;j--){ if(a[j]>a[i]){ r=j; break; } } int t=a[r]; a[r]=a[i]; a[i]=t; for(int j=0;j<(a.length-(i+1))/2;j++){ t=a[j+i+1]; a[j+i+1]=a[a.length-1-j]; a[a.length-1-j]=t; } return true; } } return false; } static int factorial(int n) { if (n == 1) return 1; else return n * factorial(n - 1); } public static void main(String args[]) throws Exception { FastScanner in=new FastScanner(); int n=in.nextInt(); long l[]=new long[n]; long r[]=new long[n]; long L=0; long R=0; for(int i=0;i<n;i++){ l[i]=in.nextLong(); r[i]=in.nextLong(); L+=l[i]; R+=r[i]; } int ans=0; long t_L=0,t_R,best=Math.abs(L-R); for(int i=0;i<n;i++){ t_L=L-l[i]; t_L+=r[i]; t_R=R-r[i]; t_R+=l[i]; if(Math.abs(t_R-t_L)>best){ best=Math.abs(t_R-t_L); ans=i+1; } } System.out.println(ans); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import itertools from fractions import gcd from math import sqrt from bisect import bisect_left import heapq def get(a): return map(a , raw_input().split()) def I(): return int(raw_input()) def Str(): return raw_input() n = I() store = [] sm_1 , sm_2 = 0,0 for i in xrange(n): a , b = get(int) store.append((a,b)) sm_1 += a sm_2 += b cur = abs(sm_1 - sm_2) #print cur greedy = dict() k = 0 for i in store: cost = abs((sm_1 - i[0]+i[1]) - (sm_2 - i[1]+i[0])) #print cost, greedy[cost] = k k += 1 ans = max(greedy.keys()) if ans <= cur: print 0 else:print greedy[ans] + 1
PYTHON
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int l[100100], r[100100], a[100100]; int main() { int n, lsum, rsum, x, m, f; while (~scanf("%d", &n)) { lsum = 0; rsum = 0; for (int i = 1; i <= n; i++) { scanf("%d%d", &l[i], &r[i]); lsum += l[i]; rsum += r[i]; } m = abs(lsum - rsum); f = 0; for (int i = 1; i <= n; i++) { lsum = lsum - l[i] + r[i]; rsum = rsum - r[i] + l[i]; if (abs(lsum - rsum) > m) { f = 1; m = abs(lsum - rsum); x = i; } lsum = lsum - r[i] + l[i]; rsum = rsum - l[i] + r[i]; } if (f) cout << x << endl; else cout << 0 << endl; } return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskCF378D2B solver = new TaskCF378D2B(); solver.solve(1, in, out); out.close(); } static class TaskCF378D2B { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); int[] lis = new int[n]; int[] ris = new int[n]; int L = 0; int R = 0; for (int i = 0; i < n; i++) { int li = in.nextInt(); int ri = in.nextInt(); lis[i] = li; ris[i] = ri; L += li; R += ri; } int startBeauty = Math.abs(L - R); int maxBeauty = startBeauty; int maxIndex = 0; for (int i = 0; i < n; i++) { int newL = L - lis[i] + ris[i]; int newR = R - ris[i] + lis[i]; int b = Math.abs(newL - newR); if (b > maxBeauty) { maxBeauty = b; maxIndex = i + 1; } } out.println(maxIndex); } } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.io.*; import java.util.*; public final class cf378div2Bfinal { public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int[][] a=new int[n][2]; int max,suml=0,sumr=0,sumrf=0,sumlf=0; for(int i=0;i<n;i++){ a[i][0]=in.nextInt(); a[i][1]=in.nextInt(); sumlf+=a[i][0]; sumrf+=a[i][1]; } int max1=Math.abs(sumrf-sumlf); max=max1; //int next=-1; int pos=0; for(int i=0;i<n;i++){ suml=sumlf-a[i][0]+a[i][1]; sumr=sumrf-a[i][1]+a[i][0]; if(Math.abs(sumr-suml) > max){ max=Math.abs(sumr-suml); pos=i; //System.out.println("this is "+i+" max is "+max+" "+suml+" "+sumr ); } // System.out.println(max); } if(max1==max){ System.out.println(0); }else{ System.out.println(pos+1); } //System.out.println(max); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int arr[10000000], brr[10000000]; int main() { int n, i, j, k, l, t, r, e = 0; ; int mi = 1; cin >> n; long long sum = 0, rum = 0, d, m; for (i = 0; i < n; i++) { cin >> arr[i] >> brr[i]; sum += arr[i]; rum += brr[i]; } long long mx = abs(sum - rum); for (i = 0; i < n; i++) { d = sum - arr[i]; d += brr[i]; m = rum - brr[i]; m += arr[i]; if (abs(d - m) > mx) { e = i + 1; mx = abs(d - m); } } cout << e << endl; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import sys import math import itertools import collections def ii(): return int(input()) def mi(): return map(int, input().split()) def li(): return list(map(int, input().split())) def lcm(a, b): return abs(a * b) // math.gcd(a, b) def wr(arr): return ' '.join(map(str, arr)) def revn(n): return str(n)[::-1] def dd(): return collections.defaultdict(int) def ddl(): return collections.defaultdict(list) def sieve(n): if n < 2: return list() prime = [True for _ in range(n + 1)] p = 3 while p * p <= n: if prime[p]: for i in range(p * 2, n + 1, p): prime[i] = False p += 2 r = [2] for p in range(3, n + 1, 2): if prime[p]: r.append(p) return r def divs(n, start=1): r = [] for i in range(start, int(math.sqrt(n) + 1)): if (n % i == 0): if (n / i == i): r.append(i) else: r.extend([i, n // i]) return r def divn(n, primes): divs_number = 1 for i in primes: if n == 1: return divs_number t = 1 while n % i == 0: t += 1 n //= i divs_number *= t def prime(n): if n == 2: return True if n % 2 == 0 or n <= 1: return False sqr = int(math.sqrt(n)) + 1 for d in range(3, sqr, 2): if n % d == 0: return False return True def convn(number, base): newnumber = 0 while number > 0: newnumber += number % base number //= base return newnumber def cdiv(n, k): return n // k + (n % k != 0) n = ii() lb = rb = 0 p = [] for i in range(n): l, r = mi() p.append([l, r]) lb += l rb += r b = abs(rb - lb) ans = 0 for i in range(n): tb = abs(rb - p[i][1] + p[i][0] - (lb - p[i][0] + p[i][1])) if tb > b: ans = i + 1 b = tb print(ans)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n, r = 0, l = 0; vector<int> d; cin >> n; d.resize(n); for (int i = 0; i < n; ++i) { int x, y; cin >> x >> y; l += x; r += y; d[i] = x - y; } int maxx = abs(r - l), n_max = -1; for (int i = 0; i < n; ++i) { int dl = l - d[i], dr = r + d[i]; if (abs(dr - dl) > maxx) { maxx = abs(dr - dl); n_max = i + 1; } } if (n_max == -1) cout << 0 << endl; else cout << n_max; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; pair<long long, long long> arr[100009]; int main() { int n; scanf("%d", &n); long long L = 0, R = 0; for (int i = 0; i < n; i++) { cin >> arr[i].first >> arr[i].second; L += arr[i].first; R += arr[i].second; } long long mx = ((L - R) > 0 ? (L - R) : (-(L - R))); int ind = -1; for (int i = 0; i < n; i++) { long long l = arr[i].first, r = arr[i].second; long long l1 = L - l + r, r1 = R - r + l; long long mx1 = ((l1 - r1) > 0 ? (l1 - r1) : (-(l1 - r1))); if (mx < mx1) { mx = mx1; ind = i; } } if (ind != -1) cout << ind + 1 << endl; else cout << "0\n"; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n=int(input()) l=[list(map(int,input().split())) for _ in range(n)] for i in range(n): l[i]=l[i][0]-l[i][1] ans=0 su=sum(l) ma=abs(su) for i in range(n): if abs(su-l[i]-l[i])>ma: ma=abs(su-l[i]-l[i]); ans=i+1 print(ans)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
// Working program with FastReader import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class TestClass { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[] ) throws Exception { FastReader sc=new FastReader(); int n=sc.nextInt(); int[][] a=new int[n][2]; long sum_0=0,sum_1=0; for(int i=0;i<n;i++){ a[i][0]=sc.nextInt(); a[i][1]=sc.nextInt(); sum_0+=a[i][0]; sum_1+=a[i][1]; } long ans=Math.abs(sum_0-sum_1); long ans_t=0; long max=ans; int ans_col=0; for(int i=0;i<n;i++){ ans_t=Math.abs((sum_0-a[i][0]+a[i][1])-(sum_1-a[i][1]+a[i][0])); if(ans_t>max){ max=Math.max(max,ans_t); if(max==ans_t){ ans_col=i+1; } } } System.out.println(ans_col); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int n; while (cin >> n) { int L, R; L = R = 0; vector<int> r(n, 0), l(n, 0); for (int i = 0; i < n; ++i) { cin >> l[i] >> r[i]; L += l[i]; R += r[i]; } int ans = 0, b = abs(L - R); for (int i = 0; i < n; ++i) { int dif = abs((L - l[i] + r[i]) - (R - r[i] + l[i])); if (dif > b) { b = dif; ans = i + 1; } } cout << ans << endl; } }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int n, x[1000001], y[1000001]; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> n; vector<int> vec; long long sum1 = 0, sum2 = 0; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; sum1 += x[i]; sum2 += y[i]; } int ans1 = 0, ans2 = 0; for (int i = 0; i < n; i++) { if (x[i] >= y[i]) { ans1++; } } for (int i = 0; i < n; i++) { if (x[i] <= y[i]) { ans2++; } } if (ans1 == n || ans2 == n) { cout << 0; return 0; } int cur = 0; for (int i = 0; i < n; i++) { cur = max(x[i], y[i]); vec.push_back(sum1 - x[i] + cur); vec.push_back(sum2 - y[i] + cur); } int mx = vec[0]; int in = 0; for (int i = 0; i < vec.size(); i++) { if (mx < vec[i]) { mx = vec[i]; in = i; } } if ((in + 1) % 2 == 0) { cout << (in + 1) / 2; } else { cout << (in + 1) / 2 + 1; } return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
arr = [] L, R = 0, 0 for _ in range(int(input().strip())): l, r = map(int, input().strip().split()) L += l R += r arr.append((l, r)) currBeauty = abs(L-R) ans = 0 for i in range(len(arr)): l, r = arr[i] newL = L-l+r newR = R-r+l newBeauty = abs(newL-newR) if newBeauty > currBeauty: currBeauty = newBeauty ans = i+1 print(ans)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()); l = [0] * n; r = [0] * n; cur_res = 0; for i in range(n): l[i], r[i] = [int(x) for x in input().split()]; cur_res += l[i] - r[i]; max_res = abs(cur_res); max_indx = -1; for i in range(n): tmp_res = abs(cur_res - 2 * (l[i] - r[i])); if tmp_res > max_res: max_res = tmp_res; max_indx = i; print(max_indx + 1);
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = input() a = [0] * n for i in range(n): x, y = map(int, raw_input().split()) a[i] = x - y s = sum(a) j = -1 m = abs(s) for i in range(n): if abs(s - 2*a[i]) > m: j = i m = abs(s - 2*a[i]) print j + 1
PYTHON
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Code { public static void main(String[] args) { // Use the Scanner class Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] l = new int[n]; int[] r = new int[n]; for (int i = 0; i < n; ++i) { l[i] = sc.nextInt(); r[i] = sc.nextInt(); } System.out.println(f(l,r,n)); } static int f(int[] l, int[] r, int n) { int s_l = 0; int s_r = 0; for (int i = 0; i < n; ++i) { s_l += l[i]; s_r += r[i]; } int cur_beauty = Math.abs(s_l- s_r); int res = 0; for (int i =0; i < n; ++i) { int new_s_l = s_l - l[i] + r[i]; int new_s_r = s_r - r[i] + l[i]; int new_beauty = Math.abs(new_s_r - new_s_l); if (new_beauty > cur_beauty) { cur_beauty = new_beauty; res = i + 1; } } return res; } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; int main() { int t, n, a, b, high1, high2, temp1, temp2, cat1, cat2, c, d, e, f; long long sum1 = 0, sum2 = 0, s1, s2, ss1, ss2; cin >> t; n = 1; int l = 0, r = 0; high1 = -9999999; high2 = -9999999; cat1 = 0; cat2 = 0; while (n <= t) { cin >> a >> b; sum1 += a; sum2 += b; temp1 = a - b; temp2 = b - a; if (a > b) { l = 1; } else if (b > a) { r = 1; } if (temp1 > high1) { high1 = temp1; cat1 = n; c = a; d = b; } if (temp2 > high2) { high2 = temp2; cat2 = n; e = a; f = b; } n++; } if (l == 1 && r == 1) { s1 = sum1 - c + d; s2 = sum2 - d + c; ss1 = sum1 - e + f; ss2 = sum2 + e - f; if (s1 > ss1) sum1 = s1; else sum1 = ss1; if (s2 > ss2) sum2 = s2; else sum2 = ss2; if (sum1 > sum2) { cout << cat2 << endl; } else if (sum1 < sum2) cout << cat1 << endl; else { if (high1 > high2) cout << cat1 << endl; else if (high1 < high2) cout << cat2 << endl; else { if (cat1 < cat2) cout << cat1 << endl; else cout << cat2 << endl; } } } else cout << "0" << endl; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
from sys import stdin lines = stdin.read().splitlines() lines.pop(0) rows = [] initialLeftSum = 0 initialRightSum = 0 for line in lines: row = [int(x) for x in line.split()] rows.append(row) initialLeftSum += row[0] initialRightSum += row[1] maxBeauty = abs(initialLeftSum - initialRightSum) rowtoswitch = 0 for idx,row in enumerate(rows): rowMaxBeauty = abs(initialLeftSum - initialRightSum + 2*(row[1] - row[0])) if(rowMaxBeauty > maxBeauty): maxBeauty = rowMaxBeauty rowtoswitch = idx + 1 print(rowtoswitch)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 2e18; const double PI = acos(-1.0); const double E = exp(1); const double EPS = 1e-8; const long long mod = 1e4 + 7; const int maxn = 1e2 + 5; void pre() { freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout); } pair<int, int> pr[100000 + 5]; int ll = 0, rr = 0; int main(void) { int T; cin >> T; for (int i = 0; i < (T); i++) { int l, r; cin >> l >> r; ll += l, rr += r; pr[i].first = l, pr[i].second = r; } int max_ans = abs(ll - rr); int put = 0; for (int i = 0; i < (T); i++) { int lll = ll, rrr = rr; lll = lll - pr[i].first + pr[i].second; rrr = rrr - pr[i].second + pr[i].first; if (max_ans < abs(lll - rrr)) { max_ans = abs(lll - rrr); put = i + 1; } } cout << put << endl; return 0; }
CPP
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.*; import org.omg.PortableInterceptor.INACTIVE; public class CodeforcesRound378 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int l[] = new int[n+1], r[] = new int[n+1]; int max = 0, L = 0, R = 0, maxIndex = 0; for (int i=0; i<n; i++) { l[i] = input.nextInt(); r[i] = input.nextInt(); L += l[i]; R += r[i]; } max = Math.abs(L-R); maxIndex = 0; for (int i=0; i<n; i++) { int newL = L - l[i] + r[i]; int newR = R - r[i] + l[i]; if (Math.abs(newL - newR) > max) { max = Math.abs(newL - newR); maxIndex = i + 1; } } System.out.println(maxIndex); } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n = int(input()) t = [] for i in range(n): l, r = map(int, input().split()) t.append((l, r)) L = 0 R = 0 for i in t: L += i[0] R += i[1] m = abs(L - R) mi = 0 for i in range(n): l, r = t[i][0], t[i][1] if l == r: continue d = abs((L - l + r) - (R - r + l)) if d > m: m = d mi = i if m == abs(L - R): print(0) else: print(mi + 1)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
n=int(input()) arr = [tuple(map(int, input().split())) for i in range(n)] sl, sr = (0,0) for h in arr: sl+=h[0]; sr+=h[1] mx, a, i = abs(sl-sr), -1, 0 for h in arr: si = abs((sl-h[0]+h[1]) - (sr-h[1]+h[0])) if si>mx: mx,a=si,i i+=1 print(a+1)
PYTHON3
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] l = new int[n]; int[] r = new int[n]; for (int i = 0; i < n; i++) { l[i] = sc.nextInt(); r[i] = sc.nextInt(); } System.out.println(solve(l, r)); sc.close(); } static int solve(int[] l, int[] r) { int sumL = Arrays.stream(l).sum(); int sumR = Arrays.stream(r).sum(); int result = 0; int maxDiff = Math.abs(sumL - sumR); for (int i = 0; i < l.length; i++) { int diff = Math.abs((sumL - l[i] + r[i]) - (sumR - r[i] + l[i])); if (diff > maxDiff) { result = i + 1; maxDiff = diff; } } return result; } }
JAVA
733_B. Parade
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step. There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg. The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|. No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty. Input The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively. Output Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached. Consider that columns are numbered from 1 to n in the order they are given in the input data. If there are several answers, print any of them. Examples Input 3 5 6 8 9 10 3 Output 3 Input 2 6 5 5 6 Output 1 Input 6 5 9 1 3 4 8 4 5 23 54 12 32 Output 0 Note In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5. If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9. It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
2
8
#! /usr/bin/env python # -*- coding: utf-8 -*- if __name__ == '__main__': n = raw_input() l = [] ll = [] sum_a, sum_b = 0, 0 for i in range(int(n)): a = raw_input().split(' ') l.append(int(a[0])-int(a[1])) ll.append((int(a[0]),int(a[1]))) sum_a += int(a[0]) sum_b += int(a[1]) cn = abs(sum_a - sum_b) l = sorted(l) if len(l) >= 2: sum1 = abs(sum_a - sum_b - 2*l[0]) sum2 = abs(sum_a - sum_b - 2*l[-1]) if sum1 < sum2: sum1 = sum2 l[0] = l[-1] if sum1 > cn: for i in range(len(ll)): if ll[i][0] - ll[i][1] == l[0]: print i + 1 break else: print 0 else: print 0
PYTHON