import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; import java.util.ArrayList; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static class Task { static boolean search(ArrayList path) { if (path.size() == 3) { int v0 = path.get(0).getValue(); int v1 = path.get(1).getValue(); int v2 = path.get(2).getValue(); return (v0 != v1 && v1 != v2 && v2 != v0) && ((v1 < v0 && v1 < v2) || (v1 > v0 && v1 > v2)); } Task.Vertex v = path.get(path.size() - 1); for (int i = 0; i < v.getDegree(); i++) { ArrayList next = new ArrayList<>(); next.addAll(path); next.add(v.getEdge(i).getVertex()); if (search(next)) { return true; } } return false; } public void solve(int testNumber, Scanner in, PrintWriter out) { final int n = in.nextInt(); final int m = in.nextInt(); Task.Vertex[] v = new Task.Vertex[n]; for (int i = 0; i < n; i++) { v[i] = new Task.Vertex(in.nextInt()); } for (int i = 0; i < m; i++) { int index1 = in.nextInt() - 1; int index2 = in.nextInt() - 1; v[index1].setEdge(v[index2]); v[index2].setEdge(v[index1]); } boolean exist = false; for (int i = 0; i < n; i++) { ArrayList path = new ArrayList<>(); path.add(v[i]); if (search(path)) { exist = true; break; } } out.printf("%s", exist ? "YES" : "NO"); } static class Vertex { private int value; private ArrayList edges = new ArrayList(); Vertex() { } Vertex(int v) { this.value = v; } void setEdge(Task.Vertex v) { this.edges.add(new Task.Edge(v)); } int getValue() { return this.value; } int getDegree() { return this.edges.size(); } Task.Edge getEdge(int index) { return this.edges.get(index); } } static class Edge { private Task.Vertex v; private int cost; Edge() { this.v = v; } Edge(Task.Vertex v) { this.v = v; } Edge(Task.Vertex v, int c) { this.v = v; this.cost = c; } Task.Vertex getVertex() { return this.v; } } } }