Codeforces847m weather tomorrow

Posted by yjjr's blog on February 6, 2018

标签:模拟,计算几何

Vasya cameup with his own weather forecasting method. He knows the information about theaverage air temperature for each of the last n days. Assume that the average air temperaturefor each day is integral.

Vasyabelieves that if the average temperatures over the last n days form an arithmetic progression, wherethe first term equals to the average temperature on the first day, the secondterm equals to the average temperature on the second day and so on, then theaverage temperature of the next (n + 1)-th daywill be equal to the next term of the arithmetic progression. Otherwise,according to Vasya's method, the temperature of the (n + 1)-th day will be equal to the temperature ofthe n-th day.

Your taskis to help Vasya predict the average temperature for tomorrow, i. e. for the (n + 1)-th day.

Input

The firstline contains a single integer n (2 ≤ n ≤ 100) — the number ofdays for which the average air temperature is known.

The secondline contains a sequence of integers t1, t2, ..., tn ( - 1000 ≤ ti ≤ 1000) —where ti is theaverage temperature in the i-th day.

Output

Print theaverage air temperature in the (n + 1)-th day,which Vasya predicts according to his method. Note that the absolute value ofthe predicted temperature can exceed 1000.

Example

Input

5
10 5 0 -5 -10

Output

-15

Input

4
1 1 1 1

Output

1

Input

3
5 1 -5

Output

-5

Input

2
900 1000

Output

1100

Note

In thefirst example the sequence of the average temperatures is an arithmeticprogression where the first term is 10 and eachfollowing terms decreases by 5. So thepredicted average temperature for the sixth day is  - 10 - 5 =  - 15.

In thesecond example the sequence of the average temperatures is an arithmeticprogression where the first term is 1 and eachfollowing terms equals to the previous one. So the predicted averagetemperature in the fifth day is 1.

In thethird example the average temperatures do not form an arithmetic progression,so the average temperature of the fourth day equals to the temperature of thethird day and equals to  - 5.

In thefourth example the sequence of the average temperatures is an arithmeticprogression where the first term is 900 and eachthe following terms increase by100. Sopredicted average temperature in the third day is 1000 + 100 = 1100.

 

计算几何的入门题,初二数学的难度,判断这些点是否在同一条直线上

Code

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
 
int n,d,flag,a[305];
 
int main()
{
    cin>>n;
         rep(i,1,n)scanf("%d",&a[i]);
         d=a[2]-a[1];
         rep(i,2,n-1)
             if(a[i+1]-a[i]!=d)flag=1;
         if(flag==0)cout<<a[n]+d;
         else cout<<a[n];
         return 0;
}



本文可以转载,但必须附上原文链接,否则你会终生找不到妹子!!!欢迎关注我的CSDN: ahyjjr