Exact Inference in High-order Structured Prediction