Sereja and Dima play a game. The rules of the game are very simple. The players have n cards in a row. Each card contains a number, all numbers on the cards are distinct. The players take turns, Sereja moves first. During his turn a player can take one card: either the leftmost card in a row, or the rightmost one. The game ends when there is no more cards. The player who has the maximum sum of numbers on his cards by the end of the game, wins.
Sereja and Dima are being greedy. Each of them chooses the card with the larger number during his move.
Inna is a friend of Sereja and Dima. She knows which strategy the guys are using, so she wants to determine the final score, given the initial state of the game. Help her.
The first line contains integer n (1 ≤ n ≤ 1000) — the number of cards on the table. The second line contains space-separated numbers on the cards from left to right. The numbers on the cards are distinct integers from 1 to 1000.
On a single line, print two integers. The first number is the number of Sereja's points at the end of the game, the second number is the number of Dima's points at the end of the game.
4 4 1 2 10
12 5
7 1 2 3 4 5 6 7
16 12
In the first sample Sereja will take cards with numbers 10 and 2, so Sereja's sum is 12. Dima will take cards with numbers 4 and 1, so Dima's sum is 5.
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int n,i,a[1005];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int start=0,end=n-1,p=0,q=0,flag=0;
while(start<end)
{
if(a[start]>a[end]&&flag==0)
{
p+=a[start];
start++;
flag=1;
}
else if(a[start]<a[end]&&flag==0)
{
p+=a[end];
end--;
flag=1;
}
else if(a[start]>a[end]&&flag!=0)
{
q+=a[start];
start++;
flag=0;
}
else if(a[start]<a[end]&&flag!=0)
{
q+=a[end];
end--;
flag=0;
}
}
if(flag==0) p+=a[end];
else q+=a[end];
printf("%d %d\n",p,q);
return 0;
}
Sereja loves integer sequences very much. He especially likes stairs.
Sequence a1, a2, ..., a|a| (|a| is the length of the sequence) is stairs if there is such index i (1 ≤ i ≤ |a|), that the following condition is met:
For example, sequences [1, 2, 3, 2] and [4, 2] are stairs and sequence [3, 1, 2] isn't.
Sereja has m cards with numbers. He wants to put some cards on the table in a row to get a stair sequence. What maximum number of cards can he put on the table?
The first line contains integer m (1 ≤ m ≤ 105) — the number of Sereja's cards. The second line contains m integers bi (1 ≤ bi ≤ 5000) — the numbers on the Sereja's cards.
In the first line print the number of cards you can put on the table. In the second line print the resulting stairs.
5 1 2 3 4 5
5 5 4 3 2 1
6 1 1 2 2 3 3
5 1 2 3 2 1
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int n,i,k,a[5005]={0};
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&k);
a[k]++;
}
int sum=0,max=1,min=5000;
for(i=0;i<5005;i++)
{
if(a[i]>=2) {sum+=2;if(i>max) max=i;if(i<min) min=i;}
if(a[i]==1) {sum++;if(i>max) max=i;if(i<min) min=i;}
}
if(a[max]>=2) sum--;
printf("%d\n",sum);
for(i=min;i<max;i++)
if(a[i]>=2) {printf("%d ",i);a[i]--;}
printf("%d ",max);
for(i=max-1;i>min;i--)
if(a[i]>0) printf("%d ",i);
printf("%d\n",min);
return 0;
}
Sereja loves number sequences very much. That's why he decided to make himself a new one following a certain algorithm.
Sereja takes a blank piece of paper. Then he starts writing out the sequence in m stages. Each time he either adds a new number to the end of the sequence or takes l first elements of the current sequence and adds them c times to the end. More formally, if we represent the current sequence as a1, a2, ..., an, then after we apply the described operation, the sequence transforms intoa1, a2, ..., an[, a1, a2, ..., al] (the block in the square brackets must be repeated c times).
A day has passed and Sereja has completed the sequence. He wonders what are the values of some of its elements. Help Sereja.
The first line contains integer m (1 ≤ m ≤ 105) — the number of stages to build a sequence.
Next m lines contain the description of the stages in the order they follow. The first number in the line is a type of stage (1 or 2). Type 1 means adding one number to the end of the sequence, in this case the line contains integer xi (1 ≤ xi ≤ 105) — the number to add. Type 2 means copying a prefix of length li to the end ci times, in this case the line further contains two integers li, ci (1 ≤ li ≤ 105, 1 ≤ ci ≤ 104), li is the length of the prefix, ci is the number of copyings. It is guaranteed that the length of prefix li is never larger than the current length of the sequence.
The next line contains integer n (1 ≤ n ≤ 105) — the number of elements Sereja is interested in. The next line contains the numbers of elements of the final sequence Sereja is interested in. The numbers are given in the strictly increasing order. It is guaranteed that all numbers are strictly larger than zero and do not exceed the length of the resulting sequence. Consider the elements of the final sequence numbered starting from 1 from the beginning to the end of the sequence.
Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64dspecifier.
Print the elements that Sereja is interested in, in the order in which their numbers occur in the input.
6 1 1 1 2 2 2 1 1 3 2 5 2 1 4 16 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 2 1 2 3 1 2 1 2 3 1 2 1 2 3 4