Mahmoud and a Triangle time limit per test 2 seconds memory limit per test megabytes input standard input output stand ARD output
Mahmoud have n line segments, the i-th of them have length AI. Ehab challenged him to use exactly 3 line segments to form a non-degenerate triangle. Mahmoud doesn ' t accept challenges unless he's sure he can win, so he asked you to tell him if he should accept the Challe Nge. 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 the line segments or change any length. A non-degenerate triangle is a triangle with positive area. Input
The first line contains a single integer n (3≤n≤105)-the number of line segments Mahmoud have.
The second line contains n integers a1, a2, ..., an (1≤ai≤109)-the lengths of line segments Mahmoud have. Output
In the-line print ' YES ' if he can choose exactly three line segments and form a non-degenerate triangle with them, an D "NO" otherwise. Examples input
5
1 5 3 2 4
Output
YES
Input
3
4 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.
Test instructions: Give the number of N, ask if you can choose 3 numbers to form a triangle
Problem-solving ideas: First, all the numbers from small to large to sort, if there are three of the number of triangles can be formed, then the adjacent three number must be possible
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <climits>
using namespace std;
#define LL Long Long
int main ()
{
int n,a[100090];
while (~SCANF ("%d", &n))
{for
(int i=1;i<=n;i++)
scanf ("%d", &a[i]);
Sort (a+1,a+1+n);
int flag=0;
for (int i=3;i<=n;i++)
if (a[i]<a[i-1]+a[i-2]) flag=1;
if (flag) printf ("yes\n");
else printf ("no\n");
}
return 0;
}