Skip to content

Instantly share code, notes, and snippets.

@modos
Created April 13, 2023 23:17
Show Gist options
  • Save modos/61218c7ca390f8eeb4194d19c16c44bc to your computer and use it in GitHub Desktop.
Save modos/61218c7ca390f8eeb4194d19c16c44bc to your computer and use it in GitHub Desktop.
باقر حال نداره ولی پول داره
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int A[] = new int[11];
for (int i = 1; i <= n; i++)
A[i] = scanner.nextInt();
int memo[][] = new int[10001][11];
int INF = 1000000000;
for (int i = 1; i <= m; i++)
memo[i][0] = INF;
for (int j = 1; j <= n; j++) {
for (int i = 0; i <= m; i++) {
memo[i][j] = INF;
for (int k = 1; k * k <= i; k++) {
if (memo[i][j] > memo[i - k * k][j - 1] + (A[j] - k) * (A[j] - k))
memo[i][j] = memo[i - k * k][j - 1] + (A[j] - k) * (A[j] - k);
}
}
}
if (memo[m][n] == INF)
System.out.println(-1);
else
System.out.println(memo[m][n]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment