int longestZigZag( vector <int> sequence )
{
int n = sequence.size();
vector <int> dp (n, 1);
vector <int> dif (n, 0);
int temp;
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
{
temp = sequence[i] - sequence[j];
if (temp != 0 && temp*dif[j] <= 0 && dp[j] + 1 > dp[i])
{
dp[i] = dp[j] + 1;
dif[i] = temp;
}
}
sort (dp.begin(), dp.end());
return dp[n-1];
}
aW50IGxvbmdlc3RaaWdaYWcoIHZlY3RvciA8aW50PiBzZXF1ZW5jZSApIAp7CglpbnQgbiA9IHNlcXVlbmNlLnNpemUoKTsKCXZlY3RvciA8aW50PiBkcCAobiwgMSk7Cgl2ZWN0b3IgPGludD4gZGlmIChuLCAwKTsKCWludCB0ZW1wOwoJZm9yIChpbnQgaSA9IDE7IGkgPCBuOyBpKyspCgkJZm9yIChpbnQgaiA9IDA7IGogPCBpOyBqKyspCgkJewoJCQl0ZW1wID0gc2VxdWVuY2VbaV0gLSBzZXF1ZW5jZVtqXTsKCQkJaWYgKHRlbXAgIT0gMCAmJiB0ZW1wKmRpZltqXSA8PSAwICYmIGRwW2pdICsgMSA+IGRwW2ldKQoJCQl7CQkJCSAgIAoJCQkJZHBbaV0gPSBkcFtqXSArIDE7CgkJCQlkaWZbaV0gPSB0ZW1wOwoJCQl9CgkJfQoJc29ydCAoZHAuYmVnaW4oKSwgZHAuZW5kKCkpOwoJcmV0dXJuIGRwW24tMV07Cn0=