Mahmoud and a Triangle (CodeForces

来源:互联网 发布:淘宝闺蜜投诉入口 编辑:程序博客网 时间:2024/05/22 06:58

Mahmoud has n line segments, the i-th of them has length ai. Ehab challenged him to useexactly 3 line segments to form a non-degenerate triangle. Mahmoud doesn't accept challenges unless he is sure he can win, so he asked you to tell him if he should accept the challenge. Given the lengths of the line segments, check if he can choose exactly 3 of them to form a non-degenerate triangle.

Mahmoud should use exactly 3 line segments, he can't concatenate two line segments or change any length. A non-degenerate triangle is a triangle with positive area.

Input

The first line contains single integer n (3 ≤ n ≤ 105) — the number of line segments Mahmoud has.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — the lengths of line segments Mahmoud has.

Output

In the only line print "YES" if he can choose exactly three line segments and form a non-degenerate triangle with them, and "NO" otherwise.

Example
Input
51 5 3 2 4
Output
YES
Input
34 1 2
Output
NO
Note

For the first example, he can use line segments with lengths 2, 4 and 5 to form a non-degenerate triangle.


不要上来就三层for循环暴力,我们提倡和平。分析一下会发现很简单

分析:首先进行排序。因为三角形满足任意两边之和大于第三边(a+b>c),任意两边之差小于第三边(c-b<a)。所以使得a+b尽可能大,c尽可能小。那么可以取连续的三个数进行判断即可。

#include <iostream>#include <algorithm>using namespace std;long num[100005];int main(){    int n;    cin>>n;    for(int i = 0; i<n; i++)        cin>>num[i];    sort(num, num+n);    for(int i = 0; i + 2<n; i++){        if(num[i] + num[i+1] > num[i+2]){            cout<<"YES"<<endl;            return 0;        }    }    cout<<"NO"<<endl;    return 0;}

原创粉丝点击