A - Sereja and Dima
Problem Link: [Codeforces 381A##link##]
- [tab]
- C++
#include<bits/stdc++.h> using namespace std; // #define int long long #define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define endl "\n" #define MAX 100000 int main(){ fast; int n; cin>>n; vector<int> score(n); for(int i=0;i<n;i++){ cin>>score[i]; } int i = 0; int j = n-1; int sereja=0,dima=0; bool sturn = 0 , dturn = 1; while(i<=j){ if(score[i] > score[j]){ if(sturn == 0){ sereja += score[i]; }else{ dima += score[i]; } i++; sturn = !sturn; dturn = !dturn; }else{ if(sturn == 0){ sereja += score[j]; }else{ dima += score[j]; } sturn = !sturn; dturn = !dturn; j--; } } cout<<sereja<<" "<<dima<<endl; }
- Python3
ln = int(input()) crds = list(map(int, input().split(" "))) s = 0 d = 0 trn = 1 while len(crds)>0: if crds[0]>crds[-1]: mx = crds[0] del crds[0] else: mx = crds[-1] del crds[-1] if trn%2!=0: s += mx else: d += mx trn += 1 print(s, d)
- JAVA
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int ar[]=new int[n]; int t,s=0,d=0; for(t=0;t<n;t++){ ar[t]=sc.nextInt(); } int a=0,z=n-1,f=0; for(t=0;t<n;t++){ if(ar[a]>ar[z]){ if(f==0){ s+=ar[a];f=1;a++; } else{ d+=ar[a];a++;f=0;} }else{ if(f==0){ s+=ar[z];f=1;z--; } else{ d+=ar[z];z--;f=0;} } } System.out.println(s+" "+d); } }