File tree Expand file tree Collapse file tree 1 file changed +90
-0
lines changed
Expand file tree Collapse file tree 1 file changed +90
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.util.* ;
4+ import java.io.* ;
5+
6+ class Edge {
7+ int x;
8+ long c;
9+ Edge (int x , long c ){
10+ this . x = x;
11+ this . c = c;
12+ }
13+ }
14+
15+ class Element {
16+ long dist, cnt;
17+ int node;
18+ Element (long dist , long cnt , int node ){
19+ this . dist = dist;
20+ this . cnt = cnt;
21+ this . node = node;
22+ }
23+ }
24+
25+ class Main {
26+
27+ // IO field
28+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
29+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
30+ static StringTokenizer st;
31+
32+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
33+ static int nextInt() {return Integer . parseInt(st. nextToken());}
34+ static long nextLong() {return Long . parseLong(st. nextToken());}
35+ static void bwEnd() throws Exception {bw. flush();bw. close();}
36+
37+ // Additional field
38+ static long [] dp;
39+ static int [] A ;
40+ static int N , M ;
41+
42+
43+ public static void main(String [] args) throws Exception {
44+
45+ ready();
46+ solve();
47+
48+ bwEnd();
49+ }
50+
51+ static void ready() throws Exception {
52+
53+ nextLine();
54+ N = nextInt();
55+ M = nextInt();
56+ dp = new long [M + 1 ];
57+ Arrays . fill(dp, - 1 );
58+ A = new int [N ];
59+ for (int i= 0 ;i< N ;i++ ) A [i] = Integer . parseInt(br. readLine());
60+
61+ }
62+
63+ static void solve() throws Exception {
64+
65+ dp[A [0 ]] = 0 ;
66+ for (int i= 1 ;i< N ;i++ ) {
67+ long [] ndp = new long [M + 1 ];
68+ Arrays . fill(ndp, - 1 );
69+ for (int j= 1 ;j<= M ;j++ ) {
70+ if (dp[j] != - 1 ) {
71+ if (j + 1 + A [i] <= M ) {
72+ if (ndp[j+ 1 + A [i]] == - 1 ) ndp[j+ 1 + A [i]] = dp[j];
73+ else ndp[j+ 1 + A [i]] = Math . min(ndp[j+ 1 + A [i]], dp[j]);
74+ }
75+ if (ndp[A [i]] == - 1 ) ndp[A [i]] = dp[j] + (M - j)* (M - j);
76+ else ndp[A [i]] = Math . min(ndp[A [i]], dp[j] + (M - j)* (M - j));
77+ }
78+ }
79+ dp = ndp;
80+ }
81+
82+ long ans = Long . MAX_VALUE ;
83+ for (int i= 1 ;i<= M ;i++ ) if (dp[i] != - 1 ) ans = Math . min(ans, dp[i]);
84+ bw. write(ans+ " \n " );
85+
86+ }
87+
88+ }
89+
90+ ```
You can’t perform that action at this time.
0 commit comments