1.

/**
* ACM Training 2009
* ACM Contest Problem # 19.09. "Laser Sculpture"
* Link: http://uva.onlinejudge.org/contests/227-44fb9aab/11683.pdf
*
* @author Felix Dietrich
* @version 1.0, 09/20/2009
*
* Methode: Simulation, Geometry
* Status : Accepted
* Runtime: 0.160
*/
import java.util.*;

public class Main
{

public static void main(String... args)
{
Scanner sc = new Scanner(System.in);
int[] remaining;
int A,C;
int laserOnTimes;
int maxheight;
int minheight;

while(true)
{
// initialize
A = sc.nextInt();
C = sc.nextInt();

if(A == 0 && C == 0)
return;

laserOnTimes = 0;
maxheight = 0;
minheight = A;

// calculate
remaining = new int[C];
for(int i=0; i<C; i++)
{
remaining[i] = sc.nextInt();

// determine maximum and minimum height
if(remaining[i] > maxheight)
maxheight = remaining[i];
if(remaining[i] < minheight)
minheight = remaining[i];

//already got first height
if(i > 0)
{
//if the last one was higher than the current, add the difference
if(remaining[i] < remaining[i-1])
{
laserOnTimes += remaining[i-1] - remaining[i];
}
}
else if(i == 0)
{
//start lasering
laserOnTimes = A - remaining[0];
}
}

System.out.println(laserOnTimes);
}
}
}



2.

/**
* ACM Training 2009
* ACM Problem #11683 - Laser Sculpture
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=78&page=show_problem&problem=2730
*
* @author Dennis Wilfert
* @version 1.0, 09/20/2009
*
* Status : Accepted
* Runtime: 0.192
*/
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class Main {

public static void main(String[] args) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(new BufferedOutputStream(System.out));
StringTokenizer token;

token = new StringTokenizer(reader.readLine());
int h = Integer.parseInt(token.nextToken());
int w = Integer.parseInt(token.nextToken());
int sum, tmp;

while (w != 0) {
token = new StringTokenizer(reader.readLine());
sum = 0;
while(w > 0){
// Solange tmp größer h ist wird nur h = tmp gesetzt.
// Ist tmp kleiner h wird zur Summe h-tmp dazugerechnet
tmp = Integer.parseInt(token.nextToken());
if(tmp < h){
sum += (h-tmp);
}

h = tmp;
w--;
}
writer.println(sum);

token = new StringTokenizer(reader.readLine());
h = Integer.parseInt(token.nextToken());
w = Integer.parseInt(token.nextToken());
}
writer.flush();
}
}