Optimal synchronization problem for a multi-agent system